我正在开发语音识别。 我使用 SpeechRecognizer 。
我希望在按下按钮时开始语音识别,并在再次按下按钮时结束语音识别。
但是,如果您在交谈时犹豫1秒钟,就会呼叫onEndOfSpeech()
。
因此,当调用startListening()
时,我再次调用onEndOFSpeech()
。
但是,如果您再次在startListening()
期间讲话,则只有在调用onReadyForSpeech()
和调用onBeginningOfSpeech()
之前,语音才能被识别。 (大约500毫秒)
onEndOfSpeech()
被呼叫并且仍然能够识别语音?=>我尝试了“ EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS”,“ EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS”,“ EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS”
但是他们没有工作。
startListening()
立即获得语音识别? =>目前,我无法识别大约500毫秒。请注意,只有在重新生成 SpeechRecognizer 后,您才能重新启动。
我附加了 SpeechRecognizer 来源。
public class SpeechToTextUtil implements RecognitionListener {
private final TranslationActivity mTranslationActivity;
private String[] languages={"ko-KR","en-US","zh-CN","ja-JP"};
private Context mContext;
private SpeechRecognizer mRecognizer;
private Intent intent;
private int mLanguagePosition;
private boolean mBeginSpeech;
private float mRestoreRmsValue = 0;
private int mNoSpeech = 0;
public interface OnSTTResultListener {
void onSTTResult(List<String> mResult, int error);
void onSTTPartialResults(List<String> mResult);
}
private OnSTTResultListener onSTTResultListener;
public void setOnSTTResultListener(OnSTTResultListener onSTTResultListener) {
this.onSTTResultListener = onSTTResultListener;
}
public SpeechToTextUtil (Context context) {
mContext = context;
mTranslationActivity = (TranslationActivity)mContext;
}
public void startSpeechToText(int languagePosition) {
mLanguagePosition = languagePosition;
if (mRecognizer == null) {
mRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext);
Log.i(Util.TAG, this.getClass().getSimpleName()+" mRecognizer create langauge = "+languages[languagePosition]);
}
mRecognizer.setRecognitionListener(this);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, mContext.getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, languages[languagePosition]);
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);
Log.i(Util.TAG, this.getClass().getSimpleName()+" mRecognizer startListening");
mRecognizer.startListening(intent);
}
public void stopSpeechToText() {
if(mRecognizer != null){
mRecognizer.stopListening();
}
}
public void destroySpeechToText() {
Log.i(Util.TAG, this.getClass().getSimpleName()+" destroySpeechToText");
if(mRecognizer != null){
mRecognizer.stopListening();
mRecognizer.destroy();
mRecognizer.cancel();
mRecognizer = null;
}
}
public SpeechRecognizer getRecognizer() {
return mRecognizer;
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onReadyForSpeech");
}
@Override
public void onBeginningOfSpeech() {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onBeginningOfSpeech");
mBeginSpeech = true;
}
@Override
public void onRmsChanged(float rmsdB) {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onRmsChanged rmsdB = " + rmsdB);
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onBufferReceived");
}
@Override
public void onEndOfSpeech() {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onEndOfSpeech");
mBeginSpeech = false;
if (mTranslationActivity.isSpeechStart()) {
destroySpeechToText();
startSpeechToText(mLanguagePosition);
}
}
@Override
public void onError(int error) {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onError = "+error);
onSTTResultListener.onSTTResult(null,error);
}
@Override
public void onResults(Bundle results) {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onResults");
String key = SpeechRecognizer.RESULTS_RECOGNITION;
List<String> mResult = results.getStringArrayList(key);
Log.i(Util.TAG, this.getClass().getSimpleName()+" mResult = "+mResult);
onSTTResultListener.onSTTResult(mResult,0);
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onPartialResults = "+partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION));
onSTTResultListener.onSTTPartialResults(partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION));
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.i(Util.TAG, this.getClass().getSimpleName()+" onEvent");
}
}