Skip to main content

seek

Jump to a point in a sound. Reach for it to build a seek bar, or to skip an intro.

seek(id: string, time: number, skipDispatchEvent?: boolean): void;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of the sound or stream.
timenumberrequiredPosition in seconds. Values below 0 go to the start.
skipDispatchEventbooleanfalseWhen true, no seeked event is dispatched.

Returns​

Nothing.

Example​

import { SoundHub } from 'soundhub';

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

soundHub.play('music');

// Skip to 15 seconds
soundHub.seek('music', 15);

// Build a progress display
const current = soundHub.getCurrentTime('music');
const duration = soundHub.getDuration('music');
console.log(`${current} / ${duration} seconds`);

Try it​

Try it

Jump to any point

Idle
0:00 / 0:00

Drag and let go: seek runs once, on release, with the time in seconds.

Code
Your clicks show up here as soundhub calls

Good to know​

  • On a playing sound the source restarts at the new point. On a paused or stopped sound only the position is stored, and the next resume() or play() starts there.
  • time is a position in the file, whatever the playback rate: seek('music', 10) lands 10 seconds into the file at any rate. getCurrentTime and getDuration use the same scale. Before 6.4.0 a buffered sound multiplied time by the rate.
  • Seeking to the end or past it does not clamp. A playing looped sound moves on to its next loop, a stopped sound is left alone, and otherwise the sound ends and dispatches ended.
  • Dispatches SoundEventsEnum.SEEKED ('seeked') with the currentTime where the seek landed.
  • On a stream from loadStream, time is seconds in the file, clamped to its duration. For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​