Android Question Soundpool doesn't play

Kevin Moloney

Member
Licensed User
Hi. I'm debugging using my cell. I want to play a simple sound, but this is just getting me silence:

B4X:
    Dim LoadID As Int
    Dim NewLoad As Int
    
    LoadID=sp.Load(File.DirAssets,"Ring06.wav")
    'MsgboxAsync(LoadID,"LoadID")
    
    NewLoad=sp.Play(LoadID,1,1,1,1,1)
    'MsgboxAsync(NewLoad,"NewLoad")

Note the msgbox lines are commented out.

I tried soundpool in a different app and it seemed to work when I had msgboxes but stopped when I didn't.

Thoughts?
 

Kevin Moloney

Member
Licensed User
Note: I've confirmed that Ringo6.wav is in DirAssets. This is the entire code:
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Public sp As SoundPool
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
    sp.Initialize(1)
    
    Dim LoadID As Int
    Dim NewLoad As Int
    
    LoadID=sp.Load(File.DirAssets,"Ring06.wav")
    'MsgboxAsync(LoadID,"LoadID")
    
    NewLoad=sp.Play(LoadID,1,1,1,1,1)
    'MsgboxAsync(NewLoad,"NewLoad")
    
End Sub
 
Upvote 0

Kevin Moloney

Member
Licensed User
Note: I've confirmed that Ringo6.wav is in DirAssets. This is the entire code:
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Public sp As SoundPool
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
    sp.Initialize(1)
   
    Dim LoadID As Int
    Dim NewLoad As Int
   
    LoadID=sp.Load(File.DirAssets,"Ring06.wav")
    'MsgboxAsync(LoadID,"LoadID")
   
    NewLoad=sp.Play(LoadID,1,1,1,1,1)
    'MsgboxAsync(NewLoad,"NewLoad")
   
End Sub
Odd ... it works with the msgbox, but doesnt when the msgbox is commented out. When that happens, sp.Play returns 0. sp.Load returns 1.

B4X:
    LoadId=sp.Load(File.DirAssets,"Ring06.wav")
    Msgbox(LoadId,"LoadId when Loading")
    Log("LoadID is :" & LoadId)
    
    NewLoad=sp.Play(LoadId,1,1,1,1,1)
    Log("NewLoad is: " & NewLoad)
 
Upvote 0

Kevin Moloney

Member
Licensed User
Moving sp.Load to Activity_Create doesn't solve the problem. With the msgbox, the logs return a matching positve number for both sp.Load and sp,Play. When the msgbox is commented out, log returns a positive number for sp.Load, but always 0 for sp.Play.
 
Upvote 0

Kevin Moloney

Member
Licensed User
This is the same app I was trying to get sound out of. The sound portion is at the bottom of the program. If the msgbox is commented out, I get no sound, but it works with the msgbox. Thanks for your help!! :)
 

Attachments

  • Scramble v1.1.zip
    215.3 KB · Views: 185
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
If I recall correctly (from an app I made years ago), sound files loading is not immediate. It can take perhaps a few miliseconds, probably depending on the file size.
That's the reason why loading and immediately playing won't work in some cases, but loading + msgbox or even loading at Service_Start before the activity is created, will work. At least it is how I understood it.

If there aren't too many sounds to be played, I'd simply load them all at start, and make sure that a minimum time has passed before playing them (I guess that Sleep(100) should be more than enough).
 
Upvote 0

Kevin Moloney

Member
Licensed User
If I recall correctly (from an app I made years ago), sound files loading is not immediate. It can take perhaps a few miliseconds, probably depending on the file size.
That's the reason why loading and immediately playing won't work in some cases, but loading + msgbox or even loading at Service_Start before the activity is created, will work. At least it is how I understood it.

If there aren't too many sounds to be played, I'd simply load them all at start, and make sure that a minimum time has passed before playing them (I guess that Sleep(100) should be more than enough).
Jordi ... I tried sleep 5000 and it still didn't work, but using the starter service did.
 
Upvote 0

Kevin Moloney

Member
Licensed User
[...I wonder if this has something to do with wav format?] Nope ... I substituted a 2-second ogg file and added sleep 5000 after sp.Load. Still didn't play without the msgbox. Is this going to remain an anomaly?
 
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Well, seems that 2 things are needed
  • According to HERE, SoundPool should be declared in Process Globals (I guess Starter's Globals are also such).
  • Depending on the file size/format, it will need a small delay.

Check the attached project: sp is declared in Main module's Process_Globals --> Without the msgBox, it won't work. With a small Sleep(100) delay, it will work.
 

Attachments

  • Scramble v1.1_JCP.zip
    215.3 KB · Views: 220
Upvote 0

walt61

Active Member
Licensed User
Longtime User
If I recall correctly (from an app I made years ago), sound files loading is not immediate. It can take perhaps a few miliseconds, probably depending on the file size.
That's the reason why loading and immediately playing won't work in some cases, but loading + msgbox or even loading at Service_Start before the activity is created, will work. At least it is how I understood it.

If there aren't too many sounds to be played, I'd simply load them all at start, and make sure that a minimum time has passed before playing them (I guess that Sleep(100) should be more than enough).
That's what I thought too (but wasn't sure): that there would be some async aspect to loading the file.
 
Upvote 0
Top