SoundPool Question

walterf25

Expert
Licensed User
Longtime User
Hello All, i have this code on an activity, but the sound doesn't play when the activity is started.

B4X:
Activity.LoadLayout("Green1")
   sp.Initialize(1)
   sound = sp.Load(File.DirAssets, "ding.mp3")
   sp.Play(sound, 1, 1, 1, 0, 1)

but if i put the
sp.play(sound, 1, 1, 1, 0, 1)

inside a Sub Button1_click then it plays when i click the button, i'm confused why won't the sound play as soon as the activity starts?

Any thoughts?

Thanks
Walter
 

stevel05

Expert
Licensed User
Longtime User
Soundpool is not like Media Player in that when you issue the Load command, it decompresses the whole sound into memory before it can play. There is a related thread here with a couple of possible solutions, although if you choose the second one, I would use a timer rather than a loop to test if it's loaded.

If this introduces too much delay, I would suggest using media player for this purpose.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Hello All, i have this code on an activity, but the sound doesn't play when the activity is started.

B4X:
Activity.LoadLayout("Green1")
   sp.Initialize(1)
   sound = sp.Load(File.DirAssets, "ding.mp3")
   sp.Play(sound, 1, 1, 1, 0, 1)

but if i put the


inside a Sub Button1_click then it plays when i click the button, i'm confused why won't the sound play as soon as the activity starts?

Any thoughts?

Thanks
Walter

Don't put sp.Play in Activity_Create. It's not the place for it. Move it in Activity_Resume and that should work.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
soundpool issue

Hi all, thanks for all the suggestions, @informatix, i actually tried putting the
command in the Activity_Resume but in did not work either. I finally got it working this way.

I had to initialize the sp variable in the Sub_Globals and load the .mp3 files from the main activity, then when i call them from the other two activities like this
called from Green1 activity
B4X:
Main.sp.Play(Main.loadid, 1, 1, 1, 0, 1)
and this called from Red1 Activity
B4X:
Main.sp1.Play(Main.loadid1, 1, 1, 1, 0, 1)

and this workd good, i'm not sure why but it works, :sign0060:

Thanks all for your help!
 
Upvote 0
Top