use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.
the class QueueHelper method getRandomQueue.
/**
* Create a random queue with at most {@link #RANDOM_QUEUE_SIZE} elements.
*
* @param musicProvider the provider used for fetching music.
* @return list containing {@link MediaSessionCompat.QueueItem}'s
*/
public static List<MediaSessionCompat.QueueItem> getRandomQueue(MusicProvider musicProvider) {
List<MediaMetadataCompat> result = new ArrayList<>(RANDOM_QUEUE_SIZE);
Iterable<MediaMetadataCompat> shuffled = musicProvider.getShuffledMusic();
for (MediaMetadataCompat metadata : shuffled) {
if (result.size() == RANDOM_QUEUE_SIZE) {
break;
}
result.add(metadata);
}
LogHelper.d(TAG, "getRandomQueue: result.size=", result.size());
return convertToQueue(result, MEDIA_ID_MUSICS_BY_SEARCH, "random");
}
use of android.support.v4.media.MediaMetadataCompat 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.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.
the class QueueManager method updateMetadata.
public void updateMetadata() {
MediaSessionCompat.QueueItem currentMusic = getCurrentMusic();
if (currentMusic == null) {
mListener.onMetadataRetrieveError();
return;
}
final String musicId = MediaIDHelper.extractMusicIDFromMediaID(currentMusic.getDescription().getMediaId());
MediaMetadataCompat metadata = mMusicProvider.getMusic(musicId);
if (metadata == null) {
throw new IllegalArgumentException("Invalid musicId " + musicId);
}
mListener.onMetadataChanged(metadata);
// locked screen and in other places.
if (metadata.getDescription().getIconBitmap() == null && metadata.getDescription().getIconUri() != null) {
String albumUri = metadata.getDescription().getIconUri().toString();
AlbumArtCache.getInstance().fetch(albumUri, new AlbumArtCache.FetchListener() {
@Override
public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
mMusicProvider.updateMusicArt(musicId, bitmap, icon);
// If we are still playing the same music, notify the listeners:
MediaSessionCompat.QueueItem currentMusic = getCurrentMusic();
if (currentMusic == null) {
return;
}
String currentPlayingId = MediaIDHelper.extractMusicIDFromMediaID(currentMusic.getDescription().getMediaId());
if (musicId.equals(currentPlayingId)) {
mListener.onMetadataChanged(mMusicProvider.getMusic(currentPlayingId));
}
}
});
}
}
use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.
the class MusicProviderTest method testFavorite.
@Test
public void testFavorite() throws Exception {
MediaMetadataCompat metadata = provider.getShuffledMusic().iterator().next();
String musicId = metadata.getDescription().getMediaId();
assertFalse(provider.isFavorite(musicId));
provider.setFavorite(musicId, true);
assertTrue(provider.isFavorite(musicId));
provider.setFavorite(musicId, false);
assertFalse(provider.isFavorite(musicId));
}
use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.
the class MusicProviderTest method testSearchBySongTitle.
@Test
public void testSearchBySongTitle() throws Exception {
int count = 0;
for (MediaMetadataCompat metadata : provider.searchMusicBySongTitle("Romantic")) {
String title = metadata.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
assertTrue(title.contains("Romantic"));
count++;
}
assertEquals(2, count);
}
Aggregations