Search in sources :

Example 1 with PlaybackStateCompat

use of android.support.v4.media.session.PlaybackStateCompat in project AntennaPod by AntennaPod.

the class CustomMRControllerDialog method updateState.

private void updateState() {
    PlaybackStateCompat state;
    if (!viewsCreated || mediaController == null || (state = mediaController.getPlaybackState()) == null) {
        return;
    }
    boolean isPlaying = state.getState() == PlaybackStateCompat.STATE_BUFFERING || state.getState() == PlaybackStateCompat.STATE_PLAYING;
    boolean supportsPlay = (state.getActions() & (PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE)) != 0;
    boolean supportsPause = (state.getActions() & (PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE)) != 0;
    if (isPlaying && supportsPause) {
        playPauseButton.setVisibility(View.VISIBLE);
        playPauseButton.setImageResource(getThemeResource(getContext(), android.support.v7.mediarouter.R.attr.mediaRoutePauseDrawable));
        playPauseButton.setContentDescription(getContext().getResources().getText(android.support.v7.mediarouter.R.string.mr_controller_pause));
    } else if (!isPlaying && supportsPlay) {
        playPauseButton.setVisibility(View.VISIBLE);
        playPauseButton.setImageResource(getThemeResource(getContext(), android.support.v7.mediarouter.R.attr.mediaRoutePlayDrawable));
        playPauseButton.setContentDescription(getContext().getResources().getText(android.support.v7.mediarouter.R.string.mr_controller_play));
    } else {
        playPauseButton.setVisibility(View.GONE);
    }
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat)

Example 2 with PlaybackStateCompat

use of android.support.v4.media.session.PlaybackStateCompat in project AntennaPod by AntennaPod.

the class CustomMRControllerDialog method updateViews.

private void updateViews() {
    if (!viewsCreated || token == null || mediaController == null) {
        rootView.setVisibility(View.GONE);
        return;
    }
    MediaMetadataCompat metadata = mediaController.getMetadata();
    MediaDescriptionCompat description = metadata == null ? null : metadata.getDescription();
    if (description == null) {
        rootView.setVisibility(View.GONE);
        return;
    }
    PlaybackStateCompat state = mediaController.getPlaybackState();
    MediaRouter.RouteInfo route = MediaRouter.getInstance(getContext()).getSelectedRoute();
    CharSequence title = description.getTitle();
    boolean hasTitle = !TextUtils.isEmpty(title);
    CharSequence subtitle = description.getSubtitle();
    boolean hasSubtitle = !TextUtils.isEmpty(subtitle);
    boolean showTitle = false;
    boolean showSubtitle = false;
    if (route.getPresentationDisplayId() != MediaRouter.RouteInfo.PRESENTATION_DISPLAY_ID_NONE) {
        // The user is currently casting screen.
        titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_casting_screen);
        showTitle = true;
    } else if (state == null || state.getState() == PlaybackStateCompat.STATE_NONE) {
        // (Only exception is bluetooth where we don't show anything.)
        if (!route.isDeviceTypeBluetooth()) {
            titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_no_media_selected);
            showTitle = true;
        }
    } else if (!hasTitle && !hasSubtitle) {
        titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_no_info_available);
        showTitle = true;
    } else {
        if (hasTitle) {
            titleView.setText(title);
            showTitle = true;
        }
        if (hasSubtitle) {
            subtitleView.setText(subtitle);
            showSubtitle = true;
        }
    }
    if (showSubtitle) {
        titleView.setSingleLine();
    } else {
        titleView.setMaxLines(2);
    }
    titleView.setVisibility(showTitle ? View.VISIBLE : View.GONE);
    subtitleView.setVisibility(showSubtitle ? View.VISIBLE : View.GONE);
    updateState();
    if (rootView.getVisibility() != View.VISIBLE) {
        artView.setVisibility(View.GONE);
        rootView.setVisibility(View.VISIBLE);
    }
    if (fetchArtSubscription != null) {
        fetchArtSubscription.unsubscribe();
    }
    fetchArtSubscription = Observable.fromCallable(() -> fetchArt(description)).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
        fetchArtSubscription = null;
        if (artView == null) {
            return;
        }
        if (result.first != null) {
            if (!((Boolean) artView.getTag())) {
                artView.setBackgroundColor(result.second);
            }
            artView.setImageBitmap(result.first);
            artView.setVisibility(View.VISIBLE);
        } else {
            artView.setVisibility(View.GONE);
        }
    }, error -> Log.e(TAG, Log.getStackTraceString(error)));
}
Also used : Context(android.content.Context) ImageButton(android.widget.ImageButton) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) Palette(android.support.v7.graphics.Palette) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat) Uri(android.net.Uri) FrameLayout(android.widget.FrameLayout) ImageView(android.widget.ImageView) AccessibilityEventCompat(android.support.v4.view.accessibility.AccessibilityEventCompat) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) RemoteException(android.os.RemoteException) PendingIntent(android.app.PendingIntent) NonNull(android.support.annotation.NonNull) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat) MarginLayoutParamsCompat(android.support.v4.view.MarginLayoutParamsCompat) ApGlideSettings(de.danoeh.antennapod.core.glide.ApGlideSettings) Drawable(android.graphics.drawable.Drawable) Observable(rx.Observable) AccessibilityManager(android.view.accessibility.AccessibilityManager) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Schedulers(rx.schedulers.Schedulers) View(android.view.View) AccessibilityEvent(android.view.accessibility.AccessibilityEvent) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) Log(android.util.Log) R(de.danoeh.antennapod.R) Target(com.bumptech.glide.request.target.Target) TextUtils(android.text.TextUtils) ViewGroup(android.view.ViewGroup) ExecutionException(java.util.concurrent.ExecutionException) MediaRouter(android.support.v7.media.MediaRouter) TextView(android.widget.TextView) Glide(com.bumptech.glide.Glide) MediaRouteControllerDialog(android.support.v7.app.MediaRouteControllerDialog) TypedValue(android.util.TypedValue) Bitmap(android.graphics.Bitmap) Pair(android.support.v4.util.Pair) Configuration(android.content.res.Configuration) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) Subscription(rx.Subscription) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaRouter(android.support.v7.media.MediaRouter) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 3 with PlaybackStateCompat

use of android.support.v4.media.session.PlaybackStateCompat in project Shuttle by timusus.

the class VideoCastManager method clearMediaSession.

/*
     * Clears Media Session
     */
public void clearMediaSession() {
    LOGD(TAG, "clearMediaSession()");
    if (isFeatureEnabled(CastConfiguration.FEATURE_LOCKSCREEN)) {
        if (mLockScreenFetchTask != null) {
            mLockScreenFetchTask.cancel(true);
        }
        if (mMediaSessionIconFetchTask != null) {
            mMediaSessionIconFetchTask.cancel(true);
        }
        mAudioManager.abandonAudioFocus(null);
        if (mMediaSessionCompat != null) {
            mMediaSessionCompat.setMetadata(null);
            PlaybackStateCompat playbackState = new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_NONE, 0, 1.0f).build();
            mMediaSessionCompat.setPlaybackState(playbackState);
            mMediaSessionCompat.release();
            mMediaSessionCompat.setActive(false);
            mMediaSessionCompat = null;
        }
    }
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) Builder(com.google.android.gms.cast.Cast.CastOptions.Builder)

Example 4 with PlaybackStateCompat

use of android.support.v4.media.session.PlaybackStateCompat in project android-UniversalMusicPlayer by googlesamples.

the class MediaItemViewHolder method getStateFromController.

public static int getStateFromController(Context context) {
    MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
    PlaybackStateCompat pbState = controller.getPlaybackState();
    if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
        return MediaItemViewHolder.STATE_NONE;
    } else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        return MediaItemViewHolder.STATE_PLAYING;
    } else {
        return MediaItemViewHolder.STATE_PAUSED;
    }
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 5 with PlaybackStateCompat

use of android.support.v4.media.session.PlaybackStateCompat in project android-UniversalMusicPlayer by googlesamples.

the class PlaybackManagerTest method testPlayFromSearch.

@Test
public void testPlayFromSearch() throws Exception {
    // Using a CountDownLatch, we will check if all callbacks are called correctly when
    // a onPlayFromMediaId command is issued.
    final CountDownLatch latch = new CountDownLatch(5);
    final String expectedMusicId = musicProvider.searchMusicBySongTitle("Music 3").iterator().next().getDescription().getMediaId();
    QueueManager queueManager = new QueueManager(musicProvider, resources, new SimpleMetadataUpdateListener() {

        @Override
        public void onMetadataChanged(MediaMetadataCompat metadata) {
            // Latch countdown 1: QueueManager will change appropriately
            assertEquals(expectedMusicId, metadata.getDescription().getMediaId());
            latch.countDown();
        }
    });
    SimplePlaybackServiceCallback serviceCallback = new SimplePlaybackServiceCallback() {

        @Override
        public void onPlaybackStart() {
            // Latch countdown 2: PlaybackService will get a onPlaybackStart call
            latch.countDown();
        }

        @Override
        public void onPlaybackStateUpdated(PlaybackStateCompat newState) {
            if (newState.getState() == PlaybackStateCompat.STATE_PLAYING) {
                // Latch countdown 3: PlaybackService will get a state updated call (here we
                // ignore the unrelated state changes)
                latch.countDown();
            }
        }

        @Override
        public void onNotificationRequired() {
            // Latch countdown 4: PlaybackService will get call to show a media notification
            latch.countDown();
        }
    };
    Playback playback = new SimplePlayback() {

        @Override
        public void play(MediaSessionCompat.QueueItem item) {
            // Latch countdown 5: Playback will be called with the correct queueItem
            assertEquals(expectedMusicId, MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
            latch.countDown();
        }
    };
    PlaybackManager playbackManager = new PlaybackManager(serviceCallback, resources, musicProvider, queueManager, playback);
    playbackManager.getMediaSessionCallback().onPlayFromSearch("Music 3", null);
    latch.await(5, TimeUnit.SECONDS);
    // Finally, check if the current music in queueManager is as expected
    assertEquals(expectedMusicId, MediaIDHelper.extractMusicIDFromMediaID(queueManager.getCurrentMusic().getDescription().getMediaId()));
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)13 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)10 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)6 RemoteException (android.os.RemoteException)4 View (android.view.View)4 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3 PendingIntent (android.app.PendingIntent)2 ComponentName (android.content.ComponentName)2 Context (android.content.Context)2 Configuration (android.content.res.Configuration)2 Bitmap (android.graphics.Bitmap)2 Drawable (android.graphics.drawable.Drawable)2 Uri (android.net.Uri)2 Bundle (android.os.Bundle)2 NonNull (android.support.annotation.NonNull)2 MediaDescriptionCompat (android.support.v4.media.MediaDescriptionCompat)2 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)2 Pair (android.support.v4.util.Pair)2 MarginLayoutParamsCompat (android.support.v4.view.MarginLayoutParamsCompat)2