Responding to media buttons on a headset in Android

Viewed 233

I am developing an app that needs to respond to button presses on a headset. I have been studying MediaSession and related classes, and trying to use this to respond to those button presses simply by displaying a toast that shows what button was pressed. From what I can tell, MediaSession.Callback is not being called at all and I am having problems understanding why. After reading through endless Q&As on SO and other places, from what I can tell I am doing exactly what others have done successfully. I have tried registering broadcast receivers, using various intents, simple key codes... nothing seems to work. Even switching from MediaSession to MediaSessionCompat had no effect. My belief is that there is something that needs to be implemented that will allow android to send media notifications to my app, but I cannot find anything on that, so I am not even sure if that is something that needs to be done. Any help is appreciated, and thanks in advance.

My implementation so far

public class MainActivity extends AppCompatActivity
{
    MediaSessionCompat m_MediaSession;

    final MediaSessionCompat.Callback m_MediaCallback = new MediaSessionCompat.Callback()
    {
        @Override
        public boolean onMediaButtonEvent(@NonNull Intent mediaButtonIntent)
        {
            Toast.makeText(MainActivity.this, "media button pressed", Toast.LENGTH_SHORT).show();
            return super.onMediaButtonEvent(mediaButtonIntent);
        }
        @Override
        public void onPlay()
        {
            Toast.makeText(MainActivity.this, "play button pressed", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onPause()
        {
            Toast.makeText(MainActivity.this, "pause button pressed", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        m_MediaSession = new MediaSessionCompat(getApplicationContext(), "MediaSessionTag");
        m_MediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                                MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        m_MediaSession.setCallback(m_MediaCallback);
        PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
        builder.setActions(PlaybackStateCompat.ACTION_PLAY |
                           PlaybackStateCompat.ACTION_PAUSE |
                           PlaybackStateCompat.ACTION_PLAY_PAUSE |
                           PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                           PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
               .setState(PlaybackStateCompat.STATE_NONE, 0, (float) 1.0);
        m_MediaSession.setPlaybackState(builder.build());
        m_MediaSession.setActive(true);
    }

    @Override
    public void onDestroy()
    {
        m_MediaSession.release();
        super.onDestroy();
    }
}
0 Answers
Related