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
Suspend and resume the AudioContext
IdleAudioContextunavailable
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') withisMaster: trueonce 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
autoSuspendtimer. A manual suspend is not undone byplay()the way an auto suspend is, so callresumeContextyourself. The exception is a touch device withautoUnlockon: thereplay()on a suspended context runs the unlock, which resumes it and dispatchesunlockedinstead ofcontext_resumed. - The hub already mutes itself when the tab is hidden (
autoMuteOnHiddeninSoundHubConfig). Suspending goes further: nothing is processed at all.
See also
resumeContext: wake the context up again.pauseAllSounds: pause the sounds but keep the context running.SoundHubConfig:autoSuspendputs the context to sleep after a stretch of silence.getContext: theAudioContextitself, for itsstate.