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

The following examples show how to use android.support.v4.media.session.PlaybackStateCompat#STATE_ERROR . 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: MediaSessionPlaybackReporter.java    From PainlessMusicPlayer with Apache License 2.0 6 votes vote down vote up
@PlaybackStateCompat.State
private static int toPlaybackStateCompat(@NonNull final PlaybackState state) {
    switch (state) {
        case STATE_LOADING:
            return PlaybackStateCompat.STATE_BUFFERING;

        case STATE_PLAYING:
            return PlaybackStateCompat.STATE_PLAYING;

        case STATE_PAUSED:
            return PlaybackStateCompat.STATE_PAUSED;

        case STATE_ERROR:
            return PlaybackStateCompat.STATE_ERROR;

        case STATE_IDLE:
        default:
            return PlaybackStateCompat.STATE_NONE;
    }
}
 
Example 2
Source File: CastPlayback.java    From klingar with Apache License 2.0 6 votes vote down vote up
@Override public void pause() {
  try {
    if (remoteMediaClient.hasMediaSession()) {
      remoteMediaClient.pause();
      currentPosition = (int) remoteMediaClient.getApproximateStreamPosition();
    } else {
      loadMedia(currentTrack, false);
    }
  } catch (JSONException e) {
    Timber.e(e, "Exception pausing cast playback");
    state = PlaybackStateCompat.STATE_ERROR;
    if (callback != null) {
      callback.onPlaybackStatusChanged();
    }
  }
}
 
Example 3
Source File: CastPlayback.java    From klingar with Apache License 2.0 6 votes vote down vote up
@Override public void seekTo(int position) {
  if (currentTrack == null) {
    state = PlaybackStateCompat.STATE_ERROR;
    if (callback != null) {
      callback.onPlaybackStatusChanged();
    }
    return;
  }
  try {
    if (remoteMediaClient.hasMediaSession()) {
      remoteMediaClient.seek(new MediaSeekOptions.Builder().setPosition(position).build());
      currentPosition = position;
    } else {
      currentPosition = position;
      loadMedia(currentTrack, false);
    }
  } catch (JSONException e) {
    Timber.e(e, "Exception pausing cast playback");
    state = PlaybackStateCompat.STATE_ERROR;
    if (callback != null) {
      callback.onPlaybackStatusChanged();
    }
  }
}
 
Example 4
Source File: MediaBrowserFragment.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowserCompat.MediaItem item = getItem(position);
    int itemState = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        itemState = MediaItemViewHolder.STATE_PLAYABLE;
        MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController();
        if (controller != null && controller.getMetadata() != null) {
            String currentPlaying = controller.getMetadata().getDescription().getMediaId();
            String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                    item.getDescription().getMediaId());
            if (currentPlaying != null && currentPlaying.equals(musicId)) {
                PlaybackStateCompat pbState = controller.getPlaybackState();
                if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
                    itemState = MediaItemViewHolder.STATE_NONE;
                } else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
                    itemState = MediaItemViewHolder.STATE_PLAYING;
                } else {
                    itemState = MediaItemViewHolder.STATE_PAUSED;
                }
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
            item.getDescription(), itemState);
}
 
Example 5
Source File: MediaSessionManager.java    From react-native-jw-media-player with MIT License 5 votes vote down vote up
private void updatePlaybackState(PlayerState playerState) {
	PlaybackStateCompat.Builder newPlaybackState = getPlaybackStateBuilder();
	long capabilities = getCapabilities(playerState);
	//noinspection WrongConstant
	newPlaybackState.setActions(capabilities);

	int playbackStateCompat = PlaybackStateCompat.STATE_NONE;
	switch (playerState) {
		case PLAYING:
			playbackStateCompat = PlaybackStateCompat.STATE_PLAYING;
			break;
		case PAUSED:
			playbackStateCompat = PlaybackStateCompat.STATE_PAUSED;
			break;
		case BUFFERING:
			playbackStateCompat = PlaybackStateCompat.STATE_BUFFERING;
			break;
		case IDLE:
			if (mReceivedError) {
				playbackStateCompat = PlaybackStateCompat.STATE_ERROR;
			} else {
				playbackStateCompat = PlaybackStateCompat.STATE_STOPPED;
			}
			break;
	}

	if (mPlayer != null) {
		newPlaybackState.setState(playbackStateCompat, (long)mPlayer.getPosition(), mPlayer.getPlaybackRate()); // PLAYBACK_RATE
		mMediaSessionCompat.setPlaybackState(newPlaybackState.build());
		mNotificationWrapper
				.createNotification(mPlayer.getContext(), mMediaSessionCompat, capabilities);
	}
}
 
Example 6
Source File: PlayerView.java    From Bop with Apache License 2.0 5 votes vote down vote up
private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;
    }

    switch (state.getState()) {
        case PlaybackStateCompat.STATE_PLAYING:
            mFabView.setImageDrawable((ExtraUtils.getThemedIcon(getApplicationContext(), ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_pause))));
            break;
        case PlaybackStateCompat.STATE_PAUSED:
            mFabView.setImageDrawable((ExtraUtils.getThemedIcon(getApplicationContext(), ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_play))));
            break;
        case PlaybackStateCompat.STATE_NONE:
            break;
        case PlaybackStateCompat.STATE_STOPPED:
            finish();
            break;
        default:
        case PlaybackStateCompat.STATE_BUFFERING:
            break;
        case PlaybackStateCompat.STATE_CONNECTING:
            break;
        case PlaybackStateCompat.STATE_ERROR:
            break;
        case PlaybackStateCompat.STATE_FAST_FORWARDING:
            break;
        case PlaybackStateCompat.STATE_REWINDING:
            break;
        case PlaybackStateCompat.STATE_SKIPPING_TO_NEXT:
            break;
        case PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS:
            break;
        case PlaybackStateCompat.STATE_SKIPPING_TO_QUEUE_ITEM:
            break;
    }

}
 
Example 7
Source File: CastPlayback.java    From klingar with Apache License 2.0 5 votes vote down vote up
@Override public void play(Track track) {
  try {
    loadMedia(track, true);
    state = PlaybackStateCompat.STATE_BUFFERING;
    if (callback != null) {
      callback.onPlaybackStatusChanged();
    }
  } catch (JSONException e) {
    Timber.e(e, "Exception loading media");
    state = PlaybackStateCompat.STATE_ERROR;
    if (callback != null) {
      callback.onPlaybackStatusChanged();
    }
  }
}
 
Example 8
Source File: MusicService.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 */
private void updatePlaybackState(String error) {
    LogUtils.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();
    }
}
 
Example 9
Source File: MediaBrowserFragment.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
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;
    } else {
        // otherwise, if state is ERROR and metadata!=null, use playback state error message:
        MediaControllerCompat controller = mMediaControllerProvider.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);
    LogUtils.d(TAG, "checkForUserVisibleErrors. forceError=", forceError,
            " showError=", showError,
            " isOnline=", NetworkHelper.isOnline(getActivity()));
}
 
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: MediaWidget.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPlaybackStateChanged(int state) {

    // Making transformation rule for the warning icon is too
    // much overkill for me.
    if (state == PlaybackStateCompat.STATE_ERROR) {
        mButtonPlayPause.setImageDrawable(mWarningDrawable);
    } else {
        mButtonPlayPause.setImageDrawable(mPlayPauseDrawable);
    }

    if (DEBUG) Log.d(TAG, "Playback state is " + state);

    final int imageDescId;
    switch (state) {
        case PlaybackStateCompat.STATE_ERROR:
            imageDescId = R.string.media_play_description;
            break;
        case PlaybackStateCompat.STATE_PLAYING:
            mPlayPauseDrawable.transformToPause();
            imageDescId = R.string.media_pause_description;
            break;
        case PlaybackStateCompat.STATE_BUFFERING:
        case PlaybackStateCompat.STATE_STOPPED:
            mPlayPauseDrawable.transformToStop();
            imageDescId = R.string.media_stop_description;
            break;
        case PlaybackStateCompat.STATE_PAUSED:
        default:
            mPlayPauseDrawable.transformToPlay();
            imageDescId = R.string.media_play_description;
            break;
    }

    mButtonPlayPause.setContentDescription(getFragment().getString(imageDescId));
}
 
Example 12
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;
    }
}