music help

Working on a game right now and trying to get the music to play continuously throughout the world. Yet start if a level is selected from the level select screen.

This is the code I have currently, It loads the level but doesn’t play the music

any help would be great, thanks

[code]local world1Soundtrack = audio.loadStream(“AClimber_World_1.mp3”)
local world1Soundtrack = false

function music()
if (world1Soundtrack == false) then
world1Soundtrack = true
audio.play(world1Soundtrack)
else if (world1Soundtrack == true) then
end
end
end
[import]uid: 94237 topic_id: 18684 reply_id: 318684[/import]

Hey, shakmbakm, it looks like your handle (world1Soundtrack) is overwritten in the second line. You’d want to assign a different variable for the true/false flag, while leaving the line 1 of your code intact. So, something like:

[lua]local world1Soundtrack = audio.loadStream(“AClimber_World_1.mp3”)
local world1SoundtrackOn = false

function music()
if (world1SoundtrackOn == false) then
world1SoundtrackOn = true
audio.play(world1Soundtrack)
elseif (world1SoundtrackOn == true) then
end
end
end[/lua]

Naomi [import]uid: 67217 topic_id: 18684 reply_id: 71807[/import]

I see what you mean, I tried it out but the music still isn’t playing. [import]uid: 94237 topic_id: 18684 reply_id: 71963[/import]

Hey, shakmbakm, you might want to check this out: http://developer.anscamobile.com/reference/index/audioplay

Maybe what you need to do is to follow how the example handles the backgroundMusic in the audioplay reference page.

If not, I hope someone more knowledgeable will jump in and help you out.

Naomi [import]uid: 67217 topic_id: 18684 reply_id: 71966[/import]

Solved
Hi shakmbakm
Try this, same as Naomi but taken off one of the ends and added music()

[code]
local world1Soundtrack = audio.loadStream(“AClimber_World_1.mp3”)
local world1SoundtrackOn = false

function music()
if (world1SoundtrackOn == false) then
world1SoundtrackOn = true
audio.play(world1Soundtrack)
elseif (world1SoundtrackOn == true) then

end
end
music()
[/code] [import]uid: 47300 topic_id: 18684 reply_id: 72003[/import]

that worked great! thanks for the help everyone [import]uid: 94237 topic_id: 18684 reply_id: 72007[/import]

Interesting, that did work and it plays the music now, but when it goes to the next level it starts playing the music again over the previous music. [import]uid: 94237 topic_id: 18684 reply_id: 72052[/import]

Hey, @shakmbakm, if you want the background music continuing throughout the world, and only time you want the background music to restart is when you go to level select screen, you might really want to look into assigning a channel for the background music. It’s really nicely explained here:

http://developer.anscamobile.com/reference/index/audioplay

[lua]-- copied from the example posted on the reference page
backgroundMusic = audio.loadStream(“backgroundMusic.m4a”)
backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1, fadein=5000 } ) – play the background music on channel 1, loop infinitely, and fadein over 5 seconds [/lua]

Naomi [import]uid: 67217 topic_id: 18684 reply_id: 72058[/import]

I would put the music in the main file.

main.lua

local world1Soundtrack = audio.loadStream("AClimber\_World\_1.mp3")  
  
\_G.playWorld1Soundtrack = function()  
  
 audio.play(world1Soundtrack)  
  
end  

Then in each level (module) put:

_G.playWorld1Soundtrack()

I don’t think you need the if statement cos it seems to work without!
[import]uid: 47300 topic_id: 18684 reply_id: 72065[/import]

ok I see, do I put that code the you typed in main and keep the current function? [import]uid: 94237 topic_id: 18684 reply_id: 72067[/import]

that worked to keep the music ruining continuously but if i choose any level but the first one the music doesn’t start playing. [import]uid: 94237 topic_id: 18684 reply_id: 72074[/import]

Try just putting my suggestion in main and get rid of yours.

Every level put _G.playWorld1Soundtrack() [import]uid: 47300 topic_id: 18684 reply_id: 72076[/import]

yeah i think I just put it in wrong, It seems to be working now, thanks so much! [import]uid: 94237 topic_id: 18684 reply_id: 72077[/import]

For it to play continuously then put timer on it.
_G.bbbbbb = timer.performWithDelay(20, _G.playWorld1Soundtrack, 0)

[import]uid: 47300 topic_id: 18684 reply_id: 72080[/import]

does that also go in main?
[import]uid: 94237 topic_id: 18684 reply_id: 72092[/import]

No just put it in every level:

  
if world1Soundtrack then  
timer.cancel(\_G.bbbbbb)  
\_G.bbbbbb = nil  
\_G.bbbbbb = timer.performWithDelay(20, \_G. playWorld1Soundtrack, 0)  
else  
\_G.bbbbbb = timer.performWithDelay(20, \_G. playWorld1Soundtrack, 0)  
end  
  

That should do it.
[import]uid: 47300 topic_id: 18684 reply_id: 72095[/import]

how do i get the loop to stop? [import]uid: 94237 topic_id: 18684 reply_id: 72096[/import]

when I go back to the menu the level music continues to play, how do I get it to stop after the last level? [import]uid: 94237 topic_id: 18684 reply_id: 72101[/import]

Putting:
audio.play(world1Soundtrack, {channel=32})
instead will make it play on channel 32 (or whatever channel you want)
Then in main.lua you can put a function:

 \_G.stopWorld1Soundtrack = function()  
 audio.stop(32)  
 timer.cancel(\_G.bbbbbb)  
 \_G.bbbbbb = nil  
 end  

Then whenever you want you can call the stop function:

_G.stopWorld1Soundtrack()
[import]uid: 47300 topic_id: 18684 reply_id: 72102[/import]

I think I’m doing it wrong because its not working, can you be a little more specific. [import]uid: 94237 topic_id: 18684 reply_id: 72103[/import]