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

1
2
3
4
5
6
7
8
9
10
11
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

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:

1
2
3
4
5
6
7
8
9
10
11
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

I see what you mean, I tried it out but the music still isn't playing.

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

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

1
2
3
4
5
6
7
8
9
10
11
12
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()

that worked great! thanks for the help everyone

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.

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

1
2
3
-- 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 

I would put the music in the main file.

main.lua

1
2
3
4
5
6
7
local world1Soundtrack = audio.loadStream("AClimber_World_1.mp3")
 
_G.playWorld1Soundtrack = function()
 
      audio.play(world1Soundtrack)
 
end

ok I see, do I put that code the you typed in main and keep the current function?

that worked to keep the music ruining continuously but if i choose any level but the first one the music doesn't start playing.

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

Every level put _G.playWorld1Soundtrack()

yeah i think I just put it in wrong, It seems to be working now, thanks so much!

For it to play continuously then put timer on it.

_G.bbbbbb = timer.performWithDelay(20, _G.playWorld1Soundtrack, 0)

does that also go in main?

No just put it in every level:

1
2
3
4
5
6
7
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

how do i get the loop to stop?

when I go back to the menu the level music continues to play, how do I get it to stop after the last level?

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:

1
2
3
4
5
          _G.stopWorld1Soundtrack = function()
                     audio.stop(32)
                     timer.cancel(_G.bbbbbb)
                     _G.bbbbbb = nil
          end

I think I'm doing it wrong because its not working, can you be a little more specific.

main.lua:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local world1Soundtrack = audio.loadStream("AClimber_World_1.mp3")
 
         _G.playWorld1Soundtrack = function()
          
                     audio.play(world1Soundtrack, {channel=32}) 
         end
 
 
 
         _G.stopWorld1Soundtrack = function()
                     audio.stop(32)
                     timer.cancel(_G.bbbbbb)
                     _G.bbbbbb = nil
          end

this is working, the only thing is that when the level transitions the music skips about a second and we don't know why. We are using the code above right now. Any help would be great. Thank you

views:1430 update:2011/12/30 9:10:41
corona forums © 2003-2011