android.support.v4.media.session.MediaControllerCompat Java Examples
The following examples show how to use
android.support.v4.media.session.MediaControllerCompat.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlaybackControlBaseActivity.java From LyricHere with Apache License 2.0 | 6 votes |
private void connectToSession(MediaSessionCompat.Token token) { try { LogUtils.d(TAG, "Session Token: " + token); mMediaController = new MediaControllerCompat(this, token); mMediaController.registerCallback(mMediaControllerCallback); if (shouldShowControls()) { showPlaybackControls(); } else { LogUtils.d(TAG, "connectionCallback.onConnected: " + "hiding controls because metadata is null"); hidePlaybackControls(); } if (mControlsFragment != null) { mControlsFragment.onConnected(); } onMediaControllerConnected(); } catch (RemoteException ex) { LogUtils.e(TAG, ex.getMessage()); } }
Example #2
Source File: MediaSessionPlaybackActivity.java From android-PictureInPicture with Apache License 2.0 | 6 votes |
private void initializeMediaSession() { mSession = new MediaSessionCompat(this, TAG); mSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); mSession.setActive(true); MediaControllerCompat.setMediaController(this, mSession.getController()); MediaMetadataCompat metadata = new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mMovieView.getTitle()) .build(); mSession.setMetadata(metadata); MediaSessionCallback mMediaSessionCallback = new MediaSessionCallback(mMovieView); mSession.setCallback(mMediaSessionCallback); int state = mMovieView.isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; updatePlaybackState( state, MEDIA_ACTIONS_ALL, mMovieView.getCurrentPosition(), mMovieView.getVideoResourceId()); }
Example #3
Source File: MediaStyleHelper.java From AndroidAudioExample with MIT License | 6 votes |
/** * Build a notification using the information from the given media session. Makes heavy use * of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information. * * @param context Context used to construct the notification. * @param mediaSession Media session to get information. * @return A pre-built notification with information from the given media session. */ static NotificationCompat.Builder from(Context context, MediaSessionCompat mediaSession) { MediaControllerCompat controller = mediaSession.getController(); MediaMetadataCompat mediaMetadata = controller.getMetadata(); MediaDescriptionCompat description = mediaMetadata.getDescription(); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder .setContentTitle(description.getTitle()) .setContentText(description.getSubtitle()) .setSubText(description.getDescription()) .setLargeIcon(description.getIconBitmap()) .setContentIntent(controller.getSessionActivity()) .setDeleteIntent( MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP)) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); return builder; }
Example #4
Source File: MediaBrowserHelper.java From android-MediaBrowserService with Apache License 2.0 | 6 votes |
@Override public void onConnected() { try { // Get a MediaController for the MediaSession. mMediaController = new MediaControllerCompat(mContext, mMediaBrowser.getSessionToken()); mMediaController.registerCallback(mMediaControllerCallback); // Sync existing MediaSession state to the UI. mMediaControllerCallback.onMetadataChanged(mMediaController.getMetadata()); mMediaControllerCallback.onPlaybackStateChanged( mMediaController.getPlaybackState()); MediaBrowserHelper.this.onConnected(mMediaController); } catch (RemoteException e) { Log.d(TAG, String.format("onConnected: Problem: %s", e.toString())); throw new RuntimeException(e); } mMediaBrowser.subscribe(mMediaBrowser.getRoot(), mMediaBrowserSubscriptionCallback); }
Example #5
Source File: TrackFragment.java From Melophile with Apache License 2.0 | 6 votes |
@OnClick(R.id.play_pause) public void playPause() { lastState = null; MediaControllerCompat controllerCompat = MediaControllerCompat.getMediaController(getActivity()); PlaybackStateCompat stateCompat = controllerCompat.getPlaybackState(); if (stateCompat != null) { MediaControllerCompat.TransportControls controls = controllerCompat.getTransportControls(); switch (stateCompat.getState()) { case PlaybackStateCompat.STATE_PLAYING: case PlaybackStateCompat.STATE_BUFFERING: controls.pause(); break; case PlaybackStateCompat.STATE_NONE: case PlaybackStateCompat.STATE_PAUSED: case PlaybackStateCompat.STATE_STOPPED: controls.play(); break; default: Log.d(TAG, "State " + stateCompat.getState()); } } }
Example #6
Source File: TtsService.java From android-app with GNU General Public License v3.0 | 6 votes |
private NotificationCompat.Builder generateNotificationBuilderFrom( Context context, MediaSessionCompat mediaSession) { NotificationCompat.Builder builder = new NotificationCompat.Builder( context, NotificationsHelper.CHANNEL_ID_TTS) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setDeleteIntent(generateActionIntent(PlaybackStateCompat.ACTION_STOP)); if (mediaSession != null) { MediaControllerCompat controller = mediaSession.getController(); builder.setContentIntent(controller.getSessionActivity()); MediaMetadataCompat mediaMetadata = controller.getMetadata(); if (mediaMetadata != null) { MediaDescriptionCompat description = mediaMetadata.getDescription(); builder.setContentTitle(description.getTitle()) .setContentText(description.getSubtitle()) .setSubText(description.getDescription()) .setLargeIcon(description.getIconBitmap()); } } return builder; }
Example #7
Source File: MainActivity.java From YouTube-In-Background with MIT License | 6 votes |
/** * Check if the MediaSession is active and in a "playback-able" state * (not NONE and not STOPPED). * * @return true if the MediaSession's state requires playback controls to be visible. */ protected boolean shouldShowControls() { LogHelper.e(TAG, "shouldShowControls"); MediaControllerCompat mediaController = MediaControllerCompat.getMediaController(this); if (mediaController == null || mediaController.getMetadata() == null || mediaController.getPlaybackState() == null) { return false; } switch (mediaController.getPlaybackState().getState()) { case STATE_ERROR: case STATE_NONE: case STATE_STOPPED: return false; default: return true; } }
Example #8
Source File: MainActivity.java From YouTube-In-Background with MIT License | 6 votes |
private void connectToSession(MediaSessionCompat.Token token) throws RemoteException { LogHelper.e(TAG, "connectToSession"); MediaControllerCompat mediaController = new MediaControllerCompat(this, token); MediaControllerCompat.setMediaController(this, mediaController); mediaController.registerCallback(mediaControllerCallback); if (shouldShowControls()) { showPlaybackControls(); } else { LogHelper.e(TAG, "connectionCallback.onConnected: hiding controls because metadata is null"); hidePlaybackControls(); } if (playbackControlsFragment != null) { playbackControlsFragment.onConnected(); } onMediaControllerConnected(); }
Example #9
Source File: TtsFragment.java From android-app with GNU General Public License v3.0 | 6 votes |
private void initMediaController() { destroyMediaController(); if (mediaSessionToken == null) return; try { mediaController = new MediaControllerCompat(activity, mediaSessionToken); } catch (RemoteException e) { Log.e(TAG, "initMediaController()", e); } if (mediaController != null) { mediaController.registerCallback(mediaCallback); MediaControllerCompat.setMediaController(activity, mediaController); } }
Example #10
Source File: PlaybackControlsFragment.java From YouTube-In-Background with MIT License | 6 votes |
public void onConnected() { MediaControllerCompat controller = MediaControllerCompat.getMediaController(activity); LogHelper.e(TAG, "onConnected, mediaController==null? ", controller == null); if (controller != null) { controller.registerCallback(mediaControllerCallback); onPlaybackStateChanged(controller.getPlaybackState()); onMetadataChanged(controller.getMetadata()); PlaybackStateCompat state = controller.getPlaybackState(); updatePlaybackState(state); MediaMetadataCompat metadata = controller.getMetadata(); if (metadata != null) { updateMediaDescription(metadata.getDescription()); updateDuration(metadata); } updateProgress(); if (state != null && (state.getState() == STATE_PLAYING || state.getState() == STATE_BUFFERING)) { scheduleSeekbarUpdate(); } } }
Example #11
Source File: PlaybackControlBaseActivity.java From LyricHere with Apache License 2.0 | 6 votes |
/** * Check if the MediaSession is active and in a "playback-able" state * (not NONE and not STOPPED). * * @return true if the MediaSession's state requires playback controls to be visible. */ protected boolean shouldShowControls() { MediaControllerCompat mediaController = mMediaController; if (mediaController == null || mediaController.getMetadata() == null || mediaController.getPlaybackState() == null) { return false; } switch (mediaController.getPlaybackState().getState()) { case PlaybackState.STATE_ERROR: case PlaybackState.STATE_NONE: case PlaybackState.STATE_STOPPED: return false; default: return true; } }
Example #12
Source File: FullScreenPlayerActivity.java From LyricHere with Apache License 2.0 | 6 votes |
private void connectToSession(MediaSessionCompat.Token token) { try { mMediaController = new MediaControllerCompat(FullScreenPlayerActivity.this, token); if (mMediaController.getMetadata() == null) { finish(); return; } mMediaController.registerCallback(mCallback); PlaybackStateCompat state = mMediaController.getPlaybackState(); updatePlaybackState(state); MediaMetadataCompat metadata = mMediaController.getMetadata(); if (metadata != null) { updateMediaDescription(metadata.getDescription()); updateDuration(metadata); } updateProgress(); if (state != null && (state.getState() == PlaybackStateCompat.STATE_PLAYING || state.getState() == PlaybackStateCompat.STATE_BUFFERING)) { scheduleSeekbarUpdate(); } } catch (RemoteException e) { e.printStackTrace(); } }
Example #13
Source File: MediaBrowserFragment.java From LyricHere with Apache License 2.0 | 6 votes |
@Override public View getView(int position, View convertView, ViewGroup parent) { MediaBrowserCompat.MediaItem item = getItem(position); int itemState = MediaItemViewHolder.STATE_NONE; if (item.isPlayable()) { itemState = MediaItemViewHolder.STATE_PLAYABLE; MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController(); if (controller != null && controller.getMetadata() != null) { String currentPlaying = controller.getMetadata().getDescription().getMediaId(); String musicId = MediaIDHelper.extractMusicIDFromMediaID( item.getDescription().getMediaId()); if (currentPlaying != null && currentPlaying.equals(musicId)) { PlaybackStateCompat pbState = controller.getPlaybackState(); if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) { itemState = MediaItemViewHolder.STATE_NONE; } else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) { itemState = MediaItemViewHolder.STATE_PLAYING; } else { itemState = MediaItemViewHolder.STATE_PAUSED; } } } } return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent, item.getDescription(), itemState); }
Example #14
Source File: NotificationUtil.java From Prodigal with Apache License 2.0 | 6 votes |
private NotificationCompat.Builder notificationBuilder(PlayerService context, MediaSessionCompat session) { MediaControllerCompat controller = session.getController(); MediaMetadataCompat mediaMetadata = controller.getMetadata(); MediaDescriptionCompat description = mediaMetadata.getDescription(); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder .setContentTitle(description.getTitle()) .setContentText(description.getSubtitle()) .setSubText(description.getDescription()) .setLargeIcon(mediaMetadata.getBitmap(MediaMetadataCompat.METADATA_KEY_ART)) .setContentIntent(clickIntent) .setDeleteIntent( MediaButtonReceiver.buildMediaButtonPendingIntent(context, receiverName, PlaybackStateCompat.ACTION_STOP)) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); return builder; }
Example #15
Source File: MusicPlayerActivity.java From LyricHere with Apache License 2.0 | 6 votes |
@Override public void onMediaItemSelected(MediaBrowserCompat.MediaItem item) { LogUtils.d(TAG, "onMediaItemSelected, mediaId=" + item.getMediaId()); if (item.isPlayable()) { final MediaControllerCompat mediaController = getSupportMediaController(); if (mediaController != null) { MediaSessionCompat.Token token = mediaController.getSessionToken(); MediaControllerCompat.TransportControls tc = mediaController.getTransportControls(); mediaController.getTransportControls().playFromMediaId(item.getMediaId(), null); } } else if (item.isBrowsable()) { navigateToBrowser(item.getMediaId()); } else { LogUtils.w(TAG, "Ignoring MediaItem that is neither browsable nor playable: ", "mediaId=", item.getMediaId()); } }
Example #16
Source File: MediaSessionPlaybackActivity.java From media-samples with Apache License 2.0 | 6 votes |
private void initializeMediaSession() { mSession = new MediaSessionCompat(this, TAG); mSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); mSession.setActive(true); MediaControllerCompat.setMediaController(this, mSession.getController()); MediaMetadataCompat metadata = new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mMovieView.getTitle()) .build(); mSession.setMetadata(metadata); MediaSessionCallback mMediaSessionCallback = new MediaSessionCallback(mMovieView); mSession.setCallback(mMediaSessionCallback); int state = mMovieView.isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; updatePlaybackState( state, MEDIA_ACTIONS_ALL, mMovieView.getCurrentPosition(), mMovieView.getVideoResourceId()); }
Example #17
Source File: RNAudioPlayerModule.java From react-native-audio-streaming-player with MIT License | 5 votes |
@Override public void onServiceConnected(ComponentName name, IBinder service) { if (service instanceof AudioPlayerService.ServiceBinder) { try { mService = ((AudioPlayerService.ServiceBinder) service).getService(); mMediaController = new MediaControllerCompat(this.reactContext, ((AudioPlayerService.ServiceBinder) service).getService().getMediaSessionToken()); } catch (RemoteException e) { Log.e("ERROR", e.getMessage()); } } }
Example #18
Source File: PlaybackControlsFragment.java From LyricHere with Apache License 2.0 | 5 votes |
public void onConnected() { MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController(); LogUtils.d(TAG, "onConnected, mediaController==null? ", controller == null); if (controller != null) { onMetadataChanged(controller.getMetadata()); onPlaybackStateChanged(controller.getPlaybackState()); controller.registerCallback(mCallback); } }
Example #19
Source File: MediaStyleHelper.java From Jockey with Apache License 2.0 | 5 votes |
/** * Build a notification using the information from the given media session. * @param context Context used to construct the notification. * @param mediaSession Media session to get information. * @return A pre-built notification with information from the given media session. */ public static NotificationCompat.Builder from(Context context, MediaSessionCompat mediaSession, String channel) { MediaControllerCompat controller = mediaSession.getController(); MediaMetadataCompat mediaMetadata = controller.getMetadata(); MediaDescriptionCompat description = mediaMetadata.getDescription(); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel); builder .setContentTitle(description.getTitle()) .setContentText(description.getSubtitle()) .setSubText(description.getDescription()) .setContentIntent(controller.getSessionActivity()) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setWhen(0) .setShowWhen(false); if (description.getIconBitmap() == null || description.getIconBitmap().isRecycled()) { builder.setLargeIcon( BitmapFactory.decodeResource(context.getResources(), R.drawable.art_default)); } else { builder.setLargeIcon(description.getIconBitmap()); } return builder; }
Example #20
Source File: PlayerView.java From Bop with Apache License 2.0 | 5 votes |
@Override protected void onStop() { super.onStop(); MediaControllerCompat controllerCompat = MediaControllerCompat.getMediaController(PlayerView.this); if (controllerCompat != null) { controllerCompat.unregisterCallback(mCallback); } }
Example #21
Source File: PlayingNowList.java From Bop with Apache License 2.0 | 5 votes |
@Override protected void onStop() { super.onStop(); MediaControllerCompat controllerCompat = MediaControllerCompat.getMediaController(PlayingNowList.this); if (controllerCompat != null) { controllerCompat.unregisterCallback(mCallback); } }
Example #22
Source File: MainScreen.java From Bop with Apache License 2.0 | 5 votes |
@Override protected void onStop() { super.onStop(); MediaControllerCompat controllerCompat = MediaControllerCompat.getMediaController(MainScreen.this); if (controllerCompat != null) { controllerCompat.unregisterCallback(mCallback); } }
Example #23
Source File: MusicPlayerActivity.java From LyricHere with Apache License 2.0 | 5 votes |
@Override protected void onMediaControllerConnected() { if (mVoiceSearchParams != null) { // If there is a bootstrap parameter to start from a search query, we // send it to the media session and set it to null, so it won't play again // when the activity is stopped/started or recreated: String query = mVoiceSearchParams.getString(SearchManager.QUERY); final MediaControllerCompat mediaController = getSupportMediaController(); if (mediaController != null) { mediaController.getTransportControls().playFromSearch(query, mVoiceSearchParams); } mVoiceSearchParams = null; } getBrowseFragment().onConnected(); }
Example #24
Source File: PlayerConnection.java From blade-player with GNU General Public License v3.0 | 5 votes |
@Override public void onServiceConnected(ComponentName name, IBinder service) { musicPlayer = ((PlayerService.PlayerBinder) service).getService(); try{musicController = new MediaControllerCompat(applicationContext, musicPlayer.getSessionToken());} catch(Exception e) { exit(1); } if(playOnConnect != null) {musicPlayer.setCurrentPlaylist(playOnConnect, positionOnConnect); playOnConnect = null;} if(connectionCallback != null) connectionCallback.onConnected(); }
Example #25
Source File: MediaStyleHelper.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * Build a notification using the information from the given media session. Makes heavy use * of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information. * @param context Context used to construct the notification. * @param mediaSession Media session to get information. * @return A pre-built notification with information from the given media session. */ public static NotificationCompat.Builder from( Context context, MediaSessionCompat mediaSession) { MediaControllerCompat controller = mediaSession.getController(); if (controller == null) { return null; } MediaMetadataCompat mediaMetadata = controller.getMetadata(); if (mediaMetadata == null) { return null; } MediaDescriptionCompat description = mediaMetadata.getDescription(); if (description == null) { return null; } NotificationCompat.Builder builder = new NotificationCompat.Builder(context, BackgroundAudioService.CHANNEL_ID); builder .setContentTitle(description.getTitle()) .setContentText(description.getSubtitle()) .setSubText(description.getDescription()) .setLargeIcon(description.getIconBitmap()) .setContentIntent(controller.getSessionActivity()) .setDeleteIntent( MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP)) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); return builder; }
Example #26
Source File: MusicController.java From klingar with Apache License 2.0 | 5 votes |
/** * Set a new MediaController with the current session token */ void setMediaController(MediaControllerCompat newMediaController) { if (mediaController != null) { mediaController.unregisterCallback(callback); mediaController = null; } mediaController = newMediaController; mediaController.registerCallback(callback); }
Example #27
Source File: MusicService.java From klingar with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); Timber.d("onCreate"); KlingarApp.get(this).component().inject(this); Playback playback = new LocalPlayback(getApplicationContext(), musicController, audioManager, wifiManager, client); playbackManager = new PlaybackManager(queueManager, this, AndroidClock.DEFAULT, playback); session = new MediaSessionCompat(this, "MusicService"); try { MediaControllerCompat mediaController = new MediaControllerCompat(this.getApplicationContext(), session.getSessionToken()); musicController.setMediaController(mediaController); } catch (RemoteException e) { Timber.e(e, "Could not create MediaController"); throw new IllegalStateException(); } session.setCallback(playbackManager.getMediaSessionCallback()); Context context = getApplicationContext(); Intent intent = new Intent(context, KlingarActivity.class); session.setSessionActivity(PendingIntent.getActivity(context, 99, intent, FLAG_UPDATE_CURRENT)); playbackManager.updatePlaybackState(); mediaNotificationManager = new MediaNotificationManager(this, musicController, queueManager, rx); castSessionManager = CastContext.getSharedInstance(this).getSessionManager(); castSessionManagerListener = new CastSessionManagerListener(); castSessionManager.addSessionManagerListener(castSessionManagerListener, CastSession.class); mediaRouter = MediaRouter.getInstance(getApplicationContext()); timelineManager = new TimelineManager(musicController, queueManager, media, rx); timelineManager.start(); }
Example #28
Source File: MediaSeekBar.java From android-MediaBrowserService with Apache License 2.0 | 5 votes |
public void setMediaController(final MediaControllerCompat mediaController) { if (mediaController != null) { mControllerCallback = new ControllerCallback(); mediaController.registerCallback(mControllerCallback); } else if (mMediaController != null) { mMediaController.unregisterCallback(mControllerCallback); mControllerCallback = null; } mMediaController = mediaController; }
Example #29
Source File: MediaBrowserHelper.java From android-MediaBrowserService with Apache License 2.0 | 5 votes |
public MediaControllerCompat.TransportControls getTransportControls() { if (mMediaController == null) { Log.d(TAG, "getTransportControls: MediaController is null!"); throw new IllegalStateException("MediaController is null!"); } return mMediaController.getTransportControls(); }
Example #30
Source File: MainActivity.java From android-MediaBrowserService with Apache License 2.0 | 5 votes |
@Override protected void onChildrenLoaded(@NonNull String parentId, @NonNull List<MediaBrowserCompat.MediaItem> children) { super.onChildrenLoaded(parentId, children); final MediaControllerCompat mediaController = getMediaController(); // Queue up all media items for this simple sample. for (final MediaBrowserCompat.MediaItem mediaItem : children) { mediaController.addQueueItem(mediaItem.getDescription()); } // Call prepare now so pressing play just works. mediaController.getTransportControls().prepare(); }