addEventListener
addEventListener(
type: SoundEventsEnum,
callback: (event: SoundEvent) => void,
filter?: { originalId?: string; instanceId?: string; instancePattern?: RegExp }
): void;
Listeners / Event handling: Manage event listeners for sound-related events.
Add an event listener for a specific SoundEventsEnum type. Optionally, provide a filter to narrow down which events trigger the callback.
| Parameter | Type | Description |
|---|---|---|
type | SoundEventsEnum | The event type to listen for |
callback | (event: SoundEvent) => void | Function to call when the event occurs |
filter | { originalId?: string; instanceId?: string; instancePattern?: RegExp } (optional) | Filter to narrow which events trigger the callback |
Filter Options
| Property | Type | Description |
|---|---|---|
originalId | string (optional) | Only trigger for events from a specific sound ID |
instanceId | string (optional) | Only trigger for events from a specific instance ID |
instancePattern | RegExp (optional) | Regex pattern to match instance IDs |
Example
import { SoundManager, SoundEventsEnum } from 'sound-manager-ts';
const soundManager = new SoundManager();
// Listen to all play events
soundManager.addEventListener(SoundEventsEnum.PLAY, (event) => {
console.log('Sound played:', event);
});
// Listen only to events from a specific sound
soundManager.addEventListener(SoundEventsEnum.STOP, (event) => {
console.log('Sound stopped:', event.soundId);
}, { originalId: 'bg-music' });