Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Checking if a sound channel is playing - Gideros Forum

Checking if a sound channel is playing

petecpetec Member
edited February 2012 in General questions
Hi

I've written some code that checks if a sound channel's position is 0 or at the end of the sound file so that I can tell if the channel is playing or stopped, but wondered if I'd missed something easier? Is there anything like SoundChannel:isPlaying() or SoundChannel:isActive() that would simply return whether it is playing or not as true or false?

Thanks, Pete

Comments

  • atilimatilim Maintainer
    Currently there isn't such a function but SoundChannel dispatches Event.COMPLETE event when it finishes its playing.
    local channel = sound:play()
    channel:addEventListener(Event.COMPLETE, function(channel) channel.complete = true end, channel)
    I'll add SoundChannel:isPlaying() function with the next version.
  • atilimatilim Maintainer
    edited February 2012
    I forgot to tell that, it's better to do a nil check, on the returned SoundChannel object. http://www.giderosmobile.com/documentation/reference_manual.html#Sound:play
    local channel = sound:play()
    if channel ~= nil then
        channel:addEventListener(Event.COMPLETE, function(channel)
            channel.complete = true
        end, channel)
    end
  • Thanks, the isPlaying() function will be useful for me.

    Some of the things I do give spoken feedback and, if a user touches the thing that triggers the feedback a second time whilst the feedback is still playing from their first touch, I don't want a second instance of the sound file to play over the first one. So the complete listener doesn't do it for me in that case as I know the sound won't have finished!

    It's no great problem as I'm just getting the current channel position and checking if it is somewhere between 0 and the sound file's length and that tells me if it's playing or not. But an isPlaying() function would be handy.

    As ever, thanks for the quick and helpful reply.
    Pete
  • atilimatilim Maintainer
    But after sound:play() you know that the channel is playing until it dispatches complete event. I mean:
    local channel = sound:play()
    if channel ~= nil then
        channel.isPlaying = true
        channel:addEventListener(Event.COMPLETE, function(channel)
            channel.isPlaying = false
        end, channel)
    end
    But checking the position between 0 and end of sound is also totally valid. :)
  • Oh I see- I'm being rather dumb here!
Sign In or Register to comment.