Skip to main content

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 usually false before the first click or tap, even with sounds loaded.
  • Streams do not count. A hub that only holds streams from loadStream reports false.
  • Overlapping instances and sprite sounds count as loaded sounds.
  • A context suspended with suspendContext, or by the autoSuspend option, makes it false as well.

See also​