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

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

Introduction

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

Prototype

long ACTION_PLAY

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

Click Source Link

Document

Indicates this session supports the play command.

Usage

From source file:com.wojtechnology.sunami.MediaSessionCompatHelper.java

private static void ensureTransportControls(MediaSessionCompat session, PlaybackStateCompat playbackState) {
    long actions = playbackState.getActions();
    Object remoteObj = session.getRemoteControlClient();
    if (actions != 0 && remoteObj != null) {

        int transportControls = 0;

        if ((actions & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS;
        }// w ww .  j  a  v  a 2  s  .c o m

        if ((actions & PlaybackStateCompat.ACTION_REWIND) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_REWIND;
        }

        if ((actions & PlaybackStateCompat.ACTION_PLAY) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PLAY;
        }

        if ((actions & PlaybackStateCompat.ACTION_PLAY_PAUSE) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE;
        }

        if ((actions & PlaybackStateCompat.ACTION_PAUSE) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_PAUSE;
        }

        if ((actions & PlaybackStateCompat.ACTION_STOP) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_STOP;
        }

        if ((actions & PlaybackStateCompat.ACTION_FAST_FORWARD) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD;
        }

        if ((actions & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_NEXT;
        }

        if ((actions & PlaybackStateCompat.ACTION_SEEK_TO) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE;
        }

        if ((actions & PlaybackStateCompat.ACTION_SET_RATING) != 0) {
            transportControls |= RemoteControlClient.FLAG_KEY_MEDIA_RATING;
        }

        ((RemoteControlClient) remoteObj).setTransportControlFlags(transportControls);
    }
}

From source file:com.rks.musicx.services.MediaSession.java

public static void lockscreenMedia(MediaSessionCompat mediaSessionCompat, MusicXService musicXService,
        String what) {/*w  w w.  ja v a2  s.c o  m*/
    if (musicXService == null) {
        return;
    }
    MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
    if (what.equals(PLAYSTATE_CHANGED) || what.equals(META_CHANGED)) {
        int state = MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()
                ? PlaybackStateCompat.STATE_PAUSED
                : PlaybackStateCompat.STATE_PLAYING;
        mediaSessionCompat.setPlaybackState(new PlaybackStateCompat.Builder()
                .setState(state, musicXService.getPlayerPos(), 1.0f)
                .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
                        | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                        | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                .build());

        builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, musicXService.getsongTitle());
        builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, musicXService.getDuration());
        builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, musicXService.getsongArtistName());
        builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, musicXService.getsongAlbumName());
        handler.post(new Runnable() {
            @Override
            public void run() {
                ArtworkUtils.ArtworkLoader(musicXService, 300, 300, musicXService.getsongAlbumName(),
                        musicXService.getsongAlbumID(), new palette() {
                            @Override
                            public void palettework(Palette palette) {

                            }
                        }, new bitmap() {
                            @Override
                            public void bitmapwork(Bitmap bitmap) {
                                builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap);
                                mediaSessionCompat.setMetadata(builder.build());
                            }

                            @Override
                            public void bitmapfailed(Bitmap bitmap) {
                                builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap);
                                mediaSessionCompat.setMetadata(builder.build());
                            }
                        });
            }
        });
    }
}

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

public static Notification createNotification(Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mMetadata = controller.getMetadata();
    PlaybackStateCompat mPlaybackState = controller.getPlaybackState();

    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }//from w w  w  .  j  a  v  a2  s. com

    boolean isPlaying = mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING;
    NotificationCompat.Action action = isPlaying
            ? new NotificationCompat.Action(R.drawable.ic_pause_white_24dp,
                    context.getString(R.string.label_pause),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                            PlaybackStateCompat.ACTION_PAUSE))
            : new NotificationCompat.Action(R.drawable.ic_play_arrow_white_24dp,
                    context.getString(R.string.label_play), MediaButtonReceiver
                            .buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY));

    MediaDescriptionCompat description = mMetadata.getDescription();
    Bitmap art = description.getIconBitmap();
    if (art == null) {
        // use a placeholder art while the remote art is being downloaded.
        art = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_default_art);
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
    notificationBuilder.setStyle(new NotificationCompat.MediaStyle()
            // show only play/pause in compact view.
            .setShowActionsInCompactView(new int[] { 0 }).setMediaSession(mediaSession.getSessionToken()))
            .addAction(action).setSmallIcon(R.drawable.ic_notification).setShowWhen(false)
            .setContentIntent(controller.getSessionActivity()).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    return notificationBuilder.build();
}

From source file:MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MediaSessionCompat mediaSession = new MediaSessionCompat(this, getApplication().getPackageName());
    mediaSession.setCallback(mMediaSessionCallback);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setActive(true);//from w ww  . j  a v a2s.c  om
    PlaybackStateCompat state = new PlaybackStateCompat.Builder().setActions(PlaybackStateCompat.ACTION_PLAY
            | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE
            | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS).build();
    mediaSession.setPlaybackState(state);

}

From source file:net.simno.klingar.playback.PlaybackManager.java

private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
            | PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    if (playback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {/*w  w  w.j  a  v a  2  s.  co m*/
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }
    return actions;
}

From source file:com.doctoror.fuckoffmusicplayer.data.reporter.MediaSessionPlaybackReporter.java

@Override
public void reportPlaybackStateChanged(@NonNull final PlaybackState state,
        @Nullable final CharSequence errorMessage) {
    @PlaybackStateCompat.State//from   w w w.  ja  v  a 2 s  .c  o m
    final int playbackState = toPlaybackStateCompat(state);
    final boolean isPlaying = playbackState == PlaybackStateCompat.STATE_PLAYING;
    final PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
                    | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_STOP
                    | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
                    | PlaybackStateCompat.ACTION_SEEK_TO | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH)
            .setState(playbackState, 0, isPlaying ? 1 : 0);

    if (errorMessage != null) {
        builder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_APP_ERROR, errorMessage);
    }

    mMediaSession.setPlaybackState(builder.build());
}

From source file:com.classiqo.nativeandroid_32bitz.playback.PlaybackManager.java

private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
            | PlaybackStateCompat.ACTION_SKIP_TO_NEXT;

    if (mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {// www .  ja v  a 2s .  c  o  m
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }

    return actions;
}

From source file:com.reallynourl.nourl.fmpfoldermusicplayer.ui.notifications.MediaNotification.java

private static void updateMediaSession(MediaSessionCompat mediaSession) {
    ExtendedFile currentFile = MediaManager.getInstance().getCurrentFile();
    if (currentFile != null) {
        long validActions = PlaybackStateCompat.ACTION_STOP;
        if (MediaManager.getInstance().canPlay()) {
            validActions |= PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE;
        }//w w  w  .  ja v  a 2s.c om
        if (MediaManager.getInstance().hasNext()) {
            validActions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
        }
        if (MediaManager.getInstance().hasPrevious()) {
            validActions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
        }
        int playState = MediaManager.getInstance().isPlaying() ? PlaybackStateCompat.STATE_PLAYING
                : PlaybackStateCompat.STATE_PAUSED;
        mediaSession.setMetadata(new MediaMetadataCompat.Builder()
                .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, currentFile.getParentFile().getName())
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, currentFile.getNameWithoutExtension())
                .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, MediaManager.getInstance().getDuration())
                .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER,
                        MediaManager.getInstance().getPlaylist().getCurrentIndex() + 1)
                .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS,
                        MediaManager.getInstance().getPlaylist().size())
                //.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)
                .build());
        mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
                .setState(playState, MediaManager.getInstance().getPosition(), 1.0f).setActions(validActions)
                .build());
    } else {
        mediaSession.setActive(false);
    }
}

From source file:com.appdevper.mediaplayer.app.PlaybackManager.java

private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
            | PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    if (mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    }//from  ww w  . ja  v  a  2  s  . c  o  m
    return actions;
}

From source file:com.example.android.uamp.playback.PlaybackManager.java

private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
            | PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    if (mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {/*w w w  .j ava  2s .  c  om*/
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }
    return actions;
}