Example usage for android.support.v4.media.session PlaybackStateCompat STATE_STOPPED

List of usage examples for android.support.v4.media.session PlaybackStateCompat STATE_STOPPED

Introduction

In this page you can find the example usage for android.support.v4.media.session PlaybackStateCompat STATE_STOPPED.

Prototype

int STATE_STOPPED

To view the source code for android.support.v4.media.session PlaybackStateCompat STATE_STOPPED.

Click Source Link

Document

State indicating this item is currently stopped.

Usage

From source file:rocks.stalin.android.app.playback.RemotePlayback.java

public void addClient(MessageConnection connection) {
    if (mState != PlaybackStateCompat.STATE_STOPPED && mState != PlaybackStateCompat.STATE_NONE)
        throw new RuntimeException("You can't add a client while playing... yet, You were in state: " + mState);
    RemotePlayerBackend remoteBackend = new RemotePlayerBackend(connection, timeProvider, scheduler);
    createMediaPlayerIfNeeded();/*from w w w  .j a v a 2  s . c o m*/
    mediaPlayer.connectBackend(remoteBackend);
}

From source file:com.example.android.leanback.MediaSessionService.java

/**
 * Prepare the first item in the list. And setup the listener for media player.
 *///from  w w w .j  ava2  s  . com
private void initializePlayer() {
    // This service can be created for multiple times, the objects will only be created when
    // it is null
    if (mPlayer != null) {
        return;
    }
    mPlayer = new MediaPlayer();

    // Set playback state to none to create a valid playback state. So controls row can get
    // information about the supported actions.
    mMediaSession.setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_NONE).build());
    // Activate media session
    if (!mMediaSession.isActive()) {
        mMediaSession.setActive(true);
    }

    // Set up listener and audio stream type for underlying music player.
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    // set up listener when the player is prepared.
    mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mInitialized = true;
            // Every time when the player is prepared (when new data source is set),
            // all listeners will be notified to toggle the UI to "pause" status.
            notifyUiWhenPlayerIsPrepared();

            // When media player is prepared, the callback functions will be executed to update
            // the meta data and playback state.
            onMediaSessionMetaDataChanged();
            mMediaSession
                    .setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PAUSED).build());
        }
    });

    // set up listener for player's error.
    mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
            if (DEBUG) {
                PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(
                        PlaybackStateCompat.STATE_ERROR);
                builder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_APP_ERROR, MEDIA_PLAYER_ERROR_MESSAGE);
                mMediaSession.setPlaybackState(builder.build());
            }
            return true;
        }
    });

    // set up listener to respond the event when current music item is finished
    mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        /**
         * Expected Interaction Behavior:
         * 1. If current media item's playing speed not equal to normal speed.
         *
         *    A. MEDIA_ACTION_REPEAT_ALL
         *       a. If current media item is the last one. The first music item in the list will
         *          be prepared, but it won't play until user press play button.
         *
         *          When user press the play button, the speed will be reset to normal (1.0f)
         *          no matter what the previous media item's playing speed is.
         *
         *       b. If current media item isn't the last one, next media item will be prepared,
         *          but it won't play.
         *
         *          When user press the play button, the speed will be reset to normal (1.0f)
         *          no matter what the previous media item's playing speed is.
         *
         *    B. MEDIA_ACTION_REPEAT_ONE
         *       Different with previous scenario, current item will go back to the start point
         *       again and play automatically. (The reason to enable auto play here is for
         *       testing purpose and to make sure our designed API is flexible enough to support
         *       different situations.)
         *
         *       No matter what the previous media item's playing speed is, in this situation
         *       current media item will be replayed in normal speed.
         *
         *    C. MEDIA_ACTION_REPEAT_NONE
         *       a. If current media is the last one. The service will be closed, no music item
         *          will be prepared to play. From the UI perspective, the progress bar will not
         *          be reset to the starting point.
         *
         *       b. If current media item isn't the last one, next media item will be prepared,
         *          but it won't play.
         *
         *          When user press the play button, the speed will be reset to normal (1.0f)
         *          no matter what the previous media item's playing speed is.
         *
         * @param mp Object of MediaPlayer
         */
        @Override
        public void onCompletion(MediaPlayer mp) {

            // When current media item finishes playing, always reset rewind/ fastforward state
            mFastForwardSpeedFactorIndex = 0;
            mRewindSpeedFactorIndex = 0;
            // Set player's playback speed back to normal
            mPlayer.setPlaybackParams(mPlayer.getPlaybackParams()
                    .setSpeed(mFastForwardSpeedFactors[mFastForwardSpeedFactorIndex]));
            // Pause the player, and update the status accordingly.
            mPlayer.pause();
            mMediaSession
                    .setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PAUSED).build());

            if (mRepeatState == MEDIA_ACTION_REPEAT_ALL && mCurrentIndex == mMediaItemList.size() - 1) {
                // if the repeat mode is enabled but the shuffle mode is not enabled,
                // will go back to the first music item to play
                if (mShuffleMode == PlaybackStateCompat.SHUFFLE_MODE_NONE) {
                    mCurrentIndex = 0;
                } else {
                    // Or will choose a music item from playing list randomly.
                    mCurrentIndex = generateMediaItemIndex();
                }
                mCurrentMediaItem = mMediaItemList.get(mCurrentIndex);
                // The ui will also be changed from playing state to pause state through
                // setDataSource() operation
                setDataSource();
            } else if (mRepeatState == MEDIA_ACTION_REPEAT_ONE) {
                // Play current music item again.
                // The ui will stay to be "playing" status for the reason that there is no
                // setDataSource() function call.
                mPlayer.start();
                mMediaSession.setPlaybackState(
                        createPlaybackStateBuilder(PlaybackStateCompat.STATE_PLAYING).build());
            } else if (mCurrentIndex < mMediaItemList.size() - 1) {
                if (mShuffleMode == PlaybackStateCompat.SHUFFLE_MODE_NONE) {
                    mCurrentIndex++;
                } else {
                    mCurrentIndex = generateMediaItemIndex();
                }
                mCurrentMediaItem = mMediaItemList.get(mCurrentIndex);
                // The ui will also be changed from playing state to pause state through
                // setDataSource() operation
                setDataSource();
            } else {
                // close the service when the playlist is finished
                // The PlaybackState will be updated to STATE_STOPPED. And onPlayComplete
                // callback will be called by attached glue.
                mMediaSession.setPlaybackState(
                        createPlaybackStateBuilder(PlaybackStateCompat.STATE_STOPPED).build());
                stopSelf();
            }
        }
    });

    final MediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {
        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            mBufferedProgress = getDuration() * percent / 100;
            PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(
                    PlaybackStateCompat.STATE_BUFFERING);
            builder.setBufferedPosition(mBufferedProgress);
            mMediaSession.setPlaybackState(builder.build());
        }
    };
    mPlayer.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
}

From source file:com.scooter1556.sms.android.fragment.tv.TvAudioPlayerFragment.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null || playbackControlsRow == null) {
        return;//from  w ww .  ja v  a2s. co  m
    }

    lastPlaybackState = state;

    switch (state.getState()) {

    case PlaybackStateCompat.STATE_PLAYING:
        scheduleSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PAUSE);
        break;

    case PlaybackStateCompat.STATE_PAUSED:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        break;

    case PlaybackStateCompat.STATE_NONE:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        resetPlaybackRow();
        updatePlayListRow();
        break;

    case PlaybackStateCompat.STATE_STOPPED:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        playbackControlsRow.setCurrentTime(0);
        break;

    case PlaybackStateCompat.STATE_BUFFERING:
        stopSeekbarUpdate();
        break;

    default:
        Log.d(TAG, "Unhandled state: " + state.getState());
    }

    // Custom Actions
    for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
        switch (action.getAction()) {
        case MediaService.STATE_SHUFFLE_ON:
            shuffleAction.setIcon(
                    ContextCompat.getDrawable(getActivity(), R.drawable.ic_shuffle_enabled_white_48dp));

            // Update interface if necessary
            if (shuffleMode == null || !shuffleMode.equals(MediaService.STATE_SHUFFLE_ON)) {
                shuffleMode = MediaService.STATE_SHUFFLE_ON;
                updatePlayListRow();
            }

            break;

        case MediaService.STATE_SHUFFLE_OFF:
            shuffleAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_shuffle_white_48dp));

            // Update interface if necessary
            if (shuffleMode == null || !shuffleMode.equals(MediaService.STATE_SHUFFLE_OFF)) {
                shuffleMode = MediaService.STATE_SHUFFLE_OFF;
                updatePlayListRow();
            }

            break;

        case MediaService.STATE_REPEAT_NONE:
            repeatAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_white_48dp));
            break;

        case MediaService.STATE_REPEAT_ALL:
            repeatAction
                    .setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_enable_white_48dp));
            break;

        case MediaService.STATE_REPEAT_ONE:
            repeatAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_one_white_48dp));
            break;
        }
    }

    rowsAdapter.notifyArrayItemRangeChanged(rowsAdapter.indexOf(playbackControlsRow), 1);
}

From source file:com.scooter1556.sms.android.fragment.tv.TvVideoPlayerFragment.java

@Override
public void stop(boolean notifyListeners) {
    Log.d(TAG, "stop()");

    playbackState = PlaybackStateCompat.STATE_STOPPED;

    if (notifyListeners && callback != null) {
        callback.onPlaybackStatusChanged(playbackState);
    }//from  w  w  w  . jav a2  s.  c  o m

    currentPosition = getCurrentStreamPosition();

    // Relax all resources
    relaxResources(true);
}

From source file:com.scooter1556.sms.android.fragment.tv.TvVideoPlayerFragment.java

@Override
public void play(MediaSessionCompat.QueueItem item) {
    Log.d(TAG, "Play media item with id " + item.getDescription().getMediaId());

    boolean mediaHasChanged = !TextUtils.equals(item.getDescription().getMediaId(), currentMediaID);

    if (mediaHasChanged) {
        currentPosition = 0;/*www.j av a  2s. co m*/
        currentMediaID = item.getDescription().getMediaId();
    }

    if (playbackState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mediaPlayer != null) {
        configMediaPlayerState();
    } else {
        playbackState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false);
        initialiseStream();
    }
}

From source file:com.scooter1556.sms.lib.android.service.AudioPlayerService.java

public void stop() {
    if (player == null) {
        return;/*from   w w w .  j a  v a  2 s  . c  om*/
    }
    cleanupPlayer();
    currentListPosition = 0;
    isPaused = false;

    // Update state
    mediaState = PlaybackStateCompat.STATE_STOPPED;
    updatePlaybackState();

    // Update listeners
    for (AudioPlayerListener listener : listeners) {
        listener.PlaybackStateChanged();
        listener.PlaylistPositionChanged();
    }
}

From source file:com.scooter1556.sms.lib.android.service.AudioPlayerService.java

public void start() {
    if (mediaState == PlaybackStateCompat.STATE_STOPPED) {
        play();//ww w.  j a  va  2 s. c  om
    } else if (mediaState == PlaybackStateCompat.STATE_PAUSED) {
        player.start();

        // Update state
        mediaState = PlaybackStateCompat.STATE_PLAYING;
        updatePlaybackState();

        wakeLock.acquire();
        wifiLock.acquire();
    }

    isPaused = false;

    // Update listeners
    for (AudioPlayerListener listener : listeners) {
        listener.PlaybackStateChanged();
    }
}

From source file:com.scooter1556.sms.lib.android.service.AudioPlayerService.java

@Override
public void onStateChanged(SMSAudioPlayer player, boolean playWhenReady, int playbackState) {
    switch (playbackState) {
    case ExoPlayer.STATE_BUFFERING:
        // Update state
        mediaState = PlaybackStateCompat.STATE_BUFFERING;
        updatePlaybackState();/*w  ww  .  j  av a2s.co  m*/
        break;

    case ExoPlayer.STATE_ENDED:
        // Clean-up resources
        cleanupPlayer();

        if (mediaElementList.size() > (currentListPosition + 1)) {
            currentListPosition++;
            initialiseTrack(currentListPosition);

            // Update state
            mediaState = PlaybackStateCompat.STATE_SKIPPING_TO_NEXT;
            updatePlaybackState();
        } else {
            // Playlist has ended
            cleanupPlayer();
            currentListPosition = 0;

            // Update state
            mediaState = PlaybackStateCompat.STATE_STOPPED;
            updatePlaybackState();
        }

        // Update listeners
        for (AudioPlayerListener listener : listeners) {
            listener.PlaybackStateChanged();
            listener.PlaylistPositionChanged();
        }

        break;

    case ExoPlayer.STATE_IDLE:
        break;

    case ExoPlayer.STATE_PREPARING:
        // Update state
        mediaState = PlaybackStateCompat.STATE_CONNECTING;
        updatePlaybackState();

        break;

    case ExoPlayer.STATE_READY:
        if (playWhenReady) {
            // Update state
            mediaState = PlaybackStateCompat.STATE_PLAYING;
            updatePlaybackState();
        }

        break;

    default:
        break;
    }
}

From source file:com.example.android.leanback.MediaSessionService.java

private void stop() {
    if (mPlayer != null) {
        mPlayer.stop();/* www.j  av a 2s .  c o m*/
        // Update playbackState.
        mMediaSession.setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_STOPPED).build());
    }
}

From source file:dk.nota.lyt.libvlc.PlaybackService.java

protected void publishState(int state) {
    if (mMediaSession == null)
        return;// w w  w. ja  v  a2 s.c o m
    PlaybackStateCompat.Builder bob = new PlaybackStateCompat.Builder();
    bob.setActions(PLAYBACK_ACTIONS);
    switch (state) {
    case MediaPlayer.Event.Playing:
        bob.setState(PlaybackStateCompat.STATE_PLAYING, getCurrentMediaPosition(), getRate());
        break;
    case MediaPlayer.Event.Stopped:
        bob.setState(PlaybackStateCompat.STATE_STOPPED, getCurrentMediaPosition(), getRate());
        break;
    default:
        bob.setState(PlaybackStateCompat.STATE_PAUSED, getCurrentMediaPosition(), getRate());
    }
    PlaybackStateCompat pbState = bob.build();
    mMediaSession.setPlaybackState(pbState);
    mMediaSession.setActive(state != PlaybackStateCompat.STATE_STOPPED);
}