@nafr/echo-ui
Version:
A UI library born for WAA
81 lines (80 loc) • 3.69 kB
TypeScript
/// <reference types="react" />
import * as Tone from 'tone';
export interface UsePlayerProps {
volume?: number;
loop?: boolean;
mute?: boolean;
onReady?: () => void;
onPlay?: () => void;
onPause?: () => void;
onStop?: () => void;
onFinish?: () => void;
onError?: () => void;
}
/**
* usePlayer is a custom React hook that provides functionalities to play, pause, stop, and manage audio playback using Tone.js.
* It allows control over playback volume, looping, muting, and offers callbacks for various audio playback states.
*
* @param {UsePlayerProps} props - Configuration properties for the player.
* @param {number} props.volume - Initial volume for the player (default: 5).
* @param {boolean} props.loop - Whether the audio should loop (default: false).
* @param {boolean} props.mute - Whether the audio should be muted (default: false).
* @param {Function} props.onReady - Callback executed when the player is ready.
* @param {Function} props.onPlay - Callback executed when the audio starts playing.
* @param {Function} props.onPause - Callback executed when the audio is paused.
* @param {Function} props.onStop - Callback executed when the audio is stopped.
* @param {Function} props.onFinish - Callback executed when the audio finishes playing.
* @param {Function} props.onError - Callback executed in case of an error.
*
* @returns {object} An object containing various properties and methods for audio playback control:
* - player: The Tone.js Player instance.
* - audioDuration: The duration of the loaded audio.
* - isReady: Boolean indicating if the player is ready.
* - isPlaying: Boolean indicating if the audio is currently playing.
* - isFinish: Boolean indicating if the audio has finished playing.
* - volume: Current volume of the player.
* - loop: Boolean indicating if the audio is set to loop.
* - mute: Boolean indicating if the audio is muted.
* - time: Current playback time.
* - percentage: Current playback progress as a percentage.
* - pickTime: Method to set the playback start time.
* - init: Method to initialize the player with an AudioBuffer.
* - play: Method to start playback.
* - pause: Method to pause playback.
* - stop: Method to stop playback.
* - getTime: Method to get the current playback time.
* - setPickTime: Method to set a specific playback time.
* - setVolume: Method to set the volume.
* - setLoop: Method to toggle looping.
* - setMute: Method to toggle mute.
* - observe: Method to start observing playback state.
* - cancelObserve: Method to stop observing playback state.
* - error: Boolean indicating if an error has occurred.
* - errorMessage: The error message in case of an error.
*/
export declare const usePlayer: (props?: UsePlayerProps) => {
player: import("react").MutableRefObject<Tone.Player | null>;
audioDuration: import("react").MutableRefObject<number>;
isReady: boolean;
isPlaying: boolean;
isFinish: boolean;
volume: number;
loop: boolean;
mute: boolean;
time: number;
percentage: number;
pickTime: import("react").MutableRefObject<number>;
init: (audioBuffer: AudioBuffer, chain?: Tone.InputNode[]) => void;
play: () => void;
pause: () => void;
stop: () => void;
getTime: () => void;
setPickTime: (time: number) => void;
setVolume: import("react").Dispatch<import("react").SetStateAction<number>>;
setLoop: import("react").Dispatch<import("react").SetStateAction<boolean>>;
setMute: import("react").Dispatch<import("react").SetStateAction<boolean>>;
observe: () => void;
cancelObserve: () => void;
error: boolean;
errorMessage: string;
};