Search code examples
javaandroidcontrolsmedia-player

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 ?


Solution

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