Skip to main content

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​

ParameterTypeDefaultDescription
idstringrequiredID 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​

Try it

One button for mute and unmute

Idle
Statestopped
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 0 with setSoundVolume is quiet, not muted, so toggleMute mutes it. Before 6.4.0 a buffered sound was judged by its volume.
  • For a stream from loadStream the choice is made on its mute state.
  • Dispatches muted or unmuted, through mute and unmute.

See also​