AudioScheduledSourceNode.stop()

AudioScheduledSourceNode 上的stop()方法将声音安排在指定的时间停止播放。如果没有指定时间,声音将立即停止播放。

每次在同一个节点上调用 stop() 时,指定的时间将替换任何未发生的计划停止时间。如果节点已经停止,则此方法无效。

备注: 如果计划的停止时间发生在节点计划的开始时间之前,则节点永远不会开始运行。

语法

AudioScheduledSourceNode.stop([when]);

参数

when 可选

声音停止播放的时间,单位为秒。这个值在 AudioContext 用于其 currentTime 属性的同一时间坐标系统中指定。省略这个参数,设置为 0 或者负值都会立即停止播放。

Return value

Exceptions

InvalidStateNode

节点还没有通过调用start()方法被播放。

RangeError

when 指定为负值时。

Example

This example demonstrates starting an oscillator node, scheduled to begin playing at once and to stop playing in one second. The stop time is determined by taking the audio context's current time from AudioContext.currentTime and adding 1 second.

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

/* Let's play a sine wave for one second. */

osc.start();
osc.stop(context.currentTime + 1);

Specifications

Specification
Web Audio API
# dom-audioscheduledsourcenode-stop

Browser compatibility

BCD tables only load in the browser

See also