Search in sources :

Example 16 with MediaMetadataCompat

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");
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) ArrayList(java.util.ArrayList)

Example 17 with MediaMetadataCompat

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;
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) ArrayList(java.util.ArrayList) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 18 with MediaMetadataCompat

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));
                }
            }
        });
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Bitmap(android.graphics.Bitmap) AlbumArtCache(com.example.android.uamp.AlbumArtCache) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 19 with MediaMetadataCompat

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));
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Test(org.junit.Test)

Example 20 with MediaMetadataCompat

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);
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Test(org.junit.Test)

Aggregations

MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)31 Test (org.junit.Test)13 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)8 Bitmap (android.graphics.Bitmap)6 Uri (android.net.Uri)4 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)4 Bundle (android.os.Bundle)3 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 Builder (com.google.android.gms.cast.Cast.CastOptions.Builder)3 MediaInfo (com.google.android.gms.cast.MediaInfo)3 FetchBitmapTask (com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask)3 ArrayList (java.util.ArrayList)3 PendingIntent (android.app.PendingIntent)2 Context (android.content.Context)2 Intent (android.content.Intent)2 NotFoundException (android.content.res.Resources.NotFoundException)2 RemoteException (android.os.RemoteException)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2