A customizable React audio player component with TypeScript support, mobile compatibility, and comprehensive accessibility features
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Comprehensive documentation of all props and event handlers available for the H5AudioPlayer component.
import AudioPlayer from 'react-h5-audio-player';
import { ReactNode, CSSProperties } from 'react';Standard HTML5 audio element properties directly supported by the component.
interface CoreAudioProps {
/** HTML5 Audio tag src property */
src?: string;
/** HTML5 Audio tag autoPlay property */
autoPlay?: boolean;
/** HTML5 Audio tag loop property */
loop?: boolean;
/** HTML5 Audio tag muted property */
muted?: boolean;
/** Volume level from 0 to 1 (won't work on most mobile devices) */
volume?: number;
/** HTML5 Audio tag preload property */
preload?: AUDIO_PRELOAD_ATTRIBUTE;
/** CORS attribute for cross-origin requests */
crossOrigin?: React.AudioHTMLAttributes<HTMLAudioElement>['crossOrigin'];
/** Media group for synchronized playback */
mediaGroup?: string;
}Props that control how the audio player behaves and responds to user interactions.
interface BehaviorProps {
/** Whether to play audio after src prop is changed */
autoPlayAfterSrcChange?: boolean;
/** Enable/disable default keyboard shortcuts */
hasDefaultKeyBindings?: boolean;
/** Time interval in milliseconds to trigger onListen event */
listenInterval?: number;
/** Progress indicator refresh interval in milliseconds */
progressUpdateInterval?: number;
/** Default jump step for rewind/forward in milliseconds */
progressJumpStep?: number;
/** Separate backward/forward jump steps in milliseconds */
progressJumpSteps?: {
backward?: number;
forward?: number;
};
/** Volume increment for up/down arrow keys */
volumeJumpStep?: number;
}Default Values:
progressJumpStep: 5000 (5 seconds)progressJumpSteps: { backward: 5000, forward: 5000 }hasDefaultKeyBindings: trueprogressUpdateInterval: 20mslistenInterval: 1000msProps controlling the visual appearance and layout of the player interface.
interface UIDisplayProps {
/** Custom CSS classes */
className?: string;
/** Inline CSS styles */
style?: CSSProperties;
/** Layout orientation */
layout?: MAIN_LAYOUT;
/** Show rewind/forward buttons */
showJumpControls?: boolean;
/** Show previous/next buttons */
showSkipControls?: boolean;
/** Show download progress indicator */
showDownloadProgress?: boolean;
/** Show filled progress indicator */
showFilledProgress?: boolean;
/** Show filled volume indicator */
showFilledVolume?: boolean;
/** Time display format */
timeFormat?: TIME_FORMAT;
}Default Values:
layout: 'stacked'showJumpControls: trueshowSkipControls: falseshowDownloadProgress: trueshowFilledProgress: trueshowFilledVolume: falsetimeFormat: 'auto'Props for adding custom content and default display values.
interface CustomContentProps {
/** Custom header content above the player */
header?: ReactNode;
/** Custom footer content below the player */
footer?: ReactNode;
/** Default current time display when audio not loaded */
defaultCurrentTime?: ReactNode;
/** Default duration display when audio not loaded */
defaultDuration?: ReactNode;
/** Child elements (e.g., <track> elements for captions) */
children?: ReactNode;
}Standard HTML5 audio events with optional callback handlers.
interface HTML5AudioEvents {
/** Fired when playback is aborted */
onAbort?: (e: Event) => void;
/** Fired when enough data is available to start playing */
onCanPlay?: (e: Event) => void;
/** Fired when enough data is available to play through without buffering */
onCanPlayThrough?: (e: Event) => void;
/** Fired when playback reaches the end */
onEnded?: (e: Event) => void;
/** Fired when playback starts after being paused or delayed */
onPlaying?: (e: Event) => void;
/** Fired when a seek operation begins */
onSeeking?: (e: Event) => void;
/** Fired when a seek operation completes */
onSeeked?: (e: Event) => void;
/** Fired when data loading is stalled */
onStalled?: (e: Event) => void;
/** Fired when data loading is suspended */
onSuspend?: (e: Event) => void;
/** Fired when loading begins */
onLoadStart?: (e: Event) => void;
/** Fired when metadata has finished loading */
onLoadedMetaData?: (e: Event) => void;
/** Fired when the first frame has finished loading */
onLoadedData?: (e: Event) => void;
/** Fired when playback is waiting for more data */
onWaiting?: (e: Event) => void;
/** Fired when the media element is reset to its initial state */
onEmptied?: (e: Event) => void;
/** Fired when an error occurs during loading or playback */
onError?: (e: Event) => void;
/** Fired during playback at intervals specified by listenInterval */
onListen?: (e: Event) => void;
/** Fired when volume changes */
onVolumeChange?: (e: Event) => void;
/** Fired when playback is paused */
onPause?: (e: Event) => void;
/** Fired when playback starts */
onPlay?: (e: Event) => void;
}Event handlers for custom player controls and interactions.
interface CustomControlEvents {
/** Called when previous button is clicked */
onClickPrevious?: (e: React.SyntheticEvent) => void;
/** Called when next button is clicked */
onClickNext?: (e: React.SyntheticEvent) => void;
/** Called when play() promise is rejected */
onPlayError?: (err: Error) => void;
/** Called when currentTime change fails */
onChangeCurrentTimeError?: (err: Error) => void;
}The complete interface combining all prop categories:
interface PlayerProps extends
CoreAudioProps,
BehaviorProps,
UIDisplayProps,
CustomContentProps,
HTML5AudioEvents,
CustomControlEvents {
// UI Customization (see UI Customization & Layout doc)
customIcons?: CustomIcons;
customProgressBarSection?: CustomUIModules;
customControlsSection?: CustomUIModules;
customAdditionalControls?: CustomUIModules;
customVolumeControls?: CustomUIModules;
i18nAriaLabels?: I18nAriaLabels;
// Advanced Features (see Advanced Features & MSE doc)
mse?: MSEPropsObject;
}When hasDefaultKeyBindings is true (default), these keyboard shortcuts are available:
| Key | Action |
|---|---|
| Space | Play/Pause (when player or progress bar is focused) |
| ← (Left Arrow) | Rewind by progressJumpStep |
| → (Right Arrow) | Forward by progressJumpStep |
| ↑ (Up Arrow) | Increase volume by volumeJumpStep |
| ↓ (Down Arrow) | Decrease volume by volumeJumpStep |
| L | Toggle loop mode |
| M | Toggle mute |
Basic Configuration:
<AudioPlayer
src="audio.mp3"
autoPlay={false}
loop={false}
volume={0.8}
onPlay={e => console.log('Started playing')}
onPause={e => console.log('Paused')}
onEnded={e => console.log('Finished playing')}
/>Advanced Event Handling:
<AudioPlayer
src="podcast.mp3"
listenInterval={500}
onListen={e => {
const audio = e.target as HTMLAudioElement;
console.log('Current time:', audio.currentTime);
// Update progress, save position, etc.
}}
onLoadedMetaData={e => {
const audio = e.target as HTMLAudioElement;
console.log('Duration:', audio.duration);
}}
onError={e => {
console.error('Audio error:', e);
// Handle error, show message, etc.
}}
/>Custom Controls Integration:
const [currentTrack, setCurrentTrack] = useState(0);
const playlist = ['song1.mp3', 'song2.mp3', 'song3.mp3'];
<AudioPlayer
src={playlist[currentTrack]}
showSkipControls={true}
onClickPrevious={() => {
setCurrentTrack(prev => Math.max(0, prev - 1));
}}
onClickNext={() => {
setCurrentTrack(prev => Math.min(playlist.length - 1, prev + 1));
}}
onEnded={() => {
// Auto-advance to next track
if (currentTrack < playlist.length - 1) {
setCurrentTrack(prev => prev + 1);
}
}}
/>Install with Tessl CLI
npx tessl i tessl/npm-react-h5-audio-player