isReady
Check whether the hub can make sound right now: the audio context is running and at least one sound is loaded. Use it to enable a play button only once audio will actually come out.
isReady(): boolean;
Parameters
This method takes no parameters.
Returns
boolean: true when the AudioContext state is 'running' and the hub holds at least one buffered sound. false otherwise.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
playButton.disabled = !soundHub.isReady();
playButton.addEventListener('click', async () => {
await soundHub.resumeContext();
soundHub.play('music');
});
Good to know
- Browsers start the audio context suspended until the user interacts with the page, so
isReady()is usuallyfalsebefore the first click or tap, even with sounds loaded. - Streams do not count. A hub that only holds streams from
loadStreamreportsfalse. - Overlapping instances and sprite sounds count as loaded sounds.
- A context suspended with
suspendContext, or by theautoSuspendoption, makes itfalseas well.
See also
resumeContext: start the audio context after a user gesture.getContext: read the context state yourself.getSoundCount: how many sounds are loaded.