可靠暂停媒体播放全系统的Andr​​oid媒体播放、全系统、可靠、oid

2023-09-06 03:19:14 作者:来生★续缘

标题,使这种声音更简单比它..我想播出的意图,将暂停大多数音乐播放器。

The title makes this sound much simpler than it is.. I'm trying to broadcast an intent that will pause most music players.

我知道我可以使用创建的KeyEvent为将发送键code_MEDIA_PLAY_PAUSE这一点:

I know I can use create a KeyEvent for that will broadcast KEYCODE_MEDIA_PLAY_PAUSE with this:

    long eventTime = SystemClock.uptimeMillis(); 
    Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
    downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); 
    ctx.sendOrderedBroadcast(downIntent, null);

和的作品。这将暂停股票媒体播放器我怎么想它,以及支持耳机播放/暂停按钮,其他大多数的音乐播放器。但由于某些原因,它仅与股票的音乐播放器工作一次。我可以preSS按钮来调用这个,它会暂停音乐。如果我重新开始玩它,并再次打我的暂停按钮,这是行不通的。有一次,我重启设备,它会再次合作。但后来,潘多拉,它都能正常工作,因为它应该。

And that works. It will pause the stock media player how I want it to, along with most other music players that support headphone play/pause buttons. But for some reason, it only works once with the stock music player. I can press the button to call this, and it'll pause music. If I start playing it again, and hit my pause button again, it doesn't work. Once I reboot the device, it'll work once again. But then, with Pandora, it works consistently as it should.

我想我也许能解决的,只是暂停,而无需使用的KeyEvent。我想这与AudioManager和另一个意图:

I thought I might be able to work around that and just pause without using a KeyEvent. I tried this with AudioManager and another intent:

    AudioManager mAudioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);    
    if (mAudioManager.isMusicActive()) {
        Intent mediaIntent = new Intent("com.android.music.musicservicecommand");
        mediaIntent.putExtra("command", "pause");
        ctx.sendBroadcast(mediaIntent);
    }

这也适用,它都能正常工作。但它的内在脆弱,因为直接发送意图到Android音乐播放器,这是它会工作的唯一的球员。潘多拉,Last.FM等,并没有控制我需要它与大多数音乐播放器的工作。

And this also works, and it works consistently. But it's inherently fragile, because by sending the intent directly to the Android music player, that's the only player it'll work with. No control of Pandora, Last.FM, etc. and I need it to work with most music players.

我只是不知道下一步去哪里。我不挑剔什么样的解决方案,我只要它的作品给出。如果你能帮助我做了KeyEvent的工作,太棒了,如果你有一些完全不同的解决方案,精彩绝伦。我只是希望它的工作!谢谢!

I'm just not sure where to go next. I'm not picky about what kind of solution I'm given as long as it works. If you can help me make the KeyEvent work, fantastic, if you have some totally different solution, fantastic. I just want it to work! Thanks!

推荐答案

要暂停整个系统的音响,你不开始的意图,而是从系统请求音频焦点。

To pause system wide audio, you don't start an intent but rather request audio focus from the system.

所以,当你想暂停一切执行以下操作(在的onCreate建议):

So when you wish to pause everything do the following (suggested in onCreate):

//The Variables we'll need to create
AudioManager am;
OnAudioFocusChangeListener af;

//I do nothing with this listener, but it's required for the next step.
af = new OnAudioFocusChangeListener() {
      public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
          // Lower the volume
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                // Raise it back to normal
           }    
      }    
}; 

//Do the actual pause
am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int request = am.requestAudioFocus(af,
    AudioManager.STREAM_MUSIC,
    AudioManager.AUDIOFOCUS_GAIN);

但是,不要忘了让使用去聚焦:

But don't forget to let go of focus using:

am.abandonAudioFocus(af);