gpt4 book ai didi

android - 录音时语音识别不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:54:46 25 4
gpt4 key购买 nike

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

    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没有录音功能,语音识别完美。

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

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

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

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

谢谢!

最佳答案

--编辑-- module对于 Opus-Record,同时语音识别也运行

--编辑--“V1BETA1”流式传输,连续识别,对 sample project 稍作改动.更改“readData()”,因此“sData”中的原始 PCM 由 2 个线程共享(示例项目中的 fileSink 线程、recognizerAPI 线程)。对于接收器,只需使用在每个“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 和 fileSink,比如 MP3)< strong>然后你必须在它从麦克风退出 HAL 层时拆分某些东西。

例如:

  1. mic's buffer 中提取原始音频作为 PCM 16

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

  3. 在编码之前或之后将 STRM 1 发送到 STT 的 API(有些 STT API 接受 Raw PCM 16 或编码)

  4. STRM 2 到编码器,然后到 fileSink 以捕获录音

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

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

STT API 引用:Google “pultz speech-api”。请注意,那里提到了 API 的用例。

关于android - 录音时语音识别不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19274880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com