import { Observable } from "@babylonjs/core/Misc/observable"; import type { Nullable } from "@babylonjs/core/types"; import type { IDisposeObservable } from "../IDisposeObserable"; import type { IAudioPlayer } from "./IAudioPlayer"; /** * Audio element pool interface * * Pass to the constructor of `StreamAudioPlayer` to pool the audio element */ export interface IAudioElementPool { /** * Rent the audio element * @returns The audio element */ rent(): HTMLAudioElement; /** * Return the audio element * * Returned audio element should not be used anymore * @param audioElement The audio element */ return(audioElement: HTMLAudioElement): void; } /** * This class is used to pooling the audio element */ export declare class AudioElementPool implements IAudioElementPool { private readonly _audioElements; /** * Rent the audio element * @returns The audio element */ rent(): HTMLAudioElement; /** * Return the audio element * * Returned audio element should not be used anymore * @param audioElement The audio element */ return(audioElement: HTMLAudioElement): void; } /** * Stream audio player options */ export interface IStreamAudioPlayerOptions { /** * Whether to pooling the audio element * * If `true`, the `StreamAudioPlayer.DefaultAudioElementPool` is used as the audio element pool * If `false`, the audio element is created on demand * If `IAudioElementPool`, the specified audio element pool is used * * Default is `false` */ pool?: boolean | IAudioElementPool; } /** * Stream audio player * * This class is used to play the audio from the stream * * It is suitable for playing long sounds because it plays even if all of the audio is not loaded * * Wrapper of `HTMLAudioElement` which handles audio playback permission issues gracefully */ export declare class StreamAudioPlayer implements IAudioPlayer { /** * Default global audio element pool instance */ static readonly DefaultAudioElementPool: IAudioElementPool; /** * On load error observable * * This observable is notified when the audio load is failed */ readonly onLoadErrorObservable: Observable; /** * On duration changed observable * * This observable is notified when the audio duration is changed */ readonly onDurationChangedObservable: Observable; /** * On mute state changed observable * * This observable is notified when the mute state is changed */ readonly onPlaybackRateChangedObservable: Observable; /** * On mute state changed observable * * This observable is notified when the mute state is changed */ readonly onMuteStateChangedObservable: Observable; /** * On play observable * * This observable is notified when the player is played */ readonly onPlayObservable: Observable; /** * On pause observable * * This observable is notified when the player is paused */ readonly onPauseObservable: Observable; /** * On seek observable * * This observable is notified when the player is seeked */ readonly onSeekObservable: Observable; private readonly _pool; private _audio; private _duration; private _playbackRate; private _isVirtualPlay; private _virtualStartTime; private _virtualPaused; private _virtualPauseCurrentTime; private _metadataLoaded; private _disposeObserver; /** * Create a stream audio player * * In general disposeObservable should be `Scene` of Babylon.js * * @param disposeObservable Objects that limit the lifetime of this instance * @param options Options */ constructor(disposeObservable: Nullable, options?: IStreamAudioPlayerOptions); private readonly _onDurationChanged; private readonly _onLoadError; private readonly _onPlay; private readonly _onPause; private _ignoreSeekedEventOnce; private readonly _onSeek; /** * Audio duration (in seconds) */ get duration(): number; /** * Current time (in seconds) * * This property may be slow to update */ get currentTime(): number; set currentTime(value: number); /** @internal */ _setCurrentTimeWithoutNotify(value: number): void; /** * Volume (0.0 to 1.0) */ get volume(): number; set volume(value: number); /** * Whether the audio is muted */ get muted(): boolean; /** * Mute the audio */ mute(): void; /** * Unmute the audio * * Unmute is possible failed if user interaction is not performed * @returns Whether the audio is unmuted */ unmute(): Promise; /** * Playback rate (0.07 to 16.0) */ get playbackRate(): number; set playbackRate(value: number); /** @internal */ _setPlaybackRateWithoutNotify(value: number): void; /** * Determines whether or not the browser should adjust the pitch of the audio to compensate for changes to the playback rate made by setting */ get preservesPitch(): boolean; set preservesPitch(value: boolean); /** * Whether the player is paused */ get paused(): boolean; /** * Audio source URL */ get source(): string; set source(value: string); /** * Whether the audio metadata(durations) is loaded */ get metadataLoaded(): boolean; private _virtualPlayAsync; private _playRequestBlocking; /** * Play the audio from the current position * * If context don't have permission to play the audio, play audio in a mute state */ play(): Promise; /** * Pause the audio */ pause(): void; /** * Dispose the player */ dispose(): void; }