2016-04-24 89 views
3

我基於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持續運行的應用程序日誌。這是否意味着語音識別也會持續運行,從而導致我的應用程序不能接聽任何語音?

+0

「監聽器停止」=相應的函數返回?錯誤代碼/例外情況如何? –

+1

@ivan_pozdeev通過監聽器停止,沒有onResults方法調用或onEndOfSpeech甚至onError,您可以簡單地聽到開始監聽聲音並立即在停止監聽聲音之後,儘管這不會在調試控制檯中複製。在控制檯中,您可以看到監聽器開始聆聽,但它永遠不會停止,因爲onRmsChanged之後不斷調用,即使停止了聆聽聲音。 奇怪的是,我添加了互聯網權限,並與所有用戶權限進行交互,似乎像以前一樣正常工作。不知道如何或受到什麼影響。 – caseylouisee

+0

也許安全模型有所變化。任何(自動)更新安裝? –

回答

0

看來解決方案是確保設備連接到互聯網。我認爲TTS引擎需要在更改時連接到正確的互聯網主機,因此如果沒有互聯網連接就不會聽。

+0

如果您已經在手機中安裝了本地語言包,語音識別器將會很快並且可以脫機工作。 –