我的widget切换按钮需要帮助。我创建了2个开关按钮,用于声音开关和音乐开关但问题是每次我关闭并打开音乐开关音乐(mp3声音)没有响应意味着它的速度是每次我切换时快速转发音乐开/关。接下来的问题是每次我关闭声音开关它也关掉音乐(mp3声音)。 继承我的代码:
- 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
答案 0 :(得分:1)
我不确定你的方式是好的。我是初学者,但我想帮助你:)
来自Corona有关audio.setVolume()
为特定频道设置音量,或设置主音量 体积。
所以audio.setVolume()
会影响所有声音和音乐。
也许尝试使用变量来确定是否播放声音和音乐。
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
每当你播放声音放代码
if utils.soundOn then
utils.playSound("your_sound_effect_name")
end
或
if utils.musicOn then
utils.playMusic()
end
详细了解audio。