AudioContext:振荡器 - 知道什么时候播放过所有声音
问题描述:
我正在使用this javascript api(miniMusic)。我能够创建一个音乐,然后导出JavaScript代码。我也能够运行它。AudioContext:振荡器 - 知道什么时候播放过所有声音
我希望能够知道我的音乐什么时候结束,所以我可以再次播放并控制它。
with(new AudioContext)
for(i in D=[12,,,13,,,18,,,,,,12,,,13,,,18,,,,,,12,,,13,,,18,,,15,,,12,,,8,,,12,,,13]) {
with(createOscillator())
if(D[i]) {
connect(destination)
frequency.value=800*1.06**(13-D[i]),
type='square',
start(i*.1),
stop(i*.1+.1)
}
}
// -> onEnd = function (...) {}
循环立即运行,所以我不能使用索引来定位音乐播放的音符。 有人可以帮我吗?
答
振荡器有一个onend函数,当音调结束时调用,但是您链接的api会为每个音符创建一个新的振荡器,您可以计算播放的音符数,然后在音符数等于曲调中的音符数量。
例
with(new AudioContext)
for (i in D = [12, , , 13, , , 18, , , , , , 12, , , 13, , , 18, , , , , , 12, , , 13, , , 18, , , 15, , , 12, , , 8, , , 12, , , 13]) {
with(createOscillator())
if (D[i]) {
onended = function() {
console.log('Note has stopped playing');
}
connect(destination)
frequency.value = 800 * 1.06 ** (13 - D[i]),
type = 'square',
start(i * .1),
stop(i * .1 + .1)
}
}
希望这有助于!
工作得很好!谢谢 – DigitalEvolution