Represents an audio clip that can be loaded and played independently. The AudioClip class encapsulates the URL of the audio resource and provides methods for playback control (play, pause, stop) and querying duration.

Constructors

  • Creates a new AudioClip instance with the specified URL.

    Parameters

    • url: string

      The URL of the audio resource to load. This can be a path to an audio file or a MediaStream URL.

    Returns AudioClip

Properties

url: string

The URL of the audio resource to load. This can be a path to an audio file or a MediaStream URL.

Accessors

  • get audioElement(): undefined | HTMLAudioElement

    The underlying HTMLAudioElement, or undefined if not yet created. Use this to connect the element to the Web Audio API via createMediaElementSource().

    Returns undefined | HTMLAudioElement

    The HTMLAudioElement if the clip has been loaded or played, otherwise undefined.

  • get currentTime(): number

    Current playback position in seconds.

    Returns number

  • set currentTime(value: number): void

    Parameters

    • value: number

    Returns void

  • get isPlaying(): boolean

    Whether the clip is currently playing.

    Returns boolean

    true if the clip is actively playing audio.

  • get loop(): boolean

    Whether the clip should loop when reaching the end.

    Returns boolean

  • set loop(value: boolean): void

    Parameters

    • value: boolean

    Returns void

  • get progress(): number

    Normalized playback progress from 0 to 1.

    Returns number

    The current playback position as a value between 0 and 1, or 0 if the duration is unknown.

  • get volume(): number

    Playback volume from 0 (silent) to 1 (full).

    Returns number

  • set volume(value: number): void

    Parameters

    • value: number

    Returns void

Methods

  • The total duration of the audio clip in seconds. Loads the audio metadata if not already available.

    Returns Promise<number>

    A promise that resolves with the duration in seconds.

  • Pauses playback at the current position. Call play to resume.

    Returns void

  • Plays the audio clip from the current position.

    Returns Promise<void>

    A promise that resolves when playback finishes, or rejects on error. If the clip is looping, the promise will never resolve on its own – call stop or pause to end playback.

  • Seeks to a normalized position (0–1) in the clip.

    Parameters

    • position: number

      A value between 0 (start) and 1 (end).

    Returns void

  • Stops playback and resets the position to the beginning.

    Returns void