Assign channels

I’m triggering  an audio.play() event inside a startDrag() method, inside the “ended or cancelled” conditional phase, and specifying a channel, like this-

[lua]releaseSoundplay = audio.play(releaseSound, {channel=8,loop=0})

audio.setVolume( 0.1, { channel=8 } ) – set the volume on channel 8[/lua]

I need to specify channels in order to adjust volumes of individual sounds.

I find that in the simulator, if I trigger startDrag() events in quick succession, I get errors along the following line-

[lua]Warning: audio error: Requested channel (8) is in use[/lua]

I suppose since channel assignment is being set on each phase conditional, this is causing the errors.

My questions are-

Am I correct in this?

If so, is it possible to set the channel outside of the audio.play() event so the startDrag() phase doesn’t trigger the channel assignment each time?

Any other approaches, tips, suggestions to solving this would be greatly appreciated.

Thanks.

Hi @tom12,

A better approach would be to play the audio file on any available channel (automatic behavior), use the number that the audio.play() function returns (it will return the channel number it picks), then assign a volume to that channel. A further fail-safe would be: confirm that the channel number returned is not 0 (sound can’t be played for whatever reason), because if you assign 0 to the setVolume() API, Corona will interpret that as setting that volume on all channels.

Hope this helps,

Brent

Thanks for the tips, Brent. However, I’m still having trouble figuring out how to return the assigned channel number- If I use auto assignment, and my code looks like this-

audioSoundPlay = audio.play(audioSound, {loop=1})

How do I return audioSoundPlay’s channel? Is there a function in the API for that? I’ve looked all over the documentation, tutorials, but haven’t seen anything like that. I’d like to be able to access it along the lines of “return audioSoundPlay.channel” (as pseudocode) but I don’t think that’s quite the lua/corona way. I tried assigning a variable to the channel number, but I just get nil back when I try to return the value. 

Any help would be appreciated.

Hi @tom12,

The way Lua/Corona works, in this case…

[lua]

local audioSoundPlay = audio.play( audioSound, {loop=1} )

[/lua]

…the variable “audioSoundPlay” will actually be the channel number it plays on, because Corona returns that channel number to the variable. Then, setting the volume on that variable should work as you expect:

[lua]

audio.setVolume( 0.1, { channel=audioSoundPlay } )

[/lua]

So, give this a test and let me know if it works.

Best regards,

Brent

Hi, Brent- Sorry it’s taken me so long to get back to you- your advice worked.

Thanks! 

Hi @tom12,

A better approach would be to play the audio file on any available channel (automatic behavior), use the number that the audio.play() function returns (it will return the channel number it picks), then assign a volume to that channel. A further fail-safe would be: confirm that the channel number returned is not 0 (sound can’t be played for whatever reason), because if you assign 0 to the setVolume() API, Corona will interpret that as setting that volume on all channels.

Hope this helps,

Brent

Thanks for the tips, Brent. However, I’m still having trouble figuring out how to return the assigned channel number- If I use auto assignment, and my code looks like this-

audioSoundPlay = audio.play(audioSound, {loop=1})

How do I return audioSoundPlay’s channel? Is there a function in the API for that? I’ve looked all over the documentation, tutorials, but haven’t seen anything like that. I’d like to be able to access it along the lines of “return audioSoundPlay.channel” (as pseudocode) but I don’t think that’s quite the lua/corona way. I tried assigning a variable to the channel number, but I just get nil back when I try to return the value. 

Any help would be appreciated.

Hi @tom12,

The way Lua/Corona works, in this case…

[lua]

local audioSoundPlay = audio.play( audioSound, {loop=1} )

[/lua]

…the variable “audioSoundPlay” will actually be the channel number it plays on, because Corona returns that channel number to the variable. Then, setting the volume on that variable should work as you expect:

[lua]

audio.setVolume( 0.1, { channel=audioSoundPlay } )

[/lua]

So, give this a test and let me know if it works.

Best regards,

Brent

Hi, Brent- Sorry it’s taken me so long to get back to you- your advice worked.

Thanks!