BCA Student Stories & UpdatesLive
Custom HTML5 Audio & Video Player (Media API & Real-time Progress Bar)
While modern browsers offer default native media controls via the controls attribute, commercial streaming platforms (YouTube, Netflix, Spotify) require fully customized player skins. The HTML5 Media API exposes rich methods and events that allow developers to design bespoke media interfaces with custom timelines, play buttons, and volume bars.
Day 44 Step-by-Step Practical Flow
🎯1. What You Will Learn Today
Custom HTML5 Audio & Video Player (Media API & Real-time Progress Bar) — Comprehensive Practical Guide
Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.
While modern browsers offer default native media controls via the controls attribute, commercial streaming platforms (YouTube, Netflix, Spotify) require fully customized player skins. The HTML5 Media API exposes rich methods and events that allow developers to design bespoke media interfaces with custom timelines, play buttons, and volume bars.
"Think of the HTML5 Media API like the remote control of a physical DVD/Blu-ray player: the play button sends a laser command to spin the disc, the timeline slider moves the laser head to a specific chapter, and the volume wheel adjusts the speaker amplifier."
👨🏫3. Teacher Demonstration Code
See how play/pause toggles, timeline progress scrubbing, and duration formatting are wired up using vanilla JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Custom Media Player</title>
<style>
body { font-family: system-ui, sans-serif; background: #060c18; color: #fff; padding: 24px; text-align: center; }
.player-card { max-width: 480px; margin: auto; background: #0c1328; border: 1px solid #1e293b; padding: 20px; border-radius: 12px; }
.progress-bar { width: 100%; height: 8px; background: #1e293b; border-radius: 4px; cursor: pointer; margin: 16px 0; position: relative; }
.progress-fill { width: 0%; height: 100%; background: #04AA6D; border-radius: 4px; }
.controls { display: flex; justify-content: space-between; align-items: center; }
button { background: #2563eb; color: #fff; border: none; padding: 8px 16px; border-radius: 6px; cursor: pointer; font-weight: bold; }
</style>
</head>
<body>
<div class="player-card">
<h3>🎵 Nextsem Podcast Player</h3>
<audio id="audioTrack" src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"></audio>
<div class="progress-bar" id="progressBar"><div class="progress-fill" id="progressFill"></div></div>
<div class="controls">
<button id="playBtn">▶ Play</button>
<span id="timeDisplay" style="font-family:monospace;font-size:13px;">00:00 / 00:00</span>
<button id="muteBtn" style="background:#475569;">🔊</button>
</div>
</div>
<script>
const audio = document.getElementById("audioTrack");
const playBtn = document.getElementById("playBtn");
const fill = document.getElementById("progressFill");
playBtn.addEventListener("click", () => {
if (audio.paused) { audio.play(); playBtn.textContent = "⏸ Pause"; }
else { audio.pause(); playBtn.textContent = "▶ Play"; }
});
audio.addEventListener("timeupdate", () => {
const pct = (audio.currentTime / audio.duration) * 100;
fill.style.width = pct + "%";
});
</script>
</body>
</html>2. Hands-On Practical Lab: Follow Instructions in VS Code
Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.
Day 44 Practical Task Test Studio
Write an event listener on playBtn that checks video.paused and calls video.play() or video.pause().
Calculate the percentage of media played using (media.currentTime / media.duration) * 100.
Listen to input event on volumeSlider and set media.volume to e.target.value.
Spot the Bug: Unhandled Play Promise Rejection on Autoplay
Tap or click on the exact line below that contains the error:
Click any code linewindow.addEventListener("DOMContentLoaded", () => { const audio = document.getElementById("player"); audio.play(); // ❌ Fails on modern browsers!});Senior Developer Forensic Analysis:
Modern browsers block unmuted audio and video autoplay to protect users from unexpected noise and bandwidth consumption. media.play() returns a Promise which rejects if no user gesture (click or tap) has occurred yet. You must either start muted (video.muted = true) or wait for a user button click.
📝Independent Practice: Assignment
📄 index.htmlUniversity Video Lecture Mini-Player Studio
- •Build a responsive custom video player with play/pause, restart, and 10s skip (+/- 10s) buttons.
- •Implement an interactive timeline slider that highlights the elapsed time with emerald green color (#04AA6D).
- •Display elapsed time and total duration formatted neatly as mm:ss (e.g. 02:45 / 15:30).
- •Add a volume control slider and fullscreen toggle button using document.fullscreenElement / requestFullscreen().
Open Visual Studio Code on your PC (code .), create index.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BCA Day 44 - Video Lecture Player</title>
<style>
body { font-family: "Segoe UI", sans-serif; background: #060c18; color: #fff; padding: 24px; text-align: center; }
.player-wrap { max-width: 640px; margin: auto; background: #0c1328; border: 1px solid #1e293b; border-radius: 12px; overflow: hidden; }
video { width: 100%; display: block; background: #000; }
.controls { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.timeline { width: 100%; height: 6px; background: #1e293b; border-radius: 3px; cursor: pointer; position: relative; }
.timeline-fill { width: 0%; height: 100%; background: #04AA6D; border-radius: 3px; }
.btn-row { display: flex; justify-content: space-between; align-items: center; }
button { padding: 6px 14px; background: #2563eb; color: #fff; border: none; border-radius: 6px; font-weight: bold; cursor: pointer; }
</style>
</head>
<body>
<div class="player-wrap">
<video id="vid" src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"></video>
<div class="controls">
<div class="timeline" id="timeline"><div class="timeline-fill" id="fill"></div></div>
<div class="btn-row">
<button id="playBtn">▶ Play</button>
<span id="timeText" style="font-family:monospace;font-size:12px;">00:00 / 00:00</span>
</div>
</div>
</div>
<script>
const vid = document.getElementById("vid");
const playBtn = document.getElementById("playBtn");
const fill = document.getElementById("fill");
const timeText = document.getElementById("timeText");
function formatTime(sec) {
if (isNaN(sec)) return "00:00";
const m = Math.floor(sec / 60);
const s = Math.floor(sec % 60);
return (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
}
playBtn.addEventListener("click", () => {
if (vid.paused) { vid.play(); playBtn.textContent = "⏸ Pause"; }
else { vid.pause(); playBtn.textContent = "▶ Play"; }
});
vid.addEventListener("timeupdate", () => {
const pct = (vid.currentTime / vid.duration) * 100;
fill.style.width = pct + "%";
timeText.textContent = formatTime(vid.currentTime) + " / " + formatTime(vid.duration);
});
</script>
</body>
</html>🎓4. Viva Check: Mandatory Conceptual Test
Why is it necessary to call e.preventDefault() during the dragover event when implementing HTML5 Drag and Drop?
5. Mandatory Step: Push to GitHub to Unlock Next Lesson
Target Repo: html-css-projects
Exact GitHub Setup Guide & Expected Structure
Day 44 RequirementsFollow these exact folder and file names to prevent verification errors.
index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.day-44/ for today so you don't overwrite previous days.main.You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 44 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 43: Building an Interactive Digital Signature & Sketch Pad (Canvas Mouse & Touch Events) first.
Complete Day 43 First