我需要知道正在播放的源的当前时间,但我不能使用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
}
然后要获得音轨的实际位置,我应该这样做:
var current_position = context.currentTime-inittime;
这工作正常,而我不在源中更改playbackRate:
source.playbackRate.value
我需要动态更改此值以将音频轨道与正在播放的另一个音轨同步,因此如果轨道的实际位置低于从“服务器”接收的位置,则需要加速,否则减慢速度更高.但是,如果我改变播放率,我怎么知道音轨的位置当前在哪里?
事实上现在如果我使用
var current_position = context.currentTime-inittime;
current_position将是从播放开始所花费的时间,该时间与播放的当前时间位置不同,更改播放值.