2013-11-01 64 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不叫在谷歌搜索应用程序的最新版本。

这也是我的经验,因此如果您想存储语音数据以及“翻译”它,您将不得不使用另一个语音识别提供程序。