use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project Shuttle by timusus.
the class MediaSessionManager method updateMediaSession.
private void updateMediaSession(final String action) {
int playState = playbackManager.isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
long playbackActions = getMediaSessionActions();
QueueItem currentQueueItem = queueManager.getCurrentQueueItem();
PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
builder.setActions(playbackActions);
switch(queueManager.shuffleMode) {
case QueueManager.ShuffleMode.OFF:
builder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(SHUFFLE_ACTION, context.getString(R.string.btn_shuffle_on), R.drawable.ic_shuffle_off_circled).build());
break;
case QueueManager.ShuffleMode.ON:
builder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(SHUFFLE_ACTION, context.getString(R.string.btn_shuffle_off), R.drawable.ic_shuffle_on_circled).build());
break;
}
builder.setState(playState, playbackManager.getSeekPosition(), 1.0f);
if (currentQueueItem != null) {
builder.setActiveQueueItemId((long) currentQueueItem.hashCode());
}
PlaybackStateCompat playbackState = builder.build();
if (action.equals(InternalIntents.PLAY_STATE_CHANGED) || action.equals(InternalIntents.POSITION_CHANGED) || action.equals(SHUFFLE_ACTION)) {
mediaSession.setPlaybackState(playbackState);
} else if (action.equals(InternalIntents.META_CHANGED) || action.equals(InternalIntents.QUEUE_CHANGED)) {
if (currentQueueItem != null) {
MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, String.valueOf(currentQueueItem.getSong().id)).putString(MediaMetadataCompat.METADATA_KEY_ARTIST, currentQueueItem.getSong().artistName).putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, currentQueueItem.getSong().albumArtistName).putString(MediaMetadataCompat.METADATA_KEY_ALBUM, currentQueueItem.getSong().albumName).putString(MediaMetadataCompat.METADATA_KEY_TITLE, currentQueueItem.getSong().name).putLong(MediaMetadataCompat.METADATA_KEY_DURATION, currentQueueItem.getSong().duration).putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, (long) (queueManager.queuePosition + 1)).putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null).putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, (long) (queueManager.getCurrentPlaylist().size()));
// If we're in car mode, don't wait for the artwork to load before setting session metadata.
if (CarHelper.isCarUiMode(context)) {
mediaSession.setMetadata(metaData.build());
}
mediaSession.setPlaybackState(playbackState);
mediaSession.setQueue(QueueItemKt.toMediaSessionQueueItems(queueManager.getCurrentPlaylist()));
mediaSession.setQueueTitle(context.getString(R.string.menu_queue));
if (settingsManager.showLockscreenArtwork() || CarHelper.isCarUiMode(context)) {
updateMediaSessionArtwork(metaData);
} else {
mediaSession.setMetadata(metaData.build());
}
}
}
}
use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project android-UniversalMusicPlayer by googlesamples.
the class QueueHelper method isQueueItemPlaying.
/**
* Determine if queue item matches the currently playing queue item
*
* @param context for retrieving the {@link MediaControllerCompat}
* @param queueItem to compare to currently playing {@link MediaSessionCompat.QueueItem}
* @return boolean indicating whether queue item matches currently playing queue item
*/
public static boolean isQueueItemPlaying(Context context, MediaSessionCompat.QueueItem queueItem) {
// Queue item is considered to be playing or paused based on both the controller's
// current media id and the controller's active queue item id
MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
if (controller != null && controller.getPlaybackState() != null) {
long currentPlayingQueueId = controller.getPlaybackState().getActiveQueueItemId();
String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
if (queueItem.getQueueId() == currentPlayingQueueId && currentPlayingMediaId != null && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
return true;
}
}
return false;
}
use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project android-UniversalMusicPlayer by googlesamples.
the class LocalPlayback method play.
@Override
public void play(QueueItem item) {
mPlayOnFocusGain = true;
tryToGetAudioFocus();
registerAudioNoisyReceiver();
String mediaId = item.getDescription().getMediaId();
boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
if (mediaHasChanged) {
mCurrentMediaId = mediaId;
}
if (mediaHasChanged || mExoPlayer == null) {
// release everything except the player
releaseResources(false);
MediaMetadataCompat track = mMusicProvider.getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
if (source != null) {
// Escape spaces for URLs
source = source.replaceAll(" ", "%20");
}
if (mExoPlayer == null) {
mExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, new DefaultTrackSelector(), new DefaultLoadControl());
mExoPlayer.addListener(mEventListener);
}
// Android "O" makes much greater use of AudioAttributes, especially
// with regards to AudioFocus. All of UAMP's tracks are music, but
// if your content includes spoken word such as audiobooks or podcasts
// then the content type should be set to CONTENT_TYPE_SPEECH for those
// tracks.
final AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(CONTENT_TYPE_MUSIC).setUsage(USAGE_MEDIA).build();
mExoPlayer.setAudioAttributes(audioAttributes);
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext, Util.getUserAgent(mContext, "uamp"), null);
// Produces Extractor instances for parsing the media data.
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
// The MediaSource represents the media to be played.
MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(source), dataSourceFactory, extractorsFactory, null, null);
// Prepares media to play (happens on background thread) and triggers
// {@code onPlayerStateChanged} callback when the stream is ready to play.
mExoPlayer.prepare(mediaSource);
// If we are streaming from the internet, we want to hold a
// Wifi lock, which prevents the Wifi radio from going to
// sleep while the song is playing.
mWifiLock.acquire();
}
configurePlayerState();
}
use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project react-native-track-player by react-native-kit.
the class Track method toQueueItem.
public QueueItem toQueueItem() {
if (queueItem != null)
return queueItem;
MediaDescriptionCompat desc = new MediaDescriptionCompat.Builder().setMediaId(id).setMediaUri(url).setTitle(title).setSubtitle(artist).setDescription(description).setIconUri(artwork).build();
queueItem = new QueueItem(desc, queueId);
return queueItem;
}
use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project react-native-track-player by react-native-kit.
the class Metadata method updateQueue.
public void updateQueue(Playback playback) {
List<QueueItem> items = new ArrayList<>();
for (Track track : playback.getQueue()) {
items.add(track.toQueueItem());
}
session.setQueue(items);
}
Aggregations