Java Code Examples for android.media.session.PlaybackState#getState()

The following examples show how to use android.media.session.PlaybackState#getState() . 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: MediaSessionRecord.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void setPlaybackState(PlaybackState state) {
    int oldState = mPlaybackState == null
            ? PlaybackState.STATE_NONE : mPlaybackState.getState();
    int newState = state == null
            ? PlaybackState.STATE_NONE : state.getState();
    synchronized (mLock) {
        mPlaybackState = state;
    }
    final long token = Binder.clearCallingIdentity();
    try {
        mService.onSessionPlaystateChanged(MediaSessionRecord.this, oldState, newState);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    mHandler.post(MessageHandler.MSG_UPDATE_PLAYBACK_STATE);
}
 
Example 2
Source File: PlayerState.java    From scroball with MIT License 6 votes vote down vote up
public void setPlaybackState(PlaybackState playbackState) {
  if (playbackItem == null) {
    return;
  }

  playbackItem.updateAmountPlayed();

  int state = playbackState.getState();
  boolean isPlaying = state == PlaybackState.STATE_PLAYING;

  if (isPlaying) {
    Log.d(TAG, "Track playing");
    postEvent(playbackItem.getTrack());
    playbackItem.startPlaying();
    notificationManager.updateNowPlaying(playbackItem.getTrack());
    scheduleSubmission();
  } else {
    Log.d(TAG, String.format("Track paused (state %d)", state));
    postEvent(Track.empty());
    playbackItem.stopPlaying();
    notificationManager.removeNowPlaying();
    scrobbler.submit(playbackItem);
  }
}
 
Example 3
Source File: PlaybackOverlayFragment.java    From BuildingForAndroidTV with MIT License 6 votes vote down vote up
@Override
public void onPlaybackStateChanged(PlaybackState state) {
    Log.d(TAG, "playback state changed: " + state.getState());
    if (state.getState() == PlaybackState.STATE_PLAYING && mCurrentPlaybackState != PlaybackState.STATE_PLAYING) {
        mCurrentPlaybackState = PlaybackState.STATE_PLAYING;
        startProgressAutomation();
        setFadingEnabled(true);
        mPlayPauseAction.setIndex(PlayPauseAction.PAUSE);
        mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PAUSE));
        notifyChanged(mPlayPauseAction);
    } else if (state.getState() == PlaybackState.STATE_PAUSED && mCurrentPlaybackState != PlaybackState.STATE_PAUSED) {
        mCurrentPlaybackState = PlaybackState.STATE_PAUSED;
        stopProgressAutomation();
        setFadingEnabled(false);
        mPlayPauseAction.setIndex(PlayPauseAction.PLAY);
        mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PLAY));
        notifyChanged(mPlayPauseAction);
    }

    int currentTime = (int)state.getPosition();
    mPlaybackControlsRow.setCurrentTime(currentTime);
    mPlaybackControlsRow.setBufferedProgress(currentTime + SIMULATED_BUFFERED_TIME);
}
 
Example 4
Source File: MediaSessionRecord.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private PlaybackState getStateWithUpdatedPosition() {
    PlaybackState state;
    long duration = -1;
    synchronized (mLock) {
        state = mPlaybackState;
        if (mMetadata != null && mMetadata.containsKey(MediaMetadata.METADATA_KEY_DURATION)) {
            duration = mMetadata.getLong(MediaMetadata.METADATA_KEY_DURATION);
        }
    }
    PlaybackState result = null;
    if (state != null) {
        if (state.getState() == PlaybackState.STATE_PLAYING
                || state.getState() == PlaybackState.STATE_FAST_FORWARDING
                || state.getState() == PlaybackState.STATE_REWINDING) {
            long updateTime = state.getLastPositionUpdateTime();
            long currentTime = SystemClock.elapsedRealtime();
            if (updateTime > 0) {
                long position = (long) (state.getPlaybackSpeed()
                        * (currentTime - updateTime)) + state.getPosition();
                if (duration >= 0 && position > duration) {
                    position = duration;
                } else if (position < 0) {
                    position = 0;
                }
                PlaybackState.Builder builder = new PlaybackState.Builder(state);
                builder.setState(state.getState(), position, state.getPlaybackSpeed(),
                        currentTime);
                result = builder.build();
            }
        }
    }
    return result == null ? state : result;
}
 
Example 5
Source File: MediaService.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPlaybackStateChanged(@NonNull PlaybackState state) {
    super.onPlaybackStateChanged(state);
    byte[] data = new byte[1];
    data[0] = (byte)(state.getState() == PlaybackState.STATE_PLAYING ?  1 : 0);
    mDevice.write(mediaPlayingCharac, data, MediaService.this);
}
 
Example 6
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
private void updatePlaybackState(PlaybackState state) {
    mCurrentState = state;
    if (state == null || state.getState() == PlaybackState.STATE_PAUSED ||
            state.getState() == PlaybackState.STATE_STOPPED) {
        mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_play_arrow_black_36dp));
    } else {
        mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_pause_black_36dp));
    }
    mPlaybackControls.setVisibility(state == null ? View.GONE : View.VISIBLE);
}
 
Example 7
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
private void updatePlaybackState(PlaybackState state) {
    mCurrentState = state;
    if (state == null || state.getState() == PlaybackState.STATE_PAUSED ||
            state.getState() == PlaybackState.STATE_STOPPED) {
        mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_play_arrow_black_36dp));
    } else {
        mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_pause_black_36dp));
    }
    mPlaybackControls.setVisibility(state == null ? View.GONE : View.VISIBLE);
}
 
Example 8
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
private void updatePlaybackState(PlaybackState state) {
    mCurrentState = state;
    if (state == null || state.getState() == PlaybackState.STATE_PAUSED ||
            state.getState() == PlaybackState.STATE_STOPPED) {
        mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_play_arrow_black_36dp));
    } else {
        mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_pause_black_36dp));
    }
    mPlaybackControls.setVisibility(state == null ? View.GONE : View.VISIBLE);
}
 
Example 9
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
private void updatePlaybackState(PlaybackState state) {
    mCurrentState = state;
    if (state == null || state.getState() == PlaybackState.STATE_PAUSED ||
            state.getState() == PlaybackState.STATE_STOPPED) {
        mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_play_arrow_black_36dp));
    } else {
        mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_pause_black_36dp));
    }
    mPlaybackControls.setVisibility(state == null ? View.GONE : View.VISIBLE);
}
 
Example 10
Source File: MediaController2Lollipop.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
@Nullable
private MediaController pickBestMediaController(
        @NonNull List<MediaController> list) {
    int mediaControllerScore = -1;
    MediaController mediaController = null;
    for (MediaController mc : list) {
        if (mc == null) continue;
        int mcScore = 0;

        // Check for the current state
        PlaybackState state = mc.getPlaybackState();
        if (state != null) {
            switch (state.getState()) {
                case PlaybackState.STATE_STOPPED:
                case PlaybackState.STATE_ERROR:
                    break;
                default:
                    mcScore++;
                    break;
            }
        }

        if (mcScore > mediaControllerScore) {
            mediaControllerScore = mcScore;
            mediaController = mc;
        }
    }
    return mediaController;
}
 
Example 11
Source File: MediaNotificationManager.java    From android-music-player with Apache License 2.0 4 votes vote down vote up
public void update(MediaMetadata metadata, PlaybackState state, MediaSession.Token token) {
    if (state == null || state.getState() == PlaybackState.STATE_STOPPED ||
            state.getState() == PlaybackState.STATE_NONE) {
        mService.stopForeground(true);
        try {
            mService.unregisterReceiver(this);
        } catch (IllegalArgumentException ex) {
            // ignore receiver not registered
        }
        mService.stopSelf();
        return;
    }
    if (metadata == null) {
        return;
    }
    boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
    Notification.Builder notificationBuilder = new Notification.Builder(mService);
    MediaDescription description = metadata.getDescription();

    notificationBuilder
            .setStyle(new Notification.MediaStyle()
                    .setMediaSession(token)
                    .setShowActionsInCompactView(0, 1, 2))
            .setColor(mService.getApplication().getResources().getColor(R.color.notification_bg))
            .setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setContentIntent(createContentIntent())
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
            .setOngoing(isPlaying)
            .setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
            .setShowWhen(isPlaying)
            .setUsesChronometer(isPlaying);

    // If skip to next action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(mPrevAction);
    }

    notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);

    // If skip to prev action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(mNextAction);
    }

    Notification notification = notificationBuilder.build();

    if (isPlaying && !mStarted) {
        mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
        mService.startForeground(NOTIFICATION_ID, notification);
        mStarted = true;
    } else {
        if (!isPlaying) {
            mService.stopForeground(false);
            mStarted = false;
        }
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
}
 
Example 12
Source File: MediaNotificationManager.java    From io2015-codelabs with Apache License 2.0 4 votes vote down vote up
public void update(MediaMetadata metadata, PlaybackState state, MediaSession.Token token) {
    if (state == null || state.getState() == PlaybackState.STATE_STOPPED ||
            state.getState() == PlaybackState.STATE_NONE) {
        mService.stopForeground(true);
        try {
            mService.unregisterReceiver(this);
        } catch (IllegalArgumentException ex) {
            // ignore receiver not registered
        }
        mService.stopSelf();
        return;
    }
    if (metadata == null) {
        return;
    }
    boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
    Notification.Builder notificationBuilder = new Notification.Builder(mService);
    MediaDescription description = metadata.getDescription();

    notificationBuilder
            .setStyle(new Notification.MediaStyle()
                    .setMediaSession(token)
                    .setShowActionsInCompactView(0, 1, 2))
            .setColor(mService.getApplication().getResources().getColor(R.color.notification_bg))
            .setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setContentIntent(createContentIntent())
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
            .setOngoing(isPlaying)
            .setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
            .setShowWhen(isPlaying)
            .setUsesChronometer(isPlaying);

    // If skip to next action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(mPrevAction);
    }

    notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);

    // If skip to prev action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(mNextAction);
    }

    Notification notification = notificationBuilder.build();

    if (isPlaying && !mStarted) {
        mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
        mService.startForeground(NOTIFICATION_ID, notification);
        mStarted = true;
    } else {
        if (!isPlaying) {
            mService.stopForeground(false);
            mStarted = false;
        }
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
}