Search in sources :

Example 31 with MediaControllerCompat

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();
}
Also used : MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 32 with MediaControllerCompat

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);
}
Also used : AbstractDetailsDescriptionPresenter(android.support.v17.leanback.widget.AbstractDetailsDescriptionPresenter) SkipPreviousAction(android.support.v17.leanback.widget.PlaybackControlsRow.SkipPreviousAction) SkipNextAction(android.support.v17.leanback.widget.PlaybackControlsRow.SkipNextAction) Action(android.support.v17.leanback.widget.Action) PlayPauseAction(android.support.v17.leanback.widget.PlaybackControlsRow.PlayPauseAction) OnActionClickedListener(android.support.v17.leanback.widget.OnActionClickedListener) PlaybackControlsRowPresenter(android.support.v17.leanback.widget.PlaybackControlsRowPresenter) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) PlaybackControlsRow(android.support.v17.leanback.widget.PlaybackControlsRow)

Example 33 with MediaControllerCompat

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;
}
Also used : MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 34 with MediaControllerCompat

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);
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) Bundle(android.os.Bundle) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 35 with MediaControllerCompat

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;
    }
}
Also used : MusicStateSpec(nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) MusicSpec(nodomain.freeyourgadget.gadgetbridge.model.MusicSpec) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) RemoteException(android.os.RemoteException)

Aggregations

MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)35 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)7 Intent (android.content.Intent)5 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)5 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)5 ComponentName (android.content.ComponentName)3 FragmentActivity (android.support.v4.app.FragmentActivity)3 View (android.view.View)3 PendingIntent (android.app.PendingIntent)2 Bundle (android.os.Bundle)2 RemoteException (android.os.RemoteException)2 ListRow (android.support.v17.leanback.widget.ListRow)2 ListRowPresenter (android.support.v17.leanback.widget.ListRowPresenter)2 MediaBrowserCompat (android.support.v4.media.MediaBrowserCompat)2 MediaDescriptionCompat (android.support.v4.media.MediaDescriptionCompat)2 NotificationCompat (androidx.core.app.NotificationCompat)2 ServiceConnection (android.content.ServiceConnection)1 Bitmap (android.graphics.Bitmap)1 IBinder (android.os.IBinder)1 CallSuper (android.support.annotation.CallSuper)1