Question

When I send to the default player

Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
ACTIVITY.sendBroadcast(i);

it starts to play. When I send

Intent i = new Intent("com.android.music.musicservicecommand");
        i.putExtra("command", "stop");
        ACTIVITY.sendBroadcast(i);

It stops. But when I try to play again then it doesn't play. It just plays for a second and then stops again. I saw on the log I get two intents. The

intent.getBooleanExtra("playing", false);

The first time is "true" and then is "false". This isn't happening when I try the "togglepause". I also noticed that when I send play both the Android music app and the Google Play music starts.

I figured out that Google Play music was causing that. Apparently, both apps get the intent and when I send play, the music player answers with isPlaying = true and the Google Play music isPlaying = false. How to fix that or how to send or to receive only fron the default player?

Était-ce utile?

La solution

A little late but posting for others who are searching for this. You can control default media player by sending an ordered broadcast to the system that a media button has been pressed and released.The following code will play or pause music depending on the current state:

  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); 
  sendOrderedBroadcast(downIntent, null); 

  Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 
  KeyEvent upEvent = new KeyEvent(eventtime, eventtime, 
  KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); 
  upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); 
  sendOrderedBroadcast(upIntent, null); 

The same way you can alert the system that the next or previous track button has been pressed.

Autres conseils

Instead of issuing the "stop" command, have you tried "pause" ?

   Intent i = new Intent("com.android.music.musicservicecommand");
   i.putExtra("command", "pause");

It seems possible to call pause/play as many times as you like and it restarts just fine.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top