How Can I Play Audio In Reverse With Web Audio Api?
How can I play audio in reverse with the web audio API? I can't seem to find anything in the API docs...
Solution 1:
You could do something like this:
var context = newAudioContext(),
request = newXMLHttpRequest();
request.open('GET', 'path/to/audio.mp3', true);
request.responseType = 'arraybuffer';
request.addEventListener('load', function(){
context.decodeAudioData(request.response, function(buffer){
var source = context.createBufferSource();
Array.prototype.reverse.call( buffer.getChannelData(0) );
Array.prototype.reverse.call( buffer.getChannelData(1) );
source.buffer = buffer;
});
});
It's a super simple example - but the point is basically that you can grab the Float32Array
instance for each channel in the AudioBuffer and reverse them.
Solution 2:
if you have an AudioBufferSourceNode element:
audioBufferSourceNode.playbackRate = -1;
-EDIT-
Webkit doesn't have that feature.
Solution 3:
You can use <audio>
element of html5 and set playbackRate
property to negative value.
In Javascript you can do the following
var song = document.getElementsByTagName('audio')[0];
song.playbackRate = -1;
Solution 4:
playbackRate = -1.0;
works in Safari (9.1.2) now!
Post a Comment for "How Can I Play Audio In Reverse With Web Audio Api?"