Skip to main content

isPlaying

Check whether a sound is playing. Use it to show the right icon on a play/pause button, or to avoid starting a sound twice.

isPlaying(id: string): boolean;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of the sound, stream or overlapping instance.

Returns​

boolean: true while the sound is playing. false when it is paused, stopped, has ended, or the id is not loaded.

Example​

import { SoundHub } from 'soundhub';

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

soundHub.play('rain', { loop: true });
soundHub.isPlaying('rain'); // true

soundHub.pause('rain');
soundHub.isPlaying('rain'); // false

Try it​

Try it

Ask a sound where it is

Idle
isPlaying()false
isPaused()false
isStopped()true

Exactly one of the three is true at any moment. The values are read five times a second.

Code
Your clicks show up here as soundhub calls

Good to know​

  • With overlap, every play runs as a separate instance (click:1, click:2). The base id stays stopped, so isPlaying('click') is false while its instances play. Ask about the instance id that play returns.
  • A sound that is fading out still counts as playing until it stops.
  • For an id that is not loaded, the answer is false, but the error is also logged, stored for getLastError and dispatched as an error event. Check hasSound first when the id may not exist.

See also​