AudioContext.getOutputTimestamp() - Web APIs 编辑

The getOutputTimestamp() property of the AudioContext interface returns a new AudioTimestamp object containing two audio timestamp values relating to the current audio context.

The two values are as follows:

  • AudioTimestamp.contextTime: The time of the sample frame currently being rendered by the audio output device (i.e., output audio stream position), in the same units and origin as the context's AudioContext.currentTime. Basically, this is the time after the audio context was first created.
  • AudioTimestamp.performanceTime: An estimation of the moment when the sample frame corresponding to the stored contextTime value was rendered by the audio output device, in the same units and origin as performance.now(). This is the time after the document containing the audio context was first rendered.

Syntax

var timestamp = AudioContext.getOutputTimestamp()

Parameters

None.

Returns

An AudioTimestamp object, which has the following properties.

  • contextTime: A point in the time coordinate system of the currentTime for the BaseAudioContext; the time after the audio context was first created.
  • performanceTime: A point in the time coordinate system of a Performance interface; the time after the document containing the audio context was first rendered

Examples

In the following code we start to play an audio file after a play button is clicked, and start off a requestAnimationFrame loop running, which constantly outputs the contextTime and performanceTime.

You can see full code of this example at output-timestamp (see it live also).

play.addEventListener('click', () => {
  if(!audioCtx) {
    audioCtx = new window.AudioContext();
  }

  getData();
  source.start(0);
  play.setAttribute('disabled', 'disabled');

  rAF = requestAnimationFrame(outputTimestamps);
});

stop.addEventListener('click', () => {
  source.stop(0);
  play.removeAttribute('disabled');
  cancelAnimationFrame(rAF);
});

// function to output timestamps

function outputTimestamps() {
  let ts = audioCtx.getOutputTimestamp()
  console.log('Context time: ' + ts.contextTime + ' | Performance time: ' + ts.performanceTime);
  rAF = requestAnimationFrame(outputTimestamps);
}

Specifications

SpecificationStatusComment
Web Audio API
The definition of 'getOutputTimestamp()' in that specification.
Working DraftInitial definition.

Browser Compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:21 次

字数:4320

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文