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
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID 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
Ask a sound where it is
IdleisPlaying()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, soisPlaying('click')isfalsewhile its instances play. Ask about the instance id thatplayreturns. - 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 forgetLastErrorand dispatched as anerrorevent. CheckhasSoundfirst when the id may not exist.
See also
isPaused: check for a paused sound.isStopped: check for a stopped sound.getSoundState: state, time and progress in one object.