语音识别和录音

时间:2013-11-01 11:47:44

标签: android voice-recognition voice-recording

我在尝试设计一个可以录制语音并同时将语音转换为文本的应用程序时遇到过这个错误。我使用Google API作为语音识别部分和audioRecorder对象进行录制。它没有用,因此我转而使用onBufferReceived()来检索进程中的字节(当用户说话时)。 Google API代码现在位于我的代码的onResults()部分,它可以在没有用户界面的情况下进行语音识别。

这是代码

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 个答案:

答案 0 :(得分:1)

根据comment on this similar post,在最新版本的Google搜索应用中未调用onBufferReceived。

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

相关问题