Search in sources :

Example 6 with RemoteControlClient

use of android.media.RemoteControlClient in project Shuttle by timusus.

the class MusicService method setupMediaSession.

private void setupMediaSession() {
    mediaSession = new MediaSessionCompat(this, "Shuttle", mediaButtonReceiverComponent, null);
    mediaSession.setCallback(new MediaSessionCompat.Callback() {

        @Override
        public void onPause() {
            pause();
            pausedByTransientLossOfFocus = false;
        }

        @Override
        public void onPlay() {
            play();
        }

        @Override
        public void onSeekTo(long pos) {
            seekTo(pos);
        }

        @Override
        public void onSkipToNext() {
            gotoNext(true);
        }

        @Override
        public void onSkipToPrevious() {
            prev();
        }

        @Override
        public void onStop() {
            pause();
            pausedByTransientLossOfFocus = false;
            releaseServiceUiAndStop();
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            Log.e("MediaButtonReceiver", "OnMediaButtonEvent called");
            MediaButtonIntentReceiver.MediaButtonReceiverHelper.onReceive(MusicService.this, mediaButtonEvent);
            return true;
        }
    });
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    // For some reason, MediaSessionCompat doesn't seem to pass all of the available 'actions' on as
    // transport control flags for the RCC, so we do that manually
    RemoteControlClient remoteControlClient = (RemoteControlClient) mediaSession.getRemoteControlClient();
    if (remoteControlClient != null) {
        remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    }
}
Also used : RemoteControlClient(android.media.RemoteControlClient) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Aggregations

RemoteControlClient (android.media.RemoteControlClient)6 PendingIntent (android.app.PendingIntent)5 Intent (android.content.Intent)5 SuppressLint (android.annotation.SuppressLint)1 TargetApi (android.annotation.TargetApi)1 ComponentName (android.content.ComponentName)1 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)1 MediaButtonIntentReceiver (org.moire.ultrasonic.receiver.MediaButtonIntentReceiver)1