Skip to main content

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.

ParameterTypeDescription
typeSoundEventsEnumThe event type to listen for
callback(event: SoundEvent) => voidFunction to call when the event occurs
filter{ originalId?: string; instanceId?: string; instancePattern?: RegExp } (optional)Filter to narrow which events trigger the callback

Filter Options

PropertyTypeDescription
originalIdstring (optional)Only trigger for events from a specific sound ID
instanceIdstring (optional)Only trigger for events from a specific instance ID
instancePatternRegExp (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' });