SpeechRecognizer 将重复,而不是 运行 永久
SpeechRecognizer will be repeated, instead of running permanently
我正在尝试开发类似于 Google 助手的东西。所以当我说“好的应用程序”时,它应该处理。所以我刚刚在后台创建了一个 运行 的服务:
public class SttService extends Service implements RecognitionListener {
private static final String TAG = "SstService";
SpeechRecognizer mSpeech;
private void speak() {
mSpeech = SpeechRecognizer.createSpeechRecognizer(SttService.this);
mSpeech.setRecognitionListener(SttService.this);
Intent intent1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
mSpeech.startListening(intent1);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: I am running");
speak();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind: ");
return null;
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.e(TAG, "onReadyForSpeech: ");
}
@Override
public void onBeginningOfSpeech() {
Log.e(TAG, "onBeginningOfSpeech: ");
}
@Override
public void onRmsChanged(float rmsdB) {
// Log.e(TAG, "onRmsChanged: ");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.e(TAG, "onBufferReceived: ");
}
@Override
public void onEndOfSpeech() {
Log.e(TAG, "onEndOfSpeech: ");
}
@Override
public void onError(int error) {
Log.e(TAG, "onError: " + error);
if (error == 7) {
speak();
}
}
@Override
public void onResults(Bundle results) {
ArrayList<String> resultsStringArrayList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String mainResult = resultsStringArrayList.get(0);
Log.e(TAG, "onResults: " + mainResult);
if (mainResult.equalsIgnoreCase("Okay App")) {
System.out.println("What's up?");
}
mSpeech.destroy();
mSpeech = null;
speak();
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.e(TAG, "onPartialResults: ");
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.e(TAG, "onEvent: ");
}
}
我的问题是应用程序没有永久监听。它开始收听然后有一个结果。当我什么都不说时,它会听大约 2 秒,然后 SpeechRecognizer 将被销毁,这样另一个语音就可以开始了。所以在它被销毁的时候,有一个中断,当我在此期间说些什么时,它不会被识别。
我的应用程序没有按照我的要求运行。可能我做的完全错了。所以我想要实现的是一个 SpeechRecognizer 永久运行并且只在我说“Okay App”时处理。我该怎么做?
SpeechRecognizer 就是这样设计的。它不是为了永久的背景聆听,而是为了短期的即时响应。就像有人点击搜索栏中的麦克风按钮一样。如果你想要永久的背景聆听,你将不得不降低水平并自己做。
我正在尝试开发类似于 Google 助手的东西。所以当我说“好的应用程序”时,它应该处理。所以我刚刚在后台创建了一个 运行 的服务:
public class SttService extends Service implements RecognitionListener {
private static final String TAG = "SstService";
SpeechRecognizer mSpeech;
private void speak() {
mSpeech = SpeechRecognizer.createSpeechRecognizer(SttService.this);
mSpeech.setRecognitionListener(SttService.this);
Intent intent1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
mSpeech.startListening(intent1);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: I am running");
speak();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind: ");
return null;
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.e(TAG, "onReadyForSpeech: ");
}
@Override
public void onBeginningOfSpeech() {
Log.e(TAG, "onBeginningOfSpeech: ");
}
@Override
public void onRmsChanged(float rmsdB) {
// Log.e(TAG, "onRmsChanged: ");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.e(TAG, "onBufferReceived: ");
}
@Override
public void onEndOfSpeech() {
Log.e(TAG, "onEndOfSpeech: ");
}
@Override
public void onError(int error) {
Log.e(TAG, "onError: " + error);
if (error == 7) {
speak();
}
}
@Override
public void onResults(Bundle results) {
ArrayList<String> resultsStringArrayList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String mainResult = resultsStringArrayList.get(0);
Log.e(TAG, "onResults: " + mainResult);
if (mainResult.equalsIgnoreCase("Okay App")) {
System.out.println("What's up?");
}
mSpeech.destroy();
mSpeech = null;
speak();
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.e(TAG, "onPartialResults: ");
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.e(TAG, "onEvent: ");
}
}
我的问题是应用程序没有永久监听。它开始收听然后有一个结果。当我什么都不说时,它会听大约 2 秒,然后 SpeechRecognizer 将被销毁,这样另一个语音就可以开始了。所以在它被销毁的时候,有一个中断,当我在此期间说些什么时,它不会被识别。
我的应用程序没有按照我的要求运行。可能我做的完全错了。所以我想要实现的是一个 SpeechRecognizer 永久运行并且只在我说“Okay App”时处理。我该怎么做?
SpeechRecognizer 就是这样设计的。它不是为了永久的背景聆听,而是为了短期的即时响应。就像有人点击搜索栏中的麦克风按钮一样。如果你想要永久的背景聆听,你将不得不降低水平并自己做。