Sounds not always being played

Hi,

I am doing a soundboard app and when pressing the buttons the sound doesn’t always play and yet other times it plays every time and I can’t work out why.

This is my code for setting up the button -

 local btnAaaooww = widget.newButton( { onEvent = btnAaaooww\_Click, defaultFile = "charlieimages/aaaooww-button.png", overFile = "charlieimages/aaaooww-button-over.png", width = 360, height = 213, } ) btnAaaooww.anchorX = 0.5 btnAaaooww.x = screenLeft + (btnAaaooww.height / 2) btnAaaooww.y = screenBottom - (btnAaaooww.width / 2) btnAaaooww:rotate( -90 )

I am rotating everything as the app is landscape but the soundboard is portrait.

This is the event code

local function btnAaaooww\_Click( event ) if ( "ended" == event.phase ) then currentSound = audio.loadSound( audioPath .. "Aaaoow.aif" ) currentPlaying = audio.play(currentSound) end return true end

Any ideas?

Thanks

Dave

Hi Dave,

How big is this sound file you’re loading? Generally, I don’t recommend loading sounds “on demand”… instead, pre-load them into memory and then play them on demand. By doing both of these actions in consecutive lines, it’s very likely that the sound isn’t always ready to play (not yet loaded) by the time you call “audio.play()” on the very next line.

Best regards,

Brent

I think Brent has hit the head on the nail. You might want to consider audio.loadStream(), but another possibility is that if the player rolls off of the button while the touch event is still happening, you will get an “cancelled” phase and not an “ended” phase. You might want to consider triggering on the “began” phase too.

Rob

Cheers guys, will give both ideas a go and report back.

EDIT: Sorry forgot to add sound files are from couple hundred k to around 1.5mb

Thanks,

Dave

You should definitely be using audio.loadStream() on larger sounds. It allows them to start playing while they are still being loaded and decoded.

Rob

Hi,

Changed to loadStream, will report back when tested.

I changed the events to “began” but the buttons stopped working then, so changed back to “ended”?

Thanks,

Anyway to check if a sound has completed loading? There is an onComplete for checking if a sound has finished playing so it would be strange if there is no way to check if it has finished loading.

When audio.loadSound() completes, the sound has fully loaded and then your next line of code will execute. This is one of the reasons it should not be used on larger sounds as it can make the app feel sluggish.  In theory, you don’t need to know when audio.loadStream() finishes since it’s immediately available to begin playing. 

Rob

Thanks Rob, reason I was asking is I have tonnes of small and some large sound files in several arrays I wish to load at app start and wanted to do a loading progress bar (as they are .wav format it can take up to 10 seconds to load them all).

Of course, loading sounds in a normal loop the screen won’t refresh until the loop is finished hence you never see the bar progress.

So I thought if i use a timer delay instead of a loop that might work, but the issue now is that there is no way to know if the sounds have completed loading.

So does anyone have a proper way of doing a loader screen while loading in many large sounds?

Should I use a listener for each sound I am loading to check if it is no longer null (which then should indicate it has completed loading)?

I feel like that is a lot of work for something that should be quite simple. I’m assuming i’m missing something here?

Cheers

I understand, but the point is that audio.loadStream() should not add to loading time in any significant manner, where audio.loadSound() will slow loading time.

Rob

Forgot to report back :frowning:

loadStream seem to do the trick and app got released fine alongside the album and was advertised in the album sleeve which was nice.

Thanks for the help.

Hi Dave,

How big is this sound file you’re loading? Generally, I don’t recommend loading sounds “on demand”… instead, pre-load them into memory and then play them on demand. By doing both of these actions in consecutive lines, it’s very likely that the sound isn’t always ready to play (not yet loaded) by the time you call “audio.play()” on the very next line.

Best regards,

Brent

I think Brent has hit the head on the nail. You might want to consider audio.loadStream(), but another possibility is that if the player rolls off of the button while the touch event is still happening, you will get an “cancelled” phase and not an “ended” phase. You might want to consider triggering on the “began” phase too.

Rob

Cheers guys, will give both ideas a go and report back.

EDIT: Sorry forgot to add sound files are from couple hundred k to around 1.5mb

Thanks,

Dave

You should definitely be using audio.loadStream() on larger sounds. It allows them to start playing while they are still being loaded and decoded.

Rob

Hi,

Changed to loadStream, will report back when tested.

I changed the events to “began” but the buttons stopped working then, so changed back to “ended”?

Thanks,

Anyway to check if a sound has completed loading? There is an onComplete for checking if a sound has finished playing so it would be strange if there is no way to check if it has finished loading.

When audio.loadSound() completes, the sound has fully loaded and then your next line of code will execute. This is one of the reasons it should not be used on larger sounds as it can make the app feel sluggish.  In theory, you don’t need to know when audio.loadStream() finishes since it’s immediately available to begin playing. 

Rob

Thanks Rob, reason I was asking is I have tonnes of small and some large sound files in several arrays I wish to load at app start and wanted to do a loading progress bar (as they are .wav format it can take up to 10 seconds to load them all).

Of course, loading sounds in a normal loop the screen won’t refresh until the loop is finished hence you never see the bar progress.

So I thought if i use a timer delay instead of a loop that might work, but the issue now is that there is no way to know if the sounds have completed loading.

So does anyone have a proper way of doing a loader screen while loading in many large sounds?

Should I use a listener for each sound I am loading to check if it is no longer null (which then should indicate it has completed loading)?

I feel like that is a lot of work for something that should be quite simple. I’m assuming i’m missing something here?

Cheers

I understand, but the point is that audio.loadStream() should not add to loading time in any significant manner, where audio.loadSound() will slow loading time.

Rob