我经常读到无法使用Web Audio API暂停/恢复音频文件。
但现在我看到了一个例子,他们实际上可以暂停和恢复它。我试图弄清楚他们是怎么做到的。我认为也许source.looping = false
是关键,但事实并非如此。
现在我的音频总是从头开始重新播放。
这是我当前的代码
var context = new (window.AudioContext || window.webkitAudioContext)();
function AudioPlayer() {
this.source = context.createBufferSource();
this.analyser = context.createAnalyser();
this.stopped = true;
}
AudioPlayer.prototype.setBuffer = function(buffer) {
this.source.buffer = buffer;
this.source.looping = false;
};
AudioPlayer.prototype.play = function() {
this.source.connect(this.analyser);
this.analyser.connect(context.destination);
this.source.noteOn(0);
this.stopped = false;
};
AudioPlayer.prototype.stop = function() {
this.analyser.disconnect();
this.source.disconnect();
this.stopped = true;
};
有人知道该怎么做,让它工作吗?