Android语音识别会立即停止收听

时间:2016-04-24 18:39:08

标签: java android speech-recognition

我根据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正在持续运行。这是否意味着语音识别也在持续运行,因此导致我的应用程序不接受任何语音?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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