Java Code Examples for android.support.v4.media.session.PlaybackStateCompat#STATE_PAUSED

The following examples show how to use android.support.v4.media.session.PlaybackStateCompat#STATE_PAUSED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MediaSessionPlaybackActivity.java    From media-samples with Apache License 2.0 6 votes vote down vote up
private void initializeMediaSession() {
    mSession = new MediaSessionCompat(this, TAG);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                    | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mSession.setActive(true);
    MediaControllerCompat.setMediaController(this, mSession.getController());

    MediaMetadataCompat metadata = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mMovieView.getTitle())
            .build();
    mSession.setMetadata(metadata);

    MediaSessionCallback mMediaSessionCallback = new MediaSessionCallback(mMovieView);
    mSession.setCallback(mMediaSessionCallback);

    int state =
            mMovieView.isPlaying()
                    ? PlaybackStateCompat.STATE_PLAYING
                    : PlaybackStateCompat.STATE_PAUSED;
    updatePlaybackState(
            state,
            MEDIA_ACTIONS_ALL,
            mMovieView.getCurrentPosition(),
            mMovieView.getVideoResourceId());
}
 
Example 2
Source File: BackgroundAudioService.java    From YouTube-In-Background with MIT License 6 votes vote down vote up
/**
     * Pauses video
     */
    private void pauseVideo()
    {
        if (playState == PlaybackStateCompat.STATE_PLAYING) {
            // Pause media player and cancel the 'foreground service' state.
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                currentPosition = mediaPlayer.getCurrentPosition();
            }
            // while paused, retain the MediaPlayer but give up audio focus
            relaxResources(false);
        }
        playState = PlaybackStateCompat.STATE_PAUSED;
        unregisterAudioNoisyReceiver();
//        stopForeground(true);
//        if (mCallback != null) {
//            mCallback.onPlaybackStatusChanged(playState);
//        }
    }
 
Example 3
Source File: Playback.java    From react-native-streaming-audio-player with MIT License 6 votes vote down vote up
public void pause() {
    if (mState == PlaybackStateCompat.STATE_PLAYING) {
        // Pause media player and cancel the 'foreground service' state.
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
            mCurrentPosition = mMediaPlayer.getCurrentPosition();
        }
        // while paused, retain the MediaPlayer but give up audio focus
        relaxResources(false);
    }
    mState = PlaybackStateCompat.STATE_PAUSED;
    if (mCallback != null) {
        mCallback.onPlaybackStateChanged(mState);
    }

    unregisterAudioNoisyReceiver();
}
 
Example 4
Source File: TrackFragment.java    From Melophile with Apache License 2.0 6 votes vote down vote up
@OnClick(R.id.play_pause)
public void playPause() {
  lastState = null;
  MediaControllerCompat controllerCompat = MediaControllerCompat.getMediaController(getActivity());
  PlaybackStateCompat stateCompat = controllerCompat.getPlaybackState();
  if (stateCompat != null) {
    MediaControllerCompat.TransportControls controls =
            controllerCompat.getTransportControls();
    switch (stateCompat.getState()) {
      case PlaybackStateCompat.STATE_PLAYING:
      case PlaybackStateCompat.STATE_BUFFERING:
        controls.pause();
        break;
      case PlaybackStateCompat.STATE_NONE:
      case PlaybackStateCompat.STATE_PAUSED:
      case PlaybackStateCompat.STATE_STOPPED:
        controls.play();
        break;
      default:
        Log.d(TAG, "State " + stateCompat.getState());
    }
  }
}
 
Example 5
Source File: PlayerService.java    From AndroidAudioExample with MIT License 6 votes vote down vote up
private void refreshNotificationAndForegroundStatus(int playbackState) {
    switch (playbackState) {
        case PlaybackStateCompat.STATE_PLAYING: {
            startForeground(NOTIFICATION_ID, getNotification(playbackState));
            break;
        }
        case PlaybackStateCompat.STATE_PAUSED: {
            NotificationManagerCompat.from(PlayerService.this).notify(NOTIFICATION_ID, getNotification(playbackState));
            stopForeground(false);
            break;
        }
        default: {
            stopForeground(true);
            break;
        }
    }
}
 
Example 6
Source File: LocalPlayback.java    From klingar with Apache License 2.0 6 votes vote down vote up
@Override @State public int getState() {
  if (exoPlayer == null) {
    return exoPlayerNullIsStopped ? PlaybackStateCompat.STATE_STOPPED
        : PlaybackStateCompat.STATE_NONE;
  }
  switch (exoPlayer.getPlaybackState()) {
    case Player.STATE_IDLE:
    case Player.STATE_ENDED:
      return PlaybackStateCompat.STATE_PAUSED;
    case Player.STATE_BUFFERING:
      return PlaybackStateCompat.STATE_BUFFERING;
    case Player.STATE_READY:
      return exoPlayer.getPlayWhenReady() ? PlaybackStateCompat.STATE_PLAYING
          : PlaybackStateCompat.STATE_PAUSED;
    default:
      return PlaybackStateCompat.STATE_NONE;
  }
}
 
Example 7
Source File: LocalPlayback.java    From YouTube-In-Background with MIT License 6 votes vote down vote up
@Override
public int getState()
{
    if (exoPlayer == null) {
        return exoPlayerNullIsStopped ? PlaybackStateCompat.STATE_STOPPED : PlaybackStateCompat.STATE_NONE;
    }

    switch (exoPlayer.getPlaybackState()) {
        case STATE_IDLE:
            return PlaybackStateCompat.STATE_PAUSED;
        case STATE_BUFFERING:
            return PlaybackStateCompat.STATE_BUFFERING;
        case STATE_READY:
            return exoPlayer.getPlayWhenReady() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
        case STATE_ENDED:
            return PlaybackStateCompat.STATE_PAUSED;
        default:
            return PlaybackStateCompat.STATE_NONE;
    }
}
 
Example 8
Source File: MusicService.java    From android-MediaBrowserService with Apache License 2.0 6 votes vote down vote up
@Override
public void onPlaybackStateChange(PlaybackStateCompat state) {
    // Report the state to the MediaSession.
    mSession.setPlaybackState(state);

    // Manage the started state of this service.
    switch (state.getState()) {
        case PlaybackStateCompat.STATE_PLAYING:
            mServiceManager.moveServiceToStartedState(state);
            break;
        case PlaybackStateCompat.STATE_PAUSED:
            mServiceManager.updateNotificationForPause(state);
            break;
        case PlaybackStateCompat.STATE_STOPPED:
            mServiceManager.moveServiceOutOfStartedState(state);
            break;
    }
}
 
Example 9
Source File: MusicPlaybackService.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
private void updateMediaSessionPlayState() {
    PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder();
    int playState;
    if (isPlaying()) {
        playState = PlaybackStateCompat.STATE_PLAYING;
    } else {
        playState = PlaybackStateCompat.STATE_PAUSED;
    }
    long currentPosition = getCurrentPosition();
    playbackStateBuilder.setState(playState, currentPosition, (float) 1.0).setActions(
            getPlaybackStateActions()
    );
    mMediaSession.setPlaybackState(playbackStateBuilder.build());
}
 
Example 10
Source File: PlaybackControlsFragment.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
private void onPlaybackStateChanged(PlaybackStateCompat state) {
    LogUtils.d(TAG, "onPlaybackStateChanged ", state);
    if (getActivity() == null) {
        LogUtils.w(TAG, "onPlaybackStateChanged called when getActivity null," +
                "this should not happen if the callback was properly unregistered. Ignoring.");
        return;
    }
    if (state == null) {
        return;
    }
    boolean enablePlay = false;
    switch (state.getState()) {
        case PlaybackStateCompat.STATE_PAUSED:
        case PlaybackStateCompat.STATE_STOPPED:
            enablePlay = true;
            break;
        case PlaybackStateCompat.STATE_ERROR:
            LogUtils.e(TAG, "error playbackstate: ", state.getErrorMessage());
            Toast.makeText(getActivity(), state.getErrorMessage(), Toast.LENGTH_LONG).show();
            break;
    }

    if (enablePlay) {
        mPlayPause.setImageDrawable(
                ActivityCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_36dp));
    } else {
        mPlayPause.setImageDrawable(
                ActivityCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_36dp));
    }

    MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController();
    String extraInfo = null;
    if (controller != null && controller.getExtras() != null) {
        String castName = controller.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            extraInfo = getResources().getString(R.string.casting_to_device, castName);
        }
    }
    setExtraInfo(extraInfo);
}
 
Example 11
Source File: MediaPlayerAdapter.java    From android-MediaBrowserService with Apache License 2.0 5 votes vote down vote up
/**
 * Set the current capabilities available on this session. Note: If a capability is not
 * listed in the bitmask of capabilities then the MediaSession will not handle it. For
 * example, if you don't want ACTION_STOP to be handled by the MediaSession, then don't
 * included it in the bitmask that's returned.
 */
@PlaybackStateCompat.Actions
private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
                   | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
                   | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                   | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
    switch (mState) {
        case PlaybackStateCompat.STATE_STOPPED:
            actions |= PlaybackStateCompat.ACTION_PLAY
                       | PlaybackStateCompat.ACTION_PAUSE;
            break;
        case PlaybackStateCompat.STATE_PLAYING:
            actions |= PlaybackStateCompat.ACTION_STOP
                       | PlaybackStateCompat.ACTION_PAUSE
                       | PlaybackStateCompat.ACTION_SEEK_TO;
            break;
        case PlaybackStateCompat.STATE_PAUSED:
            actions |= PlaybackStateCompat.ACTION_PLAY
                       | PlaybackStateCompat.ACTION_STOP;
            break;
        default:
            actions |= PlaybackStateCompat.ACTION_PLAY
                       | PlaybackStateCompat.ACTION_PLAY_PAUSE
                       | PlaybackStateCompat.ACTION_STOP
                       | PlaybackStateCompat.ACTION_PAUSE;
    }
    return actions;
}
 
Example 12
Source File: AudioStreamingManager.java    From DMAudioStreamer with Apache License 2.0 5 votes vote down vote up
private void updateProgress() {
    if (instance.mLastPlaybackState == 0 || instance.mLastPlaybackState < 0) {
        return;
    }
    if (instance.mLastPlaybackState != PlaybackStateCompat.STATE_PAUSED && instance.currentSessionCallback != null) {
        instance.currentSessionCallback.currentSeekBarPosition((int) audioPlayback.getCurrentStreamPosition());
    }
}
 
Example 13
Source File: MusicService.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
/**
 * Helper to switch to a different Playback instance
 *
 * @param playback switch to this playback
 */
private void switchToPlayer(Playback playback, boolean resumePlaying) {
    if (playback == null) {
        throw new IllegalArgumentException("Playback cannot be null");
    }
    // suspend the current one.
    int oldState = mPlayback.getState();
    int pos = mPlayback.getCurrentStreamPosition();
    String currentMediaId = mPlayback.getCurrentMediaId();
    LogUtils.d(TAG, "Current position from " + playback + " is ", pos);
    mPlayback.stop(false);
    playback.setCallback(this);
    playback.setCurrentStreamPosition(pos < 0 ? 0 : pos);
    playback.setCurrentMediaId(currentMediaId);
    playback.start();
    // finally swap the instance
    mPlayback = playback;
    switch (oldState) {
        case PlaybackStateCompat.STATE_BUFFERING:
        case PlaybackStateCompat.STATE_CONNECTING:
        case PlaybackStateCompat.STATE_PAUSED:
            mPlayback.pause();
            break;
        case PlaybackStateCompat.STATE_PLAYING:
            if (resumePlaying && QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
                mPlayback.play(mPlayingQueue.get(mCurrentIndexOnQueue));
            } else if (!resumePlaying) {
                mPlayback.pause();
            } else {
                mPlayback.stop(true);
            }
            break;
        case PlaybackStateCompat.STATE_NONE:
            break;
        default:
            LogUtils.d(TAG, "Default called. Old state is ", oldState);
    }
}
 
Example 14
Source File: Playback.java    From react-native-audio-streaming-player with MIT License 5 votes vote down vote up
/**
 * Called when media player is done playing current song.
 *
 * @see OnCompletionListener
 */
@Override
public void onCompletion(MediaPlayer player) {
    if (mCallback != null) {
        mState = PlaybackStateCompat.STATE_PAUSED;
        mCallback.onCompletion();
    }
}
 
Example 15
Source File: MediaSessionManager.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
public void updatePlaybackState() {
    int state = (mPlayService.isPlaying() ||
            mPlayService.isPreparing()) ? PlaybackStateCompat.STATE_PLAYING :
            PlaybackStateCompat.STATE_PAUSED;
    mMediaSession.setPlaybackState(
            new PlaybackStateCompat.Builder()
                    .setActions(MEDIA_SESSION_ACTIONS)
                    .setState(state, mPlayService.getCurrentPosition(), 1)
                    .build());
}
 
Example 16
Source File: FullScreenPlayerActivity.java    From LyricHere with Apache License 2.0 4 votes vote down vote up
private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;
    }
    mLastPlaybackState = state;
    if (mMediaController != null && mMediaController.getExtras() != null) {
        String castName = mMediaController.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        String line3Text = castName == null ? "" : getResources()
                .getString(R.string.casting_to_device, castName);
        mLine3.setText(line3Text);
    }

    switch (state.getState()) {
        case PlaybackStateCompat.STATE_PLAYING:
            mLoading.setVisibility(INVISIBLE);
            mPlayPause.setVisibility(VISIBLE);
            mPlayPause.setImageDrawable(mPauseDrawable);
            mControllers.setVisibility(VISIBLE);
            scheduleSeekbarUpdate();
            break;
        case PlaybackStateCompat.STATE_PAUSED:
            mControllers.setVisibility(VISIBLE);
            mLoading.setVisibility(INVISIBLE);
            mPlayPause.setVisibility(VISIBLE);
            mPlayPause.setImageDrawable(mPlayDrawable);
            stopSeekbarUpdate();
            break;
        case PlaybackStateCompat.STATE_NONE:
        case PlaybackStateCompat.STATE_STOPPED:
            mLoading.setVisibility(INVISIBLE);
            mPlayPause.setVisibility(VISIBLE);
            mPlayPause.setImageDrawable(mPlayDrawable);
            stopSeekbarUpdate();
            break;
        case PlaybackStateCompat.STATE_BUFFERING:
            mPlayPause.setVisibility(INVISIBLE);
            mLoading.setVisibility(VISIBLE);
            mLine3.setText(R.string.loading);
            stopSeekbarUpdate();
            break;
        default:
            LogUtils.d(TAG, "Unhandled state ", state.getState());
    }

    mSkipNext.setVisibility((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) == 0
            ? INVISIBLE : VISIBLE);
    mSkipPrev.setVisibility((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) == 0
            ? INVISIBLE : VISIBLE);
}
 
Example 17
Source File: MusicService.java    From Muzesto with GNU General Public License v3.0 4 votes vote down vote up
private void updateMediaSession(final String what) {
    int playState = mIsSupposedToBePlaying
            ? PlaybackStateCompat.STATE_PLAYING
            : PlaybackStateCompat.STATE_PAUSED;

    if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                    .setState(playState, position(), 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                    .build());
        }
    } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) {
        Bitmap albumArt = ImageLoader.getInstance().loadImageSync(TimberUtils.getAlbumArtUri(getAlbumId()).toString());
        if (albumArt != null) {

            Bitmap.Config config = albumArt.getConfig();
            if (config == null) {
                config = Bitmap.Config.ARGB_8888;
            }
            albumArt = albumArt.copy(config, false);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setMetadata(new MediaMetadataCompat.Builder()
                    .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName())
                    .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName())
                    .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration())
                    .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1)
                    .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().length)
                    .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName())
                    .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
                            mShowAlbumArtOnLockscreen ? albumArt : null)
                    .build());

            mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                    .setState(playState, position(), 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                    .build());
        }
    }
}
 
Example 18
Source File: AudioPlaybackListener.java    From DMAudioStreamer with Apache License 2.0 4 votes vote down vote up
@Override
public void play(MediaMetaData item) {
    mPlayOnFocusGain = true;
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentPosition = 0;
        mCurrentMediaId = mediaId;
    }
    if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false); // release everything except MediaPlayer
        String source = item.getMediaUrl();
        if (source != null) {
            source = source.replaceAll(" ", "%20"); // Escape spaces for URLs
        }
        try {
            createMediaPlayerIfNeeded();
            mState = PlaybackStateCompat.STATE_BUFFERING;
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(source);

            mMediaPlayer.prepareAsync();
            mWifiLock.acquire();

            if (mCallback != null) {
                mCallback.onPlaybackStatusChanged(mState);
            }

        } catch (IOException ex) {
            Logger.e(TAG, ex, "Exception playing song");
            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}
 
Example 19
Source File: TrackFragment.java    From Melophile with Apache License 2.0 4 votes vote down vote up
public void updatePlaybackState(PlaybackStateCompat stateCompat) {
  if (stateCompat == null) return;
  lastState = stateCompat;
  updateRepeatMode(isActionApplied(stateCompat.getActions(),
          PlaybackStateCompat.ACTION_SET_REPEAT_MODE));
  updateShuffleMode(isActionApplied(stateCompat.getActions(),
          PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED));
  //check the state
  switch (stateCompat.getState()) {
    case PlaybackStateCompat.STATE_PLAYING:
      playPause.setVisibility(VISIBLE);
      if (playPause.isPlay()) {
        playPause.change(false, true);
      }
      startSeekBarUpdate();
      break;
    case PlaybackStateCompat.STATE_PAUSED:
      // mControllers.setVisibility(VISIBLE);
      // mLoading.setVisibility(INVISIBLE);
      playPause.setVisibility(VISIBLE);
      if (!playPause.isPlay()) {
        playPause.change(true, true);
      }
      stopSeekBarUpdate();
      break;
    case PlaybackStateCompat.STATE_NONE:
    case PlaybackStateCompat.STATE_STOPPED:
      playPause.setVisibility(VISIBLE);
      if (playPause.isPlay()) {
        playPause.change(false, true);
      }
      stopSeekBarUpdate();
      break;
    case PlaybackStateCompat.STATE_BUFFERING:
      playPause.setVisibility(INVISIBLE);
      stopSeekBarUpdate();
      break;
    default:
      Log.d(TAG, "Unhandled state " + stateCompat.getState());
  }
}
 
Example 20
Source File: MediaNotificationManager.java    From YouTube-In-Background with MIT License 4 votes vote down vote up
/**
 * This may look strange, but the documentation for [Service.startForeground]
 * notes that "calling this method does *not* put the service in the started
 * state itself, even though the name sounds like it."
 *
 * @param state current playback state
 */
private void updateNotification(PlaybackStateCompat state)
{
    int updatedState = state.getState();

    // Skip building a notification when state is "none" and metadata is null.
    Notification notification = skipBuildNotification(updatedState) ? buildNotification(sessionToken) : null;

    if (notification != null && !hasRegisterReceiver) {
        mediaController.registerCallback(callback);
        IntentFilter filter = new IntentFilter();
        filter.addAction(CUSTOM_ACTION_NEXT);
        filter.addAction(CUSTOM_ACTION_PAUSE);
        filter.addAction(CUSTOM_ACTION_PLAY);
        filter.addAction(CUSTOM_ACTION_PREV);
        filter.addAction(CUSTOM_ACTION_STOP);
        exoAudioService.registerReceiver(this, filter);
        hasRegisterReceiver = true;
    }

    switch (updatedState) {
        case STATE_BUFFERING:
        case STATE_PLAYING:
            if (notification != null) {
                notificationManager.notify(NOTIFICATION_ID, notification);
            }

            if (!isForegroundService) {
                Intent intent = new Intent(context, BackgroundExoAudioService.class);
                ContextCompat.startForegroundService(context, intent);
                exoAudioService.startForeground(NOTIFICATION_ID, notification);
                isForegroundService = true;
            }

            break;
        case PlaybackStateCompat.STATE_CONNECTING:
        case PlaybackStateCompat.STATE_ERROR:
        case PlaybackStateCompat.STATE_FAST_FORWARDING:
        case PlaybackStateCompat.STATE_NONE:
        case PlaybackStateCompat.STATE_PAUSED:
        case PlaybackStateCompat.STATE_REWINDING:
        case PlaybackStateCompat.STATE_SKIPPING_TO_NEXT:
        case PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS:
        case PlaybackStateCompat.STATE_SKIPPING_TO_QUEUE_ITEM:
        case PlaybackStateCompat.STATE_STOPPED:
            if (isForegroundService) {
                exoAudioService.stopForeground(false);
                isForegroundService = false;

                // If playback has ended, also stop the service.
                if (updatedState == PlaybackStateCompat.STATE_NONE) {
                    exoAudioService.stopSelf();
                }

                if (notification != null) {
                    notificationManager.notify(NOTIFICATION_ID, notification);
                } else {
                    removeNowPlayingNotification();
                }
            }

            break;
    }
}