2017-08-02 44 views
0

我有一個問題,我的Android應用程序。我的設備是Vuzix M300,我試圖讓語音控制工作。 我的問題是,當我初始化我的SpeechRecognizer並調用startListining方法時,一切都運行,期望沒有語音被識別。我已經嘗試了相同的代碼在Android手機上工作得很好。Android SpeechRecognizer不會注意到麥克風輸入

這裏是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //grant access to internet 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    //set layout 
    setContentView(R.layout.activity_main); 

    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 
    speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE); 
    speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName()); 
    speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "de-DE"); 
    speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false); 

    speechRecognizer.setRecognitionListener(prepareRegnitionListener()); 
    speechRecognizer.startListening(speechRecognizerIntent); 
    //startListening(0); 
} 

private RecognitionListener prepareRegnitionListener() { 
    // TODO Auto-generated method stub 
    return new RecognitionListener() { 

     @Override 
     public void onRmsChanged(float rmsdB) { 
      //Didn´t use 
     } 

     @Override 
     public void onResults(Bundle results) { 
      ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
      Log.d(MainActivity,"Completed speech recognition: Result: " + matches); 
      String match = matches.get(0); 
     } 

     @Override 
     public void onReadyForSpeech(Bundle params) { 
      Log.d(MainActivity, "ReadyforSpeech"); 
     } 

     @Override 
     public void onPartialResults(Bundle partialResults) { 
      // Nothing 
      ArrayList<String> matches = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
      Log.d(MainActivity,"Completed speech recognition: Result: " + matches); 
     } 

     @Override 
     public void onEvent(int eventType, Bundle params) { 
      // Nothing 

     } 

     @Override 
     public void onError(int error) { 
      switch (error){ 
       case SpeechRecognizer.ERROR_AUDIO: 
        Log.e(MainActivity,"Failed to recognize speech: Audio recording error."); 
        startListening(1000); 
        break; 
       case SpeechRecognizer.ERROR_CLIENT: 
        Log.e(MainActivity,"Failed to recognize speech: Insufficient permissions."); 
        startListening(1000); 
        break; 
       case SpeechRecognizer.ERROR_NO_MATCH: 

        Log.d(MainActivity,"Failed to recognize speech: No recognition results matched. Retrying..."); 
        startListening(1000); 
        break; 
       default: 
        Log.e(MainActivity,"Failed to recognize speech. Unknown error" + error); 
        startListening(1000); 

      } 


     } 

     @Override 
     public void onEndOfSpeech() { 
      Log.d(MainActivity, "EndofSpeech"); 
     } 

     @Override 
     public void onBufferReceived(byte[] buffer) { 
      //Didn´t use 

     } 

     @Override 
     public void onBeginningOfSpeech() { 
      Log.d(MainActivity, "beginnofSpeech");//Do something when speaking starts 
     } 
    }; 
} 

private void startListening(int delay){ 
    if(delay > 0){ 
     Timer t = new Timer(); 
     t.schedule(new TimerTask(){ 

         @Override 
         public void run() { 
          mainHandler.post(new Runnable() { 

           @Override 
           public void run() { 
            speechRecognizer.startListening(speechRecognizerIntent); 
            Log.d(MainActivity,"Start delayed listening to speech"); 
            speechRecognizer.stopListening(); 
           } 
          }); 

         }} 
       , delay); 
    }else{ 
     speechRecognizer.startListening(speechRecognizerIntent); 
     Log.d(MainActivity,"Start instant listening to speech."); 
    } 


} 

我運行的是VUZIX與Android 6和VUZIX固件1.2(最新版)。

我得到錯誤代碼ERROR_SPEECH_TIMEOUT就像我會得到,如果沒有語音輸入(但我給)。

我已經試過幾件事情就像使用startActivityForResult方法獲取語音控制運行(具有相同的結果...)

+0

嘗試DroidSpeech,這需要照顧所有繁重的工作,並且它很容易實現 - https://github.com/vikramezhil/DroidSpeech –

回答

0

你必須設置權限爲您的應用程序和手動爲谷歌的APK,之後它在我的應用程序中使用Google語音識別器工作。