app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java
private void initMediaSessions() {
        // Make sure the media player will acquire a wake-lock while playing. If we don't do
        // that, the CPU might go to sleep while the song is playing, causing playback to stop.
        //
        // Remember that to use this, we have to declare the android.permission.WAKE_LOCK
        // permission in AndroidManifest.xml.
        mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

        ComponentName eventReceiver = new ComponentName(getApplicationContext().getPackageName(),
                MediaButtonIntentReceiver.class.getName());
        PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(
                getApplicationContext(),
                0,
                new Intent(Intent.ACTION_MEDIA_BUTTON),
                PendingIntent.FLAG_UPDATE_CURRENT
        );

        mSession = new MediaSessionCompat(getApplicationContext(), "simple player session",
                eventReceiver, buttonReceiverIntent);

        try {
            mController = new MediaControllerCompat(getApplicationContext(), mSession.getSessionToken());

            mSession.setCallback(
                    new MediaSessionCompat.Callback() {
                        @Override
                        public void onPlay() {
                            super.onPlay();
                            buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
                        }

                        @Override
                        public void onPause() {

                            super.onPause();
                            pauseVideo();
                            buildNotification(generateAction(android.R.drawable.ic_media_play, "Play", ACTION_PLAY));
                        }

                        @Override
                        public void onSkipToNext() {
                            super.onSkipToNext();
                            if (!isStarting) {
                                playNext();
                            }
                            buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
                        }

                        @Override
                        public void onSkipToPrevious() {
                            super.onSkipToPrevious();
                            if (!isStarting) {
                                playPrevious();
                            }
                            buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
                        }

                        @Override
                        public void onStop() {
                            super.onStop();
                            stopPlayer();
                            //remove notification and stop service
                            NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                            notificationManager.cancel(1);
                            Intent intent = new Intent(getApplicationContext(), BackgroundAudioService.class);
                            stopService(intent);
                        }

                        @Override
                        public void onSetRating(RatingCompat rating) {
                            super.onSetRating(rating);
                        }
                    }
            );
        } catch (RemoteException re) {
            re.printStackTrace();
        }
    }
/**
     * Initializes media sessions and receives media events
     */
Initializes media sessions and receives media events
Initializes media sessions and receives media events
private void initMediaSessions() {
         Remember that to use this, we have to declare the android.permission.WAKE_LOCK
         permission in AndroidManifest.xml.
        mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        ComponentName eventReceiver = new ComponentName(getApplicationContext().getPackageName(),
                MediaButtonIntentReceiver.class.getName());
        PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(
                getApplicationContext(),
                0,
                new Intent(Intent.ACTION_MEDIA_BUTTON),
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        mSession = new MediaSessionCompat(getApplicationContext(), "simple player session",
                eventReceiver, buttonReceiverIntent);
        try {
            mController = new MediaControllerCompat(getApplicationContext(), mSession.getSessionToken());
            mSession.setCallback(
                    new MediaSessionCompat.Callback() {
                        @Override
                        public void onPlay() {
                            super.onPlay();
                            buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
                        }
                        @Override
                        public void onPause() {
                            super.onPause();
                            pauseVideo();
                            buildNotification(generateAction(android.R.drawable.ic_media_play, "Play", ACTION_PLAY));
                        }
                        @Override
                        public void onSkipToNext() {
                            super.onSkipToNext();
                            if (!isStarting) {
                                playNext();
                            }
                            buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
                        }
                        @Override
                        public void onSkipToPrevious() {
                            super.onSkipToPrevious();
                            if (!isStarting) {
                                playPrevious();
                            }
                            buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
                        }
                        @Override
                        public void onStop() {
                            super.onStop();
                            stopPlayer();
                            remove notification and stop service
                            NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                            notificationManager.cancel(1);
                            Intent intent = new Intent(getApplicationContext(), BackgroundAudioService.class);
                            stopService(intent);
                        }
                        @Override
                        public void onSetRating(RatingCompat rating) {
                            super.onSetRating(rating);
                        }
                    }
            );
        } catch (RemoteException re) {
            re.printStackTrace();
        }
    }
private void initMediaSessions ( ) { mMediaPlayer . setWakeMode ( getApplicationContext ( ) , PowerManager . PARTIAL_WAKE_LOCK ) ; ComponentName eventReceiver = new ComponentName ( getApplicationContext ( ) . getPackageName ( ) , MediaButtonIntentReceiver . class . getName ( ) ) ; PendingIntent buttonReceiverIntent = PendingIntent . getBroadcast ( getApplicationContext ( ) , 0 , new Intent ( Intent . ACTION_MEDIA_BUTTON ) , PendingIntent . FLAG_UPDATE_CURRENT ) ; mSession = new MediaSessionCompat ( getApplicationContext ( ) , "simple player session" , eventReceiver , buttonReceiverIntent ) ; try { mController = new MediaControllerCompat ( getApplicationContext ( ) , mSession . getSessionToken ( ) ) ; mSession . setCallback ( new MediaSessionCompat . Callback ( ) { @ Override public void onPlay ( ) { super . onPlay ( ) ; buildNotification ( generateAction ( android . R . drawable . ic_media_pause , "Pause" , ACTION_PAUSE ) ) ; } @ Override public void onPause ( ) { super . onPause ( ) ; pauseVideo ( ) ; buildNotification ( generateAction ( android . R . drawable . ic_media_play , "Play" , ACTION_PLAY ) ) ; } @ Override public void onSkipToNext ( ) { super . onSkipToNext ( ) ; if ( ! isStarting ) { playNext ( ) ; } buildNotification ( generateAction ( android . R . drawable . ic_media_pause , "Pause" , ACTION_PAUSE ) ) ; } @ Override public void onSkipToPrevious ( ) { super . onSkipToPrevious ( ) ; if ( ! isStarting ) { playPrevious ( ) ; } buildNotification ( generateAction ( android . R . drawable . ic_media_pause , "Pause" , ACTION_PAUSE ) ) ; } @ Override public void onStop ( ) { super . onStop ( ) ; stopPlayer ( ) ; NotificationManager notificationManager = ( NotificationManager ) getApplicationContext ( ) . getSystemService ( Context . NOTIFICATION_SERVICE ) ; notificationManager . cancel ( 1 ) ; Intent intent = new Intent ( getApplicationContext ( ) , BackgroundAudioService . class ) ; stopService ( intent ) ; } @ Override public void onSetRating ( RatingCompat rating ) { super . onSetRating ( rating ) ; } } ) ; } catch ( RemoteException re ) { re . printStackTrace ( ) ; } }
Initializes media sessions and receives media events
Initializes media sessions and receives media events
// Make sure the media player will acquire a wake-lock while playing. If we don't do // that, the CPU might go to sleep while the song is playing, causing playback to stop. // // Remember that to use this, we have to declare the android.permission.WAKE_LOCK // permission in AndroidManifest.xml. //remove notification and stop service