I am trying to wait for ExoPlayer to finish playing a playlist and then append a song to the end of the playlist and starting the playlist from that added song.
I tried doing this, but it does not work:
override fun onPlaybackStateChanged(playbackState: Int) {
super.onPlaybackStateChanged(playbackState)
if (playbackState == STATE_ENDED) {
playerHolder.getPlayer().addMediaItem(
playerHolder.getPlayer().currentMediaItem!!
)
playerHolder.getPlayer().play()
}
}
Nothing is played after that code is run.
The reason the code in the question does not work is because the playWhenReady
field of the player is true when in the callback, so play()
has no effect. A seek call is needed to start the playback. The following code does what was trying to be done.
override fun onPlaybackStateChanged(playbackState: Int) {
super.onPlaybackStateChanged(playbackState)
if (playbackState == STATE_ENDED) {
val player = playerHolder.getPlayer()
player.addMediaItem(
player.currentMediaItem!!
)
player.seekTo(
player.mediaItemCount - 1,
C.TIME_UNSET
)
}
}