Skip to main content

suspendContext

Suspend the audio context. Every sound freezes where it is and the audio hardware can go idle, which saves battery on a phone. Use it when your app goes into the background or a game is paused, and resumeContext to carry on.

suspendContext(): Promise<void>;

Parameters​

This method takes no parameters.

Returns​

Promise<void>: resolves once the context is suspended. It never rejects. A failure is logged, stored for getLastError and dispatched as an error event.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');

soundHub.play('music', { loop: true });

// Game paused: freeze all audio
await soundHub.suspendContext();

// Game resumed: carry on from the same spot
await soundHub.resumeContext();

Try it​

Try it

Suspend and resume the AudioContext

Idle
AudioContextunavailable
Soundstopped

Suspending freezes all audio processing, and the sound still counts as playing. Resume and it carries on.

Code
Your clicks show up here as soundhub calls

Good to know​

  • Dispatches SoundEventsEnum.CONTEXT_SUSPENDED ('context_suspended') with isMaster: true once the context is suspended.
  • Sounds keep their state. A playing sound still reports playing, and because the context clock stops, its position stays put until you resume.
  • It cancels a pending autoSuspend timer. A manual suspend is not undone by play() the way an auto suspend is, so call resumeContext yourself. The exception is a touch device with autoUnlock on: there play() on a suspended context runs the unlock, which resumes it and dispatches unlocked instead of context_resumed.
  • The hub already mutes itself when the tab is hidden (autoMuteOnHidden in SoundHubConfig). Suspending goes further: nothing is processed at all.

See also​