What You'll Build

In this tutorial, you'll build a modern JavaScript music player entirely from scratch — no libraries, no frameworks, just clean HTML, CSS, and vanilla JavaScript. The result is a production-quality audio player with a dynamic playlist, full playback controls, and animated visual effects.

This project is ideal if you want to practice DOM manipulation, event-driven programming, and the HTML5 Audio API — all in one real-world project.


Key Features of This Music Player

▶️
Play / Pause / Skip
Full playback controls including next and previous track navigation.
📋
Dynamic Playlist
Songs are rendered from a JavaScript array — easy to add or remove tracks.
📊
Seekable Progress Bar
Click or drag anywhere on the bar to jump to that position in the track.
🔀
Shuffle Mode
Randomly selects the next song using Math.random() with duplicate prevention.
🔁
Repeat Mode
Loops the current track automatically when it ends.
🔊
Volume & Mute
Adjustable volume slider with mute toggle and dynamic icon updates.
Animated Particles
Visual particle effects and rotating album art for a premium feel.
📱
Fully Responsive
Adapts cleanly from mobile to desktop with CSS media queries.

Get the Source Code

The full source code is split across three files — index.html, style.css, and script.js. Explore the code below or wait for the countdown to get the GitHub Gist link.

HTML — index.html
CSS — style.css
/* Loading source code... */
JS — script.js
// Loading source code...
💡 Quick Start: Download all three files into the same folder and open index.html in your browser. No server or build process required — the site runs immediately.

Full source code available after countdown

10 seconds...

How It Works — Step by Step

Let's break down the eight core building blocks of this JavaScript music player so you fully understand what each part does.

01

Songs Data Structure

A JavaScript array stores each song as an object with title, artist, cover, and src properties. This flat structure makes it trivial to add tracks — just push a new object into the array.

02

Centralized State Object

A single state object tracks isPlaying, currentIndex, isShuffle, isRepeat, and volume. Centralizing state prevents bugs from scattered variables and makes the app easy to reason about.

03

Playback Controls with the Audio API

togglePlay() calls audio.play() or audio.pause() based on state.isPlaying. nextSong() and prevSong() update the index and call loadSong() to swap the audio source and album art.

04

Seekable Progress Bar

The timeupdate event fires continuously during playback. Each time it fires, the progress bar's width is recalculated as (currentTime / duration) × 100%. A click listener on the bar reverses this formula to allow seeking.

05

Shuffle & Repeat Modes

Shuffle uses Math.floor(Math.random() × songs.length) to pick a random index, with a loop to avoid the current track. Repeat sets audio.currentTime = 0 and replays when the ended event fires. Toast notifications confirm mode changes.

06

Volume Slider & Mute Toggle

An input[type="range"] element controls audio.volume (0–1). Mute stores the last volume level and sets it to 0. The volume icon swaps between muted, low, and high dynamically based on the current value.

07

Dynamic Playlist Rendering

The playlist is built programmatically: the songs array is mapped with Array.forEach() to create DOM elements, injected into a container, and assigned click listeners that call loadSong(index) directly.

08

CSS Animations & Particle Effects

The album art uses a CSS @keyframes spin animation. Particle effects are generated as absolutely-positioned <span> elements with randomized positions, opacity, and animation delays — no canvas or library needed.


JavaScript Concepts Used in This Project

This project is a great way to practice these fundamental JavaScript skills:

  • HTML5 Audio APIplay(), pause(), currentTime, duration, volume, and audio events like timeupdate and ended.
  • DOM Manipulation — Dynamically creating, querying, and updating elements with querySelector, innerHTML, and style.
  • Event Listeners — Responding to click, input, and audio events in an organized, non-repetitive way.
  • State Management — Keeping all mutable values in a single object to avoid bugs and improve readability.
  • Array Methods — Using forEach and Math.random() for playlist rendering and shuffle logic.
  • CSS Custom Properties & Animations — Using @keyframes, animation-delay, and transitions for visual effects.
⚠️ Browser Note: The HTML5 Audio API requires audio files to be served from a server (or localhost) when using relative paths. Opening index.html directly as a file:// URL may block audio loading in some browsers due to CORS policy.

Frequently Asked Questions

How do I build a music player with JavaScript?
Create a songs array with object properties (title, artist, cover, src). Use the HTML5 Audio API to control playback. Add event listeners for play, pause, next, and previous controls. Use the timeupdate event to sync the progress bar. Implement shuffle and repeat logic. Full source code is available in the tabs above.
Can I build this music player without any JavaScript framework?
Yes — this tutorial uses pure vanilla JavaScript with no jQuery, React, Vue, or any external library. You only need the native HTML5 Audio API and standard DOM methods.
How does shuffle mode work in JavaScript?
Shuffle uses Math.floor(Math.random() * songs.length) to pick a random index. A while loop ensures the new index doesn't match the current song, preventing the same track from playing twice in a row.
How do I add a seekable progress bar to an HTML audio player?
Listen to the timeupdate event on your audio element. Calculate (audio.currentTime / audio.duration) * 100 and set your progress bar's width to that percentage. For seeking, add a click listener to the bar: set audio.currentTime = (clickX / barWidth) * audio.duration.
Is this music player mobile-responsive?
Yes. The layout uses flexible CSS units (clamp(), percentages), CSS Grid/Flexbox, and media queries to ensure the player looks and works great on screens from 320px mobile to large desktop monitors.
How do I add my own songs to this player?
Open the JavaScript file and find the songs array at the top. Add a new object following the same pattern — { title: "...", artist: "...", cover: "path/to/cover.jpg", src: "path/to/audio.mp3" } — and the player will automatically include it in the playlist.

Conclusion

This music player project demonstrates how far vanilla JavaScript can take you. By combining the HTML5 Audio API, DOM manipulation, CSS animations, and a clean state management pattern, you get a professional, feature-rich audio player — with zero dependencies.

Whether you're a beginner looking to level up or an intermediate developer practicing real-world patterns, this project covers the concepts that matter most. Copy the source code above, add your own tracks, and make it your own.

Found this useful? Share it with other developers, and check out the related projects in the sidebar.