Android Question Loop backgroundmusic without pause?

Quillok

Member
Licensed User
Longtime User
Hey, me again

Today's problem: I use Mediaplayer for Backgroundmusic. I want it to loop the soundfile (*.ogg)
It loops it, but with a short pause at the end/new beginning.

The soundfile itself has no pause, in audacity it loops perfectly, but in my app there is always a pause.

I've tried it with loop = true and because this don't work, with the complete event to start it again... both did not work :/

Is this even possible with Mediaplayer? For Soundpool the file is to long... Are there some other tricks to loop it perfectly?

Thanks in advance
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It will probably be easier to convert the file to a mp3 and repeat it 10 times.

Put this code in the starter service:
B4X:
Sub Process_Globals
    Private MediaPlayers As List
End Sub

Sub Service_Create
    MediaPlayers = Array(CreateMediaPlayer, CreateMediaPlayer)
    Play
End Sub

Private Sub Play
    For Each mp As MediaPlayer In MediaPlayers
        mp.Load(File.DirAssets, "1.ogg")
    Next
    Dim index As Int
    Do While True
        Dim mp1 As MediaPlayer = MediaPlayers.Get(index Mod 2)
        Dim mp2 As MediaPlayer = MediaPlayers.Get((index + 1) Mod 2)
        index = index + 1
        mp1.Play
        mp2.Stop
        mp2.Load(File.DirAssets, "1.ogg")
        Sleep(29970)
        Log("complete")
    Loop
End Sub

Private Sub CreateMediaPlayer As MediaPlayer
    Dim mp As MediaPlayer
    mp.Initialize2("mp")
    Return mp
End Sub

It needs some fine tuning.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…