Web Audio API - 获取歌曲中的当前偏移量和源的开始事件
Web Audio API - Getting the current offset in a song & onstarted event for a source
我正在使用网络音频 API 中的源播放一段音乐曲目 API。
var source = audioCtx.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioCtx.destination);
source.start(when, offset, duration);
我知道有一个事件侦听器可以在源播放完毕后使用,source.onended
。源启动时是否可以使用事件侦听器?例如source.onstarted
?
此外,是否可以随时从源中获取当前正在播放曲目的哪一部分?或者甚至可能来自 audioCtx?
例如source.getCurrentOffset(); // 0:21
我想避免在源启动时启动 setTimeout 的当前策略,因为它不准确。
例子
source.start(when, offset, duration);
setTimeout(function() {
console.log("The track has started!");
}, 1000 * when);
谢谢!
不确定您想要什么,但来源恰好在您指定的时间开始:时间 when
。
目前无法确定源正在播放的缓冲区中的哪个位置。人们已经要求这个功能,但在规范中没有做任何事情。返回的时间总是不准确的。
你有 context.currentTime() 但这对你没有帮助。使用 setTimeout 你应该没问题。您可以使用 myArrayBuffer.duration 获取缓冲区的长度。您也可以在开始时使用 Date() 并使用另一个 Date() 确定播放了多长时间。
你能做这样的事情吗:
function my_start(when, offset, duration, callback) {
setTimeout(function() {
source.start(0, offset, duration);
callback();
}, when);
}
function onStartedCallback() {
console.log("The track has started!");
}
my_start(when, offset, duration, onStartedCallback);
我什至建议扩展 source
原型,但我知道这通常不受欢迎 - 因为您可能会覆盖某些内容。
此外,为了更准确,您可以使用 requestAnimationFrame 进行计时。
我正在使用网络音频 API 中的源播放一段音乐曲目 API。
var source = audioCtx.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioCtx.destination);
source.start(when, offset, duration);
我知道有一个事件侦听器可以在源播放完毕后使用,source.onended
。源启动时是否可以使用事件侦听器?例如source.onstarted
?
此外,是否可以随时从源中获取当前正在播放曲目的哪一部分?或者甚至可能来自 audioCtx?
例如source.getCurrentOffset(); // 0:21
我想避免在源启动时启动 setTimeout 的当前策略,因为它不准确。
例子
source.start(when, offset, duration);
setTimeout(function() {
console.log("The track has started!");
}, 1000 * when);
谢谢!
不确定您想要什么,但来源恰好在您指定的时间开始:时间 when
。
目前无法确定源正在播放的缓冲区中的哪个位置。人们已经要求这个功能,但在规范中没有做任何事情。返回的时间总是不准确的。
你有 context.currentTime() 但这对你没有帮助。使用 setTimeout 你应该没问题。您可以使用 myArrayBuffer.duration 获取缓冲区的长度。您也可以在开始时使用 Date() 并使用另一个 Date() 确定播放了多长时间。
你能做这样的事情吗:
function my_start(when, offset, duration, callback) {
setTimeout(function() {
source.start(0, offset, duration);
callback();
}, when);
}
function onStartedCallback() {
console.log("The track has started!");
}
my_start(when, offset, duration, onStartedCallback);
我什至建议扩展 source
原型,但我知道这通常不受欢迎 - 因为您可能会覆盖某些内容。
此外,为了更准确,您可以使用 requestAnimationFrame 进行计时。