Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting playbackRate on audio element connected to web audio api

I've been experimenting with connecting an audio element to the web audio api using createMediaElementSource and got it to work but one thing I need to do is change the playback rate of the audio tag and I couldn't get that to work.

If you try to run the code below, you'll see that it works until you uncomment the line where we set the playback rate. When this line is in the audio gets muted.

I know I can set the playback rate on an AudioBufferSourceNode using source.playbackRate.value but this is not what I'd like to do, I need to set the playback rate on the audio element while it's connected to the web audio api using createMediaElementSource so I don't have any AudioBufferSourceNode.

Has anyone managed to do that?

var _source,
     _audio,
     _context,
     _gainNode;

_context = new webkitAudioContext();

function play(url) {
    if (_audio) {
        _audio.pause();
    }
    _audio = new Audio(url);
    //_audio.playbackRate = 0.6;

    setTimeout(function() {
        if (!_gainNode) {
            _gainNode = _context.createGainNode();
            _gainNode.gain.value = 0.1;
            _gainNode.connect(_context.destination);
        }

        _source = _context.createMediaElementSource(_audio);
        _source.connect(_gainNode);

        _audio.play();
    }, 0);

}

play("http://geo-samples.beatport.com/items/volumes/volume2/items/3000000/200000/40000/9000/400/60/3249465.LOFI.mp3");

setTimeout(function () {
    _audio.pause();
}, 4000);
like image 670
St Kiss Avatar asked Apr 20 '12 03:04

St Kiss


People also ask

What is audio API setting?

The Web Audio API involves handling audio operations inside an audio context, and has been designed to allow modular routing. Basic audio operations are performed with audio nodes, which are linked together to form an audio routing graph.

What is audio playback rate?

playbackRate property sets the rate at which the media is being played back. This is used to implement user controls for fast forward, slow motion, and so forth. The normal playback rate is multiplied by this value to obtain the current rate, so a value of 1.0 indicates normal speed.


2 Answers

You have to set the playback rate after the audio has started playing. The only portable way I have found to make this work, is by waiting until you get a timeupdate event with valid currentTime:

_audio.addEventListener('timeupdate', function(){
    _if(!isNaN(audio.currentTime)) {
        _audio.playbackRate = 0.6;
    }
});

Note that playback rate isn't currently supported on android and that Chrome (on desktop) doesn't support playback rates lower than 0.5.

like image 78
mzedeler Avatar answered Oct 14 '22 12:10

mzedeler


Which browser are you using to test this? It seems this is not yet implemented in Firefox, but should be working on Chrome.

Mozilla bug for implementing playbackRate: https://bugzilla.mozilla.org/show_bug.cgi?id=495040

like image 28
TheJF Avatar answered Oct 14 '22 14:10

TheJF