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);
}
}
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);
}
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);
}
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 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");
}
Aggregations