react-pro-audio-player
Version:
A React component for playing collection of audio files with controls for forward, backward, play, pause, loop, sound control, and playback speed.
239 lines (172 loc) โข 7.74 kB
Markdown
# Custom Audio Player



A modern, **fully customizable** React **Audio Player** with support for **playlists, progress tracking, volume control, loop, playback speed adjustments**, and more.
## ๐ฎ Demo

## ๐ Table of Contents
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Hybrid Component](#hybrid-component)
- [Uncontrolled Component](#uncontrolled-component)
- [Props](#props)
- [Styling & Customization](#styling--customization)
- [Contributing](#contributing)
- [Changelog](#changelog)
- [License](#license)
## ๐ Features
โ
Fully customizable UI with Tailwind CSS support
โ
Supports **playlist playback**
โ
**Hybrid & Uncontrolled** component usage
โ
**Loop, volume, and playback speed controls**
โ
Supports **click-to-play** song selection
โ
Smooth **progress tracking and seek**
โ
Lightweight and optimized for performance
## ๐ฆ Installation
You can install this package via npm or yarn:
```sh
npm install react-pro-audio-player
```
or
```sh
yarn add react-pro-audio-player
```
## ๐ Usage
### 1๏ธโฃ Hybrid Component
A **hybrid component** gives you **full control** over the player's state, such as `isPlaying` and `currentSongIndex`, allowing external management of playback.
```jsx
import React, { useState } from "react";
import CustomAudioPlayer from "react-pro-audio-player";
const songsList = [
{ id: 1, url: "./assets/song1.mp3", title: "Song One", thumbnail:"Song Image" , singer:"Song Singer"},
{ id: 2, url: "./assets/song2.mp3", title: "Song Two", thumbnail:"Song Image" , singer:"Song Singer" },
{ id: 3, url: "./assets/song3.mp3", title: "Song Three", thumbnail:"Song Image" , singer:"Song Singer" },
];
const App = () => {
const [songs, setSongs] = useState(songsList);
const [isPlaying, setIsPlaying] = useState(false);
const [currentSongIndex, setCurrentSongIndex] = useState(null);
return (
<>
<div>
{songs.map((song, index) => (
<div key={song.id} onClick={() => setCurrentSongIndex(index)}>
{song.title}
</div>
))}
</div>
{currentSongIndex !== null && (
<CustomAudioPlayer
songs={songs}
isPlaying={isPlaying}
currentSongIndex={currentSongIndex}
onPlayPauseChange={setIsPlaying}
onSongChange={setCurrentSongIndex}
songUrlKey="url"
songNameKey="title"
songThumbnailKey="thumbnail"
songSingerKey="singer"
/>
)}
</>
);
};
export default App;
```
### 2๏ธโฃ Uncontrolled Component
If you want **default behavior** with minimal setup, use the **uncontrolled component**. This does not require managing state externally.
```jsx
import CustomAudioPlayer from "react-pro-audio-player";
const songsList = [
{ id: 1, url: "./assets/song1.mp3", title: "Song One" , thumbnail:"Song Image" , singer:"Song Singer" },
{ id: 2, url: "./assets/song2.mp3", title: "Song Two" , thumbnail:"Song Image" , singer:"Song Singer"},
{ id: 3, url: "./assets/song3.mp3", title: "Song Three" , thumbnail:"Song Image" , singer:"Song Singer" },
];
const App = () => {
return (
<CustomAudioPlayer songs={songsList} songUrlKey="url" songNameKey="title" songThumbnailKey="thumbnail" songSingerKey="singer" />
);
};
export default App;
```
## ๐๏ธ Props
| Prop | Type | Required | Description |
| ------------------------ | ---------- | -------- | ------------------------------------------------------------- |
| `songs` | `Array` | โ
Yes | List of songs with `{ id, url, title }` objects. |
| `songUrlKey` | `string` | โ
Yes | The key name in the song object that stores the song URL |
| `songNameKey` | `string` | โ No | The key name in the song object that stores the song name |
| `songThumbnailKey` | `string` | โ No | The key name in the song object that stores the song image |
| `songSingerKey` | `string` | โ No | The key name in the song object that stores the song singers |
| `isPlaying` | `boolean` | โ No | Controls playback state (Hybrid mode only). |
| `currentSongIndex` | `number` | โ No | Index of the currently playing song (Hybrid mode only). |
| `onPlayPauseChange` | `function` | โ No | Callback function to toggle play/pause (Hybrid mode only). |
| `onSongChange` | `function` | โ No | Callback function when song changes (Hybrid mode only). |
๐ **Note:**
- Props `isPlaying`, `currentSongIndex`, `onPlayPauseChange`, and `onSongChange` should be used **together** in **Hybrid mode**.
## ๐จ Styling & Customization
You can customize the player with **CSS classes**. The default styles use Tailwind-like classes:
| Class Name | Description |
| ---------------------- | ------------------------------------------ |
| `.custom-audio-player` | Main player container |
| `.audio-player` | Inner audio player container |
| `.progress-bar` | Custom progress bar styling |
| `.controls-wrapper` | Wrapper for playback controls |
| `.playback-btn` | Button for play, pause, next, and previous |
| `.volume-slider` | Volume control styling |
| `.playback-speed` | Dropdown for speed selection |
## ๐ญ Customization
You can enhance the audio player by adding more styles or modifying props. Example of custom styling:
```css
.custom-audio-player {
background-color: #333;
color: #fff;
padding: 10px;
border-radius: 8px;
}
```
But need to import the `CustomAudioPlayer.css` file:
```jsx
import "react-pro-audio-player/src/CustomAudioPlayer.css";
```
Before the global css file
```jsx
import "react-pro-audio-player/src/CustomAudioPlayer.css";
import "./index.css";
```
## ๐ค Contributing
We welcome contributions! Hereโs how you can help:
1. Fork the repository.
2. Create a new branch (`feature-xyz`).
3. Make your changes.
4. Submit a pull request.
GitHub Repository: [CustomAudioPlayer](https://github.com/pranavsinggh/CustomAudioPlayer-npm-package)
## ๐ Changelog
### v3.0.0 - (Latest Release)
- ๐จ **Revamped UI with Tailwind CSS support**
- ๐ต **Added songs details to be displayed**
### v2.0.0 -
- ๐ต **Added Hybrid & Uncontrolled component support**
- ๐ **Improved progress tracking and seek function**
- ๐ **Loop & playback speed controls** added
- ๐จ **Revamped UI with Tailwind CSS support**
- ๐ **Optimized state management for better performance**
### v1.0.0
- **Initial release with core audio playback features.**
- **Play/Pause, Forward/Backward, Volume Control, Loop, and Playback Speed.**
- **Responsive UI.**
## ๐ License
This package is open-source and available under the **MIT License**. Feel free to use and modify it as needed!
๐ **Enjoy building with Custom Audio Player!** ๐ง