Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  180] [ 6]  / answers: 1 / hits: 26761  / 12 Years ago, mon, july 16, 2012, 12:00:00

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?


More From » html

 Answers
4

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.



EDIT 2, 2019: See MDN for updated API calls; https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start


[#84236] Friday, July 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pierce

Total Points: 315
Total Questions: 103
Total Answers: 89

Location: Svalbard and Jan Mayen
Member since Mon, Jun 7, 2021
3 Years ago
pierce questions
Thu, Aug 27, 20, 00:00, 4 Years ago
Tue, May 19, 20, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
Tue, Feb 19, 19, 00:00, 5 Years ago
;