AudioScheduledSourceNode
The AudioScheduledSourceNode
interface is an AudioNode
which serves as a parent interface for several types of audio source nodes.
It provides ability to start and stop audio playback.
Methods
start
The above method schedules the node to start audio playback at specified time, if no time is given, it starts immediately. You can invoke this method only once in node's life.
Parameters | Type | Description |
---|---|---|
when | number | The time, in seconds, at which the node will start to play. |
Errors:
Error type | Description |
---|---|
RangeError | when is negative number. |
InvalidStateError | If node has already been started once. |
Returns undefined
.
stop
The above method schedules the node to stop audio playback at specified time, if no time is given, it stops immediately. If you invoke this method multiple times on the same node before the designated stop time, the most recent call overwrites previous one.
Parameters | Type | Description |
---|---|---|
when | number | The time, in seconds, at which the node will stop playing. |
Errors:
Error type | Description |
---|---|
RangeError | when is negative number. |
InvalidStateError | If node has not been started yet. |
Returns undefined
.
Events
onended
Experimental
Above callback is fired when source node has stopped playing. By setting this callback you can achieve pause functionality.
const [offset, setOffset] = useState(0);
/* ... */
audioBufferSourceNode.onended = (stopTime?: number) => {
setOffset((_prev) => stopTime || 0);
};
source.start(context.currentTime, offset)
Tested only on Android and iOS.