在连接到Web Audio API的音频元素上设置playbackRate [英] Setting playbackRate on audio element connected to web audio api

查看:186
本文介绍了在连接到Web Audio API的音频元素上设置playbackRate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用createMediaElementSource将音频元素连接到Web音频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.

我知道我可以使用source.playbackRate.value设置AudioBufferSourceNode上的播放速率,但这不是我想要的,我需要在音频元素连接到网络音频时设置其播放速率api使用createMediaElementSource,所以我没有任何AudioBufferSourceNode.

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.

有人能做到吗?

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

推荐答案

您必须在音频开始播放后设置 .我发现使此工作唯一可移植的方法是等待直到获得具有有效currentTimetimeupdate事件:

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

请注意,Android当前不支持播放速率,而Chrome(台式机)不支持低于0.5的播放速率.

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

这篇关于在连接到Web Audio API的音频元素上设置playbackRate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆