Skip to content
Tutorials Center
My learning

Topics

Lesson 116 of 132 ยท Projects

Project: Music Player

Build a music player in React - drive the audio element with a ref, keep play state in sync with its events, manage a playlist index and show a progress bar. Uses the site's own chime files.

A music player is a perfect lesson in React meets the outside world. The <audio> element is not a React thing: it owns its own playback state (playing, paused, current time) and you control it with imperative methods such as play() and pause(). React's job is to hold the plan (which track, what the buttons say) and to stay in sync with what the element reports. You will use refs, effects and events.

We only have our own media, so the "playlist" uses the two chime recordings we ship (/samples/media/chime.mp3 and chime.ogg), each about two seconds long, plus a third entry that reuses the MP3. Titles are labels, not real songs. Browsers also block audio until the user has interacted with the page, so nothing plays by itself: playback must start from a click, which is exactly what our Play button does. Short tracks let you watch "track ended, go to next" happen quickly.

Stage 1: play and pause with a ref

The element reports what actually happened through events (onPlay, onPause). Instead of guessing, we set our playing state from those events. Then the button label is always true, even if the browser refuses to play or the track ends by itself.

Stage 1: audio element and a ref

audio.play() returns a promise, and it rejects when the browser blocks playback or the file cannot load; always handle that. Two <source> elements let the browser pick a format it supports, as in our HTML audio lesson.

Stage 2: a playlist

The playlist is data; the current track is just an index. Next and Previous change the index (wrapping around with the modulo operator). When the index changes, the element needs the new src loaded; setting src through the key prop on the element would remount it, but a simpler way is to set the src attribute and call play() in an effect if the user was already playing.

Stage 2: playlist and wrapping

Two details to notice. wasPlaying is a ref, not state, because it is a note to the next effect and must not cause a render. And onEnded calls go(1, true): browsers fire pause just before ended, so playing is already false by then, and we pass "keep playing" explicitly. A finished track moves on and continues. Changing src makes the browser pause the old playback, so we ask for play() again once the index has changed.

Stage 3: progress bar and seeking

The element emits timeupdate several times a second. We store the current time and duration and show them in a <progress>-style range input that also lets the user seek by writing currentTime back.

Stage 3: progress and seek

The range inputs are ordinary controlled inputs: their value comes from state, and onChange pushes the new value to the element. The element stays the real source of truth for time; React state mirrors it for display.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why set the playing state from the onPlay and onPause events instead of inside the click handler?

    The audio element owns playback. Mirroring its events keeps the UI correct in cases a click handler cannot predict.
  2. 2. Why is wasPlaying a ref rather than state in the playlist stage?

    Refs store values that must survive renders without causing them. Here it records intent for the effect that runs after the index changes.
  3. 3. How does the Next button wrap from the last track to the first?

    The modulo operator keeps the index inside the array; adding length first avoids negative results when stepping backwards.

Summary

  • Imperative elements like <audio> are controlled through a ref; React keeps a mirror of their state.
  • Set UI state from the element's events, and handle the promise from play().
  • A playlist is an array plus an index; wrap with modulo.
  • Use refs for notes that must not cause renders, and write currentTime and volume to seek and change loudness.
  • Playback always starts from a user gesture, never on load.

Next: Project: Snake Game