My Blog

My WordPress Blog

My Blog

My WordPress Blog

Music Player

HTML

htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;Simple Music Player&lt;/title&gt;
&lt;link rel="stylesheet" href="styles.css"&gt;
</head> <body>
&lt;div class="music-player"&gt;
    &lt;h2 id="track-title"&gt;Track Title&lt;/h2&gt;
    &lt;audio id="audio" controls&gt;
        &lt;source src="your-audio-file.mp3" type="audio/mpeg"&gt;
        Your browser does not support the audio element.
    &lt;/audio&gt;
    &lt;div class="controls"&gt;
        &lt;button id="play"&gt;Play&lt;/button&gt;
        &lt;button id="pause"&gt;Pause&lt;/button&gt;
        &lt;button id="stop"&gt;Stop&lt;/button&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;script src="script.js"&gt;&lt;/script&gt;
</body> </html>

CSS (styles.css)

cssCopy codebody {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
} .music-player {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
text-align: center;
} .controls button {
margin: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}

JavaScript (script.js)

javascriptCopy codeconst audio = document.getElementById('audio');
const playButton = document.getElementById('play');
const pauseButton = document.getElementById('pause');
const stopButton = document.getElementById('stop');
const trackTitle = document.getElementById('track-title');

playButton.addEventListener('click', () => {
audio.play();
trackTitle.textContent = "Playing: " + audio.currentSrc.split('/').pop();
}); pauseButton.addEventListener('click', () => {
audio.pause();
}); stopButton.addEventListener('click', () => {
audio.pause();
audio.currentTime = 0; // Reset to start
trackTitle.textContent = "Track Title";
});

Instructions

  1. Set Up Your Files: Create three files in the same directory: index.html, styles.css, and script.js.
  2. Add Your Audio File: Replace "your-audio-file.mp3" in the HTML code with the path to your audio file.
  3. Open in a Browser: Open the index.html file in your web browser to test the music player.
Music Player

Leave a Reply

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

Scroll to top