androidandroid-mediasessionmediabrowserservicecompatmediabrowser

PlaybackState for MediaControllerCompat


I was following through this guide to building a background audio app with MediaSessionCompat and bumped into a problem.

In my Activity, I connect my UI to media controller as below

private MediaBrowserCompat.ConnectionCallback mediaBrowserConnectionCallbacks = new MediaBrowserCompat.ConnectionCallback() {
    @Override
    public void onConnected() {
        super.onConnected();
        try {
            mediaControllerCompat = new MediaControllerCompat(StreamPlayerActivity.this, mediaBrowserCompat.getSessionToken());
            mediaControllerCompat.registerCallback(mediaControllerCallbacks);
            mediaControllerCompat.setMediaController(StreamPlayerActivity.this, mediaControllerCompat);
            mediaControllerCompat.getTransportControls().playFromMediaId(String.valueOf(R.raw.hann), null);
            Log.d(TAG, "connected\t" + currentState + "\t" + mediaControllerCompat.getPlaybackState());
        } catch( RemoteException e ) {
            e.printStackTrace();
            Log.d(TAG, "connection failed");
        }
    }
};

private MediaControllerCompat.Callback mediaControllerCallbacks = new MediaControllerCompat.Callback() {
    @Override
    public void onPlaybackStateChanged(PlaybackStateCompat state) {
        super.onPlaybackStateChanged(state);
        if( state == null ) {
            return;
        }

        switch( state.getState() ) {
            case PlaybackStateCompat.STATE_PLAYING: {
                currentState = STATE_PLAYING;
                break;
            }
            case PlaybackStateCompat.STATE_PAUSED: {
                currentState = STATE_PAUSED;
                break;
            }
        }
    }
};

In my Service, I build my session as follows

private MediaSessionCompat.Callback mediaSessionCallbacks = new MediaSessionCompat.Callback() {
    @Override
    public void onPlay() {
        super.onPlay();
        if( !successfullyRetrievedAudioFocus() ) {
            return;
        }

        mediaSessionCompat.setActive(true);
        setMediaPlaybackState(PlaybackStateCompat.STATE_PLAYING);

        mediaPlayer.start();
        Log.d(TAG, "mp started");
    }

    @Override
    public void onPause() {
        super.onPause();

        if( mediaPlayer.isPlaying() ) {
            mediaPlayer.pause();
            setMediaPlaybackState(PlaybackStateCompat.STATE_PAUSED);
        }
    }

    @Override
    public void onPlayFromMediaId(String mediaId, Bundle extras) {
        super.onPlayFromMediaId(mediaId, extras);

        try {
            AssetFileDescriptor afd = getResources().openRawResourceFd(Integer.valueOf(mediaId));
            if (afd == null) {
                return;
            }

            try {
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                Log.d(TAG, "datasource set");
            } catch (IllegalStateException e) {
                mediaPlayer.release();
                initMediaPlayer();
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            }

            afd.close();
            initMediaSessionMetadata();

        } catch (IOException e) {
            return;
        }

        try {
            mediaPlayer.prepare();
            Log.d(TAG, "media prepared");
        } catch (IOException e) { Log.d(TAG, "media preparation failed");
        }
    }
};

Basically, I've followed the website's guide line by line, except that I left out Notification that displays current audio's information. When testing on my device, I get a NullPointerException error.

The logcat located this to be occurring when trying to mediaControllerCompat.getPlaybackState().getState(). I figured that my MediaControllerCompat.getPlaybackState() is null. Even the Android Devleopers Site for MediaControllerCompat does not seem to resolve my issue here. Maybe it's just me but I don't see any code that defines MediaControllerCompat's PlaybackState before trying to retrieve its value.

How do I resolve this?

Thank you in advance for help.


Solution

  • It was a silly mistake. I for got to add setMediaPlaybackState(PlaybackStateCompat.STATE_PLAYING); after my mediaPlayer was prepared.