Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Sound:LoopWithCount() - Cancel the delayed call? — Gideros Forum

Sound:LoopWithCount() - Cancel the delayed call?

MellsMells Guru
edited August 2013 in General questions
Hi,

I encounter a bug in my app and it could be related to the delayedCall in Sound:playWithLoopCount()

Here is the code in GiderosCodingEasy :
function Sound:playWithLoopCount(startTime, nbLoops)
	nbLoops = nbLoops or 1
	local soundLength = self:getLength()
 
	local channel = self:play(startTime, true) -- loop infinitely
 
	-- set looping as false after (loops - 0.5) count
	Timer.delayedCall((nbLoops - 0.5) * soundLength, function() channel:setLooping(false) end)
 
	return channel
end
@ar2rsawseen Is there a way to cancel that delayed call?
I think my bug is connected to it.
The strange thing is that it happens rarely (the channel plays 2 or 3ms of the sound and doesn't stop looping).
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps

Comments

  • @Mells I use Timer.stopAll() to cancel delayed timers. The only problem is that it stops any other timers as well so that might not be suitable for you. It would be handy to be able to stop individual timers - I seem to remember that might happen sometime.
  • @Mells Timer.delayedCall returns the instance of Timer class, so you can do something like:
    local timer = Timer.delayedCall((nbLoops - 0.5) * soundLength, function() channel:setLooping(false) end)
    timer:stop()
  • MellsMells Guru
    edited August 2013
    @petec
    Timer.stopAll() would be useful, but I need to stop a specific timer in this case as you guessed.

    @ar2rsawseen
    Thanks.

    1. Do you think this is something that should be modified at the level of the implementation in GiderosCodingEasy?
    I feel the ability to cancel the call is important.

    Ex : my sound is looping. I change scene and want to stop the sound, remove it from the scene, nil it, cancel the call.
    It would be really convenient to have it as a property of sound.
    Or maybe return it with the channel?
    return channel, timer
    I am asking because I don't want to modify the classes too much myself between the lib updates.

    2. Edit : solved, I had misunderstood your example.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • @ar2rsawseen
    I was referring to my (2) only.

    What do you think about modifying the implementation in GiderosCodingEasy to :
    function Sound:playWithLoopCount(startTime, nbLoops)
    	nbLoops = nbLoops or 1
    	local soundLength = self:getLength()
     
    	local channel = self:play(startTime, true) -- loop infinitely
     
    	-- set looping as false after (loops - 0.5) count
    	local timer = Timer.delayedCall((nbLoops - 0.5) * soundLength, function() channel:setLooping(false) end)
     
    	return channel, timer
    end
    so that getting the timer is available but optional?
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Maybe, or maybe implement it internally as in, save timer as internal property and inside stop method check if there is a timer, then also stop it, would that work for you? :)
  • MellsMells Guru
    edited August 2013
    Both would work, I guess i like the ability to name the timer how I wanti don't know about others though because the need hasn't been expressed until now.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
Sign In or Register to comment.