Skip to main content

pause

Pause a playing sound. The position is kept, so resume carries on from the same point. Use stop instead when you want the next play to start from the top.

pause(id: string, skipDispatchEvent?: boolean): void;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of the sound or stream to pause.
skipDispatchEventbooleanfalseWhen true, no paused event is dispatched.

Returns​

Nothing.

Example​

import { SoundHub } from 'soundhub';

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

soundHub.play('music', { loop: true });

// Pause, the position is kept
soundHub.pause('music');

// Later: carry on from the same point
soundHub.resume('music');

Try it​

Try it

Pause and pick up again

Idle
Sound'tokyo-train'
Statestopped

Pause keeps the playback position. Resume carries on from exactly that point.

Code
Your clicks show up here as soundhub calls

Good to know​

  • Does nothing when the sound is not playing or is already paused. No event is dispatched then.
  • A pending fadeOutBeforeEndDuration fade is cancelled, and progress tracking stops until the sound plays again.
  • Dispatches SoundEventsEnum.PAUSED ('paused').
  • Works on streams from loadStream too. For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​

  • resume: carry on from where the sound was paused.
  • stop: halt the sound and forget its position.
  • pauseAllSounds: pause everything at once.
  • isPaused: check whether a sound is paused.