Problem While Playing Background Music

Hello ! 

I have been toying around with background music and such and I have an issue, that I can’t seem to fix: I have different scenes in the game, a main menu, a sub-menu and a game scene. All the background music plays fine the first time, but when I finish the game and I get back to the main menu, the music does not start again. 

I create the background music for the menu and the sub menu with this code: 

function scene:createScene( event ) local screenGroup = self.view backgroundMusic = audio.loadStream("music/menu.mp3")

and I play it in the enterScene part 

function scene:enterScene( event ) ... backgroundMusicChannel = audio.play( backgroundMusic, {channel=1,loops=-1 } )

It works fine until I finish the game and get back to the main menu, which it should start playing again. 

The funny thing is it seems to work okay if I remove the audio options ( it only plays once ).

To change the background music I use the following code:

audio.fadeOut({ channel=1, time=2500 } )

Before changing the scene and starting playing (inside the game class) the next song. 

Channel 1 is reserved and it seems that channel 1 state never fully stops. 

I’ve tried printing some audio properties on the console prompt :

print (audio.isChannelActive( backgroundMusicChannel )) print (audio.isChannelPaused( backgroundMusicChannel )) print (audio.isChannelPlaying( backgroundMusicChannel ))

And it returns :

true false true

For the first and the other iterations if I don’t go back from the submenu to the menu, I mean if I go from the main menu to the submenu and then I play and after playing I get back to the main menu. Otherwise, if I get back from the submenu I get the values

false false false 

Anyone has any tips about what can I be doing wrong? 

Thanks! 

Hi @paytopwn,

For your reference variables like “backgroundMusic” and “backgroundMusicChannel”, where are you declaring those? In “main.lua”? Is the reference to them the same from scene to scene, so the app can track which song is playing and when? I think basically you have a scoping issue going on, in that Lua doesn’t know (from scene to scene) what these variables should reference. See if you can unify these variables in the “storyboard” table and confirm that the reference is exactly the same from scene to scene. Then, implement your management techniques like fading out, loading a new stream, disposing of an old stream, etc.

Best regards,

Brent

Hi Brent!

I declare the variables in menu-scene.lua , that’s the first scene my main.lua class calls. It would be better to declare them in the main.lua file ? The thing is that if it was a scoping problem, I wouldn’t be able to fade it out from gmenu-scene.lua

(The program has a main.lua that calls a menu-scene.lua and from here I can call diferent scenes, but the important one is gmenu-scene.lua which is the one that calls game-scene.lua).

I don’t really get what you mean when you talk about the storyboard table, but it seems that the reference is the same from scene to scene :S 

Thanks! 

Hi @paytopwn,

I should probably have clarified better… however you choose to declare and manage these variables, I think confirming that they’re the same variable (reference) is especially crucial when dealing with audio. Because audio is such a “core” aspect of the engine, you should ensure that you’re not accidentally using different handles on the same channel, etc.

Have you considered using Graham Ranson’s “GGSound” utility? Alot of developers swear by this (and his other libraries).

https://github.com/GlitchGames/GGSound

Best of luck!

Brent

Hi @paytopwn,

For your reference variables like “backgroundMusic” and “backgroundMusicChannel”, where are you declaring those? In “main.lua”? Is the reference to them the same from scene to scene, so the app can track which song is playing and when? I think basically you have a scoping issue going on, in that Lua doesn’t know (from scene to scene) what these variables should reference. See if you can unify these variables in the “storyboard” table and confirm that the reference is exactly the same from scene to scene. Then, implement your management techniques like fading out, loading a new stream, disposing of an old stream, etc.

Best regards,

Brent

Hi Brent!

I declare the variables in menu-scene.lua , that’s the first scene my main.lua class calls. It would be better to declare them in the main.lua file ? The thing is that if it was a scoping problem, I wouldn’t be able to fade it out from gmenu-scene.lua

(The program has a main.lua that calls a menu-scene.lua and from here I can call diferent scenes, but the important one is gmenu-scene.lua which is the one that calls game-scene.lua).

I don’t really get what you mean when you talk about the storyboard table, but it seems that the reference is the same from scene to scene :S 

Thanks! 

Hi @paytopwn,

I should probably have clarified better… however you choose to declare and manage these variables, I think confirming that they’re the same variable (reference) is especially crucial when dealing with audio. Because audio is such a “core” aspect of the engine, you should ensure that you’re not accidentally using different handles on the same channel, etc.

Have you considered using Graham Ranson’s “GGSound” utility? Alot of developers swear by this (and his other libraries).

https://github.com/GlitchGames/GGSound

Best of luck!

Brent

Thanks Brent, will try that and I’ll let you know :slight_smile: And I was planning in using the GGSound utility for my next game n_n but maybe I change everything and start using it now.

Thanks Brent, will try that and I’ll let you know :slight_smile: And I was planning in using the GGSound utility for my next game n_n but maybe I change everything and start using it now.

Actually I never posted the solution to my problem. After fading out the volume of that channel gets set to 0, so I only had to rise the volume level to be able to hear the music again. 

Cheers! 

Actually I never posted the solution to my problem. After fading out the volume of that channel gets set to 0, so I only had to rise the volume level to be able to hear the music again. 

Cheers!