2013-11-01 60 views
0

我在嘗試設計可以錄製語音並將語音同時轉換爲文本的應用程序時遇到了此錯誤。我已經將Google API用於語音識別部分和audioRecorder對象進行錄製。它沒有工作,因此我轉而使用onBufferReceived()來檢索過程中的字節(當用戶說話時)。 Google API代碼現在位於我的代碼的onResults()部分,該代碼在沒有UI的情況下進行語音識別。語音識別和錄音

下面的代碼

class listener implements RecognitionListener   
    { 

     public void onBufferReceived(byte[] buffer) 
     { 
      bufferBytes = buffer; 
     // capturing the buffer into bufferBytes static variable as the user speaks 
      try { 

       bos = new BufferedOutputStream(fos); 
       bos.write(buffer); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      finally{ 
       if(bos != null){ 
        try{ 
         bos.flush(); 
         bos.close(); 
        }catch(Exception e){} 
       } 
      } 

     } 
     public void onEndOfSpeech() 
     { 
      speakButton.setText(getString(R.string.Speak)); 
      Log.d(TAG, "onEndofSpeech"); 
     } 
     public void onError(int error) 
     { 
      Log.d(TAG, "error " + error); 
      mSendText.setVisibility(View.VISIBLE); 
      mSendText.setText("error retriving text, please once check your Data Connection "); 
     } 
     public void onResults(Bundle results)     
     { 
      String str = new String(); 
      Log.d(TAG, "onResults " + results); 
      ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
      for (int i = 0; i < data.size(); i++) 
      { 
       Log.d(TAG, "result " + data.get(i)); 
       str += data.get(i); 
      } 
      mSendText.setVisibility(View.VISIBLE); 
      mSendText.setText(data.get(0)+"");  
     } 
    } 

回答

1

根據comment on this similar post,onBufferReceived不叫在谷歌搜索應用程序的最新版本。

這也是我的經驗,因此如果您想存儲語音數據以及「翻譯」它,您將不得不使用另一個語音識別提供程序。