【问题标题】:Voice recognition fails to work when the voice is under recording正在录制语音时语音识别无法正常工作
【发布时间】:2013-10-16 23:31:36
【问题描述】:

我正在开发一个功能,当按下按钮时,它将启动语音识别,同时记录用户所说的话。代码如下:

    button_start.setOnTouchListener( new View.OnTouchListener() 
    {
        @Override
        public boolean onTouch(View arg0, MotionEvent event) 
        {   
                if (pressed == false)
                {
                    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "zh-HK");
                    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1); 
                    sr.startListening(intent);
                    Log.i("111111","11111111");
                    pressed = true;
                }

                recordAudio();

            }

            if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
            {                   
                stopRecording();
            }
            return false;
        }
    });             
}

   public void recordAudio()
   {
      isRecording = true;   
      try 
      {
          mediaRecorder = new MediaRecorder();
          mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          mediaRecorder.setOutputFile(audioFilePath);
          mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          mediaRecorder.prepare();
      } 
      catch (Exception e) 
      {
          e.printStackTrace();
      }
      mediaRecorder.start();            
   }    

   public void stopRecording()
   {            
       if (isRecording)
       {    
           mediaRecorder.stop();
           mediaRecorder.reset();    // set state to idle
           mediaRecorder.release();
           mediaRecorder = null;
           isRecording = false;
       }
       else 
       {
           mediaPlayer.release();
           mediaPlayer.reset();
           mediaPlayer = null;
       }
   }




class listener implements RecognitionListener          
{
    // standard codes onReadyForSpeech, onBeginningOfSpeech, etc
}

问题:

我一步步制作了app,一开始app没有录音功能,语音识别完美。

经过多次测试,认为语音识别没问题,我开始使用MediaRecorder加入录音功能。

然后我进行了测试,一旦按下 button_start,ERROR3 AUDIO 消息甚至在我尝试说话之前就立即出现。

我播放录音。声音也被正确记录和保存。

发生了什么?为什么使用语音识别不能同时录音?

谢谢!

【问题讨论】:

  • 嗨,你解决了吗?需要做同样的事情!
  • @pearmarak 如何解决这个问题。过去 5 天我遇到了同样的问题,但没有得到任何解决方案。请帮我 。给我一些建议
  • 嗨,这个问题解决了吗?
  • 嗨!你找到解决办法了吗?

标签: android voice-recognition mediarecorder voice-recording


【解决方案1】:

--EDIT-- module for Opus-Record WHILE Speech-Recognition 也运行

--EDIT-- 'V1BETA1' 流式传输,连续识别,对sample project 进行了细微更改。更改“readData()”,因此“sData”中的原始 PCM 由 2 个线程(fileSink 线程、示例项目中的识别器 API 线程)共享。对于接收器,只需使用在每个“sData”IO 处刷新的 PCM 流连接一个编码器。记得对流进行 CLO,它会起作用。查看“writeAudiaDataToFile()”以了解有关 fileSink 的更多信息....

--编辑--见this thread

当您尝试这样做时,HAL 和麦克风缓冲区将会发生基本冲突:

speechRecognizer.startListening(recognizerIntent); // <-- needs mutex use of mic

mediaRecorder.start(); // <-- needs mutex use of mic

您只能选择上述操作中的一种或另一种来拥有麦克风底层的音频 API!

如果你想模仿 Google Keep 的功能,你只说一次,作为一个输入过程的输出(你的语音到麦克风),你会得到 2 种不同类型的输出(STT 和一个文件接收器,比如 MP3)那么你必须在它从麦克风退出 HAL 层时拆分一些东西。

例如:

  1. mic's buffer 中提取 PCM 16 的 RAW 音频

  2. Split the above buffer's bytes(您可以从缓冲区获取流并将流通过管道传输到 2 个位置)

  3. STRM 1 到 STT API 之前或之后编码(有 STT API 接受原始 PCM 16 或编码)

  4. STRM 2 传输到编码器,然后传输到 fileSink 以获取记录

Split 可以对麦克风产生的实际缓冲区或这些相同字节的派生流进行操作。

对于您要了解的内容,我建议您查看 getCurrentRecording()consumeRecording() here

STT API 参考:Google “pultz speech-api”。请注意,那里提到的 API 有一些用例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    相关资源
    最近更新 更多