如何用 Corona SDK 制作包含音乐和声音开关的 2 个按钮?

我需要关于我的小部件开关按钮的帮助。我已经创建了 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

enter image description here

点赞
用户7026995
用户7026995

关于audio.setVolume()的 Corana 文档:

设置特定通道或者设置主音量。

所以 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 的信息。

2017-01-02 16:11:50