How can I play audio in reverse with web audio API?

帅比萌擦擦* 提交于 2019-11-30 14:53:18

问题


How can I play audio in reverse with the web audio API? I can't seem to find anything in the API docs...


回答1:


You could do something like this:

var context = new AudioContext(),
    request = new XMLHttpRequest();
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.




回答2:


if you have an AudioBufferSourceNode element:

audioBufferSourceNode.playbackRate = -1;

-EDIT-

Webkit doesn't have that feature.

Source: https://bugs.webkit.org/show_bug.cgi?id=69725




回答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; 



回答4:


playbackRate = -1.0;

works in Safari (9.1.2) now!



来源:https://stackoverflow.com/questions/9874167/how-can-i-play-audio-in-reverse-with-web-audio-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!