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

The following examples show how to use android.support.v4.media.session.PlaybackStateCompat#PLAYBACK_POSITION_UNKNOWN . 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: PlaybackManager.java    From klingar with Apache License 2.0 6 votes vote down vote up
void updatePlaybackState() {
  long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
  if (playback != null && playback.isConnected()) {
    position = playback.getCurrentStreamPosition();
  }

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

  addCustomActions(stateBuilder);

  @State int state = playback.getState();
  stateBuilder.setState(state, position, 1.0f, androidClock.elapsedRealTime());

  serviceCallback.onPlaybackStateUpdated(stateBuilder.build());

  if (state == STATE_PLAYING || state == STATE_PAUSED) {
    serviceCallback.onNotificationRequired();
  }
}
 
Example 2
Source File: TtsService.java    From android-app with GNU General Public License v3.0 6 votes vote down vote up
private void setMediaSessionPlaybackState() {
    if (mediaSession == null) return;

    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (textInterface != null) {
        position = textInterface.getTime();
    }

    long actions = ALWAYS_AVAILABLE_PLAYBACK_ACTIONS | (state != State.PLAYING ?
            PlaybackStateCompat.ACTION_PLAY : PlaybackStateCompat.ACTION_PAUSE);

    PlaybackStateCompat playbackState = playbackStateBuilder
            .setActions(actions)
            .setState(state.playbackState, position, state == State.PLAYING ? speed : 0)
            .build();

    mediaSession.setPlaybackState(playbackState);
}
 
Example 3
Source File: AudioPlayerService.java    From react-native-audio-streaming-player with MIT License 5 votes vote down vote up
/**
 * Update the current media player state, optionally showing an error message.
 */
public void updatePlaybackState() {
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null ) {
        position = mPlayback.getCurrentPosition();
    }
    long actions =
            PlaybackStateCompat.ACTION_PLAY_PAUSE |
            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
            PlaybackStateCompat.ACTION_SKIP_TO_NEXT;

    if (mPlayback != null && mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }

    int state = mPlayback.getState();

    //noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(actions)
            .setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    mMediaSession.setPlaybackState(stateBuilder.build());

    Intent intent = new Intent("change-playback-state-event");
    intent.putExtra("state", state);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
 
Example 4
Source File: AudioPlayerService.java    From react-native-streaming-audio-player with MIT License 5 votes vote down vote up
/**
 * Update the current media player state, optionally showing an error message.
 */
public void updatePlaybackState() {
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null ) {
        position = mPlayback.getCurrentPosition();
    }
    long actions =
            PlaybackStateCompat.ACTION_PLAY_PAUSE |
            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
            PlaybackStateCompat.ACTION_SKIP_TO_NEXT;

    if (mPlayback != null && mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }

    int state = mPlayback.getState();

    //noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(actions)
            .setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    mMediaSession.setPlaybackState(stateBuilder.build());

    Intent intent = new Intent("change-playback-state-event");
    intent.putExtra("state", state);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
 
Example 5
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();
    }
}