toggleMute
Mute a sound when it can be heard, and unmute it when it cannot. One call for a mute button, without checking the state first.
toggleMute(id: string): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound or stream. |
Returns
Nothing.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.play('music', { loop: true });
muteButton.addEventListener('click', () => {
soundHub.toggleMute('music');
});
Try it
One button for mute and unmute
IdleStatestopped
Mutedno
The same call switches both ways, which suits a single mute button in your own UI.
Code
Your clicks show up here as soundhub calls
Good to know
- The choice is made on the mute state, not on the volume. A sound you set to
0withsetSoundVolumeis quiet, not muted, sotoggleMutemutes it. Before 6.4.0 a buffered sound was judged by its volume. - For a stream from
loadStreamthe choice is made on its mute state. - Dispatches
mutedorunmuted, throughmuteandunmute.
See also
mute: silence a sound.unmute: bring it back.toggleGlobalMute: the same for everything at once.