Search in sources :

Example 6 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class RemoteJSONSource method iterator.

@Override
public Iterator<MediaMetadataCompat> iterator() {
    try {
        int slashPos = CATALOG_URL.lastIndexOf('/');
        String path = CATALOG_URL.substring(0, slashPos + 1);
        JSONObject jsonObj = fetchJSONFromUrl(CATALOG_URL);
        ArrayList<MediaMetadataCompat> tracks = new ArrayList<>();
        if (jsonObj != null) {
            JSONArray jsonTracks = jsonObj.getJSONArray(JSON_MUSIC);
            if (jsonTracks != null) {
                for (int j = 0; j < jsonTracks.length(); j++) {
                    tracks.add(buildFromJSON(jsonTracks.getJSONObject(j), path));
                }
            }
        }
        return tracks.iterator();
    } catch (JSONException e) {
        LogHelper.e(TAG, e, "Could not retrieve music list");
        throw new RuntimeException("Could not retrieve music list", e);
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 7 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class CastPlayback method loadMedia.

private void loadMedia(String mediaId, boolean autoPlay) throws JSONException {
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(mediaId);
    MediaMetadataCompat track = mMusicProvider.getMusic(musicId);
    if (track == null) {
        throw new IllegalArgumentException("Invalid mediaId " + mediaId);
    }
    if (!TextUtils.equals(mediaId, mCurrentMediaId)) {
        mCurrentMediaId = mediaId;
        mCurrentPosition = 0;
    }
    JSONObject customData = new JSONObject();
    customData.put(ITEM_ID, mediaId);
    MediaInfo media = toCastMediaMetadata(track, customData);
    mRemoteMediaClient.load(media, autoPlay, mCurrentPosition, customData);
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) MediaInfo(com.google.android.gms.cast.MediaInfo) JSONObject(org.json.JSONObject)

Example 8 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class QueueHelper method getPlayingQueueFromSearch.

public static List<MediaSessionCompat.QueueItem> getPlayingQueueFromSearch(String query, Bundle queryParams, MusicProvider musicProvider) {
    LogHelper.d(TAG, "Creating playing queue for musics from search: ", query, " params=", queryParams);
    VoiceSearchParams params = new VoiceSearchParams(query, queryParams);
    LogHelper.d(TAG, "VoiceSearchParams: ", params);
    if (params.isAny) {
        // for example, favorite playlists, "I'm feeling lucky", most recent, etc.
        return getRandomQueue(musicProvider);
    }
    Iterable<MediaMetadataCompat> result = null;
    if (params.isAlbumFocus) {
        result = musicProvider.searchMusicByAlbum(params.album);
    } else if (params.isGenreFocus) {
        result = musicProvider.getMusicsByGenre(params.genre);
    } else if (params.isArtistFocus) {
        result = musicProvider.searchMusicByArtist(params.artist);
    } else if (params.isSongFocus) {
        result = musicProvider.searchMusicBySongTitle(params.song);
    }
    // Artist (podcast author). Then, we can instead do an unstructured search.
    if (params.isUnstructured || result == null || !result.iterator().hasNext()) {
        // To keep it simple for this example, we do unstructured searches on the
        // song title only. A real world application could search on other fields as well.
        result = musicProvider.searchMusicBySongTitle(query);
    }
    return convertToQueue(result, MEDIA_ID_MUSICS_BY_SEARCH, query);
}
Also used : VoiceSearchParams(com.example.android.uamp.VoiceSearchParams) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat)

Example 9 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 10 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)

Aggregations

MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)29 Test (org.junit.Test)13 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)8 Bitmap (android.graphics.Bitmap)5 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)4 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 ArrayList (java.util.ArrayList)3 PendingIntent (android.app.PendingIntent)2 Context (android.content.Context)2 Intent (android.content.Intent)2 RemoteException (android.os.RemoteException)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 MediaInfo (com.google.android.gms.cast.MediaInfo)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JSONObject (org.json.JSONObject)2 Configuration (android.content.res.Configuration)1