Skip to main content

destroy

Shut the hub down and free everything it holds. Call it when the part of your app that owns the hub goes away, such as a component unmounting or a game closing.

destroy(): void;

Parameters​

This method takes no parameters.

Returns​

Nothing.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.play('music', { loop: true });

// When the game closes
soundHub.destroy();

In a React component:

useEffect(() => {
const soundHub = new SoundHub();
// ...load and play
return () => soundHub.destroy();
}, []);

Good to know​

  • What it does, in order: unloads every stream, stops and disconnects every buffered sound, removes the listeners the hub put on document (tab visibility, audio unlock, context resume), cancels the auto suspend timer, drops all event listeners and sound groups, disconnects the master chain and closes the AudioContext.
  • The hub cannot be used afterwards. The audio context is closed, so create a new SoundHub if you need sound again.
  • Removing the document listeners matters in single-page apps: without destroy they keep the hub and its audio context alive for as long as the page is open.
  • Your event listeners are cleared without a final event, so nothing tells them the hub is gone.
  • A second call does nothing. Before 6.4.0 it closed the audio context again, which the browser rejects, and that surfaced as an unhandled promise rejection.

See also​

  • reset: clear sounds and settings but keep the hub usable.
  • unloadSound: remove one sound instead of all of them.
  • suspendContext: pause audio processing without tearing anything down.