Playing random song

I am have an app that plays one song in a scene . How do I change it so that multiple songs can be randomly chosen from to play ? This is what I have so far :

game.lua

local song1 = audio.loadSound( "sounds/Death Song.mp3" ) local song2 = audio.loadSound( "sounds/stay.mp3" ) local song3 = audio.loadSound( "sounds/menu\_music.mp3" ) local song = {song1, song2, song3} local playRandom playRandom = function () if( #song == 0 ) then return end local aleatorio= math.random(1,#song) audio.play (song[aleatorio], { onComplete = playRandom } ) table.remove ( song, aleatorio ) end playRandom() function scene:show(event) if event.phase == 'did' then eachframe.add(self) -- Each frame self:eachFrame() is called -- Only check once in a while for level end self.endLevelCheckTimer = timer.performWithDelay(2000, function() self:endLevelCheck() end, 0) -- Show help image once if not databox.isHelpShown then timer.performWithDelay(2500, function() self.sidebar:show() self:setIsPaused(true) end) end sounds.playStream( playRandom ) end end

When i use this code the songs all override each other and the song from the menu plays also . Can someone help me ?

First of all, you don’t have to load every song. You can just assign indexes to them and after you got the corresponding number, you can load the file. By the way, you should use stream instead of loadSound if the audio file duration is long like ambient music etc.

local backgroundMusic, channelBackgroundMusic local tableSongs = { "sounds/Death Song.mp3", "sounds/stay.mp3", "sounds/menu\_music.mp3" } local function playRandomSong() local numberSong = math.random(#tableSongs) backgroundMusic = audio.loadStream(tableSongs[numberSong]) channelBackgroundMusic = audio.play(backgroundMusic) end playRandomSong()

The same exact thing happens . The menu’s song plays and when I select a game level , the menu song still plays and the game’s song plays . 

local backgroundMusic, channelBackgroundMusic local tableSongs = { "sounds/Death Song.mp3", "sounds/stay.mp3", "sounds/menu\_music.mp3" } local function playRandomSong() local numberSong = math.random(#tableSongs) backgroundMusic = audio.loadStream(tableSongs[numberSong]) channelBackgroundMusic = audio.play(backgroundMusic) end playRandomSong() function scene:show(event) if event.phase == 'did' then eachframe.add(self) -- Each frame self:eachFrame() is called -- Only check once in a while for level end self.endLevelCheckTimer = timer.performWithDelay(2000, function() self:endLevelCheck() end, 0) -- Show help image once if not databox.isHelpShown then timer.performWithDelay(2500, function() self.sidebar:show() self:setIsPaused(true) end) end sounds.playStream( playRandom ) end end

When dealing with background music, you should typically reserve one channel for that music. Then play new songs on the same channel. If you don’t, then Corona will automatically pick a free audio channel and start playing new music there (while the previous continues playing).

This has the added benefit of letting you set a channel-specific volume for music, which the player can adjust from some menu.

It’s better described here:

https://docs.coronalabs.com/guide/media/audioSystem/index.html#channels-and-volume

Of course, you could also stop and dispose of the music currently playing (on whatever channel) and then start new music playing, but that won’t just happen automatically… you need to properly manage your audio handles and channels.

Brent

First of all, you don’t have to load every song. You can just assign indexes to them and after you got the corresponding number, you can load the file. By the way, you should use stream instead of loadSound if the audio file duration is long like ambient music etc.

local backgroundMusic, channelBackgroundMusic local tableSongs = { "sounds/Death Song.mp3", "sounds/stay.mp3", "sounds/menu\_music.mp3" } local function playRandomSong() local numberSong = math.random(#tableSongs) backgroundMusic = audio.loadStream(tableSongs[numberSong]) channelBackgroundMusic = audio.play(backgroundMusic) end playRandomSong()

The same exact thing happens . The menu’s song plays and when I select a game level , the menu song still plays and the game’s song plays . 

local backgroundMusic, channelBackgroundMusic local tableSongs = { "sounds/Death Song.mp3", "sounds/stay.mp3", "sounds/menu\_music.mp3" } local function playRandomSong() local numberSong = math.random(#tableSongs) backgroundMusic = audio.loadStream(tableSongs[numberSong]) channelBackgroundMusic = audio.play(backgroundMusic) end playRandomSong() function scene:show(event) if event.phase == 'did' then eachframe.add(self) -- Each frame self:eachFrame() is called -- Only check once in a while for level end self.endLevelCheckTimer = timer.performWithDelay(2000, function() self:endLevelCheck() end, 0) -- Show help image once if not databox.isHelpShown then timer.performWithDelay(2500, function() self.sidebar:show() self:setIsPaused(true) end) end sounds.playStream( playRandom ) end end

When dealing with background music, you should typically reserve one channel for that music. Then play new songs on the same channel. If you don’t, then Corona will automatically pick a free audio channel and start playing new music there (while the previous continues playing).

This has the added benefit of letting you set a channel-specific volume for music, which the player can adjust from some menu.

It’s better described here:

https://docs.coronalabs.com/guide/media/audioSystem/index.html#channels-and-volume

Of course, you could also stop and dispose of the music currently playing (on whatever channel) and then start new music playing, but that won’t just happen automatically… you need to properly manage your audio handles and channels.

Brent