如何在启动谷歌语音搜索(语音识别)后暂停我的应用程序的音频/音乐?

发布于 2024-11-02 02:00:50 字数 1487 浏览 1 评论 0原文

我制作了一个音频播放器,当用户启动谷歌语音搜索时我需要暂停它。

我刚刚尝试了两种方法,但都失败了

  1. 编写一个接收器来接收广播操作意图 “android.speech.action.RECOGNIZE_SPEECH”,但根本不起作用,无法接收来自语音搜索的回调。

  2. 编写一个服务来获取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

  1. 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.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-11-09 02:00:50

对于这个问题,我最初采取了相同的路线,但解决方案是使用 AudioManager 而不是 SpeechRecognizer。

我们的应用程序已经使用 AudioManager.OnAudioFocusChangeListener 的自定义侦听器,并且正确响应 AUDIOFOCUS_GAIN 和 AUDIOFOCUS_LOSS 状态。事实证明,语音搜索会触发 AUDIOFOCUS_LOSS_TRANSIENT 状态,因此将其添加到更改侦听器会导致执行语音搜索时播放暂停。

总结一下:

创建监听器

AudioManager.OnAudioFocusChangeListener audioFocusListener = 
    new AudioManager.OnAudioFocusChangeListener() {
    @Override
    public void onAudioFocusChange(int focusChange) {
        Logging.d(TAG, "audiofocus change");
        if(focusChange == AudioManager.AUDIOFOCUS_LOSS || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
            suspendPlayback();
        if(focusChange == AudioManager.AUDIOFOCUS_GAIN)
            resumePlayback();
    }
};

获取 AudioManager、请求焦点并传递监听器

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

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

AudioManager.OnAudioFocusChangeListener audioFocusListener = 
    new AudioManager.OnAudioFocusChangeListener() {
    @Override
    public void onAudioFocusChange(int focusChange) {
        Logging.d(TAG, "audiofocus change");
        if(focusChange == AudioManager.AUDIOFOCUS_LOSS || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
            suspendPlayback();
        if(focusChange == AudioManager.AUDIOFOCUS_GAIN)
            resumePlayback();
    }
};

Get the AudioManager, request focus, and pass the listener

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文