Hi guys. I’m trying to add sound on my app, but it happened to something weird.
For example, there are 2 sounds files. One is background sound, and another is effect sound. When it runs first time, it works well both of them, but after going back menu, the background sound is not working. The effect sound is being played well. I don’t know why only the background sound cannot be played.
I don’t know what problem it is. Give some advice, thanks
Here is code, sfx.lua is just storage to save variable as like using global
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require(“widget”)
local sfx = require(“sfx”)
sfx.backgroundSound = audio.loadStream( “backgroundSound2.mp3”)
local function playSound(event)
sfx.backChannel = audio.play( sfx.backgroundSound, {channel = 1, loops = -1} )
storyboard.gotoScene( “scene1”)
end
function scene:createScene( event )
local group = self.view
local button = widget.newButton{
width = 100,
height = 100,
label = “menu”,
onRelease = playSound
}
button.x = display.contentWidth * 0.5
button.y = display.contentHeight * 0.5
group : insert(button)
end
scene:addEventListener( “createScene”, scene )
return scene
----------------------------------------------------------------------------------------------menu.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require(“widget”)
local sfx = require(“sfx”)
local soundPath = “Sound/sounds/”
sfx.crashSound = audio.loadSound( “powerMode.mp3” )
local function playSound(event)
audio.fade( { channel = 1, time = 2000 } )
sfx.crashChannel = audio.play( sfx.crashSound, {channel = 2} )
storyboard.gotoScene( “scene2”)
end
function scene:createScene( event )
local group = self.view
local button = widget.newButton{
width = 100,
height = 100,
label = “scene1”,
onRelease = playSound
}
button.x = display.contentWidth * 0.5
button.y = display.contentHeight * 0.5
group : insert(button)
end
scene:addEventListener( “createScene”, scene )
return scene
----------------------------------------------------------------------------------------------scene1.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require(“widget”)
local function playSound(event)
storyboard.gotoScene( “menu”)
end
function scene:createScene( event )
local group = self.view
local button = widget.newButton{
width = 100,
height = 100,
label = “gobackMenu”,
onRelease = playSound
}
button.x = display.contentWidth * 0.5
button.y = display.contentHeight * 0.5
group : insert(button)
end
scene:addEventListener( “createScene”, scene )
return scene
----------------------------------------------------------------------------------------------scene2.lua