Send media action on android

1.9k views Asked by At

I'm looking for a way to send global media action (Play/pause, Next, Previous) to the phone, much like it can take it froma bluetooth paired radio/speaker.

I have found the MediaController class (http://developer.android.com/reference/android/widget/MediaController.html) but it seem more to be to implement the receiver of those action and i'm looking for a sender.

However, I can't seam to find any reference to this anywhere in the android doc...

Anyone already stumble on this ?

3

There are 3 answers

0
jrsall92 On BEST ANSWER

So if your activity is called MainActivity you can do:

//for play/pause toggle
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "togglepause");
MainActivity.this.sendBroadcast(i);

//for next
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "next");
MainActivity.this.sendBroadcast(i);

//for prev
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "previous");
MainActivity.this.sendBroadcast(i);

//to change volume
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, *value*, AudioManager.FLAG_SHOW_UI);
0
M.Baraka On

take a look on BroadCastReceiver with actions HEADSET_PLUG and MEDIA_BUTTON, also LINK

maybe that would help you

0
Wills On

You can easily send broadcast actions for media (and many other events) with ActivityManager via adb e.g. adb shell am broadcast -a android.intent.action.MEDIA_MOUNTED will mount your media. See many more here: https://developer.android.com/reference/android/content/Intent.html