Skip to content

Getting Started

Installation

Welcome to the Audio Player documentation. To get started, follow the steps below:

  1. Install the package using npm:

    bash
    npm install react-audio-play
  2. Alternatively, install with yarn:

    bash
    yarn add react-audio-play
  3. Import and use the AudioPlayer component in your project:

    js
    import { AudioPlayer } from 'react-audio-play';
    
    export default function App() {
      return <AudioPlayer src="path/to/audio.mp3" />;
    }
  4. Customize the appearance and functionality as needed.

Props

The AudioPlayer component accepts the following props:

PropTypeDescription
classNamestring, optionalA CSS class name for styling the component.
srcstring, requiredThe URL or file path of the audio file to be played.
autoPlayboolean, optionalSet to true to autoplay the audio (default: false).
preloadstring, optional
  • auto - The audio data is loaded as soon as possible.
  • metadata - Only metadata (e.g., duration) is loaded.
  • none - No preloading. Audio data is only loaded when requested.
loopboolean, optionalSet to true to loop the audio playback (default: false).
volumenumber, optionalThe initial volume level (0 to 100) of the audio (default: 100).
hasKeyBindingsboolean, optionalSpecifies whether the AudioPlayer component should enable keyboard shortcuts for volume control and seeking (default: true).
onPlayfunction, optionalCallback function to execute when the audio starts playing.
onPausefunction, optionalCallback function to execute when the audio is paused.
onEndfunction, optionalCallback function to execute when the audio playback ends.
onErrorfunction, optionalCallback function to execute if there’s an error loading or playing the audio.
backgroundColorstring, optionalSet the background color of the audio player (default: #fff).
colorstring, optionalThe text and icon color of the audio player (default: #566574).
sliderColorstring, optionalThe color of the progress slider (default: #007FFF).
volumePlacementstring, optionalControl where the volume controls are located (`top
widthstring, optionalThe width of the audio player. For example, "100%", "300px", etc.
styleobject, optionalAn object containing additional inline styles for the component.

Advanced Usage

Starting with version v1.0.4, you can access certain actions of the AudioPlayer component programmatically using a ref with the following interface:

  • play: Function to start audio playback.
  • pause: Function to pause audio playback.
  • stop: Function to stop the audio playback.
  • focus: Function to focus on the audio player component.

Keyboard Shortcuts

Below are the keyboard shortcuts available when the audio player is focused. They can be turned off by setting the hasKeyBindings prop to false.

KeyAction
SpacePlay/Pause
Rewind
Forward
Volume up
Volume down