本文介绍了Web Audio API从暂停状态恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常读到,无法使用。

但现在我看到,他们实际上可以暂停和恢复它。我试图弄清楚他们是怎么做到的。我认为可能 source.looping = false 是关键,但它不是。

现在我的音频始终是从一开始就重新播放。

这是我现在的代码

  var context = new(window.AudioContext || window.webkitAudioContext)(); 

函数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;
};

有人知道该怎么做才能使它工作吗?

://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#methodsandparams-AudioBufferSourceNoderel =nofollow> https://dvcs.w3.org/hg/audio/raw -file / tip / webaudio / specification.html#methodsandparams-AudioBufferSourceNode )

只要记住当您调用noteOff时距离缓冲区的距离,然后从那里做NoteGrainOn,然后在新的AudioBufferSourceNode上继续。



这是否合理?
$ b 编辑:
请参阅下面的注释以获取更新的API调用。


I often read that it's not possible to pause/resume audio files with the Web Audio API.
But now I saw a example where they actually made it possible to pause and resume it. I tried to figure out what how they did it. I thought maybe source.looping = falseis the key, but it wasn't.
For now my audio is always re-playing from the start.

This is my current code

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;
};

Does anybody know what to do, to get it work?

解决方案

Without spending any time checking the source of your example, I'd say you'll want to use the noteGrainOn method of the AudioBufferSourceNode (https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#methodsandparams-AudioBufferSourceNode)

Just keep track of how far into the buffer you were when you called noteOff, and then do noteGrainOn from there when resuming on a new AudioBufferSourceNode.

Did that make sense?

EDIT:See comments below for updated API calls.

这篇关于Web Audio API从暂停状态恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:11