Audio.stop not working properly, play the next audio

hi, 

I have a big problem with audio.stop()

when I call it , the next audio gets call

I want to stop all audios before changing scene

but its not working the next audio starts playing in the next scene.

here is my code so I can explain better.

here I start the first audio file.

function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then narrativeChannel = audio.play( audio1, { channel=5, onComplete=NarrationStart } ) elseif phase == "did" then end end

NarrationStart = function ( ) narrativeTimer = timer.performWithDelay( 100,function ( ) catBubble.isVisible = true catText.isVisible = true transition.from(catText, {time = 400,alpha = 0,y = catText.y - 15, onComplete = function () narrativeChannel = audio.play( audio2, { channel=5}) end })end, 1 )

and here is where I stop it 

function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then if(narrativeChannel ~= nil) then audio.stop( ) print("stoped") --gets call end transition.cancel() if(narrativeTimer ~= nil)then timer.cancel( narrativeTimer ) end elseif phase == "did" then -- Called when the scene is now off screen end end

when I change scene while the first audio1 is play, the first audio gets stoped but right after that the audio2 start…

I dont know why is that happening.

please any ideas or coments how to fix this.

I tried audio.stop() , audio.stop(channelUsed)

and nothing keeps playing the next audio I dont want that.

thanks in advance.

Info: scene Manager: Composer

Corona build : Version 2014.2463 (2014.10.14)

Hi @madTeam,

It’s a bit hard to follow your game flow here, with all of the various timers and transitions and onComplete events. There’s no reason why calling “audio.stop()” with no parameter won’t stop all audio, so my guess is that you’re somehow re-starting these track(s) because a timer is firing again, or a transition onComplete is happening. I suggest that you put in more print() statements and “slow down” your transitions and timers just for testing, so you can carefully monitor exactly what’s happening and when.

Best regards,

Brent

hi @Brent Sorrentino,

thanks for your reply.

I havent been able to fix this problem I know how the flow is going I already did step by step debug.

the main problem is this:
when I call audio.stop() all audios are stopped, but right after that the next audio on the transitions gets calls even tho when I cancel all transitions all timers and all audios… 

for instance a quick example:

audio1  transition onComplete calls audio2

thats the normal flow

when I stop audio1, and all transitions and timer and everything

audio2 gets call anyways… transition.cancel wont stop the onComplete function…

I did a temporary fix(not the right way)

make all the remaining

audios = nil

in order to just get the Warning: audio error: Can’t play because data is NULL

and game continue as intend without the wrong audios getting played.

this is not the right fix because the problem still there.

I hope you could help me with the main problem. 

thanks,

Hi @madTeam,

I’m still suspicious that your multi-nested anonymous functions and timers and onCompletes is causing some issue. You actually have two anonymous functions nested within “narrationStart()” and this could be the culprit. Can you try to pull those out and simplify this process a little? Make sure that you’re not accidentally calling the audio2 clip twice by queueing it on both a timer and a transition onComplete.

Best regards,

Brent

Hi I read in another forum this

 

"the transition.cancel() does indeed not cancel onComplete.

You could add a varable like isAudioOn and put it to false when you stop the audio. That way you can check for that variable inside your onComplete as such."

I thought that the transition.cancel also cancel the onComplete function but apparently does not.

I did what they suggest to manage this problem. I still think that the transition.cancel should cancel everything… included the onComplete function.

thanks anyways.

 

Quick update:

I just read this on corona build notes

 

  • Transition - transition.cancel cancels subsequent calls to transition.to - casenum 34170
  • Transition - transition cancel bug - casenum 34182

Release notes for build 2014.2481

im gonna update my build since I was using 2463

that was actually a bug that they fix now.

thanks,

Hi @madTeam,

It’s a bit hard to follow your game flow here, with all of the various timers and transitions and onComplete events. There’s no reason why calling “audio.stop()” with no parameter won’t stop all audio, so my guess is that you’re somehow re-starting these track(s) because a timer is firing again, or a transition onComplete is happening. I suggest that you put in more print() statements and “slow down” your transitions and timers just for testing, so you can carefully monitor exactly what’s happening and when.

Best regards,

Brent

hi @Brent Sorrentino,

thanks for your reply.

I havent been able to fix this problem I know how the flow is going I already did step by step debug.

the main problem is this:
when I call audio.stop() all audios are stopped, but right after that the next audio on the transitions gets calls even tho when I cancel all transitions all timers and all audios… 

for instance a quick example:

audio1  transition onComplete calls audio2

thats the normal flow

when I stop audio1, and all transitions and timer and everything

audio2 gets call anyways… transition.cancel wont stop the onComplete function…

I did a temporary fix(not the right way)

make all the remaining

audios = nil

in order to just get the Warning: audio error: Can’t play because data is NULL

and game continue as intend without the wrong audios getting played.

this is not the right fix because the problem still there.

I hope you could help me with the main problem. 

thanks,

Hi @madTeam,

I’m still suspicious that your multi-nested anonymous functions and timers and onCompletes is causing some issue. You actually have two anonymous functions nested within “narrationStart()” and this could be the culprit. Can you try to pull those out and simplify this process a little? Make sure that you’re not accidentally calling the audio2 clip twice by queueing it on both a timer and a transition onComplete.

Best regards,

Brent

Hi I read in another forum this

 

"the transition.cancel() does indeed not cancel onComplete.

You could add a varable like isAudioOn and put it to false when you stop the audio. That way you can check for that variable inside your onComplete as such."

I thought that the transition.cancel also cancel the onComplete function but apparently does not.

I did what they suggest to manage this problem. I still think that the transition.cancel should cancel everything… included the onComplete function.

thanks anyways.

 

Quick update:

I just read this on corona build notes

 

  • Transition - transition.cancel cancels subsequent calls to transition.to - casenum 34170
  • Transition - transition cancel bug - casenum 34182

Release notes for build 2014.2481

im gonna update my build since I was using 2463

that was actually a bug that they fix now.

thanks,