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);
}
}
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)));
}
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;
}
}
}
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;
}
}
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()));
}
Aggregations