如何在Web Audio API中使用playbackRate.value时获取当前时间 [英] how I can get current time when use playbackRate.value in Web Audio API

查看:707
本文介绍了如何在Web Audio API中使用playbackRate.value时获取当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to know the current time of a source that is playing, but I can't use context.currentTime because when I change the source.playbackRate.value the speed rate of the context don't change too, so I can't determinate where is the current position of sound. There isn't another way?

编辑,一些代码:

我用这个函数来加载和播放来自网络的mp3

I use this functions to load and play an mp3 from the network

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
}

然后获取音频的实际位置我应该做的跟踪:

Then to get the actual position of the audio track I should do:

var current_position = context.currentTime-inittime;

这项工作很好,而我不在源头更改playbackRate:

This work fine while I don't change in the source the playbackRate:

source.playbackRate.value

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

I need to change this value dynamically to synchronize the audio track to another audio track that is playing, so I need to speed up, if the actual position of the track is lower than the position received from a "server" or slow down if it is higher. But if I change the play back rate how I can know where is currently the position of the audio track?

事实上现在如果我使用

var current_position = context.currentTime-inittime;

current_position将是从播放开始所花费的时间与当前时间位置不同的时间播放dude改变播放值。

current_position will be the time spent from the begin of the playback that is different from the current time position of the playback dude the changes playbackrate value.

推荐答案

使用playbackRate的倒数,并将播放开始后经过的时间乘以那个价值。假设非离线渲染,在任何给定时间您都可以使用以下内容来计算将playbackRate考虑在内的实际位置:

Use the reciprocal of playbackRate, and multiply the time elapsed since the beginning of playback with that value. Assuming a non-offline rendering, at any given time you can use the following to calculate the actual position that takes playbackRate into consideration:

(context.currentTime - inittime) / source.playbackRate.value;

来自 AudioParam.value


值AudioParam接口的属性表示参数的当前浮点值,该值最初设置为AudioParam.defaultValue的值。

The value property of the AudioParam interface represents the parameter's current floating point value, which is initially set to the value of AudioParam.defaultValue.

这意味着什么属性的值也将考虑计划的更改。请注意,如果播放速度快速变化,计算值可能会暂时倒退。

Which means the value of the value property will take scheduled changes into consideration as well. Note, that the calculated value might go backwards momentarily if playback rate changes quickly.

这篇关于如何在Web Audio API中使用playbackRate.value时获取当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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