Corona SDK钢琴应用程序 - 交换声音等

时间:2017-10-01 00:27:00

标签: function audio lua corona piano

所以基本上我在Corona SDK(我的第一个项目)制作钢琴应用程序,并且我是新手。我已经在Corona论坛上询问了一些关于我的问题的问题,但我没有找到能帮到我的确切答案,所以我要求你的帮助。正如我刚才所说,所以我可能很难破解所需的代码,但我知道你,更有经验的Corona用户,可以很容易地做到这一点。

我为每个密钥使用此代码:(我知道media.playEventSound是非常弱的选项,我已经看到一些关于在Coronalabs上播放音频的库,如audio.loadSound等,但是如果有可能的话当然,我希望继续使用"媒体..."基于功能)

local widget = require("widget")
local C      = media.newEventSound("C.mp3")

local button_C_Press = function(event)
  media.playEventSound(C, button_C_Press)
end

local button_C = widget.newButton
{
  defaultFile = "NewKey.png",
  overFile    = "NewKey2.png",
  onPress     = button_C_Press,
}
button_C.x = 20; button_C.y = 295

我希望钢琴有2个踏板,只需按下它们就可以切换声音(我的项目文件夹共有3个不同的声音片段 - 默认和2个踏板持续音频文件)和按钮需要注意钥匙上的字母。 这就是我的问题 - 如何将这一切都集成到一个代码中? 我的意思是你能在那里给我写一个密钥的代码,就像我在下面发布的那个样本,但包括我刚才提到的那些功能吗?我真的很想解决这个问题.. 顺便说一句。我知道soundTable / fileTable方法,但它被调用,但我认为我有足够的时间单独执行每个键 - 或者使用表方法 - 我只希望它很容易,因为它是我的第一个项目因此应该是。< / p>

对不起我的英文,谢谢!

1 个答案:

答案 0 :(得分:0)

您要求提供更多代码;我在Corona论坛上推荐了这个

布尔变量:

local isPedalActive = false

当他们触摸踏板按钮时,将其设置为true:

isPedalActive = true

然后将其添加到button_C_press函数:

if event.phase == "began" then
  if isPedalActive = true then
    media.playEventSound(cPedal) --assuming you already loaded your audio above
  end
end

当然,如果您有大量钢琴键,最好不要为每个功能单独执行此操作,而是:

  1. 在widget.newButton表中为每个键设置一个特定的id。

  2. 在if语句中,将声音加载到那里,但您将检索该按钮的ID并播放该mp3文件。

  3. (仅支持一个踏板)

     --create table of key button ids and mp3 files for their pedal noises
    local keys = {
      {buttonId = "C", pedalNoise = "Cpedal.mp3"},
      {buttonId = "D", pedalNoise = "Dpedal.mp3"}
    }
    
    function pianoKeys(event)
      for i = 1, #keys do -- for each table in the keys table, load the sound for each key
        local keySound = media.newEventSound(keys[i].buttonId .. ".mp3") -- normal sound loaded
        local keypedalSound = media.newEventSound(keys[i].pedalNoise) --pedal sound loaded
        function buttonPress(event)  --When they press the key, detect if the pedal is active
          if event.phase == "began" then
            if isPedalActive == true then 
              media.playEventSound(keyPedalSound) --is active, play pedal sound
            else
              media.playEventSound(keySound) -- is not active, play regular sound
            end
          end
        end
        local pianoKey = widget.newButton({
          id = keys[i].buttonId, -- place appropriate id
          defaultFile = "new" .. keys[i].buttonId .. "key.png", -- place appropriate defaultFile
          overFile = "new" .. keys[i].buttonId .. "key2.png", -- place appropriate overFile
          onPress = buttonPress -- apply above function to each key
        })
      end 
    end
    

    我的问题 - 我不想制作合理的桌子。我宁愿单独做每个密钥。就像我在下面发布的一个密钥代码的样本一样。但是怎么样?我不知道如何把所有东西都变成一个有效的东西:/(2个踏板+音符按钮)