小编ste*_*111的帖子

如何在Web Audio API中使用playbackRate.value时获取当前时间

我需要知道正在播放的源的当前时间,但我不能使用context.currentTime,因为当我更改source.playbackRate.value时,上下文的速度也不会改变,所以我不能确定声音的当前位置在哪里.没有别的办法吗?

编辑,一些代码:

我使用这个函数从网络加载和播放mp3

function loadSoundFile(url) {
    source = null;
    var request = new XMLHttpRequest();
    request.open('GET', url, true);
    request.responseType = 'arraybuffer';
    request.onload = function(e) {
        context.decodeAudioData(request.response, initSound, function() {
            alert("error");
        });
    };
    request.send();
}

var source = null;
var inittime = 0;
function initSound(buffer) 
{

    source = context.createBufferSource();
    source.buffer = buffer;

    source.connect(context.destination);
    source.start(0);
    inittime = context.currentTime; //I save the initial time
}
Run Code Online (Sandbox Code Playgroud)

然后要获得音轨的实际位置,我应该这样做:

var current_position = context.currentTime-inittime;
Run Code Online (Sandbox Code Playgroud)

这工作正常,而我不在源中更改playbackRate:

source.playbackRate.value
Run Code Online (Sandbox Code Playgroud)

我需要动态更改此值以将音频轨道与正在播放的另一个音轨同步,因此如果轨道的实际位置低于从"服务器"接收的位置,则需要加速,否则减慢速度更高.但是,如果我改变播放率,我怎么知道音轨的位置当前在哪里?

事实上现在如果我使用

var current_position = context.currentTime-inittime;
Run Code Online (Sandbox Code Playgroud)

current_position将是从播放开始所花费的时间,该时间与播放的当前时间位置不同,更改播放值.

html5 html5-audio web-audio-api

5
推荐指数
1
解决办法
2564
查看次数

标签 统计

html5 ×1

html5-audio ×1

web-audio-api ×1