gpt4 book ai didi

android - 语音识别器 : not connected to the recognition service error after onPause

转载 作者:行者123 更新时间:2023-11-29 20:46:49 35 4
gpt4 key购买 nike

您好,我正在我的应用程序中实现连续语音收听,但是当我在 onPause 中销毁语音识别器时,出现未连接到识别服务错误。

我已经阅读了 this问题,以及相关的答案,但似乎并没有解决问题。

这是产生错误的代码:

// Activity code
@Override
public void onPause()
{
assistant.dismiss();
super.onPause();
}

@Override
public void onResume()
{
super.onResume();
assistant = new Assistant(this);
Log.d(TAG,"resume");
}

助手代码:

public class Assistant extends UtteranceProgressListener implements RecognitionListener, TextToSpeech.OnInitListener
{
private static final String TAG = "Assistant" ;

private Context context ;

private Intent intent ;
private SpeechRecognizer speechRecognizer;
private TextToSpeech textToSpeech;

private static AudioManager audioManager;

private boolean isAudioMute;

String actionAnswer ;

public Assistant ( Context context )
{
this.context = context;

isAudioMute = false ;

textToSpeech = new TextToSpeech(context, this);
textToSpeech.setOnUtteranceProgressListener(this);

if ( SpeechRecognizer.isRecognitionAvailable(context))
{
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
context.getPackageName());

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);

speechRecognizer.setRecognitionListener(this);

audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
else
{
Log.e(TAG, "speech recognizer not avaiable");
}
}

private void startListening()
{
if ( !isAudioMute )
{
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
isAudioMute = true ;
}

((Activity)context).runOnUiThread(new Runnable()
{
@Override
public void run()
{
speechRecognizer.startListening(intent);
Log.d(TAG, "startlisten");
}
});
}

private void stopListening()
{
speechRecognizer.stopListening();

try
{
// wait for annoying sound to happens, then unmute channel.
Thread.sleep(400);
}
catch (InterruptedException e)
{
e.printStackTrace();
}

Log.d(TAG, "stoplistne");

if ( isAudioMute )
{
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
isAudioMute = false ;
}
}

public void dismiss()
{
((Activity)context).runOnUiThread(new Runnable()
{
@Override
public void run()
{
speechRecognizer.stopListening();
speechRecognizer.cancel();
speechRecognizer.destroy();
speechRecognizer = null;
}
});

try
{
// wait for annoying sound to happens, then unmute channel.
Thread.sleep(400);
}
catch (InterruptedException e)
{
e.printStackTrace();
}

Log.d(TAG, "stoplistne");

if ( isAudioMute )
{
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
isAudioMute = false ;
}

textToSpeech.stop();
textToSpeech.shutdown();
textToSpeech = null;
}

private void speakOut(String text)
{
stopListening();

if (Build.VERSION.SDK_INT >= 21 )
{
if (textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, this.getClass().getName()) != TextToSpeech.SUCCESS)
Log.e(TAG, "failed to queue text " + text);
}
else
{
if (textToSpeech.speak(text.toString(), TextToSpeech.QUEUE_FLUSH, null) != TextToSpeech.SUCCESS)
Log.e(TAG, "failed to queue text " + text);
}
}

// text to speech
@Override
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
int result = textToSpeech.setLanguage(Locale.US);

if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED)
{
Log.e(TAG, "This Language is not supported");
return;
}

speakOut("Assistant Ready");

}
else
{
Log.e(TAG, "Initilization Failed!");
}
}

// SpeechRecognizer
@Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "readyforspeech");
}

// SpeechRecognizer
@Override
public void onBeginningOfSpeech()
{
Log.d(TAG, "beginningofspeech");
}

// SpeechRecognizer
@Override
public void onRmsChanged(float rmsdB)
{

}

// SpeechRecognizer
@Override
public void onBufferReceived(byte[] buffer)
{
Log.d(TAG, "bufferreceived");
}

// SpeechRecognizer
@Override
public void onEndOfSpeech()
{
Log.d(TAG, "endofspeech");
}

// SpeechRecognizer
@Override
public void onError(int error)
{
Log.d("SPEECH", "onError: " + error);
switch(error)
{
case SpeechRecognizer.ERROR_AUDIO:
Log.d(TAG,"ERROR_AUDIO");
break;
case SpeechRecognizer.ERROR_CLIENT:
Log.d(TAG,"ERROR_CLIENT");
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
Log.d(TAG,"ERROR_INSUFFICIENT_PERMISSIONS");
break;
case SpeechRecognizer.ERROR_NETWORK:
Log.d(TAG,"ERROR_NETWORK");
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
Log.d(TAG,"ERROR_NETWORK_TIMEOUT");
break;
case SpeechRecognizer.ERROR_NO_MATCH:
Log.d(TAG,"ERROR_NO_MATCH");
startListening();
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
Log.d(TAG,"ERROR_RECOGNIZER_BUSY");
break;
case SpeechRecognizer.ERROR_SERVER:
Log.d(TAG,"ERROR_SERVER");
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
Log.d(TAG,"ERROR_SPEECH_TIMEOUT");
startListening();
break;
default:
Log.d(TAG,"ERROR_UNKNOWN");
}
}

// SpeechRecognizer
@Override
public void onResults(Bundle results)
{
ArrayList<String> res = results. getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION) ;
Log.i(TAG,"res: '" + res.get(0) +"'.");
actionAnswer = res.get(0);
speakOut(actionAnswer);
}

// SpeechRecognizer
@Override
public void onPartialResults(Bundle partialResults)
{

}

// SpeechRecognizer
@Override
public void onEvent(int eventType, Bundle params)
{

}

// Utterance progress listener
@Override
public void onStart(String utteranceId)
{
Log.d(TAG, "onstart");
}

// Utterance progress listener
@Override
public void onDone(String utteranceId)
{
Log.d(TAG, "ondone");
startListening();
}

// Utterance progress listener
@Override
public void onError(String utteranceId)
{
Log.d(TAG, "onerror");
}
}

无论如何,即使发生错误,当我重新初始化语音识别器时一切正常,所以我不确定是否需要担心错误。

最佳答案

不要使用 speechRecognizer.stopListening()speechRecognizer.cancel(),请改用 speechRecognizer.destroy()

关于android - 语音识别器 : not connected to the recognition service error after onPause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30149690/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com