BenIOs
BenIOs

Reputation: 479

Android, any way to controll the default music player

I would like to add a music controller to my app so the user could pause the music and start it again. It would also be great if the user could jump between songs.

I haven't found anything in the API but I want the same behavior that is available in any headset so there must be some API right?

Anyone who has an idea on how to control the default media app?

Upvotes: 0

Views: 913

Answers (2)

Anonymous
Anonymous

Reputation: 4910

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.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006744

I haven't found anything in the API but I want the same behavior that is available in any headset so there must be some API right?

No. The media player app responds to headset events.

Anyone who has an idea on how to control the default media app?

There is no published API for the default media app AFAIK, and it is not part of the Android SDK.

Upvotes: 2

Related Questions