setLoop
setLoop(id: string, loop: boolean, maxLoops?: number): void;
Set the loop state of a sound. When loop is true, the sound will repeat indefinitely. You can optionally limit the number of loops with the maxLoops parameter.
| Parameter | Type | Description |
|---|---|---|
id | string | ID of the sound |
loop | boolean | Whether the sound should loop |
maxLoops | number (optional) | Maximum number of loops before the sound stops automatically |
Example
// @ts-ignore
import tokyoTrain from "../../assets/sounds/tokyo-train-melody.mp3";
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('tokyo-train', tokyoTrain);
// Loop indefinitely
soundHub.setLoop('tokyo-train', true);
soundHub.play('tokyo-train');
// Stop looping
// soundHub.setLoop('tokyo-train', false);
MaxLoops Example
// Loop exactly 3 times, then stop
soundHub.setLoop('tokyo-train', true, 3);
soundHub.play('tokyo-train');
Looping without a gap
setLoop() restarts the sound when it reaches the end, and that restart leaves a small gap.
For a bed or a drone, where the gap is audible, pass
seamlessLoop to play() instead and let the source node
loop the buffer itself.
Try it: Press play, then toggle the loop checkbox to hear the difference.