lua计时声音文件

时间:2015-08-14 18:37:38

标签: timer lua corona duration

我是一名音乐家,正试图为吉他手写一个音乐阅读程序。 我想计时两个连续的声音,以便第一个在第二个开始时停止。每个应该持续预定的持续时间(在该示例中定义为60000/72中的72)。作为初学者编码器,我正在努力,非常感谢任何帮助。

$time_start = microtime(true);
//Removed script
$time_end = microtime(true);
$time = $time_end - $time_start;
$time = number_format($time,0);
$time = gmdate("H:i:s",$time);   //LINE 151

$stmt = $mysqli->prepare("INSERT INTO updater(duration) VALUES(?)");
$stmt->bind_param('s', $time);
$stmt->execute();
$stmt->close();

1 个答案:

答案 0 :(得分:2)

这里有几点需要注意。对不起文字墙!

字符串(文字)

必须用引号括起来。

local aa = audio.loadStream(sounds/chord1.mp3)

变为:

local aa = audio.loadStream('sounds/chord1.mp3')

魔数

应避免在任何地方解释的值。它们使代码更难理解,更难以维护或修改。

timer.performWithDelay(60000/72, timerDown, timeLimit)  

变为:

-- Might be slight overkill but hopefully you get the idea!
local beatsToPlay = 10
local beatsPerMinute = 72
local millisPerMinute = 60 * 1000
local playTimeMinutes = beatsToPlay / beatsPerMinute
local playTimeMillis = playTimeMinutes * millisPerMinute
timer.performWithDelay(playTimeMillis, timerDown, timeLimit)

Corona API

在编程时能够阅读和理解文档是一项非常宝贵的技能。 Corona的API记录在案here

audio.loadStream()的文档会告诉您它会返回一个音频句柄,您可以使用它来播放您已经拥有的声音。它还提醒您,完成后应该丢弃手柄,这样您就需要将其添加进去。

timer.performWithDelay()的文档告诉你它需要以毫秒为单位的延迟时间以及一个当时将被激活的监听器,因此你需要编写一些描述的监听器。如果您按照指向监听器的链接或者如果您在页面下方查看示例,那么您将看到一个简单的功能就足够了。

audio.play()很好,但是如果您阅读了文档,那么它会告诉您一些您可以利用的更多功能。即options参数,其中包含durationonCompleteduration是播放声音的长度 - 以毫秒为单位。 onComplete是一个听众,会在声音播放完毕后触发。

结果

仅使用计时器:

local function playAndQueue(handle, playTime, queuedHandle, queuedPlayTime)

  audio.play(handle, { duration = playTime })

  timer.performWithDelay(playTime, function(event)
    audio.dispose(handle)
    audio.play(queuedHandle, { duration = queuedPlayTime })
  end)

  timer.performWithDelay(playTime + queuedPlayTime, function(event)
    audio.dispose(queuedHandle)
  end)

end

local audioHandle1 = audio.loadStream('sounds/chord1.mp3')
local audioHandle2 = audio.loadStream('sounds/chord2.mp3')

local beatsToPlay = 10
local beatsPerMinute = 72
local millisPerMinute = 60 * 1000
local playTimeMinutes = beatsToPlay / beatsPerMinute
local playTimeMillis = playTimeMinutes * millisPerMinute

playAndQueue(audioHandle1, playTimeMillis, audioHandle2, playTimeMillis)

使用onComplete

local function playAndQueue(handle, playTime, queuedHandle, queuedPlayTime)

  -- Before we can set the 1st audio playing we have to define what happens 
  -- when it is done (disposes self and starts the 2nd audio).
  -- Before we can start the 2nd audio we have to define what happens when
  -- it is done (disposes of the 2nd audio handle)

  local queuedCallback = function(event)
    audio.dispose(queuedHandle)
  end

  local callback = function(event)
    audio.dispose(handle)
    local queuedOpts = {
      duration = queuedPlayTime,
      onComplete = queuedCallback
    }
    audio.play(queuedHandle, queuedOpts)
  end

  local opts = {
    duration = playTime,
    onComplete = callback
  }
  audio.play(handle, opts)
end

local audioHandle1 = audio.loadStream('sounds/chord1.mp3')
local audioHandle2 = audio.loadStream('sounds/chord2.mp3')

local beatsToPlay = 10
local beatsPerMinute = 72
local millisPerMinute = 60 * 1000
local playTimeMinutes = beatsToPlay / beatsPerMinute
local playTimeMillis = playTimeMinutes * millisPerMinute

playAndQueue(audioHandle1, playTimeMillis, audioHandle2, playTimeMillis)

您可能会发现使用onComplete比使用纯计时器效果更好,因为您可能会在完成用于播放(并导致错误)之前处理音频句柄。我对Corona没有任何经验,所以我不确定它的计时器或音频库有多强大。