Reliability of pausing multimedia playback on an Android system

The name makes this sound much easier than it is. I am trying to convey an intention that pauses most music players.

I know that I can use Create KeyEvent, which will translate 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);

And it works. It pauses the media player as I want, along with most other music players that support the play / pause buttons for the headphones. But for some reason it only works once with a music player. I can press a button to make a call and it will stop the music. If I start playing again and press the pause button again, this will not work. As soon as I reboot the device, it will work again. But then, with Pandora, he works sequentially, as it should.

I thought I could get around this and just pause without using KeyEvent. I tried this with AudioManager and with another intention:

    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, , . Pandora, Last.FM .., , .

, . , , . KeyEvent , , . , ! !

+5
2

, , .

, , ( onCreate):

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

//I do nothing with this listener, but it 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);

, :

am.abandonAudioFocus(af);
+2

. , , , . keydown, , , / . .

Intent mediaEvent = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY); 
mediaEvent.putExtra(Intent.EXTRA_KEY_EVENT, event);
context.sendBroadcast(mediaEvent);

new Timer().schedule(new TimerTask() {
    @Override
    public void run() {
        Intent mediaEvent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent event = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PLAY); 
        mediaEvent.putExtra(Intent.EXTRA_KEY_EVENT, event);
        context.sendBroadcast(mediaEvent);
    }
}, 100);
+4

All Articles