2 声音和音乐 Corona SDK 的开关按钮?
2 switch Button for Sound and Music Corona SDK?
我需要有关小部件切换按钮的帮助。我已经创建了 2 个开关按钮,用于声音开关和音乐开关,但问题是每次我关闭和打开音乐开关时,音乐(mp3 声音)没有响应意味着每次我切换音乐时它的速度都在快进 on/off。下一个问题是每次我关闭声音开关时它也会关闭音乐(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
我不确定你的方法是否正确。我是初学者,但我想帮助你:)
来自关于 audio.setVolume()
的 Corona 文档
Sets the volume either for a specific channel, or sets the master
volume.
因此 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 的信息。
我需要有关小部件切换按钮的帮助。我已经创建了 2 个开关按钮,用于声音开关和音乐开关,但问题是每次我关闭和打开音乐开关时,音乐(mp3 声音)没有响应意味着每次我切换音乐时它的速度都在快进 on/off。下一个问题是每次我关闭声音开关时它也会关闭音乐(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
我不确定你的方法是否正确。我是初学者,但我想帮助你:)
来自关于 audio.setVolume()
Sets the volume either for a specific channel, or sets the master volume.
因此 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 的信息。