如何在启动谷歌语音搜索(语音识别)后暂停我的应用程序的音频/音乐?
我制作了一个音频播放器,当用户启动谷歌语音搜索时我需要暂停它。
我刚刚尝试了两种方法,但都失败了
编写一个接收器来接收广播操作意图 “android.speech.action.RECOGNIZE_SPEECH”,但根本不起作用,无法接收来自语音搜索的回调。
编写一个服务来获取SpeechRecognizer,如下,(我的部分代码)
代码:
public class VoiceSearchMonitor extends Service
{
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// Get SpeechRecognizer
SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(context);
// Create a new RecognitionListener
RecognitionListener listener = new RecognitionListener()
{
@Override
public void onReadyForSpeech(Bundle params)
{
// *** I want to pause my audio here ***
}
@Override
public void onEndOfSpeech()
{
// *** I want to resume my audio here ***
}
}
// make an intent
Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
// Start listening
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);
}
}
就是收不到回调onReadyForSpeech
, 'onEndOfSpeech' 在我启动语音搜索后,因此我无法暂停音频。我只是想知道是否有办法获取谷歌语音搜索的 SpeechRecognizer 实例,以便我可以正确获取回调?
有人知道我的问题的答案吗?感谢您的帮助!
喜悦
I made an Audio-Player and I need to pause it when the user launches google Voice Search.
I just tried 2 ways, but all failed
Write an receiver to receive broadcast action intent
"android.speech.action.RECOGNIZE_SPEECH", but it doesn't work at all, can not receive callback from Voice Search.Write a service to get SpeechRecognizer, as following, (partial of my codes)
code:
public class VoiceSearchMonitor extends Service
{
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// Get SpeechRecognizer
SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(context);
// Create a new RecognitionListener
RecognitionListener listener = new RecognitionListener()
{
@Override
public void onReadyForSpeech(Bundle params)
{
// *** I want to pause my audio here ***
}
@Override
public void onEndOfSpeech()
{
// *** I want to resume my audio here ***
}
}
// make an intent
Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
// Start listening
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);
}
}
Just can not receive the callback onReadyForSpeech
, 'onEndOfSpeech' after I launch Voice Search, so there's no way for me to pause my audio. I just wonder if there is a way to get SpeechRecognizer instance of google's Voice Search, so that I can get the callback correctly??
Does anyone know the answer to my question? Thanks for helping!!!
Joy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这个问题,我最初采取了相同的路线,但解决方案是使用 AudioManager 而不是 SpeechRecognizer。
我们的应用程序已经使用 AudioManager.OnAudioFocusChangeListener 的自定义侦听器,并且正确响应 AUDIOFOCUS_GAIN 和 AUDIOFOCUS_LOSS 状态。事实证明,语音搜索会触发 AUDIOFOCUS_LOSS_TRANSIENT 状态,因此将其添加到更改侦听器会导致执行语音搜索时播放暂停。
总结一下:
创建监听器
获取 AudioManager、请求焦点并传递监听器
I took the same route initially for this issue, but the solution is to use the AudioManager rather than the SpeechRecognizer.
Our app was already using a custom listener for AudioManager.OnAudioFocusChangeListener and was correctly responding to the AUDIOFOCUS_GAIN and AUDIOFOCUS_LOSS states. It turns out that the voice search triggers an AUDIOFOCUS_LOSS_TRANSIENT state, so adding that to the change listener resulted in playback being paused while the voice search was performed.
To summarize:
Create the listener
Get the AudioManager, request focus, and pass the listener