2 switch Button for Sound and Music Corona SDK?

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

1- You are setting the overall volume to 0 which is the cause of everything being turned off. You need to use audio channels for that matter. Here -> https://docs.coronalabs.com/api/library/audio/play.html

2- You are setting the volume to 0 for audio but don’t dispose it so you basically create a new sound object to play every time you call utils.playSound(). That’s probably the cause of music getting faster. Several ways to solve this:

a ) You can dispose the audio file when you set volume to 0. When doing this, keep in mind that every time you turn the switch on the music will start from the beginning. -> https://docs.coronalabs.com/api/library/audio/dispose.html

b ) After inserting channels into your code, you can directly set volumes of those channels to 0 when switched off and to 1 when switched on. -> https://docs.coronalabs.com/api/library/audio/setVolume.html

Personally I wouldn’t set the volume to 0. I instead set a flag that I can check app wide using a data table I include everywhere:

-- mydata.lua local M = {} M.settings = {} M.settings.soundFX = true M.settings.music = true return M

-- j-random scene: local myData = require("mydata") ... if myData.settings.soundFX then      audio.play(explosion) end

And then in my settings scene I just toggle the flags on and off. For Music I tend to stop and play since I don’t mind it starting over, but you could pause when the music is turned off and play if it’s turned back on.

Rob

1- You are setting the overall volume to 0 which is the cause of everything being turned off. You need to use audio channels for that matter. Here -> https://docs.coronalabs.com/api/library/audio/play.html

2- You are setting the volume to 0 for audio but don’t dispose it so you basically create a new sound object to play every time you call utils.playSound(). That’s probably the cause of music getting faster. Several ways to solve this:

a ) You can dispose the audio file when you set volume to 0. When doing this, keep in mind that every time you turn the switch on the music will start from the beginning. -> https://docs.coronalabs.com/api/library/audio/dispose.html

b ) After inserting channels into your code, you can directly set volumes of those channels to 0 when switched off and to 1 when switched on. -> https://docs.coronalabs.com/api/library/audio/setVolume.html

Personally I wouldn’t set the volume to 0. I instead set a flag that I can check app wide using a data table I include everywhere:

-- mydata.lua local M = {} M.settings = {} M.settings.soundFX = true M.settings.music = true return M

-- j-random scene: local myData = require("mydata") ... if myData.settings.soundFX then      audio.play(explosion) end

And then in my settings scene I just toggle the flags on and off. For Music I tend to stop and play since I don’t mind it starting over, but you could pause when the music is turned off and play if it’s turned back on.

Rob