AudioScheduledSourceNode: start() 方法

Baseline 已廣泛支援

此功能已成熟,並可在許多裝置和瀏覽器版本上使用。自 2020 年 9 月起,所有瀏覽器均已提供此功能。

AudioScheduledSourceNode 上的 start() 方法用於在指定時間安排聲音開始播放。如果未指定時間,則聲音會立即開始播放。

語法

js
start()
start(when)

引數

when 可選

聲音應開始播放的時間(以秒為單位)。此值在 AudioContext 用於其 currentTime 屬性的相同時間座標系統中指定。值為 0(或完全省略 when 引數)會導致聲音立即開始播放。

返回值

無(undefined)。

異常

InvalidStateNode DOMException

如果節點已啟動,則丟擲此異常。即使節點由於先前呼叫 stop() 而不再執行,也會發生此錯誤。

RangeError

如果為 when 指定的值為負數,則丟擲此異常。

示例

此示例演示瞭如何建立一個 OscillatorNode,該節點被安排在 2 秒後開始播放,並在之後 1 秒停止播放。這些時間是透過將所需的秒數加到透過 AudioContext.currentTime 返回的上下文的當前時間戳來計算的。

js
context = new AudioContext();
osc = context.createOscillator();
osc.connect(context.destination);

/* Schedule the start and stop times for the oscillator */

osc.start(context.currentTime + 2);
osc.stop(context.currentTime + 3);

規範

規範
Web Audio API
# dom-audioscheduledsourcenode-start

瀏覽器相容性

另見