use of android.support.v4.media.session.MediaControllerCompat in project android-UniversalMusicPlayer by googlesamples.
the class TvPlaybackActivity method onStop.
@Override
protected void onStop() {
super.onStop();
LogHelper.d(TAG, "Activity onStop");
MediaControllerCompat controllerCompat = MediaControllerCompat.getMediaController(TvPlaybackActivity.this);
if (controllerCompat != null) {
controllerCompat.unregisterCallback(mMediaControllerCallback);
}
mMediaBrowser.disconnect();
}
use of android.support.v4.media.session.MediaControllerCompat in project android-UniversalMusicPlayer by googlesamples.
the class TvPlaybackFragment method setupRows.
private void setupRows() {
PlaybackControlsRowPresenter playbackControlsRowPresenter;
playbackControlsRowPresenter = new PlaybackControlsRowPresenter(new DescriptionPresenter());
playbackControlsRowPresenter.setOnActionClickedListener(new OnActionClickedListener() {
public void onActionClicked(Action action) {
if (getActivity() == null) {
return;
}
MediaControllerCompat controller = MediaControllerCompat.getMediaController(getActivity());
if (controller == null) {
return;
}
MediaControllerCompat.TransportControls controls = controller.getTransportControls();
if (action.getId() == mPlayPauseAction.getId()) {
if (mPlayPauseAction.getIndex() == PlayPauseAction.PLAY) {
controls.play();
} else {
controls.pause();
}
} else if (action.getId() == mSkipNextAction.getId()) {
controls.skipToNext();
resetPlaybackRow();
} else if (action.getId() == mSkipPreviousAction.getId()) {
controls.skipToPrevious();
resetPlaybackRow();
}
if (action instanceof PlaybackControlsRow.MultiAction) {
((PlaybackControlsRow.MultiAction) action).nextIndex();
notifyChanged(action);
}
}
});
mPresenterSelector.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter);
}
use of android.support.v4.media.session.MediaControllerCompat in project android-UniversalMusicPlayer by googlesamples.
the class MediaIDHelper method isMediaItemPlaying.
/**
* Determine if media item is playing (matches the currently playing media item).
*
* @param context for retrieving the {@link MediaControllerCompat}
* @param mediaItem to compare to currently playing {@link MediaBrowserCompat.MediaItem}
* @return boolean indicating whether media item matches currently playing media item
*/
public static boolean isMediaItemPlaying(Activity context, MediaBrowserCompat.MediaItem mediaItem) {
// Media item is considered to be playing or paused based on the controller's current
// media id
MediaControllerCompat controller = MediaControllerCompat.getMediaController(context);
if (controller != null && controller.getMetadata() != null) {
String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(mediaItem.getDescription().getMediaId());
if (currentPlayingMediaId != null && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
return true;
}
}
return false;
}
use of android.support.v4.media.session.MediaControllerCompat in project zype-android by zype.
the class PlayerFragment method initMediaSession.
// Media session
private void initMediaSession() {
ComponentName mediaButtonReceiver = new ComponentName(getContext().getApplicationContext(), RemoteControlReceiver.class);
mediaSession = new MediaSessionCompat(getContext().getApplicationContext(), "TAG_MEDIA_SESSION", mediaButtonReceiver, null);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setClass(getActivity(), RemoteControlReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, mediaButtonIntent, 0);
mediaSession.setMediaButtonReceiver(pendingIntent);
PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder().setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE);
mediaSession.setPlaybackState(stateBuilder.build());
mediaSession.setCallback(new MediaSessionCompat.Callback() {
@Override
public void onPlay() {
super.onPlay();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
super.onPlayFromMediaId(mediaId, extras);
}
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
return super.onMediaButtonEvent(mediaButtonEvent);
}
});
MediaControllerCompat mediaController = new MediaControllerCompat(getActivity(), mediaSession);
MediaControllerCompat.setMediaController(getActivity(), mediaController);
}
use of android.support.v4.media.session.MediaControllerCompat in project Gadgetbridge by Freeyourgadget.
the class NotificationListener method handleMediaSessionNotification.
/**
* Try to handle media session notifications that tell info about the current play state.
*
* @param mediaSession The mediasession to handle.
* @return true if notification was handled, false otherwise
*/
public boolean handleMediaSessionNotification(MediaSessionCompat.Token mediaSession) {
final MusicSpec musicSpec = new MusicSpec();
final MusicStateSpec stateSpec = new MusicStateSpec();
MediaControllerCompat c;
try {
c = new MediaControllerCompat(getApplicationContext(), mediaSession);
PlaybackStateCompat s = c.getPlaybackState();
stateSpec.position = (int) (s.getPosition() / 1000);
stateSpec.playRate = Math.round(100 * s.getPlaybackSpeed());
stateSpec.repeat = 1;
stateSpec.shuffle = 1;
switch(s.getState()) {
case PlaybackStateCompat.STATE_PLAYING:
stateSpec.state = MusicStateSpec.STATE_PLAYING;
break;
case PlaybackStateCompat.STATE_STOPPED:
stateSpec.state = MusicStateSpec.STATE_STOPPED;
break;
case PlaybackStateCompat.STATE_PAUSED:
stateSpec.state = MusicStateSpec.STATE_PAUSED;
break;
default:
stateSpec.state = MusicStateSpec.STATE_UNKNOWN;
break;
}
MediaMetadataCompat d = c.getMetadata();
if (d == null)
return false;
if (d.containsKey(MediaMetadata.METADATA_KEY_ARTIST))
musicSpec.artist = d.getString(MediaMetadataCompat.METADATA_KEY_ARTIST);
if (d.containsKey(MediaMetadata.METADATA_KEY_ALBUM))
musicSpec.album = d.getString(MediaMetadataCompat.METADATA_KEY_ALBUM);
if (d.containsKey(MediaMetadata.METADATA_KEY_TITLE))
musicSpec.track = d.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
if (d.containsKey(MediaMetadata.METADATA_KEY_DURATION))
musicSpec.duration = (int) d.getLong(MediaMetadataCompat.METADATA_KEY_DURATION) / 1000;
if (d.containsKey(MediaMetadata.METADATA_KEY_NUM_TRACKS))
musicSpec.trackCount = (int) d.getLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS);
if (d.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER))
musicSpec.trackNr = (int) d.getLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER);
// finally, tell the device about it
if (mSetMusicInfoRunnable != null) {
mHandler.removeCallbacks(mSetMusicInfoRunnable);
}
mSetMusicInfoRunnable = new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSetMusicInfo(musicSpec);
}
};
mHandler.postDelayed(mSetMusicInfoRunnable, 100);
if (mSetMusicStateRunnable != null) {
mHandler.removeCallbacks(mSetMusicStateRunnable);
}
mSetMusicStateRunnable = new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSetMusicState(stateSpec);
}
};
mHandler.postDelayed(mSetMusicStateRunnable, 100);
return true;
} catch (NullPointerException | RemoteException | SecurityException e) {
return false;
}
}
Aggregations