Audio problem

Hey guys, good evening

I have a background audio which plays in menu scene …when i go to another scene, it continues playing. i want it to play throughout my app…  But the problem is, when i go to other scene and come back to menu scene, it gets doubled… Two audios which are same starts playing… Then again i do this process, the audio gets tripled… 

so how can i solve this

There are a couple of approaches, but if you’re playing your audio on a specific channel, then you should not be able to play another sound on the same channel without stopping the first sound. That said:

  1. Use a specific channel. Reserve it, and start your sound. You should not be able to play another sound if the first one is still going.

  2. Stop audio and restart it when you get to your menu scene

  3. Set a flag that you’re music is playing, don’t try and play the audio if the flag is set.

There are probably a few other ways to handle it.

Rob

3rd one helped me

But should i use global variable for that ?

main.lua :

_G.isAudioPlaying = true 

scene_menu.lua :

if isAudioPlaying == true then 

     audio.play(backgroundMusic,{loops = -1 }) 

     audio.reserveChannels( 1 )  

     isAudioPlaying = false  

end

is it okay to keep that variable global or local is too acceptable ?

and is it important to create a separate lua file for all the sounds like you have explained in “cross scene audio” blog ??.. Or describing all the audios in the main.lua is okay ?? what differ this two methods?

We almost never recommend globals given there are other options. See: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

mydata.lua  -- local M = {} M.isAudioPlaying = false return M scene\_menu.lua : local myData = require("mydata") if myData.isAudioPlaying == false then       audio.reserveChannels( 1 )      audio.play(backgroundMusic,{ channel = 1, loops = -1 })       isAudioPlaying = true end

If you use the cross scene audio blog, it’s in effect doing the same thing as the Goodbye globals blog. It’s returning a data table with your audio handles in it. If you want to pre-load your audio in main.lua and use it in later scenes, you would need to have a module like this that contains the handles you create in main.lua.  If you use the cross-scene audio tutorial, that table can contain your .isAudioPlaying.

There are a couple of approaches, but if you’re playing your audio on a specific channel, then you should not be able to play another sound on the same channel without stopping the first sound. That said:

  1. Use a specific channel. Reserve it, and start your sound. You should not be able to play another sound if the first one is still going.

  2. Stop audio and restart it when you get to your menu scene

  3. Set a flag that you’re music is playing, don’t try and play the audio if the flag is set.

There are probably a few other ways to handle it.

Rob

3rd one helped me

But should i use global variable for that ?

main.lua :

_G.isAudioPlaying = true 

scene_menu.lua :

if isAudioPlaying == true then 

     audio.play(backgroundMusic,{loops = -1 }) 

     audio.reserveChannels( 1 )  

     isAudioPlaying = false  

end

is it okay to keep that variable global or local is too acceptable ?

and is it important to create a separate lua file for all the sounds like you have explained in “cross scene audio” blog ??.. Or describing all the audios in the main.lua is okay ?? what differ this two methods?

We almost never recommend globals given there are other options. See: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

mydata.lua  -- local M = {} M.isAudioPlaying = false return M scene\_menu.lua : local myData = require("mydata") if myData.isAudioPlaying == false then       audio.reserveChannels( 1 )      audio.play(backgroundMusic,{ channel = 1, loops = -1 })       isAudioPlaying = true end

If you use the cross scene audio blog, it’s in effect doing the same thing as the Goodbye globals blog. It’s returning a data table with your audio handles in it. If you want to pre-load your audio in main.lua and use it in later scenes, you would need to have a module like this that contains the handles you create in main.lua.  If you use the cross-scene audio tutorial, that table can contain your .isAudioPlaying.