I need help regarding my widget switch button. I've created 2 switch button which is for sound switch and music switch but the problem is every time i switch off and switch on the music switch the music(mp3 sound) is being unresponsive means its speed is fastforwarding every time i switch music on/off. Next problem is Everytime i switch off sound switch it also switch off the music(mp3 sound). Heres my code:
--utils.lua
local sounds = {}
sounds["select"] = audio.loadSound("sounds/select.mp3")
sounds["score"] = audio.loadSound("sounds/score.mp3")
sounds["incorrect"] = audio.loadSound("sounds/gameover.mp3")
sounds["clap"] = audio.loadSound("sounds/clapping.mp3")
sounds["music"] = audio.loadSound("sounds/gameMusic.mp3")
M.playSound = function(name)
if sounds[name] ~= nil then
audio.play(sounds[name])
end
end
--Settings.lua
soundSwitchPressed = function(event)
local switch = event.target
utils.playSound("select")
if switch.id == "sound" then
if switch.isOn == true then
audio.setVolume(0)
else
audio.setVolume(1)
end
end
end
musicSwitchPressed = function(event)
local switch = event.target
utils.playSound("music")
if switch.id == "music" then
if switch.isOn == true then
audio.setVolume(0)
else
audio.setVolume(1)
end
end
end
local sound_switch = widget.newSwitch
{
left = _W-70,
top = navBar.y + navBar.height/2 + 44,
style = "onOff",
id = "sound",
x = 800,
y = 960,
onPress = soundSwitchPressed
}
sound_switch.xScale, sound_switch.yScale = 3, 3
uiGroup:insert(sound_switch)
local music_switch = widget.newSwitch
{
left = _W-70,
top = navBar.y + navBar.height/2 + 44,
style = "onOff",
id = "music",
x = 800,
y = 1200,
onPress = musicSwitchPressed
}
if audio.getVolume() == 0 then
sound_switch:setState({isOn=false, isAnimated=false})
music_switch:setState({isOn=false, isAnimated=false})
else
sound_switch:setState({isOn=true, isAnimated=false})
music_switch:setState({isOn=true, isAnimated=false})
end
end
I'm not sure your way is good. I'm beginer but I want help you:)
From Corona documentation about audio.setVolume()
Sets the volume either for a specific channel, or sets the master volume.
So audio.setVolume()
affects all sounds and music.
Maybe try use variable to determine whether play or not sounds and music.
utils.lua
audio.reserveChannels( 6 )
...
sounds["select"] = audio.loadSound("sounds/select.mp3")
sounds["score"] = audio.loadSound("sounds/score.mp3")
sounds["incorrect"] = audio.loadSound("sounds/gameover.mp3")
sounds["clap"] = audio.loadSound("sounds/clapping.mp3")
sounds["music"] = audio.loadSound("sounds/gameMusic.mp3")
local channels = {}
sounds["select"] = 1
sounds["score"] = 2
sounds["incorrect"] = 3
sounds["clap"] = 4
sounds["music"] = 5
music = audio.loadStream( "backgroundMusic.mp3" )
M.soundOn = true
M.musicOn = true
M.playMusic = function()
if music ~= nil then
audio.play( music, { channel = 6 })
end
end
M.playSound = function(name)
if sounds[name] ~= nil then
audio.play(sounds[name], { channel = channels[name]})
end
end
Settings.lua
...
soundSwitchPressed = function(event)
local switch = event.target
if utils.soundOn then
utils.playSound("select")
end
if switch.id == "sound" then
if switch.isOn == true then
utils.soundOn = true
else
utils.soundOn = false
audio.stop(1)
audio.stop(2)
audio.stop(3)
audio.stop(4)
audio.stop(5)
end
end
end
...
musicSwitchPressed = function(event)
local switch = event.target
if utils.musicOn then
utils.playSound("music")
end
if switch.id == "music" then
if switch.isOn == true then
utils.musicOn = true
utils.playMusic()
else
utils.musicOn = false
audio.stop(6)
end
end
end
Whenever you play sound put code
if utils.soundOn then
utils.playSound("your_sound_effect_name")
end
or
if utils.musicOn then
utils.playMusic()
end
Read more about audio.