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

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

Introduction

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

Prototype

int STATE_PLAYING

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

Click Source Link

Document

State indicating this item is currently playing.

Usage

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

public static void lockscreenMedia(MediaSessionCompat mediaSessionCompat, MusicXService musicXService,
        String what) {/*w  w w . j ava 2s.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   ww  w. j  a va2  s. co  m

    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:com.thelotradio.android.PlayerActivity.java

@Override
public void onClick(View view) {
    MediaControllerCompat controller = getSupportMediaController();
    if (controller != null) {
        int state = controller.getPlaybackState().getState();
        if (state == PlaybackStateCompat.STATE_PAUSED) {
            controller.getTransportControls().play();
        } else if (state == PlaybackStateCompat.STATE_PLAYING) {
            controller.getTransportControls().pause();
        }//  w  ww  .  ja v a 2s .c om
    }
}

From source file:org.runbuddy.tomahawk.mediaplayers.AndroidMediaPlayer.java

@Override
public void play() {
    mPlayState = PlaybackStateCompat.STATE_PLAYING;
    handlePlayState();
}

From source file:net.simno.klingar.ui.MiniPlayerController.java

private void updatePlayButton(@State int state) {
    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_BUFFERING) {
        playPauseButton.setImageState(PAUSE, true);
        playPauseButton.setContentDescription(descPause);
    } else {//  ww  w .ja v a 2  s  . c  om
        playPauseButton.setImageState(PLAY, true);
        playPauseButton.setContentDescription(descPlay);
    }
}

From source file:com.achep.acdisplay.services.media.MediaControlsHelper.java

@Override
public void onPlaybackStateChanged(int state) {
    Check.getInstance().isInMainThread();
    switch (state) {
    case PlaybackStateCompat.STATE_PLAYING:
        mHandler.removeMessages(H.MSG_HIDE_MEDIA_WIDGET);
        if (!mShowing) {
            mShowing = true;//from  w ww.  j a v  a  2 s . c om
            notifyOnStateChanged();
        }
        break;
    default:
        if (mShowing) {
            int delay = state == PlaybackStateCompat.STATE_NONE ? 500 : DELAY;
            mHandler.sendEmptyMessageDelayed(H.MSG_HIDE_MEDIA_WIDGET, delay);
        }
        break;
    }
}

From source file:com.achep.acdisplay.services.media.MediaController2KitKat.java

@NonNull
static SparseIntArray generatePlaybackCompatSparse() {
    SparseIntArray sia = new SparseIntArray();
    sia.put(RemoteControlClient.PLAYSTATE_BUFFERING, PlaybackStateCompat.STATE_BUFFERING);
    sia.put(RemoteControlClient.PLAYSTATE_PLAYING, PlaybackStateCompat.STATE_PLAYING);
    sia.put(RemoteControlClient.PLAYSTATE_PAUSED, PlaybackStateCompat.STATE_PAUSED);
    sia.put(RemoteControlClient.PLAYSTATE_ERROR, PlaybackStateCompat.STATE_ERROR);
    sia.put(RemoteControlClient.PLAYSTATE_REWINDING, PlaybackStateCompat.STATE_REWINDING);
    sia.put(RemoteControlClient.PLAYSTATE_FAST_FORWARDING, PlaybackStateCompat.STATE_FAST_FORWARDING);
    sia.put(RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS, PlaybackStateCompat.STATE_SKIPPING_TO_NEXT);
    sia.put(RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS, PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS);
    return sia;/* www.j a  va 2s . c  o  m*/
}

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

void updatePlaybackState() {
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (playback != null && playback.isConnected()) {
        position = playback.getCurrentStreamPosition();
    }/*from   www.  j a v a  2  s . c o  m*/

    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 == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        serviceCallback.onNotificationRequired();
    }
}

From source file:org.runbuddy.tomahawk.ui.fragments.AlbumsFragment.java

/**
 * Called every time an item inside a ListView or GridView is clicked
 *
 * @param view the clicked view//from ww w .  ja v a  2 s .  com
 * @param item the Object which corresponds to the click
 */
@Override
public void onItemClick(View view, final Object item, Segment segment) {
    if (getMediaController() == null) {
        Log.e(TAG, "onItemClick failed because getMediaController() is null");
        return;
    }
    if (item instanceof PlaylistEntry) {
        final PlaylistEntry entry = (PlaylistEntry) item;
        if (entry.getQuery().isPlayable()) {
            if (getPlaybackManager().getCurrentEntry() == entry) {
                // if the user clicked on an already playing track
                int playState = getMediaController().getPlaybackState().getState();
                if (playState == PlaybackStateCompat.STATE_PLAYING) {
                    getMediaController().getTransportControls().pause();
                } else if (playState == PlaybackStateCompat.STATE_PAUSED) {
                    getMediaController().getTransportControls().play();
                }
            } else {
                if (!TomahawkApp.PLUGINNAME_HATCHET.equals(mCollection.getId())) {
                    mCollection.getArtistTracks(mArtist).done(new DoneCallback<Playlist>() {
                        @Override
                        public void onDone(Playlist topHits) {
                            getPlaybackManager().setPlaylist(topHits, entry);
                            getMediaController().getTransportControls().play();
                        }
                    });
                } else {
                    HatchetCollection collection = (HatchetCollection) mCollection;
                    collection.getArtistTopHits(mArtist).done(new DoneCallback<Playlist>() {
                        @Override
                        public void onDone(Playlist topHits) {
                            getPlaybackManager().setPlaylist(topHits, entry);
                            getMediaController().getTransportControls().play();
                        }
                    });
                }
            }
        }
    } else if (item instanceof Album) {
        Album album = (Album) item;
        mCollection.getAlbumTracks(album).done(new DoneCallback<Playlist>() {
            @Override
            public void onDone(Playlist playlist) {
                Bundle bundle = new Bundle();
                bundle.putString(TomahawkFragment.ALBUM, ((Album) item).getCacheKey());
                if (playlist != null) {
                    bundle.putString(TomahawkFragment.COLLECTION_ID, mCollection.getId());
                } else {
                    bundle.putString(TomahawkFragment.COLLECTION_ID, TomahawkApp.PLUGINNAME_HATCHET);
                }
                bundle.putInt(CONTENT_HEADER_MODE, ContentHeaderFragment.MODE_HEADER_DYNAMIC);
                FragmentUtils.replace((TomahawkMainActivity) getActivity(), PlaylistEntriesFragment.class,
                        bundle);
            }
        });
    }
}

From source file:android.support.v17.leanback.media.MediaControllerGlue.java

@Override
public boolean isMediaPlaying() {
    return mMediaController.getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING;
}