4

Developing a music app. In my Music Service, I have written a custom broadcast receiver. It works with Intent.ACTION_HEADSET_PLUG but not with Intent.ACTION_MEDIA_BUTTON.

Please guide on how to control music controls from bluetooth devices (Play/Pause/Next/Previous).

Code for Intent.ACTION_HEADSET_PLUG is:

@Override
    public void onReceive(Context context, Intent intent) {

        // aux
        if(intent.getAction().equals(Intent.ACTION_HEADSET_PLUG))
        {
            int state = intent.getIntExtra("state", -1);

            if(state == 0)
            {
                // Headset is unplugged. PAUSE
                pauseSong();
                sendBroadcast();
            }
            else if(state == 1)
            {
                // headset is plugged
                resumeSong();
                sendBroadcast();
            }

        }
    }
WideFide
  • 335
  • 1
  • 3
  • 18

2 Answers2

2

As explained in the Media playback the right way talk, you must be registered as the 'preferred media app'. This is much easier when you use MediaSessionCompat as explained in the MediaSessionCompat video:

ComponentName mediaButtonReceiver =
  new ComponentName(context, YourBroadcastReceiver.class);
MediaSessionCompat mediaSession = 
  new MediaSessionCompat(context,
  tag, // Debugging tag, any string
  mediaButtonReceiver,
  null);
mediaSession.setFlags(
  MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | 
  MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(this); // a MediaSessionCompat.Callback

// This is what enables media buttons and should be called
// Immediately after getting audio focus
mediaSession.setActive(true);
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I added the code to my project but it didn't work. I watched the video and saw that more things to be added to the code before it starts working. We need to setMetadata and setState to mediaSession. But how to do it was not explained. Any idea sir? – WideFide Mar 17 '16 at 12:22
  • Just figured it out through this http://stackoverflow.com/questions/28547024/mediasession-does-not-show-background-on-lock-screen :) – WideFide Mar 17 '16 at 12:49
1

The accepted answer doesn't seem to work

I achieved it the other way with ExoPlayer

step #1 added dependencies

    implementation 'com.google.android.exoplayer:exoplayer:2.13.1'
    implementation 'com.google.android.exoplayer:extension-mediasession:2.13.1'

step #2

        mediaSession = MediaSessionCompat(this, "Vion")
        val mediaSessionConnector = MediaSessionConnector(mediaSession!!)
        mediaSessionConnector.setMediaButtonEventHandler(object: MediaSessionConnector.MediaButtonEventHandler{
            override fun onMediaButtonEvent(player: Player, controlDispatcher: ControlDispatcher, mediaButtonEvent: Intent): Boolean {
                val event: KeyEvent = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) ?: return false
                if(event.action == KeyEvent.ACTION_UP) {
                    when(event.keyCode) {
                        KeyEvent.KEYCODE_MEDIA_PREVIOUS -> {
                           
                        }
                        KeyEvent.KEYCODE_MEDIA_PAUSE -> {
                            
                        }
                        KeyEvent.KEYCODE_MEDIA_PLAY -> {
                            
                        }
                        KeyEvent.KEYCODE_MEDIA_NEXT -> {
                            
                        }
                    }
                }
                return true
            }
        })
        mediaSessionConnector.setPlayer(exoPlayer)
Nasib
  • 1,173
  • 1
  • 13
  • 23