public class MusicQueue extends Object
MediaSessionCompat.QueueItem
s that
manages the current playing item, as well as maintains a window to be used with
MediaSessionCompat.setQueue(List)
.Modifier and Type | Class and Description |
---|---|
static interface |
MusicQueue.Callback
Callback triggered when the available queue manipulation options change.
|
Modifier and Type | Field and Description |
---|---|
static int |
WINDOW_SIZE
The maximum size of the queue window/subset handed to the
MediaSessionCompat . |
Constructor and Description |
---|
MusicQueue(MediaSessionCompat mediaSession)
Constructs a
MusicQueue without a MusicQueue.Callback . |
MusicQueue(MediaSessionCompat mediaSession,
MusicQueue.Callback actionsCallback)
Constructs a
MusicQueue with a MusicQueue.Callback . |
Modifier and Type | Method and Description |
---|---|
void |
addToQueue(MediaDescriptionCompat description)
Adds a song to the end of the music queue.
|
boolean |
addToQueue(MediaDescriptionCompat description,
int index)
Adds a song to the music queue at a specific index.
|
boolean |
checkNextSongAvailable()
Checks if there is another song forward in the queue that can be played with
skipToNext() . |
boolean |
checkPreviousSongAvailable()
Checks if there are any previous songs in the queue that can be played with
skipToPrevious() . |
boolean |
checkSongAvailable(int index)
Checks if there is a song available at a specific index.
|
boolean |
checkSongAvailable(long id)
Checks if an ID corresponds to a song in the queue.
|
boolean |
checkSongAvailable(MediaDescriptionCompat description)
Checks if a song is in the queue.
|
long |
getActions()
Gets the currently available
PlaybackStateCompat actions. |
MediaSessionCompat.QueueItem |
getCurrentSong()
Gets the currently-playing song.
|
int |
getIndex()
Gets the index of the playing song in the full music queue.
|
int |
getNextSongCount()
Gets the number of songs forward in the queue, equivalent to the number of times
skipToNext()
can be successfully called. |
int |
getPreviousSongCount()
Gets the number of songs backward in the queue, equivalent to the number of times
skipToPrevious()
can be successfully called. |
int |
getQueueSize()
Gets the total size of the current music queue.
|
List<MediaSessionCompat.QueueItem> |
getQueueSublist(int fromIndex,
int toIndex)
Gets a section of the current music queue between
fromIndex (inclusive) and toIndex
(exclusive). |
int |
getWindowIndex()
Gets the index of the playing item in the current window of the music queue.
|
int |
getWindowOffset()
Gets the offset of the current window in the full music queue.
|
MediaSessionCompat.QueueItem |
peekNextSong()
Gets the next song if available, without modifying the queue.
|
MediaSessionCompat.QueueItem |
peekPreviousSong()
Gets the previous song if available, without modifying the queue.
|
MediaSessionCompat.QueueItem |
peekSong(int index)
Gets the song at a specific index.
|
MediaSessionCompat.QueueItem |
peekSong(long id)
Gets the song associated with a specific ID.
|
MediaSessionCompat.QueueItem |
peekWindowSong(int index)
Gets the song at a specific index in the current window of the music queue.
|
boolean |
removeFromQueue(int index)
Removes a song at a specific index from the queue.
|
boolean |
removeFromQueue(long id)
Removes a specific song from the queue by its ID.
|
boolean |
removeFromQueue(MediaDescriptionCompat description)
Removes a specific song from the queue.
|
MediaSessionCompat.QueueItem |
skipToNext()
Skips to the next song in the queue.
|
MediaSessionCompat.QueueItem |
skipToNext(int times)
Skips forward in the queue a number of times.
|
MediaSessionCompat.QueueItem |
skipToPrevious()
Skips to the previous song in the queue.
|
MediaSessionCompat.QueueItem |
skipToPrevious(int times)
Skips backward in the queue a number of times.
|
MediaSessionCompat.QueueItem |
skipToSong(int index)
Skips to the song at a specific index in the queue.
|
MediaSessionCompat.QueueItem |
skipToSong(long id)
Skips to a specific song in the queue.
|
public static final int WINDOW_SIZE
MediaSessionCompat
.public MusicQueue(MediaSessionCompat mediaSession)
MusicQueue
without a MusicQueue.Callback
.mediaSession
- the media session the queue is posted toMusicQueue(MediaSessionCompat, Callback)
public MusicQueue(MediaSessionCompat mediaSession, MusicQueue.Callback actionsCallback)
MusicQueue
with a MusicQueue.Callback
.mediaSession
- the media session the queue is posted toactionsCallback
- the callback triggered when the available actions changepublic void addToQueue(MediaDescriptionCompat description)
description
- the song to addpublic boolean addToQueue(MediaDescriptionCompat description, int index)
description
- the song to addindex
- the index to insert the song attrue
if a song reload is suggested due to the song being inserted at the position of the currently-playing songIndexOutOfBoundsException
- if index < 0
or index >
getQueueSize()
(one passed current final index)public boolean removeFromQueue(MediaDescriptionCompat description)
description
- the song to removetrue
if the currently-playing song was removed and a song reload is necessarypublic boolean removeFromQueue(long id)
id
- the ID of the song to removetrue
if the currently-playing song was removed and a song reload is necessarypublic boolean removeFromQueue(int index)
index
- the index of the song to removetrue
if the currently-playing song was removed and a song reload is necessaryIndexOutOfBoundsException
- if index < 0
or index >=
getQueueSize()
(index of final song)public MediaSessionCompat.QueueItem skipToSong(long id)
id
- the ID of the song to skip tonull
if id
doesn't match a song, otherwise the song that was skipped topublic MediaSessionCompat.QueueItem skipToSong(int index)
index
- the index of the song to skip tonull
if index
doesn't exist in the queue, otherwise the song that was skipped topublic MediaSessionCompat.QueueItem skipToNext()
null
if there are no songs left in the queue, otherwise the song that was skipped topublic MediaSessionCompat.QueueItem skipToNext(int times)
null
is returned and the queue remains unchanged.times
- the number of times to skip forwardnull
if the queue couldn't be skipped as requested, otherwise the song that was skipped topublic MediaSessionCompat.QueueItem skipToPrevious()
null
if there are no previous songs left in the queue, otherwise the song that was skipped topublic MediaSessionCompat.QueueItem skipToPrevious(int times)
null
is returned and the queue remains unchanged.times
- the number of times to skip backwardnull
if the queue couldn't be skipped as requested, otherwise the song that was skipped topublic MediaSessionCompat.QueueItem getCurrentSong()
null
if no song is supposed to be playing, otherwise the current songpublic MediaSessionCompat.QueueItem peekSong(long id)
id
- the ID of the song to findnull
if no song is represented by id
, otherwise the requested songskipToSong(long)
public MediaSessionCompat.QueueItem peekSong(int index)
index
- the index of the song to findnull
if index
doesn't exist in the queue, otherwise the requested songskipToSong(int)
public MediaSessionCompat.QueueItem peekWindowSong(int index)
index
- the index of the song to findnull
if the index doesn't exist in the window, otherwise the requested songskipToSong(int)
public MediaSessionCompat.QueueItem peekNextSong()
null
if there are no songs left in the queue, otherwise the next songskipToNext()
public MediaSessionCompat.QueueItem peekPreviousSong()
null
if there are no previous songs left in the queue, otherwise the previous songskipToPrevious()
public boolean checkSongAvailable(MediaDescriptionCompat description)
description
- the song to check fortrue
if description
matches a song in the queue, otherwise false
public boolean checkSongAvailable(long id)
id
- the ID to find the associated song withtrue
if id
corresponds to a song in the queue, otherwise false
public boolean checkSongAvailable(int index)
index
- the index to check for a songtrue
if index
corresponds to a song in the queue, otherwise false
public boolean checkNextSongAvailable()
skipToNext()
.true
if another song is available, otherwise false
public boolean checkPreviousSongAvailable()
skipToPrevious()
.true
if another song is available, otherwise false
public long getActions()
PlaybackStateCompat
actions.public int getQueueSize()
public List<MediaSessionCompat.QueueItem> getQueueSublist(int fromIndex, int toIndex)
fromIndex
(inclusive) and toIndex
(exclusive).fromIndex
- the index of the first element to include, must be greater than 0
toIndex
- the index of the first element to not include, must be greater than fromIndex
and not exceed getQueueSize()
[fromIndex, toIndex)
public int getIndex()
public int getWindowOffset()
public int getWindowIndex()
getWindowOffset()
,
getIndex()
public int getNextSongCount()
skipToNext()
can be successfully called. Doesn't include the current song.public int getPreviousSongCount()
skipToPrevious()
can be successfully called. Doesn't include the current song.