Java Code Examples for android.media.session.PlaybackState#STATE_NONE

The following examples show how to use android.media.session.PlaybackState#STATE_NONE . 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: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 7 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowser.MediaItem item = getItem(position);
    int itemState = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        String itemMediaId = item.getDescription().getMediaId();
        int playbackState = PlaybackState.STATE_NONE;
        if (mCurrentState != null) {
            playbackState = mCurrentState.getState();
        }
        if (mCurrentMetadata != null &&
                itemMediaId.equals(mCurrentMetadata.getDescription().getMediaId())) {
            if (playbackState == PlaybackState.STATE_PLAYING ||
                playbackState == PlaybackState.STATE_BUFFERING) {
                itemState = MediaItemViewHolder.STATE_PLAYING;
            } else if (playbackState != PlaybackState.STATE_ERROR) {
                itemState = MediaItemViewHolder.STATE_PAUSED;
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
        item.getDescription(), itemState);
}
 
Example 2
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 3
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 6 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowser.MediaItem item = getItem(position);
    int itemState = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        String itemMediaId = item.getDescription().getMediaId();
        int playbackState = PlaybackState.STATE_NONE;
        if (mCurrentState != null) {
            playbackState = mCurrentState.getState();
        }
        if (mCurrentMetadata != null &&
                itemMediaId.equals(mCurrentMetadata.getDescription().getMediaId())) {
            if (playbackState == PlaybackState.STATE_PLAYING ||
                playbackState == PlaybackState.STATE_BUFFERING) {
                itemState = MediaItemViewHolder.STATE_PLAYING;
            } else if (playbackState != PlaybackState.STATE_ERROR) {
                itemState = MediaItemViewHolder.STATE_PAUSED;
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
        item.getDescription(), itemState);
}
 
Example 4
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    final int state = mCurrentState == null ?
            PlaybackState.STATE_NONE : mCurrentState.getState();
    if (state == PlaybackState.STATE_PAUSED ||
            state == PlaybackState.STATE_STOPPED ||
            state == PlaybackState.STATE_NONE) {

        if (mCurrentMetadata == null) {
            mCurrentMetadata = MusicLibrary.getMetadata(MusicPlayerActivity.this,
                    MusicLibrary.getMediaItems().get(0).getMediaId());
            updateMetadata(mCurrentMetadata);
        }
        getMediaController().getTransportControls().playFromMediaId(
                mCurrentMetadata.getDescription().getMediaId(), null);

    } else {
        getMediaController().getTransportControls().pause();
    }
}
 
Example 5
Source File: PlaybackControlBaseActivity.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the MediaSession is active and in a "playback-able" state
 * (not NONE and not STOPPED).
 *
 * @return true if the MediaSession's state requires playback controls to be visible.
 */
protected boolean shouldShowControls() {
    MediaControllerCompat mediaController = mMediaController;
    if (mediaController == null ||
            mediaController.getMetadata() == null ||
            mediaController.getPlaybackState() == null) {
        return false;
    }
    switch (mediaController.getPlaybackState().getState()) {
        case PlaybackState.STATE_ERROR:
        case PlaybackState.STATE_NONE:
        case PlaybackState.STATE_STOPPED:
            return false;
        default:
            return true;
    }
}
 
Example 6
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowser.MediaItem item = getItem(position);
    int itemState = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        String itemMediaId = item.getDescription().getMediaId();
        int playbackState = PlaybackState.STATE_NONE;
        if (mCurrentState != null) {
            playbackState = mCurrentState.getState();
        }
        if (mCurrentMetadata != null &&
                itemMediaId.equals(mCurrentMetadata.getDescription().getMediaId())) {
            if (playbackState == PlaybackState.STATE_PLAYING ||
                playbackState == PlaybackState.STATE_BUFFERING) {
                itemState = MediaItemViewHolder.STATE_PLAYING;
            } else if (playbackState != PlaybackState.STATE_ERROR) {
                itemState = MediaItemViewHolder.STATE_PAUSED;
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
        item.getDescription(), itemState);
}
 
Example 7
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowser.MediaItem item = getItem(position);
    int itemState = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        String itemMediaId = item.getDescription().getMediaId();
        int playbackState = PlaybackState.STATE_NONE;
        if (mCurrentState != null) {
            playbackState = mCurrentState.getState();
        }
        if (mCurrentMetadata != null &&
                itemMediaId.equals(mCurrentMetadata.getDescription().getMediaId())) {
            if (playbackState == PlaybackState.STATE_PLAYING ||
                playbackState == PlaybackState.STATE_BUFFERING) {
                itemState = MediaItemViewHolder.STATE_PLAYING;
            } else if (playbackState != PlaybackState.STATE_ERROR) {
                itemState = MediaItemViewHolder.STATE_PAUSED;
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
        item.getDescription(), itemState);
}
 
Example 8
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    final int state = mCurrentState == null ?
            PlaybackState.STATE_NONE : mCurrentState.getState();
    if (state == PlaybackState.STATE_PAUSED ||
            state == PlaybackState.STATE_STOPPED ||
            state == PlaybackState.STATE_NONE) {

        if (mCurrentMetadata == null) {
            mCurrentMetadata = MusicLibrary.getMetadata(MusicPlayerActivity.this,
                    MusicLibrary.getMediaItems().get(0).getMediaId());
            updateMetadata(mCurrentMetadata);
        }
        getMediaController().getTransportControls().playFromMediaId(
                mCurrentMetadata.getDescription().getMediaId(), null);

    } else {
        getMediaController().getTransportControls().pause();
    }
}
 
Example 9
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onClick(View v) {
    final int state = mCurrentState == null ?
            PlaybackState.STATE_NONE : mCurrentState.getState();
    if (state == PlaybackState.STATE_PAUSED ||
            state == PlaybackState.STATE_STOPPED ||
            state == PlaybackState.STATE_NONE) {

        if (mCurrentMetadata == null) {
            mCurrentMetadata = MusicLibrary.getMetadata(MusicPlayerActivity.this,
                    MusicLibrary.getMediaItems().get(0).getMediaId());
            updateMetadata(mCurrentMetadata);
        }
        // ------------ CHANGE 5 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        mPlaybackManager.play(mCurrentMetadata);

        /* ------------ CHANGE 5 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        getMediaController().getTransportControls().playFromMediaId(
                mCurrentMetadata.getDescription().getMediaId(), null);
        // ------------ CHANGE 5 - END OF PLAYBACK ON A SERVICE SNIPPET */

    } else {
        // ------------ CHANGE 6 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        mPlaybackManager.pause();

        /* ------------ CHANGE 6 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        getMediaController().getTransportControls().pause();
        // ------------ CHANGE 6 - END OF PLAYBACK ON A SERVICE SNIPPET */
    }
}
 
Example 10
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
@Override
public void onClick(View v) {
    final int state = mCurrentState == null ?
            PlaybackState.STATE_NONE : mCurrentState.getState();
    if (state == PlaybackState.STATE_PAUSED ||
            state == PlaybackState.STATE_STOPPED ||
            state == PlaybackState.STATE_NONE) {

        if (mCurrentMetadata == null) {
            mCurrentMetadata = MusicLibrary.getMetadata(MusicPlayerActivity.this,
                    MusicLibrary.getMediaItems().get(0).getMediaId());
            updateMetadata(mCurrentMetadata);
        }
        // ------------ CHANGE 5 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        mPlaybackManager.play(mCurrentMetadata);

        /* ------------ CHANGE 5 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        getMediaController().getTransportControls().playFromMediaId(
                mCurrentMetadata.getDescription().getMediaId(), null);
        // ------------ CHANGE 5 - END OF PLAYBACK ON A SERVICE SNIPPET */

    } else {
        // ------------ CHANGE 6 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        mPlaybackManager.pause();

        /* ------------ CHANGE 6 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        getMediaController().getTransportControls().pause();
        // ------------ CHANGE 6 - END OF PLAYBACK ON A SERVICE SNIPPET */
    }
}
 
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);
    }
}
 
Example 13
Source File: MediaSessionRecord.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * Check if the session is currently performing playback.
 *
 * @return True if the session is performing playback, false otherwise.
 */
public boolean isPlaybackActive() {
    int state = mPlaybackState == null ? PlaybackState.STATE_NONE : mPlaybackState.getState();
    return MediaSession.isActiveState(state);
}