如何在期望的时间内运行SpeechRecognizer?

时间:2019-02-25 05:15:45

标签: android speech-recognition speech-to-text

我正在开发语音识别。 我使用 SpeechRecognizer

我希望在按下按钮时开始语音识别,并在再次按下按钮时结束语音识别。

但是,如果您在交谈时犹豫1秒钟,就会呼叫onEndOfSpeech()

因此,当调用startListening()时,我再次调用onEndOFSpeech()

但是,如果您再次在startListening()期间讲话,则只有在调用onReadyForSpeech()和调用onBeginningOfSpeech()之前,语音才能被识别。 (大约500毫秒)

  1. 如果我不敢讲话,是否有任何方法不会onEndOfSpeech()被呼叫并且仍然能够识别语音?

=>我尝试了“ EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS”,“ EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS”,“ EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS”

但是他们没有工作。

  1. 是否可以通过再次调用startListening()立即获得语音识别?

=>目前,我无法识别大约500毫秒。请注意,只有在重新生成 SpeechRecognizer 后,您才能重新启动。

  1. 如果将句子另存为音频文件,是否有免费的库或引擎可将其转换为文本?我使用的语言是英语,韩语,中文和日语。

我附加了 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");
}
}

0 个答案:

没有答案