lua 计时声音文件
lua timing sound files
我是一名音乐家,正在尝试为吉他手编写音乐阅读程序。
我想为两个连续的声音计时,以便第一个在第二个开始时停止。每个都应持续预定的持续时间(在此示例中定义为 60000/72 中的 72)。作为一名初学者编码员,我正在苦苦挣扎,非常感谢任何帮助。
-- AUDIO 1 --
local aa = audio.loadStream(sounds/chord1.mp3)
audio.play(aa)
-- TIMER 1 --
local timeLimit = 1
local function timerDown()
timeLimit = timeLimit-1
if(timeLimit==0)then
end
end
timer.performWithDelay( 60000/72, timerDown, timeLimit )
-- TIMER 2 --
local timeLimit = 1
local function timerDown()
timeLimit = timeLimit-1
if(timeLimit==0)then
-- AUDIO 2 --
local aa = audio.loadStream(sounds/chord2.mp3])
audio.play(aa)
end
end
timer.performWithDelay( 60000/72, timerDown, timeLimit )
这里有几点需要注意。对不起文字墙!
字符串(文本)
必须用引号引起来。
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)
电晕 API
在编程时能够阅读和理解文档是一项非常宝贵的技能。 Corona 的 API 记录在案 here。
audio.loadStream()
的文档告诉您它 returns 一个音频句柄,您可以使用它来播放您已经拥有的声音。它还会提醒您完成后应处理句柄,因此您需要将其添加进去。
timer.performWithDelay()
的文档告诉你,它需要以毫秒为单位的延迟时间和一个监听器,它会在那个时候被激活,所以你需要写一个监听器的一些描述。如果您跟随 link 到 listener 或者如果您查看页面下方的示例,那么您会发现一个简单的函数就足够了。
audio.play()
很好,但如果您阅读文档,它会告诉您更多功能,您可以利用这些功能。即options
参数,其中包括duration
和onComplete
。 duration
是播放声音的时间长度(以毫秒为单位)。 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 没有任何经验,所以我不确定它的计时器或音频库有多强大。
我是一名音乐家,正在尝试为吉他手编写音乐阅读程序。 我想为两个连续的声音计时,以便第一个在第二个开始时停止。每个都应持续预定的持续时间(在此示例中定义为 60000/72 中的 72)。作为一名初学者编码员,我正在苦苦挣扎,非常感谢任何帮助。
-- AUDIO 1 --
local aa = audio.loadStream(sounds/chord1.mp3)
audio.play(aa)
-- TIMER 1 --
local timeLimit = 1
local function timerDown()
timeLimit = timeLimit-1
if(timeLimit==0)then
end
end
timer.performWithDelay( 60000/72, timerDown, timeLimit )
-- TIMER 2 --
local timeLimit = 1
local function timerDown()
timeLimit = timeLimit-1
if(timeLimit==0)then
-- AUDIO 2 --
local aa = audio.loadStream(sounds/chord2.mp3])
audio.play(aa)
end
end
timer.performWithDelay( 60000/72, timerDown, timeLimit )
这里有几点需要注意。对不起文字墙!
字符串(文本)
必须用引号引起来。
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)
电晕 API
在编程时能够阅读和理解文档是一项非常宝贵的技能。 Corona 的 API 记录在案 here。
audio.loadStream()
的文档告诉您它 returns 一个音频句柄,您可以使用它来播放您已经拥有的声音。它还会提醒您完成后应处理句柄,因此您需要将其添加进去。
timer.performWithDelay()
的文档告诉你,它需要以毫秒为单位的延迟时间和一个监听器,它会在那个时候被激活,所以你需要写一个监听器的一些描述。如果您跟随 link 到 listener 或者如果您查看页面下方的示例,那么您会发现一个简单的函数就足够了。
audio.play()
很好,但如果您阅读文档,它会告诉您更多功能,您可以利用这些功能。即options
参数,其中包括duration
和onComplete
。 duration
是播放声音的时间长度(以毫秒为单位)。 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 没有任何经验,所以我不确定它的计时器或音频库有多强大。