Android语音识别立即停止
问题描述:
我基于Android网站上的一个示例开发了一个功能完备的语音识别程序。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;
}
UPDATE:
翻翻我发现onRMSChanged持续运行的应用程序日志。这是否意味着语音识别也会持续运行,从而导致我的应用程序不能接听任何语音?
答
看来解决方案是确保设备连接到互联网。我认为TTS引擎需要在更改时连接到正确的互联网主机,因此如果没有互联网连接就不会听。
+0
如果您已经在手机中安装了本地语言包,语音识别器将会很快并且可以脱机工作。 –
“监听器停止”=相应的函数返回?错误代码/例外情况如何? –
@ivan_pozdeev通过监听器停止,没有onResults方法调用或onEndOfSpeech甚至onError,您可以简单地听到开始监听声音并立即在停止监听声音之后,尽管这不会在调试控制台中复制。在控制台中,您可以看到监听器开始聆听,但它永远不会停止,因为onRmsChanged之后不断调用,即使停止了聆听声音。 奇怪的是,我添加了互联网权限,并与所有用户权限进行交互,似乎像以前一样正常工作。不知道如何或受到什么影响。 – caseylouisee
也许安全模型有所变化。任何(自动)更新安装? –