我正在移動下面顯示的橙色播放頭。有多個以綠色、淺藍色、深藍色和灰色顯示的音頻剪輯。

在移動播放頭時,當遇到音頻剪輯時,我會播放它。
考慮這個偽代碼:
playhead.callbackForEveryFrame(doStuffOnEveryFrame)
function doStuffOnEveryFrame(t) {
// I intend to make sure all clips which should be playing, are actually play!
if (clip_green.startTime < t) {
clip_green.play()
}
if (clip_lightblue.startTime < t) {
clip_lightblue.play()
}
if (clip_darkblue.startTime < t) {
clip_darkblue.play()
}
if (clip_gray.startTime < t) {
clip_gray.play()
}
}
上面的代碼效率低下。由于它play連續呼叫,如果t大于剪輯的startTime.
但最好只呼叫play一次,恰好在t穿過startTime.
我很難找到最佳方法來檢測何時t穿過startTime每個音頻剪輯的 。有人有什么建議嗎?
uj5u.com熱心網友回復:
您可以將偽代碼更改為如下所示:
let events = [clip_green, clip_lightblue, clip_darkblue, clip_gray].flatMap(clip => [
{ time: clip.start_time, do: () => clip.play() },
{ time: clip.end_time, do: () => clip.stop() }
]).sort((a, b) => a.time - b.time)
playhead.callbackForEveryFrame(doStuffOnEveryFrame)
function doStuffOnEveryFrame(t) {
while (events.length && events[0].time < t) events.shift().do()
}
為避免即使在所有操作都完成后仍會呼叫此回呼,您需要一個函式來停止偵聽幀更改。就像是:
function doStuffOnEveryFrame(t) {
while (events.length && events[0].time < t) events.shift().do()
if (!events.length) playhead.stopCallbackForEveryFrame(doStuffOnEveryFrame);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/357759.html
標籤:javascript 算法 声音的 视频
上一篇:在Javascript實時搜索上顯示“未找到結果”?
下一篇:在Ajax回應之后使用then
