Android Question [Solved] - Using MediaPlayer to queue up and play sound files just like ExoPlayer

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

Can you tell me if it's possible to queue up and play the sound files in MediaPlayer from a list object just like you can do with the ExoPlayer? I'm asking because we had to change our apps that play sounds from the ExoPlayer to the MediaPlayer because customers are updating their Samsung phones to the current version of Android version 11 which we found the hard way that the Samsung version of Android 11 does not play nicely with the ExoPlayer. It has something to do with the new way Samsung is handling battery optimising. The customers are all reporting their apps were randomly not running when their screens are turned off and also when they unplug their phones from the charger. Our solution in most of the apps was to use MediaPlayer because for some reason unknown to us is not affected by that version of Android. One of our apps does need to play 5 different sound files one after the other which was previously working with the ExoPlayer. I looked throughout the posts and did not see a code sample showing how to queue up the sound files to play on after the other like ExoPlayer can do it.

For example, I would like to add .CreateFileSource but MediaPlayer doesn't have that one as a choice.:
lstPlayerSources.Add(MediaPlayerForTicking.CreateFileSource(File.DirAssets, "chime.mp3"))

Thanks.
 
Last edited:

rleiman

Well-Known Member
Licensed User
Longtime User
You will need to manage such a queue yourself. Add the file names to a list and play the next file each time the previous one completes.
I was planning that as a worst case scenario in case MediaPlayer didn't have the same ability to queue and play them in order.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
For those who want to see how I did it, here's the sub routines I added to get the loaded list to play each sound file one after the other.

Sample coding of how I added a sound file name into the list.:
lstPlayerSources.Add("chime.mp3")

I call this PlaySoundFilesInTheList after I finish adding a string with the sound file names into the list:
Sub PlaySoundFilesInTheList

    MediaPlayerForChimes.Load(File.DirAssets, lstPlayerSources.Get(0))
    MediaPlayerForChimes.SetVolume( kvs.Get("MasterVolume"), kvs.Get("MasterVolume"))
    MediaPlayerForChimes.Play
End Sub

Sub MediaPlayerForChimes_Complete
    
    lstPlayerSources.RemoveAt(0)
    
    If lstPlayerSources.Size > 0 Then
        PlaySoundFilesInTheList
    End If
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
B4X:
Do While lstPlayerSources.Size > 0
MediaPlayerForChimes.Load(File.DirAssets, lstPlayerSources.Get(0))
MediaPlayerForChimes.SetVolume( kvs.Get("MasterVolume"), kvs.Get("MasterVolume"))
    MediaPlayerForChimes.Play
lstPlayerSources.RemoveAt(0)
Wait For MediaPlayerForChimes_Complete
Loop
Wow! Only 1 sub routine needed! That's why you are the master of B4X. šŸ˜Š
 
Upvote 0
Top