use of android.support.v4.media.session.MediaSessionCompat.QueueItem 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()));
}
use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project android-UniversalMusicPlayer by googlesamples.
the class PlaybackManagerTest method testPlay.
@Test
public void testPlay() throws Exception {
String mediaId = MediaIDHelper.MEDIA_ID_ROOT;
while (MediaIDHelper.isBrowseable(mediaId)) {
mediaId = musicProvider.getChildren(mediaId, resources).get(0).getMediaId();
}
// 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 expectedMediaId = mediaId;
QueueManager queueManager = new QueueManager(musicProvider, resources, new SimpleMetadataUpdateListener() {
@Override
public void onMetadataChanged(MediaMetadataCompat metadata) {
// Latch countdown 1: QueueManager will change appropriately
assertEquals(MediaIDHelper.extractMusicIDFromMediaID(expectedMediaId), 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(expectedMediaId, item.getDescription().getMediaId());
latch.countDown();
}
};
PlaybackManager playbackManager = new PlaybackManager(serviceCallback, resources, musicProvider, queueManager, playback);
playbackManager.getMediaSessionCallback().onPlayFromMediaId(expectedMediaId, null);
latch.await(5, TimeUnit.SECONDS);
// Finally, check if the current music in queueManager is as expected
assertEquals(expectedMediaId, queueManager.getCurrentMusic().getDescription().getMediaId());
}
use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project android-UniversalMusicPlayer by googlesamples.
the class CardPresenter method onBindViewHolder.
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
MediaDescriptionCompat description;
final CardViewHolder cardViewHolder = (CardViewHolder) viewHolder;
// Determine description and playing state of item based on instance type
cardViewHolder.setState(MediaItemViewHolder.STATE_NONE);
if (item instanceof MediaBrowserCompat.MediaItem) {
MediaBrowserCompat.MediaItem mediaItem = (MediaBrowserCompat.MediaItem) item;
LogHelper.d(TAG, "onBindViewHolder MediaItem: ", mediaItem.toString());
description = mediaItem.getDescription();
cardViewHolder.setState(MediaItemViewHolder.getMediaItemState(mContext, mediaItem));
} else if (item instanceof MediaSessionCompat.QueueItem) {
MediaSessionCompat.QueueItem queueItem = (MediaSessionCompat.QueueItem) item;
LogHelper.d(TAG, "onBindViewHolder QueueItem: ", queueItem.toString());
description = queueItem.getDescription();
if (QueueHelper.isQueueItemPlaying(mContext, queueItem)) {
cardViewHolder.setState(MediaItemViewHolder.getStateFromController(mContext));
}
} else {
throw new IllegalArgumentException("Object must be MediaItem or QueueItem, not " + item.getClass().getSimpleName());
}
cardViewHolder.setupCardView(mContext, description);
}
use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project android-UniversalMusicPlayer by googlesamples.
the class QueueHelper method convertToQueue.
private static List<MediaSessionCompat.QueueItem> convertToQueue(Iterable<MediaMetadataCompat> tracks, String... categories) {
List<MediaSessionCompat.QueueItem> queue = new ArrayList<>();
int count = 0;
for (MediaMetadataCompat track : tracks) {
// We create a hierarchy-aware mediaID, so we know what the queue is about by looking
// at the QueueItem media IDs.
String hierarchyAwareMediaID = MediaIDHelper.createMediaID(track.getDescription().getMediaId(), categories);
MediaMetadataCompat trackCopy = new MediaMetadataCompat.Builder(track).putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID).build();
// We don't expect queues to change after created, so we use the item index as the
// queueId. Any other number unique in the queue would work.
MediaSessionCompat.QueueItem item = new MediaSessionCompat.QueueItem(trackCopy.getDescription(), count++);
queue.add(item);
}
return queue;
}
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(Activity 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 = MediaControllerCompat.getMediaController(context);
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;
}
Aggregations