捕获发送到 Google 语音识别服务器的音频
为了通过 Google 服务器识别语音,我将 SpeechRecognizer 类与 RecognitionListener 结合使用,如 Stephan 的 回答 这个问题。此外,我尝试使用 RecognitionListener 中的 onBufferReceived() 回调捕获正在识别的音频信号,如下所示:
byte[] sig = new byte[500000] ;
int sigPos = 0 ;
...
public void onBufferReceived(byte[] buffer) {
System.arraycopy(buffer, 0, sig, sigPos, buffer.length) ;
sigPos += buffer.length ;
}
...
这似乎工作正常,除非 SpeechRecognizer 无法连接到 Google 服务器,当一大块音频没有复制到上述服务器时sig
数组,并抛出HTTP连接超时异常。 SpeechRecognizer最终连接到Google服务器,识别结果表明收到了完整的音频信号;只有 sig
数组缺少一些音频块。
有人遇到同样的问题吗?有解决方案的提示吗?谢谢你!
to recognize speech by Google server, I use SpeechRecognizer class in combination with RecognitionListener as suggested in Stephan's answer to this question . In addition, I try to capture the audio signal being recognized by using onBufferReceived() callback from RecognitionListener like:
byte[] sig = new byte[500000] ;
int sigPos = 0 ;
...
public void onBufferReceived(byte[] buffer) {
System.arraycopy(buffer, 0, sig, sigPos, buffer.length) ;
sigPos += buffer.length ;
}
...
This seems working fine, except when SpeechRecognizer fails connecting to the Google server, when a chunk of audio is not copied into the above-mentioned sig
array, and an HTTP connection time-out exception is thrown. SpeechRecognizer eventually connects to the Google server and recognition results indicate that a complete audio signal was received; only the sig
array is missing some audio chunk(s).
Does anybody experience the same problem? Any hint for solution? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我倾向于说这可能是识别服务行为的不一致,甚至可能是您使用的 Android 版本中的错误。但是,文档指出,不能保证调用此方法,因此它符合规范。到目前为止我注意到的是以下内容(在 Android 2.3.4 上):我在记录时获取字节,但是如果存在
SocketTimeout
,它会在一段时间后尝试将数据重新发送到服务器,但无需再次调用onBufferReceived
来获取相同的数据。用于测试的代码与您在帖子中链接的代码相同。您认为为什么您在该方法中收到的音频中缺少一些块?如果只是丢失了几个块,甚至可能会出现这样的情况:尽管丢失了这些块,但识别仍然有效。
I tend to say this might be a inconsistency in the behavior of the recognition service, maybe even a bug in the Android version you use. However, the documentation states, that it is not guaranteed that this method is called so it would fit into the specification. What I noticed so far is the following (on Android 2.3.4): I get the bytes while recording, but if there is for example a
SocketTimeout
it tries to resend the data to the server after some time, but without callingonBufferReceived
again for the same data. The code used to test that was the same as the one you have linked in your posting.Why do you think some chunks are missing from the audio you received in the method? If it were only a few chunks missing, it might even be the case, that the recognition worked although those chunks were missing.
在现代版本中 onBufferReceieved 不起作用,您可以检查 record/save而是来自语音识别意图的音频。
In modern versions onBufferReceieved does not work, you can check record/save audio from voice recognition intent instead.
实现这一目标的最佳方法是反其道而行之。使用
AudioRecord
捕获音频数据(我建议使用VOICE_COMMUNICATION
而不是MIC
作为输入,以便获得真正干净的音频),然后将其传递给SpeechRecognizer
。 :)Best way to achieve this is round the other way. Capture your audio data using the
AudioRecord
, (I'd recommend usingVOICE_COMMUNICATION
rather thanMIC
as an input so you get really clean audio), then pass it through to theSpeechRecognizer
. :)