resumeContext
Resume the audio context after suspendContext, an auto suspend, or a browser that started it suspended. Sounds that were playing carry on from where they froze.
resumeContext(): Promise<void>;
Parameters
This method takes no parameters.
Returns
Promise<void>: resolves once the context is running. 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 });
await soundHub.suspendContext();
// Resume from a user gesture, so the browser allows it
document.querySelector('#continue')!.addEventListener('click', async () => {
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_RESUMED('context_resumed') withisMaster: true. This also happens when the context was already running, so do not treat the event as proof that it was suspended. - It cancels a pending
autoSuspendtimer and clears the auto suspend state. - Browsers only let an audio context start after a user gesture. The hub already resumes a suspended context on the first click, touch or key press after it is created, so you rarely need this for the initial unlock.
See also
suspendContext: freeze all audio processing.resumeAllSounds: resume sounds you paused, which is a different thing.SoundHubConfig:autoUnlockandautoSuspend.getContext: theAudioContextitself, for itsstate.