Skip to main content

AudioInstant

import { AudioInstantManager, AudioInstantManagerGlobal } from "@arclockproject/common/library/AudioInstant";

For examples and live demos, see the AudioInstant guide.

AudioInstantManager​

Class

class AudioInstantManager

Plays sound files with the Web Audio API. Each file is downloaded and decoded once, so later plays start with no delay. Every play starts a new sound, even for the same file.

Browser only: creating one needs window.AudioContext and so it breaks in SSG and SSR.

Note: This class is not as tested as its sibling Audio. Unexpected behaviour is to be expected at the current state of the Class.

Constructor

new AudioInstantManager()

Properties

  • maxVolume (number, default 1) — Volume sounds play at, from 0 to 1. Change it with setMaxVolume.

AudioInstantManager.setMaxVolume()​

AudioInstantManager.setMaxVolume(volume: number): void

Sets the volume for new sounds and for sounds tracked by stopInstance.

Parameters

  • volume (number) — From 0 (silent) to 1 (full).

AudioInstantManager.play()​

AudioInstantManager.play(path: string, options?: { singleInstance?: boolean; loop?: boolean; gradual?: boolean; speed?: number; preservesPitch?: boolean }): Promise<void>

Plays the file at path. The first play of a file waits for it to download.

Parameters

  • path (string) — URL of the audio file.
  • options? (object) — How to play it. All are optional.
    • singleInstance? (boolean) — Not used for playback. With gradual, setting it stops the sound from being tracked for stopInstance.
    • loop? (boolean) — Repeat when it reaches the end.
    • gradual? (boolean) — Fade in over 1 second.
    • speed? (number) — Playback rate, where 1 is normal speed.
    • preservesPitch? (boolean) — Not supported here yet, and ignored at the moment.

Returns Promise<void> — Resolves once the sound has started.

Example

await AudioInstantManagerGlobal.play("/sounds/hit.mp3", { speed: 1.5 });

AudioInstantManager.stopInstance()​

AudioInstantManager.stopInstance(path: string): void

Stops the tracked sound for path. Only the latest sound played with gradual: true is tracked per file.

Parameters

  • path (string) — The same URL that was passed to play.

AudioInstantManager.stopAllInstances()​

AudioInstantManager.stopAllInstances(): void

Stops every tracked sound. See stopInstance for which sounds are tracked.

AudioInstantManagerGlobal​

Instance of AudioInstantManager

Shared AudioInstantManager for the whole app. It is created when this module is imported, so only import it in the browser.