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 , , . , ! !