Skip to main content

play

play(id: string, options?: PlayOptions, skipDispatchEvent?: boolean): void;

Playback control: Manage the playback of sounds, including play, pause, resume, stop, and seek operations.


You can use PlayOptions as second argument. Click the link PlayOptions to see more.

You can use the SoundHubConfig to configure you soundHub with default values. Click here to see more about the SoundHubConfig.

Example


// @ts-ignore
import introSpeach from "../../sounds/intro-text-speach.mp3";
// Above line is coming from your assets / sound folder directly as a variable, but you can
// also use a url.

import { SoundHub, type SoundHubConfig, type PlayOptions} from 'soundhub';

const soundHub = new SoundHub();

// There are more configuration for the SoundHubConfig.
const config: SoundHubConfig = {
autoMuteOnHidden: true,
autoResumeOnFocus: true,
debug: true,
trackProgress: false,
};

await soundHub.loadSounds([{ id: 'intro-speach', url: 'https://www.linktospeachfile.mp3' }]); // or url: introSpeach

soundHub.play('intro-speach', {
pan: 0.5,
volume: 0.8,
playbackRate: 1.15,
createNewInstance: false,
fadeInDuration: 1,
fadeOutBeforeEndDuration: 4,
loop: true,
maxLoops: 2,
startTime: 0
}
);

// Action on stop button:
soundHub.stop('intro-speach');

PlayOptions Configuration

Edit the JSON below to customize playback options

Editable
Tip: The configuration will be merged with defaults when playing. Try adjusting values like playbackRate or volume. Adding "seamlessLoop": true loops without a gap, and then maxLoops no longer applies: the sound runs until you stop it.