Skip to main content

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.

ParameterTypeDescription
idstringID of the sound
loopbooleanWhether the sound should loop
maxLoopsnumber (optional)Maximum number of loops before the sound stops automatically

Example

// @ts-ignore
import tokyoTrain from "../../assets/sounds/tokyo-train-melody.mp3";

import { SoundManager } from 'sound-manager-ts';

const soundManager = new SoundManager();

await soundManager.loadSound('tokyo-train', tokyoTrain);

// Loop indefinitely
soundManager.setLoop('tokyo-train', true);
soundManager.play('tokyo-train');

// Stop looping
// soundManager.setLoop('tokyo-train', false);

MaxLoops Example

// Loop exactly 3 times, then stop
soundManager.setLoop('tokyo-train', true, 3);
soundManager.play('tokyo-train');
Try it: Press play, then toggle the loop checkbox to hear the difference.