stop
Stop a sound and forget its position. Unlike pause, the next play starts from the beginning (or from the startTime in its play options).
stop(id: string, skipDispatchEvent?: boolean): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound, instance or stream to stop. |
skipDispatchEvent | boolean | false | When true, no stopped event is dispatched. |
Returns
Nothing.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.play('music', { loop: true });
// Stop and reset the position
soundHub.stop('music');
// Playing again starts from the beginning
soundHub.play('music');
Try it
Stop and start over
IdleSound'tokyo-train'
Statestopped
After a stop, Play starts from the beginning. Use pause to keep your place.
Code
Your clicks show up here as soundhub calls
Good to know
- Cancels any running fade and any pending
fadeOutBeforeEndDurationfade, and stops progress tracking. - Event listeners added with a filter on this exact instance id are removed.
- Dispatches
SoundEventsEnum.STOPPED('stopped'), also when the sound was already stopped. - An id that is not loaded is ignored quietly: no error is logged or dispatched.
- Works on streams from
loadStreamtoo. A stream goes back to its start offset. - On a variation name from
createVariationsit stops every take and every instance of them that is not already stopped. Added in 6.5.0
See also
pause: halt a sound and keep its position.stopAllSounds: stop everything at once.fadeOut: fade a sound out and stop it afterwards.isStopped: check whether a sound is stopped.