React Projects

Create an Audio Player with React and JavaScript

Spread the love

React is an easy to use JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create an audio player with React and JavaScript.

Create the Project

We can create the React project with Create React App.

To install it, we run:

npx create-react-app audio-player

with NPM to create our React project.

Create the Audio Player

To create the audio player, we write:

import React, { useRef, useState } from "react";

export default function App() {
  const audioPlayer = useRef();
  const [currentTime, setCurrentTime] = useState(0);
  const [seekValue, setSeekValue] = useState(0);

  const play = () => {;

  const pause = () => {

  const stop = () => {
    audioPlayer.current.currentTime = 0;

  const setSpeed = (speed) => {
    audioPlayer.current.playbackRate = speed;

  const onPlaying = () => {
      (audioPlayer.current.currentTime / audioPlayer.current.duration) * 100

  return (
    <div className="App">
        Your browser does not support the
        <code>audio</code> element.
      <br />
        onChange={(e) => {
          const seekto = audioPlayer.current.duration * ( / 100);
          audioPlayer.current.currentTime = seekto;
        <button onClick={play}>play</button>
        <button onClick={pause}>pause</button>
        <button onClick={stop}>stop</button>
        <button onClick={() => setSpeed(0.5)}>0.5x</button>
        <button onClick={() => setSpeed(1)}>1x</button>
        <button onClick={() => setSpeed(1.5)}>1.5x</button>
        <button onClick={() => setSpeed(2)}>2x</button>

We create the audioPlayer ref that we assign to the audio element.

Then we create the currentTime state to set the current time of the audio playback.

seekValue has the time value we seek to.

The play function gets the audioPlayer and call play on it to play the video.

The pause function calls pause on the audio element.

The stop function calls pause and also set the currentTime of the audio element to 0 to reset the play position back to the beginning.

The onPlaying function sets the currentTime to the audio element’s currentTime value in seconds.

setSeekValue sets the seekValue to a value between 0and 100.

Below that, we have the audio element that has the onTimeUpdate prop set to the onPlaying function to set the currentTime .

Then we show the currentTime to the user.

The range input lets us change its value by moving the slider dot.

value is set to seekValue which we can change and we change that with the onChange callback and the onPlaying function.

In the onChange prop, we also set the currentTime of the audio player element.

Below that, we have the play, pause, and stop buttons to call the respective functions when we click them.

And we have 3 buttons that call setSpeed when we click them.


We can create an audio player easily with React and JavaScript.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *