loadSounds
loadSounds(soundsToLoad: { id: string; url: string }[], signal?: AbortSignal): Promise<void>;
Load multiple sounds from an array of sound configurations. This is more efficient than calling loadSound multiple times as it batches the loading process.
An optional AbortSignal can be passed to cancel all in-flight loads — useful when a component unmounts before pending loads complete. This prevents uncaught promise rejections; an AbortError propagates as-is without triggering error logging or ERROR events.
| Parameter | Type | Description |
|---|---|---|
soundsToLoad | { id: string; url: string }[] | Array of sound configurations |
signal | AbortSignal (optional) | Signal from an AbortController to cancel all in-flight loads |
Example
// @ts-ignore
import tokyoTrain from "../../assets/sounds/tokyo-train-melody.mp3";
import bellsMelody from "../../assets/sounds/bells-melody.mp3";
import technoTune from "../../assets/sounds/techno-tune.mp3";
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
// Load multiple sounds at once
await soundHub.loadSounds([
{ id: 'tokyo-train', url: tokyoTrain },
{ id: 'bells-melody', url: bellsMelody },
{ id: 'techno-tune', url: technoTune }
]);
// All sounds are ready to play
soundHub.play('tokyo-train', { loop: true });
soundHub.play('bells-melody', { loop: true });
AbortSignal Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
const controller = new AbortController();
soundHub.loadSounds([
{ id: 'bg', url: '/audio/bg.mp3' },
{ id: 'fx', url: '/audio/fx.mp3' }
], controller.signal).catch(err => {
if (err.name !== 'AbortError') throw err;
// AbortError is silently handled — no error logging, no ERROR event
});
// Cancel on cleanup (e.g. when component unmounts):
controller.abort();
Load Sounds
Tip: Use
loadSound(id, url) to load a single sound or loadSounds([]) to batch-load multiple sounds. Loaded sounds can be played immediately. Use unloadSound(id) to free memory.