Android 语音识别立即停止收听

Android Speech Recognition stops listening immediately

我根据 Android 网站上的示例开发了一个具有全功能语音识别的程序。

我没有对代码进行任何更改,它只是突然停止工作了。它开始聆听(您可以听到噪音),然后立即停止(您听到结束噪音)。

有没有其他人遇到过这个问题或知道我该如何解决?这是代码,运行时没有错误输出,只是监听器在开始监听时几乎立即停止。

/**
 * This method is called when the Speech Recognizer starts to listen for speech input
 */ 
@Override
public void onBeginningOfSpeech() {
    Log.i("SRL", "onBeginningOfSpeech");
}

@Override
public void onBufferReceived(byte[] buffer) {
    Log.i("SRL", "onBufferReceived: " + buffer);
}

/**
 * This method is called after the speech input has been completed.
 */
@Override
public void onEndOfSpeech() {
    Log.i("SRL", "onEndOfSpeech");
}

/**
 * This method is called if there has been an error during speech input
 * @param errorCode
 */
@Override
public void onError(int errorCode) {
    String errorMessage = getErrorText(errorCode);
    Log.d("SRL", "FAILED " + errorMessage);
    m_speech = SpeechRecognizer.createSpeechRecognizer(this);
    m_speech.setRecognitionListener(this);
    m_speech.startListening(getIntent());
}

@Override
public void onEvent(int arg0, Bundle arg1) {
    Log.i("SRL", "onEvent");
}

/**
 * This method is called if the speech recognizer thinks only partial speech was
 * input/recognized
 * @param arg0
 */
@Override
public void onPartialResults(Bundle arg0) {
    Log.i("SRL", "onPartialResults");
}

/**
 * This method is called when the speech recognizer is ready for input
 * @param arg0
 */
@Override
public void onReadyForSpeech(Bundle arg0) {
    Log.i("SRL", "onReadyForSpeech");
}

/**
 * This method is called when the speech recognizer has recieved input and recognized it.
 * It updates the recognized speech text view on the screen to show users what they have input.
 * @param results the text that has been input
 */
@Override
public void onResults(Bundle results) {
    recognizedSpeech = (TextView) findViewById(R.id.recognizedSpeech);
    Log.i("SRL", "onResults");
    ArrayList<String> matches = results
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    for (String result : matches)
        text += result + "\n";
    recognizedSpeech.setText(text);

    if (recognizedSpeech.getText().toString().contains("yes")) {
        PropertySquare square = (PropertySquare) (m_board.getSquare(
                players.get(m_currentTurn).getCurrentPosition()));

        square.setOwnedBy(players.get(m_currentTurn).getName());
        convertTextToSpeech("You now own" + square.getName());
        Log.d("buyProperty yes", square.getOwnedBy());

        if(manageFunds) {
            players.get(m_currentTurn).subtractMoney(square.getPrice());
            Log.d("buyProperty yes", players.get(m_currentTurn).getName() +
                String.valueOf(players.get(m_currentTurn).getMoney()));

            funds.setText(String.valueOf(players.get(m_currentTurn).getMoney()));
        }
    }
    nextTurnRoll();
}

@Override
public void onRmsChanged(float rmsdB) {
    Log.i("SRL", "onRmsChanged: " + rmsdB);
}

/**
 * This method returns what error was caused if the speech recgonizer throws an error code
 * @param errorCode int representing the error thrown
 * @return log message including error details
 */
public static String getErrorText(int errorCode) {
    String message;
    switch (errorCode) {
        case SpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        case SpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        case SpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            break;
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            break;
        case SpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            break;
        case SpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            break;
        default:
            message = "Didn't understand, please try again.";
            break;
    }
    return message;
}

更新:

查看应用程序日志我发现 onRMSChanged 持续 运行。这是否意味着语音识别也是连续 运行,因此导致我的应用程序无法接收任何语音?

看来解决方案是确保设备已连接到互联网。我认为 TTS 引擎在更改时需要连接到正确的互联网主机,因此如果没有互联网连接则不会收听。

我通过删除我在意图上启用的离线首选参数解决了即时 NO_MATCH 错误。

//Intent.PutExtra(RecognizerIntent.ExtraPreferOffline, true);