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

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

Introduction

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

Prototype

int STATE_ERROR

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

Click Source Link

Document

State indicating this item is currently in an error state.

Usage

From source file:com.bayapps.android.robophish.ui.MediaBrowserFragment.java

private void checkForUserVisibleErrors(boolean forceError) {
    boolean showError = forceError;
    // If offline, message is about the lack of connectivity:
    if (!NetworkHelper.isOnline(getActivity())) {
        mErrorMessage.setText(R.string.error_no_connection);
        showError = true;/*ww  w  .  j  ava2  s .c  o m*/
    } else {
        // otherwise, if state is ERROR and metadata!=null, use playback state error message:
        MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController();
        if (controller != null && controller.getMetadata() != null && controller.getPlaybackState() != null
                && controller.getPlaybackState().getState() == PlaybackStateCompat.STATE_ERROR
                && controller.getPlaybackState().getErrorMessage() != null) {
            mErrorMessage.setText(controller.getPlaybackState().getErrorMessage());
            showError = true;
        } else if (forceError) {
            // Finally, if the caller requested to show error, show a generic message:
            mErrorMessage.setText(R.string.error_loading_media);
            showError = true;
        }
    }
    mErrorView.setVisibility(showError ? View.VISIBLE : View.GONE);
    LogHelper.d(TAG, "checkForUserVisibleErrors. forceError=", forceError, " showError=", showError,
            " isOnline=", NetworkHelper.isOnline(getActivity()));
}

From source file:com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector.java

private void updateMediaSessionPlaybackState() {
    PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    if (player == null) {
        builder.setActions(buildPlaybackActions()).setState(PlaybackStateCompat.STATE_NONE, 0, 0, 0);
        mediaSession.setPlaybackState(builder.build());
        return;//from   www .j av  a  2  s  .  c om
    }

    Map<String, CustomActionProvider> currentActions = new HashMap<>();
    for (CustomActionProvider customActionProvider : customActionProviders) {
        PlaybackStateCompat.CustomAction customAction = customActionProvider.getCustomAction();
        if (customAction != null) {
            currentActions.put(customAction.getAction(), customActionProvider);
            builder.addCustomAction(customAction);
        }
    }
    customActionMap = Collections.unmodifiableMap(currentActions);

    int sessionPlaybackState = playbackException != null ? PlaybackStateCompat.STATE_ERROR
            : mapPlaybackState(player.getPlaybackState(), player.getPlayWhenReady());
    if (playbackException != null) {
        if (errorMessageProvider != null) {
            Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackException);
            builder.setErrorMessage(message.first, message.second);
        }
        if (player.getPlaybackState() != Player.STATE_IDLE) {
            playbackException = null;
        }
    }
    long activeQueueItemId = queueNavigator != null ? queueNavigator.getActiveQueueItemId(player)
            : MediaSessionCompat.QueueItem.UNKNOWN_ID;
    Bundle extras = new Bundle();
    extras.putFloat(EXTRAS_PITCH, player.getPlaybackParameters().pitch);
    builder.setActions(buildPlaybackActions()).setActiveQueueItemId(activeQueueItemId)
            .setBufferedPosition(player.getBufferedPosition())
            .setState(sessionPlaybackState, player.getCurrentPosition(), player.getPlaybackParameters().speed,
                    SystemClock.elapsedRealtime())
            .setExtras(extras);
    mediaSession.setPlaybackState(builder.build());
}

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

/**
 * Set new data source and prepare the music player asynchronously.
 *//*from   w  w w  .jav a2 s .  co m*/
private void setDataSource() {
    reset();
    try {
        mPlayer.setDataSource(this.getApplicationContext(),
                mCurrentMediaItem.getMediaSourceUri(getApplicationContext()));
        mPlayer.prepareAsync();
    } catch (IOException e) {
        PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(PlaybackStateCompat.STATE_ERROR);
        builder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_APP_ERROR, CANNOT_SET_DATA_SOURCE);
        mMediaSession.setPlaybackState(builder.build());
    }
}

From source file:nuclei.media.playback.ExoPlayerPlayback.java

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    if (LOG.isLoggable(Log.DEBUG))
        LOG.d("onStateChanged=" + playbackState + ", " + playWhenReady);
    if (!mPrepared && playbackState == ExoPlayer.STATE_READY && mMediaPlayer != null) {
        mPrepared = true;//from ww w.  j  a  va 2s .c  o m
        if (!mWakeLock.isHeld())
            mWakeLock.acquire();
        if (!mWifiLock.isHeld())
            mWifiLock.acquire();
        configMediaPlayerState(true, false);
        setSurface(mSurfaceId, mSurface);
        mMediaPlayer.seekTo(mCurrentPosition);
        mMediaPlayer.setPlayWhenReady(mPlayWhenReady);
    } else if (mMediaPlayer != null && mState != PlaybackStateCompat.STATE_ERROR
            && mState != PlaybackStateCompat.STATE_BUFFERING)
        mCurrentPosition = mMediaPlayer.getCurrentPosition();

    if (mMediaPlayer != null && mMediaMetadata != null) {
        final long duration = getDuration();
        if (mMediaMetadata.getLong(MediaMetadataCompat.METADATA_KEY_DURATION) != duration)
            mMediaMetadata.setDuration(duration);
    }

    switch (playbackState) {
    case ExoPlayer.STATE_BUFFERING:
        mState = PlaybackStateCompat.STATE_BUFFERING;
        mIllegalStateRetries = 0;
        break;
    case ExoPlayer.STATE_ENDED:
        mState = PlaybackStateCompat.STATE_NONE;
        mIllegalStateRetries = 0;
        break;
    case ExoPlayer.STATE_IDLE:
        if (mState != PlaybackStateCompat.STATE_ERROR)
            mState = PlaybackStateCompat.STATE_NONE;
        break;
    case ExoPlayer.STATE_READY:
        mIllegalStateRetries = 0;
        if (isMediaPlayerPlaying()) {
            mState = PlaybackStateCompat.STATE_PLAYING;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (mPlaybackParams != null)
                    mMediaPlayer.setPlaybackParameters(mPlaybackParams);
            }
        } else
            mState = PlaybackStateCompat.STATE_PAUSED;
        break;
    default:
        mState = PlaybackStateCompat.STATE_NONE;
        break;
    }

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

    if (playbackState == ExoPlayer.STATE_ENDED) {
        mRestart = true;
        if (mCallback != null)
            mCallback.onCompletion();
    }
}

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

@Override
public void onError(Exception e) {
    cleanupPlayer();/*from w  w w.j  a  v a  2  s . c om*/
    currentListPosition = 0;

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

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

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

private void play() {
    // Only when player is not null (meaning the player has been created), the player is
    // prepared (using the mInitialized as the flag to represent it,
    // this boolean variable will only be assigned to true inside of the onPrepared callback)
    // and the media item is not currently playing (!isPlaying()), then the player can be
    // started.//from w  ww. j  av  a  2s.  c o  m

    // If the player has not been prepared, but this function is fired, it is an error state
    // from the app side
    if (!mInitialized) {
        PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(PlaybackStateCompat.STATE_ERROR);
        builder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_APP_ERROR, PLAYER_NOT_INITIALIZED);
        mMediaSession.setPlaybackState(builder.build());

        // If the player has is playing, and this function is fired again, it is an error state
        // from the app side
    } else {
        // Request audio focus only when needed
        if (mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN) != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            return;
        }

        if (mPlayer.getPlaybackParams().getSpeed() != NORMAL_SPEED) {
            // Reset to normal speed and play
            resetSpeedAndPlay();
        } else {
            // Continue play.
            mPlayer.start();
            mMediaSession
                    .setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PLAYING).build());
        }
    }

}

From source file:com.example.android.mediabrowserservice.MusicService.java

/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 *///from  www  .  ja va  2s .  c  om
private void updatePlaybackState(String error) {
    LogHelper.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSessionCompat.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        stateBuilder.setActiveQueueItemId(item.getQueueId());
    }

    mSession.setPlaybackState(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mMediaNotificationManager.startNotification();
    }
}

From source file:com.example.android.supportv4.media.MediaBrowserServiceSupport.java

/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 *//*from w w  w .  j  a v  a 2  s  . co  m*/
private void updatePlaybackState(String error) {
    Log.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSessionCompat.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        stateBuilder.setActiveQueueItemId(item.getQueueId());
    }

    mSession.setPlaybackState(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mMediaNotificationManager.startNotification();
    }
}

From source file:com.torrenttunes.android.MusicService.java

/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 *//*from   ww w . j av  a  2 s .c o  m*/
private void updatePlaybackState(String error) {
    LogHelper.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSessionCompat.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        //            stateBuilder.setActiveQueueItemId(item.getQueueId());
    }

    mSession.setPlaybackState(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mMediaNotificationManager.startNotification();
    }
}

From source file:androidx.media.widget.VideoView2.java

private int getCorrespondingPlaybackState() {
    switch (mCurrentState) {
    case STATE_ERROR:
        return PlaybackStateCompat.STATE_ERROR;
    case STATE_IDLE:
        return PlaybackStateCompat.STATE_NONE;
    case STATE_PREPARING:
        return PlaybackStateCompat.STATE_CONNECTING;
    case STATE_PREPARED:
        return PlaybackStateCompat.STATE_PAUSED;
    case STATE_PLAYING:
        return PlaybackStateCompat.STATE_PLAYING;
    case STATE_PAUSED:
        return PlaybackStateCompat.STATE_PAUSED;
    case STATE_PLAYBACK_COMPLETED:
        return PlaybackStateCompat.STATE_STOPPED;
    default://  w  w w.  j  a  va  2 s . co m
        return -1;
    }
}