Remy Grandin
Remy Grandin

Reputation: 1686

Send media action on android

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 ?

Upvotes: 22

Views: 2092

Answers (3)

Wills
Wills

Reputation: 61

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

Upvotes: 0

jrsall92
jrsall92

Reputation: 592

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);

Upvotes: 1

M.Baraka
M.Baraka

Reputation: 755

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

maybe that would help you

Upvotes: 0

Related Questions