dispatchEvent
Send an event through the hub's event bus yourself. The hub uses this internally for every event it fires. Call it when you want your own code to raise an event that existing listeners already handle, or to test a listener.
dispatchEvent(event: SoundEvent): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | SoundEvent | required | The event to send. type is required and must be a SoundEventsEnum value; every other field is optional. |
Returns
Nothing.
Example
import { SoundHub, SoundEventsEnum } from 'soundhub';
const soundHub = new SoundHub();
soundHub.addEventListener(SoundEventsEnum.ERROR, (event) => {
showToast(event.error?.message ?? 'Audio problem');
});
// Report your own audio problem through the same listener
soundHub.dispatchEvent({
type: SoundEventsEnum.ERROR,
error: new Error('Microphone not available'),
timestamp: soundHub.getContext().currentTime,
});
Good to know
- Only listeners for
event.typereceive it, and their filters apply as usual. A listener filtered on{ soundId: 'music' }only hears the event when you setsoundId: 'music'. - Nothing is filled in for you. Set
timestamp,soundIdand whatever else your listeners read. - The event does not change any state in the hub. It does keep the lock screen in step when
soundIdis the sound onsetMediaSession, and withautoSuspendon, astarted,stopped,paused,resumedorendedevent makes the hub check again whether anything is playing. - A listener that throws is caught and logged to the console, and the rest still run.
See also
addEventListener: listen for events.soundEvents: every event type and theSoundEventfields.hasEventListener: check whether anyone is listening.