Android Question Background music

Spright

Active Member
I've tried to search the forum for ways to play background music for games but I have not found any
information that has a simple demo on how to do that? Could you provide a simple demo example?

What are the guidelines regarding the acual musicformats? Should you use mp3 files? I read somewhere that .ogg files 96Khz was preferred.
Do you have to take care of incoming calls and pause / resume the music yourself or does Android do that?
 

Spright

Active Member
Many thanks! I include the smallest code that I could get working to play music with pause- and resume- functionality.
I am still wondering if there are any advantages to use Exoplayer or LibGDX inbuilt audio features instead? I don't need streaming but I do care about battery usage.

Are there any plans to add any lower level Audio DSP library? Like Oboe or maybe Bass?
 

Attachments

  • MediaPlayerCode.zip
    7.9 KB · Views: 189
Upvote 0

Spright

Active Member
I agree so far it seems to work great to play media files. I look at the API and I can't understand how to load several tracks at the beginning and have them ready for later track swapping, is this the correct handling of changing tracks with MediaPlayer? I ruled out to use several MediaPlayers but that is just my gut feeling that this would be the better option.

How would you swap tracks during runtime? Are there resources that I need to free up if I do it this way?

B4X:
Sub Process_Globals
    Dim MediaPlayer1 As MediaPlayer
End Sub

Sub Globals
    Dim counter As Initialize
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        MediaPlayer1.Initialize( )
        MediaPlayer1.Load(File.DirAssets, "track1.mp3") ' mid, mp3 or ogg file
    
    End If
End Sub

Sub Activity_Resume
    counter = counter + 1 ' Resume is called when starting the app as well so we better keep track on the number of resumes
    ' Change the music the first time that we're doing an actual resume
    If counter = 2 Then MediaPlayer1.Load(File.DirAssets, "track2.mp3")
    MediaPlayer1.Play
    Log ("Resume is now running and counter contains:")
    Log (counter)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If MediaPlayer1.IsPlaying Then MediaPlayer1.Pause
End Sub
 
Upvote 0

Spright

Active Member
Don't waste your time on activities. Switch to B4XPages.

Call MediaPlayer.Load to switch track.
I would love that but I use LibGDX as my main buildingblock for this game and I thought they would not work together well.
Is there an simple example how to use them together? Adding panels would help a lot.
 
Upvote 0
Top