Skip to navigation

Play a playlist

Play a list of videos in order with setPlaylist, navigate with next and previous, edit the list at runtime, and react to changes with PlaylistListener.

Play a list of videos in order in the FastPix Android Player.

Give the player a list and it plays the entries in order, moving on by itself when one finishes. If loop is on, the current entry repeats instead of advancing.


Set a playlist

Call setPlaylist with a list of PlaylistItem values and an optional startIndex. You can build entries from a playback ID, from a signed playback ID and token, from any setFastPixMediaItem options, or from a direct URL.

player.setPlaylist(
listOf(
PlaylistItem.fastPix("playback-id-1"),
PlaylistItem.fastPix("playback-id-2", playbackToken = token), // signed playback
PlaylistItem.fastPix { // any setFastPixMediaItem option
playbackId = "playback-id-3"
maxResolution = PlaybackResolution.HD_720
},
PlaylistItem.fromUrl("https://example.com/video.m3u8"),
),
startIndex = 0,
)

Move through the list with next, previous, and skipTo. next returns false at the last entry and previous returns false at the first entry, so you can use the return value to drive your own button states.

player.next() // false at the last entry
player.previous() // false at the first entry
player.skipTo(3)

Edit the playlist

Add, insert, remove, or clear entries at runtime. Appending is how you extend an endless feed as more videos load.

player.addToPlaylist(moreItems) // append, e.g. the next page of an endless feed
player.addToPlaylist(1, listOf(item)) // insert
player.removeFromPlaylist(2)
player.clearPlaylist()

Editing keeps the current entry playing unless it is the one removed.


Inspect the playlist

Read the current state of the list and where playback sits within it.

player.getPlaylist(); player.getCurrentIndex(); player.getCurrentItem()
player.hasNext(); player.hasPrevious()

Track playlist changes

Add a PlaylistListener to know when the current entry changes and when the list itself changes. The onPlaylistItemChanged callback reports why the entry changed through a PlaylistItemChangeReason.

player.addPlaylistListener(object : PlaylistListener {
override fun onPlaylistItemChanged(index: Int, item: PlaylistItem, reason: PlaylistItemChangeReason) {
// reason: PLAYLIST_SET, NAVIGATION, AUTO_ADVANCE or PLAYLIST_EDITED
}
override fun onPlaylistChanged(items: List<PlaylistItem>) { }
})

The reason values tell you what triggered the change:

  • PLAYLIST_SET: a new playlist was set with setPlaylist.
  • NAVIGATION: you called next, previous, or skipTo.
  • AUTO_ADVANCE: the current entry finished and the player moved on by itself.
  • PLAYLIST_EDITED: the list was edited and the current entry moved to a new index.

Single items and deprecated APIs

setMediaItem and setFastPixMediaItem still play a single item and clear any playlist. setMediaItems is deprecated and now forwards to setPlaylist, so prefer setPlaylist for new code.


Frequently asked questions

How do I play a list of videos?

Call player.setPlaylist(items, startIndex = 0) with a list of PlaylistItem values. Build each entry with PlaylistItem.fastPix("playback-id"), PlaylistItem.fastPix("playback-id", playbackToken = token) for signed playback, a PlaylistItem.fastPix { ... } block for full media-item options, or PlaylistItem.fromUrl(url) for a direct URL. The player plays the entries in order and advances by itself when one finishes.

How do I know when the playlist advances?

Add a PlaylistListener and implement onPlaylistItemChanged(index, item, reason). The reason is AUTO_ADVANCE when the current entry finished and the player moved on by itself, NAVIGATION when you called next, previous, or skipTo, PLAYLIST_SET when a new list was set, and PLAYLIST_EDITED when an edit moved the current entry to a new index.

Does adding items interrupt playback?

No. Editing the playlist keeps the current entry playing unless it is the one removed. Use addToPlaylist(items) to append, for example the next page of an endless feed, addToPlaylist(index, items) to insert, removeFromPlaylist(index) to remove, and clearPlaylist() to empty the list.

What happens to a playlist when I set a single item?

setMediaItem and setFastPixMediaItem play a single item and clear any playlist. setMediaItems is deprecated and forwards to setPlaylist, so use setPlaylist for lists of videos.


What’s next