@Override public void onDestroy() { mServiceStarted = false; mSession.release(); }
private void setUpMediaSession() { mSession = new MediaSession(this, "remusic"); mSession.setCallback(new MediaSession.Callback() { @Override public void onPause() { mSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
final MediaSession mediaSession = new MediaSession(this, "debug tag"); mediaSession.setMetadata(new MediaMetadata.Builder() .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, artwork) .putString(MediaMetadata.METADATA_KEY_ARTIST, "Pink Floyd") .build()); mediaSession.setActive(true); mediaSession.setCallback(new MediaSession.Callback() { mediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); .setMediaSession(mediaSession.getSessionToken()) final TransportControls controls = mediaSession.getController().getTransportControls();
@Override public void onCreate() { super.onCreate(); sInstance = this; mContext = this; mSession = new MediaSession(this, "WearBrowserService"); setSessionToken(mSession.getSessionToken()); mSession.setCallback(new MediaSessionCallback()); mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); }
mSession.setPlaybackState(new PlaybackState.Builder() .setState(playState, position(), 1.0f) .setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) .build()); mSession.setMetadata(new MediaMetadata.Builder() .putString(MediaMetadata.METADATA_KEY_ARTIST, getArtistName()) .putString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName()) .build()); mSession.setPlaybackState(new PlaybackState.Builder() .setState(playState, position(), 1.0f) .setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) .build());
private void releaseServiceUiAndStop() { if (isPlaying() || mPausedByTransientLossOfFocus || mPlayerHandler.hasMessages(TRACK_ENDED)) { return; } if (D) Log.d(TAG, "Nothing is playing anymore, releasing notification"); cancelNotification(); mAudioManager.abandonAudioFocus(mAudioFocusListener); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) mSession.setActive(false); if (!mServiceInUse) { saveQueue(true); stopSelf(mServiceStartId); } }
@Test public void mediaSessionCompat_creation() throws Exception { // Should not result in an exception. new MediaSession(ApplicationProvider.getApplicationContext(), "test"); } }
private void assertMediaStateIs(@PlaybackStateCompat.State int expectedState) { PlaybackState state = rule.getActivity().getMediaController().getPlaybackState(); assertNotNull(state); assertThat( "MediaSession is not in the correct state", state.getState(), is(equalTo(expectedState))); }
PlaybackState state = new PlaybackState.Builder() .setActions( PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackState.STATE_PLAYING, position, speed, SystemClock.elapsedRealtime()) .build(); mSession.setPlaybackState(state);
long getPosition() { if (controller.getPlaybackState() == null) return 0; return controller.getPlaybackState().getPosition(); } }
void playPause() { if (isPlaying) { controller.getTransportControls().pause(); } else { controller.getTransportControls().play(); } }
int getVolume() { if (controller.getPlaybackInfo() == null) return 0; return 100 * controller.getPlaybackInfo().getCurrentVolume() / controller.getPlaybackInfo().getMaxVolume(); }
void previous() { controller.getTransportControls().skipToPrevious(); }
@Override public void onDestroy() { if (D) Log.d(TAG, "Destroying service"); super.onDestroy(); // Remove any sound effects final Intent audioEffectsIntent = new Intent( AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(audioEffectsIntent); cancelNotification(); mAlarmManager.cancel(mShutdownIntent); mPlayerHandler.removeCallbacksAndMessages(null); if (CommonUtils.isJellyBeanMR2()) mHandlerThread.quitSafely(); else mHandlerThread.quit(); mPlayer.release(); mPlayer = null; mAudioManager.abandonAudioFocus(mAudioFocusListener); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) mSession.release(); getContentResolver().unregisterContentObserver(mMediaStoreObserver); closeCursor(); unregisterReceiver(mIntentReceiver); if (mUnmountReceiver != null) { unregisterReceiver(mUnmountReceiver); mUnmountReceiver = null; } mWakeLock.release(); }
MediaButtonIntentReceiver.class.getName())); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) mSession.setActive(true); if (createNewNextTrack) { setNextTrack();
MprisReceiverPlayer(MediaController controller, String name) { this.controller = controller; this.name = name; if (controller.getPlaybackState() != null) { isPlaying = controller.getPlaybackState().getState() == PlaybackState.STATE_PLAYING; } }
PlaybackState state = new PlaybackState.Builder() .setActions(PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_STOP) .setState(PlaybackStateCompat.STATE_PLAYING, 0, 1, SystemClock.elapsedRealtime()) .build(); mSession.setPlaybackState(state);