UNPKG

react-sounds

Version:

A library of ready-to-play sound effects for React applications.

71 lines (70 loc) 2.44 kB
import { Howl } from "howler"; import { LibrarySoundName, SoundOptions } from "./types"; export type SoundName = LibrarySoundName | string; export declare function isLibrarySoundName(name: any): name is LibrarySoundName; /** * Set a custom CDN base URL */ export declare function setCDNUrl(url: string): void; /** * Get the CDN base URL */ export declare function getCDNUrl(): string; export type HowlEntry = { instance: Howl; subscriptions: number; }; /** * Make a sound loader function that first tries local files then falls back to CDN */ export declare function makeRemoteSound(name: SoundName): () => Promise<HowlEntry>; export declare function claimSound(name: SoundName): Promise<Howl>; export declare function freeSound(name: SoundName): null; /** * Fetches sound data as a blob, from local filesystem or CDN */ export declare function fetchSoundBlob(name: SoundName): Promise<Blob>; /** * Preload multiple sounds by fetching their data without creating Howl instances */ export declare function preloadSounds(names: SoundName[]): Promise<void[]>; /** * Play a sound by name */ export declare function playSound(name: SoundName, options?: SoundOptions): Promise<void>; /** * Check if sounds are enabled */ export declare function isSoundEnabled(): boolean; /** * Enable or disable all sounds * This is used internally by the SoundProvider */ export declare function setSoundEnabled(enabled: boolean): void; /** * Initialize the sound enabled state * Called at startup to load saved preference */ export declare function initSoundEnabledState(): void; /** * Subscribe to sound enabled state changes */ export declare function subscribeSoundState(callback: (enabled: boolean) => void): () => void; /** * Get the local path for a sound when using the offline mode */ export declare function getLocalSoundPath(name: SoundName): Promise<string | null>; /** * Clean up unused Howl instances and blob cache entries * Call this when running into memory constraints */ export declare function cleanupUnusedSound(name: string): void; /** * Unlock the audio context globally to allow playback without direct user interaction */ export declare function unlockAudioContext(): Promise<void>; /** * Setup global event listeners to unlock audio context on user interaction * Can be called multiple times safely (will only set up listeners once) */ export declare function initAudioContextUnlock(): () => void;