Android Question SoundPool and CheckChange event

Eric H

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim sp As SoundPool
    Dim BrownNoiseId As Int
End Sub


Sub Globals
    Dim tbBrownNoise As ToggleButton
    Dim tbWhiteNoise As ToggleButton
End Sub


Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        sp.Initialize(1)
        BrownNoiseId = sp.Load(File.DirAssets, "brown.ogg")
    End If
    Activity.LoadLayout("main")
End Sub


Sub Activity_Resume

End Sub


Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
        sp.Stop(BrownNoiseId)
    End If
End Sub


Sub tbBrownNoise_CheckedChange(Checked As Boolean)
    If tbBrownNoise.Checked = True Then
        Log("brown noise on")
        sp.Play(BrownNoiseId, 1, 1, 1, -1, 1)
    Else
        Log("brown noise off")
        sp.Stop(BrownNoiseId)
    End If
End Sub

This sub is not working as expected: Sub tbBrownNoise_CheckedChange(Checked As Boolean)

brown.ogg is a 5 second audio file

The first time i click on the toggle button it starts playing the sound. When I click the toggle button again it doesn't always stop playing the sound. Further turning the toggle button ON or OFF will start or stop the sound sometimes, but not every time. Also, it doesn't seem to matter much if the button is ON or OFF, it will play or stop when it feels like it, no matter if it is ON or OFF.

Any idea why?

I would like to note that the Log() command works properly every time, so that tells me the event is firing as it should, I am just not sure why the sound isn't playing/stopping properly.

Thanks,
Eric H
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Can you zip and post your project so I can test it on my device with your layout and sound file?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
When you play a sound, it returns a play Id, that's what you need to use to turn it off. Presumably the first time it plays it returns 0, therefore it's the same as the load id and turns off, subsequent plays will have different play id's and don't turn off.

This code will work.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim sp As SoundPool
    Dim BrownNoiseId As Int
    Dim BrownNoisePlayId As Int
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
    Dim tbBrownNoise As ToggleButton
    Dim tbWhiteNoise As ToggleButton
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("main")
   
    If FirstTime Then
        sp.Initialize(1)
        BrownNoiseId = sp.Load(File.DirAssets, "brown.ogg")
    End If
   

   
   
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
'    If UserClosed = True Then
'        sp.Stop(BrownNoiseId)
'    End If
End Sub

Sub tbBrownNoise_CheckedChange(Checked As Boolean)

    If Checked = True Then
        Log("brown noise on")
        BrownNoisePlayId=sp.Play(BrownNoiseId, 1, 1, 1, -1, 1)
    Else
        Log("brown noise off")
        'If sp.Is
        sp.Stop(BrownNoisePlayId)
    End If
End Sub

Sub tbWhiteNoise_CheckedChange(Checked As Boolean)
       
End Sub
 
Upvote 0

Eric H

Active Member
Licensed User
Longtime User
I now understand what I was missing, thank you very much for the help.

I am still having a minor issue with the code you posted in that if I cycle it on and off too quickly it will not start playing even when the button is set to ON.

I've attached the new code in this zip file.

Thanks
Eric H
 

Attachments

  • newsoundpoolproject.zip
    107.4 KB · Views: 404
Last edited:
Upvote 0

Eric H

Active Member
Licensed User
Longtime User
I discovered the solution for my issue- the sound was too long (5 seconds) and SoundPool works best with shorter sounds. Since it was just brown noise on a loop, I changed the duration of my OGG clicp to 1 second and it works exactly as expected.

Thanks again for the help. Issue resolved. :)
 
Upvote 0
Top