Android Question ExoPlayer: how to dynamically append/remove files to the playlist ?

Caps64

Member
I need to update the playlist of the ExoPlayer to append new media files as they arrive from a download service, without interrupting the currently playing track. To do this, I append each new file to the playlist of sources:
B4X:
PlayerPlayList.Add(Player.CreateFileSource(dir, f))
and then I call the:
B4X:
Player.Prepare(Player.CreateListSource(PlayerPlayList))
The problem is that after call Player.Prepare(), the playback of the current track is stopped and the current track is changed to the first track of the updated playlist.

I read in the documentation of the java ExoPlayer object that:
"It’s possible to dynamically modify a playlist by adding, removing and moving MediaSources within a ConcatenatingMediaSource. This can be done both before and during playback by calling the corresponding ConcatenatingMediaSource methods. The player automatically handles modifications during playback in the correct way. For example if the currently playing MediaSource is moved, playback is not interrupted and its new successor will be played upon completion. If the currently playing MediaSource is removed, the player will automatically move to playing the first remaining successor, or transition to the ended state if no such successor exists."

How is it possibile to implement this functionality in B4A, "extending" in same way the B4A ExoPlayer library?
I would like to implement a direct access to the ExoPlayer methods to dynamically modify the playlist, something like that Erel showed in the code:
B4X:
Sub Class_Globals
    Public Player As SimpleExoPlayer
    Private jPlayer As JavaObject
    ...
End Sub

Public Sub Initialize
    Player.Initialize("PLAYER")
    jPlayer = Player
    jPlayer = jPlayer.GetField("player")
    ...
End Sub

Private Sub jgetWindowCount As Int
    Dim timeline As JavaObject = jPlayer.RunMethod("getCurrentTimeline", Null)
    Return timeline.RunMethod("getWindowCount", Null)
End Sub
 

ac9ts

Active Member
Licensed User
Longtime User
I would suggest keeping track of the playlist outside of Exoplayer.

Basically, create a playlist of media using a List, Map, Array, whatever that you can manipulate as needed. This is the playlist that you'll add, subtract, re-arrange as required by your app.

In your Exoplayer service, play the first item using the single item CreateXXXSource (NOT CreateListSource).

In the Player_Complete sub, play the next item using the single item CreateXXXSource.

Doing this, the media keeps playing while the playlist get manipulated. This is a greatly simplified way of what I do in one of my apps.
 
Upvote 0

Caps64

Member
Thank you for your suggestion. Actually I already have a Map to handle the playlist, and I agree that using CreateFileSource would semplify the things (maybe I will do so, if I cannot access the native methods of the java ExoPlayer object).
I see also two avdantages using CreateListSource, instead of preparing each single file when you need to play it:
- the transition between the current track and the next one is faster: in my project this is fine because I have to play a continuous recording cut into clips sequences
- you can use the SimpleExoPlayerView widget to control the player.

Then, before giving up, I would like to learn more about accessing and using the java object's methods in B4A. For example, I used the code:
B4X:
Private Sub jsetCurrentWindowIndex(index As Int)
    Dim position As Long = 0
    jPlayer.RunMethod("seekTo", Array(index, position))
End Sub
to change the track in the javaobject playlist, and it works fine.
Then I tried this code to get the number of items in the playlist::
B4X:
Private Sub jgetWindowCount As Int
    Dim timeline As JavaObject = jPlayer.RunMethod("getCurrentTimeline", Null)
    Return timeline.RunMethod("getWindowCount", Null)
End Sub
but it does not work, it always return 0...
Then I tried to trap all the events of the java object:
B4X:
    Dim jobj As JavaObject = Player 
    Dim event As Object = jobj.CreateEventFromUI("com.google.android.exoplayer2.Player$EventListener", "JPLAYER", False)
    jobj.GetFieldJO("player").RunMethod("addListener", Array(event))
and it seems to work, even though I'm still testing the reliability (it seems that some event escapes).
But I cannot find a complete reference of the java ExoPlayer and its methods.
 
Upvote 0
Top