Mik*_*tin 4 javascript audio html5 web-audio-api
如何为音频创建暂停功能?我已在下面的脚本中有一个播放功能.
function loadSound(url) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
// When loaded decode the data
request.onload = function() {
// decode the data
context.decodeAudioData(request.response, function(buffer) {
// when the audio is decoded play the sound
playSound(buffer);
}, onError);
}
request.send();
}
function playSound(buffer) {
sourceNode.buffer = buffer;
sourceNode.noteOn(0);
}
Run Code Online (Sandbox Code Playgroud)
但我怎么能暂停或停止呢?
cwi*_*lso 12
简短的回答是"你不能暂停它 - 你可以通过调用sourceNode.noteOff(0);"来阻止它,正如idbehold所说.
您无法暂停,原因与音频线上没有"暂停"按钮相同 - 因为数据一直在运行.你可以实现一个可以暂停的记录器节点,但是如果你没有取消它,它会在某些时候缓冲大量的数据.
实现这种情况的通常方法是跟踪你在播放中的位置(例如,记住你何时开始),然后当你想要暂停时记住那个偏移,调用noteOff(0),然后当你想要重新开始播放创建指向同一缓冲区的新节点,并使用偏移量调用noteOnGrain.
| 归档时间: |
|
| 查看次数: |
4939 次 |
| 最近记录: |