我正在开发一个功能,当按下按钮时,它将启动语音识别,同时记录用户说的内容。代码如下:
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
}
我已经逐步完成了应用程序,起初应用程序没有录制功能,语音识别功能完美。
在我多次测试并认为语音识别正常后,我开始使用MediaRecorder
合并录音功能。
然后我测试了,按下button_start后,即使在我试图发言之前, ERROR3 AUDIO
消息也会立即显示。
我播放录音。语音也会被正确记录和保存。
发生了什么事?为什么使用语音识别时无法同时录制?
谢谢!
答案 0 :(得分:5)
- 编辑 - module用于Opus-Record WHILE语音识别也可以运行
- 编辑 - 'V1BETA1'流媒体,连续,识别,对sample project进行微小更改。改变' readData()',因此'sData'中的原始PCM由2个线程共享(fileSink线程,来自示例项目的recognizerAPI线程)。对于接收器,只需使用在每个'sData'IO处刷新的PCM流连接编码器。记得CLO流,它会工作。有关fileSink的更多信息,请查看“writeAudiaDataToFile()”....
- 编辑 - 见this thread
当您尝试执行以下操作时,HAL和麦克风缓冲区会发生基本冲突:
speechRecognizer.startListening(recognizerIntent); // <-- needs mutex use of mic
和
mediaRecorder.start(); // <-- needs mutex use of mic
您只能选择上述操作中的一个或另一个来拥有麦克风底层的音频API!
如果你想模仿Google Keep的功能,你只会说一次,并且作为一个输入过程的输出(你的语音转换为麦克风),你会得到两种不同类型的输出(STT和一个说MP3的文件)< strong>那么你必须拆分从麦克风退出HAL层的东西。
例如:
Split the above buffer's bytes(您可以从缓冲区获取流并将流管道2个位置)
在编码之前或之后STRM 1到STT的API(有STT API接受原始PCM 16或编码)
STRM 2到编码器,然后到fileSink以捕获录音
Split可以在麦克风产生的实际缓冲区上运行,也可以在相同字节的派生流上运行。
对于您的目标,我建议您查看getCurrentRecording()
和consumeRecording()
here。
STT API参考:Google “pultz speech-api”。请注意,那里提到的API有用例。