List of usage examples for android.support.v4.media.session PlaybackStateCompat STATE_PLAYING
int STATE_PLAYING
To view the source code for android.support.v4.media.session PlaybackStateCompat STATE_PLAYING.
Click Source Link
From source file:org.runbuddy.tomahawk.services.PlaybackService.java
/** * Update the TomahawkMediaPlayer so that it reflects the current playState *//*from ww w .j a v a 2 s . co m*/ private void handlePlayState() { Log.d(TAG, "handlePlayState"); final Query currentQuery = mPlaybackManager.getCurrentQuery(); if (currentQuery != null && currentQuery.getMediaPlayerClass() != null) { final TomahawkMediaPlayer mp = mMediaPlayers.get(currentQuery.getMediaPlayerClass()); Runnable r = new Runnable() { @Override public void run() { switch (mPlayState) { case PlaybackStateCompat.STATE_PLAYING: // The service needs to continue running even after the bound client // (usually a MediaController) disconnects, otherwise the music playback // will stop. Calling startService(Intent) will keep the service running // until it is explicitly killed. startService(new Intent(getApplicationContext(), PlaybackService.class)); if (mWakeLock != null && mWakeLock.isHeld()) { mWakeLock.acquire(); } if (mp.isPreparing(currentQuery) || mp.isPrepared(currentQuery)) { if (!mp.isPlaying(currentQuery)) { mp.play(); } } else { prepareCurrentQuery(); } break; case PlaybackStateCompat.STATE_PAUSED: if (mp.isPlaying(currentQuery) && (mp.isPreparing(currentQuery) || mp.isPrepared(currentQuery))) { InfoSystem.get().sendPlaybackEntryPostStruct(AuthenticatorManager.get() .getAuthenticatorUtils(TomahawkApp.PLUGINNAME_HATCHET)); mp.pause(); } if (mWakeLock != null && mWakeLock.isHeld()) { mWakeLock.release(); } break; } } }; ThreadManager.get().executePlayback(mp, r); } else { releaseAllPlayers(); if (mWakeLock != null && mWakeLock.isHeld()) { mWakeLock.release(); } Log.d(TAG, "handlePlayState couldn't do anything, isPreparing: " + mIsPreparing); } }
From source file:com.torrenttunes.android.MusicService.java
/** * Helper to switch to a different Playback instance * @param playback switch to this playback */// ww w . ja va 2s. c o m private void switchToPlayer(Playback playback, boolean resumePlaying) { if (playback == null) { throw new IllegalArgumentException("Playback cannot be null"); } // suspend the current one. int oldState = mPlayback.getState(); int pos = mPlayback.getCurrentStreamPosition(); String currentMediaId = mPlayback.getCurrentMediaId(); LogHelper.d(TAG, "Current position from " + playback + " is ", pos); mPlayback.stop(false); playback.setCallback(this); playback.setCurrentStreamPosition(pos < 0 ? 0 : pos); playback.setCurrentMediaId(currentMediaId); playback.start(); // finally swap the instance mPlayback = playback; switch (oldState) { case PlaybackStateCompat.STATE_BUFFERING: case PlaybackStateCompat.STATE_CONNECTING: case PlaybackStateCompat.STATE_PAUSED: mPlayback.pause(); break; case PlaybackStateCompat.STATE_PLAYING: if (resumePlaying && QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) { mPlayback.play(mPlayingQueue.get(mCurrentIndexOnQueue)); } else if (!resumePlaying) { mPlayback.pause(); } else { mPlayback.stop(true); } break; case PlaybackStateCompat.STATE_NONE: break; default: LogHelper.d(TAG, "Default called. Old state is ", oldState); } }
From source file:com.techmighty.baseplayer.MusicService.java
private void updateMediaSession(final String what) { int playState = mIsSupposedToBePlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build());/*from www . ja v a 2 s . c om*/ } } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) { Bitmap albumArt = ImageLoader.getInstance() .loadImageSync(BasePlayerUtils.getAlbumArtUri(getAlbumId()).toString()); if (albumArt != null) { Bitmap.Config config = albumArt.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } albumArt = albumArt.copy(config, false); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mSession.setMetadata(new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName()) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName()) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName()) .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName()) .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration()) .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1) .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().length) .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName()) .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, mShowAlbumArtOnLockscreen ? albumArt : null) .build()); mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build()); } } }
From source file:com.devalladolid.musictoday.MusicService.java
private void updateMediaSession(final String what) { int playState = mIsSupposedToBePlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build());//from w w w . ja va 2s .c o m } } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) { Bitmap albumArt = ImageLoader.getInstance() .loadImageSync(TimberUtils.getAlbumArtUri(getAlbumId()).toString()); if (albumArt != null) { Bitmap.Config config = albumArt.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } albumArt = albumArt.copy(config, false); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mSession.setMetadata(new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName()) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName()) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName()) .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName()) .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration()) .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1) .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().length) .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName()) .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, mShowAlbumArtOnLockscreen ? albumArt : null) .build()); mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build()); } } }
From source file:com.example.android.leanback.MediaSessionService.java
private void audioFocusGainHandler() { // In this case the app has been granted audio focus again // Firstly, raise volume to normal mPlayer.setVolume(FULL_VOLUME, FULL_VOLUME); // If the recorded position is the same as current position // Start the player directly if (mCurrentPosition == mPlayer.getCurrentPosition()) { mPlayer.start();/*ww w. j a v a 2s .c om*/ mMediaSession.setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PLAYING).build()); // If the recorded position is not equal to current position // The player will seek to the last recorded position firstly to continue playing the // last music item } else { mPlayer.seekTo(mCurrentPosition); PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(PlaybackStateCompat.STATE_BUFFERING); builder.setBufferedPosition(mBufferedProgress); mMediaSession.setPlaybackState(builder.build()); } }
From source file:com.naman14.timber.musicplayer.MusicService.java
private void updateMediaSession(final String what) { int playState = mIsSupposedToBePlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build());/*w w w.ja v a2 s. c om*/ } } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) { //TODO: Replace below Image download using Picasso Bitmap albumArt = ImageLoader.getInstance() .loadImageSync(TimberUtils.getAlbumArtUri(getAlbumId()).toString()); if (albumArt != null) { Bitmap.Config config = albumArt.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } albumArt = albumArt.copy(config, false); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mSession.setMetadata(new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName()) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName()) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName()) .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName()) .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration()) .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1) .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().size()) .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName()) .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, mShowAlbumArtOnLockscreen ? albumArt : null) .build()); mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build()); } } }
From source file:android.support.v7.app.MediaRouteControllerDialog.java
private void updatePlaybackControlLayout() { if (canShowPlaybackControlLayout()) { CharSequence title = mDescription == null ? null : mDescription.getTitle(); boolean hasTitle = !TextUtils.isEmpty(title); CharSequence subtitle = mDescription == null ? null : mDescription.getSubtitle(); boolean hasSubtitle = !TextUtils.isEmpty(subtitle); boolean showTitle = false; boolean showSubtitle = false; if (mRoute.getPresentationDisplayId() != MediaRouter.RouteInfo.PRESENTATION_DISPLAY_ID_NONE) { // The user is currently casting screen. mTitleView.setText(R.string.mr_controller_casting_screen); showTitle = true;// ww w . j a va 2 s .co m } else if (mState == null || mState.getState() == PlaybackStateCompat.STATE_NONE) { // Show "No media selected" as we don't yet know the playback state. mTitleView.setText(R.string.mr_controller_no_media_selected); showTitle = true; } else if (!hasTitle && !hasSubtitle) { mTitleView.setText(R.string.mr_controller_no_info_available); showTitle = true; } else { if (hasTitle) { mTitleView.setText(title); showTitle = true; } if (hasSubtitle) { mSubtitleView.setText(subtitle); showSubtitle = true; } } mTitleView.setVisibility(showTitle ? View.VISIBLE : View.GONE); mSubtitleView.setVisibility(showSubtitle ? View.VISIBLE : View.GONE); if (mState != null) { boolean isPlaying = mState.getState() == PlaybackStateCompat.STATE_BUFFERING || mState.getState() == PlaybackStateCompat.STATE_PLAYING; boolean supportsPlay = (mState.getActions() & (PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE)) != 0; boolean supportsPause = (mState.getActions() & (PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE)) != 0; if (isPlaying && supportsPause) { mPlayPauseButton.setVisibility(View.VISIBLE); mPlayPauseButton.setImageResource( MediaRouterThemeHelper.getThemeResource(mContext, R.attr.mediaRoutePauseDrawable)); mPlayPauseButton .setContentDescription(mContext.getResources().getText(R.string.mr_controller_pause)); } else if (!isPlaying && supportsPlay) { mPlayPauseButton.setVisibility(View.VISIBLE); mPlayPauseButton.setImageResource( MediaRouterThemeHelper.getThemeResource(mContext, R.attr.mediaRoutePlayDrawable)); mPlayPauseButton .setContentDescription(mContext.getResources().getText(R.string.mr_controller_play)); } else { mPlayPauseButton.setVisibility(View.GONE); } } } }
From source file:androidx.mediarouter.app.MediaRouteControllerDialog.java
private void updatePlaybackControlLayout() { if (canShowPlaybackControlLayout()) { CharSequence title = mDescription == null ? null : mDescription.getTitle(); boolean hasTitle = !TextUtils.isEmpty(title); CharSequence subtitle = mDescription == null ? null : mDescription.getSubtitle(); boolean hasSubtitle = !TextUtils.isEmpty(subtitle); boolean showTitle = false; boolean showSubtitle = false; if (mRoute.getPresentationDisplayId() != MediaRouter.RouteInfo.PRESENTATION_DISPLAY_ID_NONE) { // The user is currently casting screen. mTitleView.setText(R.string.mr_controller_casting_screen); showTitle = true;/* w w w . j ava2 s . c o m*/ } else if (mState == null || mState.getState() == PlaybackStateCompat.STATE_NONE) { // Show "No media selected" as we don't yet know the playback state. mTitleView.setText(R.string.mr_controller_no_media_selected); showTitle = true; } else if (!hasTitle && !hasSubtitle) { mTitleView.setText(R.string.mr_controller_no_info_available); showTitle = true; } else { if (hasTitle) { mTitleView.setText(title); showTitle = true; } if (hasSubtitle) { mSubtitleView.setText(subtitle); showSubtitle = true; } } mTitleView.setVisibility(showTitle ? View.VISIBLE : View.GONE); mSubtitleView.setVisibility(showSubtitle ? View.VISIBLE : View.GONE); if (mState != null) { boolean isPlaying = mState.getState() == PlaybackStateCompat.STATE_BUFFERING || mState.getState() == PlaybackStateCompat.STATE_PLAYING; Context playbackControlButtonContext = mPlaybackControlButton.getContext(); boolean visible = true; int iconDrawableAttr = 0; int iconDescResId = 0; if (isPlaying && isPauseActionSupported()) { iconDrawableAttr = R.attr.mediaRoutePauseDrawable; iconDescResId = R.string.mr_controller_pause; } else if (isPlaying && isStopActionSupported()) { iconDrawableAttr = R.attr.mediaRouteStopDrawable; iconDescResId = R.string.mr_controller_stop; } else if (!isPlaying && isPlayActionSupported()) { iconDrawableAttr = R.attr.mediaRoutePlayDrawable; iconDescResId = R.string.mr_controller_play; } else { visible = false; } mPlaybackControlButton.setVisibility(visible ? View.VISIBLE : View.GONE); if (visible) { mPlaybackControlButton.setImageResource(MediaRouterThemeHelper .getThemeResource(playbackControlButtonContext, iconDrawableAttr)); mPlaybackControlButton.setContentDescription( playbackControlButtonContext.getResources().getText(iconDescResId)); } } } }
From source file:com.bluros.music.MusicService.java
private void updateMediaSession(final String what) { int playState = mIsSupposedToBePlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build());/*from w w w. j a v a 2 s . c om*/ } } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) { Bitmap albumArt = ImageLoader.getInstance() .loadImageSync(MusicUtils.getAlbumArtUri(getAlbumId()).toString()); if (albumArt != null) { Bitmap.Config config = albumArt.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } albumArt = albumArt.copy(config, false); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mSession.setMetadata(new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName()) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName()) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName()) .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName()) .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration()) .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1) .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().length) .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName()) .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, mShowAlbumArtOnLockscreen ? albumArt : null) .build()); mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build()); } } }
From source file:org.runbuddy.tomahawk.services.PlaybackService.java
private void updateMediaPlayState() { if (mMediaSession == null) { Log.e(TAG, "updateMediaPlayState failed - mMediaSession == null!"); return;//from ww w . j a va2 s .com } long actions = 0L; if (mPlaybackManager.getCurrentQuery() != null) { actions |= PlaybackStateCompat.ACTION_SET_RATING; } if (mPlayState == PlaybackStateCompat.STATE_PLAYING) { actions |= PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SEEK_TO | PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_REWIND; } else { actions |= PlaybackStateCompat.ACTION_PLAY; } if (mPlaybackManager.hasNextEntry()) { actions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT; } if (mPlaybackManager.hasPreviousEntry()) { actions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS; } Log.d(TAG, "updateMediaPlayState()"); Bundle extras = new Bundle(); extras.putInt(EXTRAS_KEY_REPEAT_MODE, mPlaybackManager.getRepeatMode()); extras.putInt(EXTRAS_KEY_SHUFFLE_MODE, mPlaybackManager.getShuffleMode()); int playState = mIsPreparing ? PlaybackStateCompat.STATE_BUFFERING : mPlayState; PlaybackStateCompat playbackStateCompat = new PlaybackStateCompat.Builder().setActions(actions) .setState(playState, getPlaybackPosition(), 1f, SystemClock.elapsedRealtime()).setExtras(extras) .build(); mMediaSession.setPlaybackState(playbackStateCompat); }