List of usage examples for android.support.v4.media.session MediaSessionCompat getController
public MediaControllerCompat getController()
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; }// w ww.j av a 2 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:androidx.media.session.MediaButtonReceiver.java
/** * Extracts any available {@link KeyEvent} from an {@link Intent#ACTION_MEDIA_BUTTON} * intent, passing it onto the {@link MediaSessionCompat} using * {@link MediaControllerCompat#dispatchMediaButtonEvent(KeyEvent)}, which in turn * will trigger callbacks to the {@link MediaSessionCompat.Callback} registered via * {@link MediaSessionCompat#setCallback(MediaSessionCompat.Callback)}. * @param mediaSessionCompat A {@link MediaSessionCompat} that has a * {@link MediaSessionCompat.Callback} set. * @param intent The intent to parse./*w ww . j a va 2s . co m*/ * @return The extracted {@link KeyEvent} if found, or null. */ public static KeyEvent handleIntent(MediaSessionCompat mediaSessionCompat, Intent intent) { if (mediaSessionCompat == null || intent == null || !Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) || !intent.hasExtra(Intent.EXTRA_KEY_EVENT)) { return null; } KeyEvent ke = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); MediaControllerCompat mediaController = mediaSessionCompat.getController(); mediaController.dispatchMediaButtonEvent(ke); return ke; }
From source file:org.runbuddy.tomahawk.utils.MediaBrowserHelper.java
/** * Override to handle requests to play a specific mediaId that was provided by your app. */// w w w . j av a 2 s . co m public void onPlayFromMediaId(final MediaSessionCompat mediaSession, final PlaybackManager playbackManager, final String mediaId, Bundle extras) { String[] parts; final MediaControllerCompat.TransportControls transportControls = mediaSession.getController() .getTransportControls(); if (MEDIA_ID_SHUFFLEDPLAY_FAVORITES.equals(mediaId)) { User.getSelf().done(new DoneCallback<User>() { @Override public void onDone(User user) { playbackManager.setPlaylist(user.getFavorites()); playbackManager.setShuffleMode(PlaybackManager.SHUFFLED); transportControls.play(); } }); } else if (MEDIA_ID_SHUFFLEDPLAY_TRACKS.equals(mediaId)) { CollectionManager.get().getUserCollection().getQueries(Collection.SORT_ALPHA) .done(new DoneCallback<Playlist>() { @Override public void onDone(Playlist collectionTracks) { playbackManager.setPlaylist(collectionTracks); playbackManager.setShuffleMode(PlaybackManager.SHUFFLED); transportControls.play(); } }); } else if ((parts = mediaId.split("", 2)).length > 1) { String leaf = parts[0]; String cacheKey = parts[1]; if (MEDIA_ID_COLLECTION_ALBUMS.equals(leaf)) { final Album album = Album.getByKey(cacheKey); CollectionManager.get().getUserCollection().getAlbumTracks(album) .done(new DoneCallback<Playlist>() { @Override public void onDone(Playlist albumTracks) { if (albumTracks != null) { playbackManager.setPlaylist(albumTracks); transportControls.play(); } else { HatchetCollection hatchetCollection = CollectionManager.get() .getHatchetCollection(); hatchetCollection.getAlbumTracks(album).done(new DoneCallback<Playlist>() { @Override public void onDone(Playlist albumTracks) { if (albumTracks != null) { playbackManager.setPlaylist(albumTracks); mediaSession.getController().getTransportControls().play(); } } }); } } }); } else if (MEDIA_ID_COLLECTION_ARTISTS.equals(leaf)) { final Artist artist = Artist.getByKey(cacheKey); CollectionManager.get().getUserCollection().getArtistTracks(artist) .done(new DoneCallback<Playlist>() { @Override public void onDone(Playlist artistTracks) { if (artistTracks != null) { playbackManager.setPlaylist(artistTracks); transportControls.play(); } else { HatchetCollection hatchetCollection = CollectionManager.get() .getHatchetCollection(); hatchetCollection.getArtistTopHits(artist).done(new DoneCallback<Playlist>() { @Override public void onDone(Playlist artistTopHits) { if (artistTopHits != null) { playbackManager.setPlaylist(artistTopHits); transportControls.play(); } } }); } } }); } else if (MEDIA_ID_PLAYLISTS.equals(leaf) || MEDIA_ID_STATIONS.equals(leaf)) { Playlist playlist = Playlist.getByKey(cacheKey); if (!(playlist instanceof StationPlaylist) && !playlist.isFilled()) { playlist = DatabaseHelper.get().getPlaylist(playlist.getId()); } playbackManager.setPlaylist(playlist); transportControls.play(); } } }
From source file:com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector.java
/** * Creates an instance. Must be called on the same thread that is used to construct the player * instances passed to {@link #setPlayer(Player, PlaybackPreparer, CustomActionProvider...)}. * * @param mediaSession The {@link MediaSessionCompat} to connect to. * @param playbackController A {@link PlaybackController} for handling playback actions, or * {@code null} if the connector should handle playback actions directly. * @param doMaintainMetadata Whether the connector should maintain the metadata of the session. If * {@code false}, you need to maintain the metadata of the media session yourself (provide at * least the duration to allow clients to show a progress bar). *///w w w. j av a 2 s . c om public MediaSessionConnector(MediaSessionCompat mediaSession, PlaybackController playbackController, boolean doMaintainMetadata) { this.mediaSession = mediaSession; this.playbackController = playbackController != null ? playbackController : new DefaultPlaybackController(); this.handler = new Handler(Looper.myLooper() != null ? Looper.myLooper() : Looper.getMainLooper()); this.doMaintainMetadata = doMaintainMetadata; mediaSession.setFlags(BASE_MEDIA_SESSION_FLAGS); mediaController = mediaSession.getController(); mediaSessionCallback = new MediaSessionCallback(); exoPlayerEventListener = new ExoPlayerEventListener(); customActionMap = Collections.emptyMap(); commandMap = new HashMap<>(); registerCommandReceiver(playbackController); }