Java Code Examples for android.support.v4.media.session.MediaSessionCompat#QueueItem

The following examples show how to use android.support.v4.media.session.MediaSessionCompat#QueueItem . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoteControlClientLP.java    From Popeens-DSub with GNU General Public License v3.0 7 votes vote down vote up
@Override
public void updatePlaylist(List<DownloadFile> playlist) {
	List<MediaSessionCompat.QueueItem> queue = new ArrayList<>();

	for(DownloadFile file: playlist) {
		Entry entry = file.getSong();
		Bundle extras = new Bundle();
		extras.putLong(MediaMetadataCompat.METADATA_KEY_DURATION,
				((entry.getDuration() == null) ? 0 : (entry.getDuration() * 1000)));

		MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
				.setMediaId(entry.getId())
				.setTitle(entry.getTitle())
				.setSubtitle(entry.getArtist())
				.setDescription(entry.getAlbum())
				.setExtras(extras)
				.build();
		MediaSessionCompat.QueueItem item = new MediaSessionCompat.QueueItem(description, entry.getId().hashCode());
		queue.add(item);
	}

	mediaSession.setQueue(queue);
	currentQueue = playlist;
}
 
Example 2
Source File: QueueHelper.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
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;

}
 
Example 3
Source File: QueueHelper.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
/**
 * Create a random queue.
 *
 * @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<>();

    for (String genre : musicProvider.getGenres()) {
        Iterable<MediaMetadataCompat> tracks = musicProvider.getMusicsByGenre(genre);
        for (MediaMetadataCompat track : tracks) {
            if (ThreadLocalRandom.current().nextBoolean()) {
                result.add(track);
            }
        }
    }
    LogUtils.d(TAG, "getRandomQueue: result.size=", result.size());

    Collections.shuffle(result);

    return convertToQueue(result, MEDIA_ID_MUSICS_BY_SEARCH, "random");
}
 
Example 4
Source File: MusicService.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 */
private void updatePlaybackState(String error) {
    LogUtils.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSessionCompat.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        stateBuilder.setActiveQueueItemId(item.getQueueId());
    }

    mSession.setPlaybackState(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mMediaNotificationManager.startNotification();
    }
}
 
Example 5
Source File: MusicService.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
private MediaMetadataCompat getCurrentPlayingMusic() {
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSessionCompat.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        if (item != null) {
            LogUtils.d(TAG, "getCurrentPlayingMusic for musicId=",
                    item.getDescription().getMediaId());
            return mMusicProvider.getMusic(
                    MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
        }
    }
    return null;
}
 
Example 6
Source File: MusicService.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
@Override
public void onMetadataChanged(String mediaId) {
    LogUtils.d(TAG, "onMetadataChanged", mediaId);
    List<MediaSessionCompat.QueueItem> queue = QueueHelper.getPlayingQueue(mediaId, mMusicProvider);
    int index = QueueHelper.getMusicIndexOnQueue(queue, mediaId);
    if (index > -1) {
        mCurrentIndexOnQueue = index;
        mPlayingQueue = queue;
        updateMetadata();
    }
}
 
Example 7
Source File: QueueHelper.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
public static List<MediaSessionCompat.QueueItem> getPlayingQueue(String mediaId,
                                                                 MusicProvider musicProvider) {

    // extract the browsing hierarchy from the media ID:
    String[] hierarchy = MediaIDHelper.getHierarchy(mediaId);

    if (hierarchy.length != 2) {
        LogUtils.e(TAG, "Could not build a playing queue for this mediaId: ", mediaId);
        return null;
    }

    String categoryType = hierarchy[0];
    String categoryValue = hierarchy[1];
    LogUtils.d(TAG, "Creating playing queue for ", categoryType, ",  ", categoryValue);

    Iterable<MediaMetadataCompat> tracks = null;
    // This sample only supports genre and by_search category types.
    switch (categoryType) {
        case MEDIA_ID_MUSICS_BY_GENRE:
            tracks = musicProvider.getMusicsByGenre(categoryValue);
            break;
        case MEDIA_ID_MUSICS_BY_SEARCH:
            tracks = musicProvider.searchMusicBySongTitle(categoryValue);
            break;
        case MEDIA_ID_MUSICS_BY_ALBUM:
            tracks = musicProvider.getMusicsByAlbum(categoryValue);
            break;
        case MEDIA_ID_MUSICS_BY_ARTIST:
            tracks = musicProvider.getMusicsByArtist(categoryValue);
            break;
    }

    if (tracks == null) {
        LogUtils.e(TAG, "Unrecognized category type: ", categoryType, " for media ", mediaId);
        return null;
    }

    return convertToQueue(tracks, hierarchy[0], hierarchy[1]);
}
 
Example 8
Source File: QueueHelper.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
public static List<MediaSessionCompat.QueueItem> getPlayingQueueFromSearch(String query,
                                                                           Bundle queryParams, MusicProvider musicProvider) {

    LogUtils.d(TAG, "Creating playing queue for musics from search: ", query,
            " params=", queryParams);

    VoiceSearchParams params = new VoiceSearchParams(query, queryParams);

    LogUtils.d(TAG, "VoiceSearchParams: ", params);

    if (params.isAny) {
        // If isAny is true, we will play anything. This is app-dependent, and can be,
        // 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);
    }

    // If there was no results using media focus parameter, we do an unstructured query.
    // This is useful when the user is searching for something that looks like an artist
    // to Google, for example, but is not. For example, a user searching for Madonna on
    // a PodCast application wouldn't get results if we only looked at the
    // 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);
}
 
Example 9
Source File: QueueHelper.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
public static int getMusicIndexOnQueue(Iterable<MediaSessionCompat.QueueItem> queue,
                                       String mediaId) {
    int index = 0;
    for (MediaSessionCompat.QueueItem item : queue) {
        if (mediaId.equals(item.getDescription().getMediaId())) {
            return index;
        }
        index++;
    }
    return -1;
}
 
Example 10
Source File: QueueHelper.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
public static int getMusicIndexOnQueue(Iterable<MediaSessionCompat.QueueItem> queue,
                                       long queueId) {
    int index = 0;
    for (MediaSessionCompat.QueueItem item : queue) {
        if (queueId == item.getQueueId()) {
            return index;
        }
        index++;
    }
    return -1;
}
 
Example 11
Source File: QueueHelper.java    From LyricHere with Apache License 2.0 4 votes vote down vote up
public static boolean isIndexPlayable(int index, List<MediaSessionCompat.QueueItem> queue) {
    return (queue != null && index >= 0 && index < queue.size());
}
 
Example 12
Source File: PlaybackControlBaseActivity.java    From LyricHere with Apache License 2.0 4 votes vote down vote up
@Override
public void onQueueChanged(List<MediaSessionCompat.QueueItem> queue) {
    super.onQueueChanged(queue);
    LogUtils.d(TAG, "onQueueChanged");
}
 
Example 13
Source File: MainActivity.java    From android-MediaBrowserService with Apache License 2.0 4 votes vote down vote up
@Override
public void onQueueChanged(List<MediaSessionCompat.QueueItem> queue) {
    super.onQueueChanged(queue);
}