暂停网络音频API播放声音 [英] Pause Web Audio API sound playback
问题描述
如何创建为我的声音暂停功能?我已经有一个播放功能,在下面我的脚本。
How can i create a pause function for my audio? I already have a play function in my script below.
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);
}
但我怎样才能暂停或停止呢?
But how can i pause or stop it?
推荐答案
简短的回答是:你不能停下 - 你可以阻止它,因为idbehold说,通过调用sourceNode.noteOff(0);
The short answer is "you can't pause it - you can stop it, as idbehold says, by calling sourceNode.noteOff(0);".
您不能暂停出于同样的原因没有在音频电缆暂停按钮 - 因为数据一直通过它运行。你可以实现可暂停录音节点,但在某些时候它会被缓冲了一个巨大的数据量,如果你不取消暂停。
You can't pause it for the same reason there isn't a "pause" button on an audio cable - because data keeps running through it. You could implement a recorder node that can pause, but at some point it's going to be buffering up a huge amount of data if you don't unpause it.
要实现该场景的常用方法是跟踪你在播放的地方(如由你开始回忆),然后当要暂停你还记得偏移,调用noteOff(0),那么当要重新开始播放创建一个新节点指向相同的缓冲液并调用noteOnGrain与偏置
The usual way to implement this scenario is to keep track of where you are in the playback (e.g. by remembering when you started), and then when you want to pause you remember that offset, call noteOff(0), then when you want to re-start the playback create a new node pointing to the same buffer and call noteOnGrain with the offset.
这篇关于暂停网络音频API播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!