Audio plays on channel even tho requested not to (returns Warning: audio error: Requested channel (4) is in use)

Here is the problem I am having with playing audio on a designated or available channels:

  1. The audio below is for a slider button that plays a click sound as the slider is being moved, so it is called quickly and repeatedly

  2. I’ve tried (and would prefer) to use “availableChannel” but that results in horrible and distorted audio

  3. The code below checks to make sure audio is not playing before it plays - it plays the audio but it still returns error messages that read “Warning: audio error: Requested channel (4) is in use.”

[lua]

if audio.isChannelPlaying( 5 ) == false then
     audio.play(chSound, { channel=5 }  )
end

[/lua]

Thanks for any help!

You’d be better off finding a free channel.

https://docs.coronalabs.com/api/library/audio/findFreeChannel.html

Also, just curious, but have you experiemented to see what these testers say?

audio.isChannelActive() << this might be better if you decide to not just get free channel

audio.isChannelPaused()

audio.isChannelPlaying() << you used this

Thanks for the tips Roaminggamer, I have tried the isChannelActive and Paused, but not the findFreeChannel. I’ll try that and check out the testers link.

I reserve the first 5 channels for music (I mix multiple background sounds and assign different volume levels to each) and then use

local availableChannel = audio.findFreeChannel(6)

To find a free channel for overlaying sound effects.

I used a “click” sound for a slider but while using the findFreeChannel didn’t give an error, the click still sounded distorted and like metal, so I changed the code to pick up only the increments - that fixed it - though I have a feeling someone would already have come up with a better solution - or at least more efficient code.

[lua]    – ON SLIDER – set up audio so it only plays one tick at a time
    local startPos
    local prevPos

    local function onSlider( event )

        local value = event.value
        local roundvalue = math.round(value)

        if “press” == event.phase then

            local roundvalue1 = math.round(value)
            startPos = roundvalue1
            prevPos = roundvalue1

        elseif “moved” == event.phase then

            local roundvalue2 = math.round(value)
            local delta = roundvalue2 - prevPos
            prevPos = roundvalue2
            --print ("delta = " … delta … "// prevPos = " … prevPos)

            if delta==1 or delta < 0 then
                local availableChannel = audio.findFreeChannel()
                audio.play( chSound, { channel=availableChannel } )
            end
        end
        return true
    end
[/lua]

>> so it is called quickly and repeatedly

i’d guess TOO quickly and TOO repeatedly (on each move event) leading to:

  • flanging, from short delays between the sounds (probably the “metallic” portion of your problem)

  • clipping, from just too much audio output summing up (probably the “distorted” portion of your problem)

You’d be better off finding a free channel.

https://docs.coronalabs.com/api/library/audio/findFreeChannel.html

Also, just curious, but have you experiemented to see what these testers say?

audio.isChannelActive() << this might be better if you decide to not just get free channel

audio.isChannelPaused()

audio.isChannelPlaying() << you used this

Thanks for the tips Roaminggamer, I have tried the isChannelActive and Paused, but not the findFreeChannel. I’ll try that and check out the testers link.

I reserve the first 5 channels for music (I mix multiple background sounds and assign different volume levels to each) and then use

local availableChannel = audio.findFreeChannel(6)

To find a free channel for overlaying sound effects.

I used a “click” sound for a slider but while using the findFreeChannel didn’t give an error, the click still sounded distorted and like metal, so I changed the code to pick up only the increments - that fixed it - though I have a feeling someone would already have come up with a better solution - or at least more efficient code.

[lua]    – ON SLIDER – set up audio so it only plays one tick at a time
    local startPos
    local prevPos

    local function onSlider( event )

        local value = event.value
        local roundvalue = math.round(value)

        if “press” == event.phase then

            local roundvalue1 = math.round(value)
            startPos = roundvalue1
            prevPos = roundvalue1

        elseif “moved” == event.phase then

            local roundvalue2 = math.round(value)
            local delta = roundvalue2 - prevPos
            prevPos = roundvalue2
            --print ("delta = " … delta … "// prevPos = " … prevPos)

            if delta==1 or delta < 0 then
                local availableChannel = audio.findFreeChannel()
                audio.play( chSound, { channel=availableChannel } )
            end
        end
        return true
    end
[/lua]

>> so it is called quickly and repeatedly

i’d guess TOO quickly and TOO repeatedly (on each move event) leading to:

  • flanging, from short delays between the sounds (probably the “metallic” portion of your problem)

  • clipping, from just too much audio output summing up (probably the “distorted” portion of your problem)