Problem While Playing Fx

Hi, 

 

I have a problem whenever I play more than one sound in my game, and I can’t hear some of them. 
I have background music playing and I was afraid that there wasn’t enough free channels for me to play, but after looking in the corona api I saw that I had more than enough to do the task. (There would be the background music, and the max number of sounds that will play at the same time - excluding the bg music - would be like 3 or 4). 

 

I load all the sounds the normal way, using the audio library : 

 

 

 

local explosionSound = audio.loadSound("fx/bomb.mp3")  

 

 

and whenever I need to play them, I call a function to do so :

 

 

 

function playExplosionSound()     if (preference.getValue("fxIsOn")) then             audio.findFreeChannel()         audio.play(explosionSound)     end end  

 

For some reasons, some times the sound plays and some other doesn’t. I have tried changing the format of the audio files to .wav but the files were huge in size and Corona SDK kept complaining about it, so I changed it.

 

I thought that maybe I was trying to use the same channel as other assigned sound, and that’s why I added the call to that function.  

 

Anyone has had a problem like this before? :slight_smile:

 

The audio.findFreeChannel() function is supposed to return a value of the free channel, so it should best be used like this:

local availableChannel = audio.findFreeChannel() if availableChannel == 0 then print( "No available audio channels to play on at the moment." ) else audio.play( explosionSound, { channel = availableChannel } ) end

Hey BeyondtheTech, 

 

I’ve corrected the way I get an availableChannel but that was not the problem :frowning: . I have an option in the game for turning on or off the audio settings (sounds and music) and the weird thing is that when I have the background music disabled, and only the fx enabled, the sound effects work just fine!  I don’t see any errors in the way I declare the music background variables

 

 

 

local playingMusic local playingMusicChannel  

 

nor in the way I load the song and start playing it in an infinite loop: 

 

 

playingMusic = audio.loadStream("music/original.mp3") playingMusicChannel = audio.play( playingMusic, { channel=2, loops=-1,  }  )  

 

I just don’t see why having a song playing in the background can affect the way I play the other sounds. 

Have you tried reserving channel 1 for the music?

 

audio.reserveChannels( 1 ) ... playingMusic = audio.loadStream("music/original.mp3") audio.play( playingMusic, { channel=1, loops=-1 } )

 

 

That way, when you’re doing the audio.findFreeChannel() function, it will look only at channels 2-32, keeping it separate from any sound effects you’re playing.

Hi @paytopwn,

You shouldn’t need to explicitly call “audio.findFreeChannel()”. If you play a sound, and there’s a free channel, it will be played on that channel automatically.

 

If your audio isn’t playing “sometimes”, have you carefully checked your logic and condition here?

 

[lua]if (preference.getValue(“fxIsOn”))[/lua]

 

What is “preference.getValue”, some function you wrote? Is it properly scoped in all cases? Have you used the “print()” command to test that its condition is set and checked in every case? This might not be an issue of the audio engine, but rather your conditional code.

 

As @BeyondtheTech says, it’s good practice to reserve one channel for music and keep it reserved for music. I usually do this on channel 1 because it’s convenient. Also, I reserve a few more channels at the beginning, like 2, 3, and 4, just in case I need to (at some point) play another music track over my main music or “blend” between two tracks (unlikely I ever will do so, but I like keeping a few channels in reserve). That leaves me with channels 5-32 to play sound effects, and I just let the audio engine select the channels for me.

 

Sincerely,

Brent Sorrentino

 

Thanks BeyondtheTech!!! 

 

I have finally found what was wrong , I had a music playing in the main menu with the reserved channel 1, and I made a fade effect of 2.5 seconds before calling the game scene (which plays another music file, without closing the channel. That way, the first audio channel was open and for some reason if a sound was played through that channel, it wouldn’t play it. Is audio.dispose() the best way to free that channel ?

audio.dispose(), if I’m not mistaken, just frees the handle, or the memory holding the audio object.  If you’re only using that particular music file for the main menu, then, yes, it’s a good idea to dispose of the handle.  But if you want to free the channel, you simply do an audio.stop( channel ) or audio.fadeOut( { channel = c, time = t } ).

 

If you really want to add a nice touch to your project, I would recommend the audio functions in CrawlSpace’s Library.  I use it to smoothly fade in and out music tracks and easily play sound effects, among other things.

@Brent

 

Thanks for answering, the preference checking thing works fine :slight_smile: I just use it to get some preferences I save and keeping track of them.

@BeyondtheTech

 

Thanks for the explaining the differences between audio.dispose and audio.close  and for showing me CrawlSpace library! I just downloaded and I’ll use it for sure in my next game :) 

The audio.findFreeChannel() function is supposed to return a value of the free channel, so it should best be used like this:

local availableChannel = audio.findFreeChannel() if availableChannel == 0 then print( "No available audio channels to play on at the moment." ) else audio.play( explosionSound, { channel = availableChannel } ) end

Hey BeyondtheTech, 

 

I’ve corrected the way I get an availableChannel but that was not the problem :frowning: . I have an option in the game for turning on or off the audio settings (sounds and music) and the weird thing is that when I have the background music disabled, and only the fx enabled, the sound effects work just fine!  I don’t see any errors in the way I declare the music background variables

 

 

 

local playingMusic local playingMusicChannel  

 

nor in the way I load the song and start playing it in an infinite loop: 

 

 

playingMusic = audio.loadStream("music/original.mp3") playingMusicChannel = audio.play( playingMusic, { channel=2, loops=-1,  }  )  

 

I just don’t see why having a song playing in the background can affect the way I play the other sounds. 

Have you tried reserving channel 1 for the music?

 

audio.reserveChannels( 1 ) ... playingMusic = audio.loadStream("music/original.mp3") audio.play( playingMusic, { channel=1, loops=-1 } )

 

 

That way, when you’re doing the audio.findFreeChannel() function, it will look only at channels 2-32, keeping it separate from any sound effects you’re playing.

Hi @paytopwn,

You shouldn’t need to explicitly call “audio.findFreeChannel()”. If you play a sound, and there’s a free channel, it will be played on that channel automatically.

 

If your audio isn’t playing “sometimes”, have you carefully checked your logic and condition here?

 

[lua]if (preference.getValue(“fxIsOn”))[/lua]

 

What is “preference.getValue”, some function you wrote? Is it properly scoped in all cases? Have you used the “print()” command to test that its condition is set and checked in every case? This might not be an issue of the audio engine, but rather your conditional code.

 

As @BeyondtheTech says, it’s good practice to reserve one channel for music and keep it reserved for music. I usually do this on channel 1 because it’s convenient. Also, I reserve a few more channels at the beginning, like 2, 3, and 4, just in case I need to (at some point) play another music track over my main music or “blend” between two tracks (unlikely I ever will do so, but I like keeping a few channels in reserve). That leaves me with channels 5-32 to play sound effects, and I just let the audio engine select the channels for me.

 

Sincerely,

Brent Sorrentino

 

Thanks BeyondtheTech!!! 

 

I have finally found what was wrong , I had a music playing in the main menu with the reserved channel 1, and I made a fade effect of 2.5 seconds before calling the game scene (which plays another music file, without closing the channel. That way, the first audio channel was open and for some reason if a sound was played through that channel, it wouldn’t play it. Is audio.dispose() the best way to free that channel ?

audio.dispose(), if I’m not mistaken, just frees the handle, or the memory holding the audio object.  If you’re only using that particular music file for the main menu, then, yes, it’s a good idea to dispose of the handle.  But if you want to free the channel, you simply do an audio.stop( channel ) or audio.fadeOut( { channel = c, time = t } ).

 

If you really want to add a nice touch to your project, I would recommend the audio functions in CrawlSpace’s Library.  I use it to smoothly fade in and out music tracks and easily play sound effects, among other things.

@Brent

 

Thanks for answering, the preference checking thing works fine :slight_smile: I just use it to get some preferences I save and keeping track of them.

@BeyondtheTech

 

Thanks for the explaining the differences between audio.dispose and audio.close  and for showing me CrawlSpace library! I just downloaded and I’ll use it for sure in my next game :)