Creating a stylish audio player involves both CSS styling for the appearance and some HTML and JavaScript for functionality. Below is an example of a modern and stylish audio player using HTML, CSS, and JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Stylish Audio Player</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#audio-player {
background: linear-gradient(135deg, #6e8efb, #a777e3);
padding: 20px;
border-radius: 12px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
width: 300px;
text-align: center;
}
#audio-file {
display: none;
}
#upload-label {
padding: 10px 25px;
background-color: #fff;
color: #6e8efb;
border-radius: 30px;
cursor: pointer;
transition: background-color 0.3s ease, color 0.3s ease;
margin-bottom: 20px;
display: inline-block;
}
#upload-label:hover {
background-color: #e0e0e0;
color: #6e8efb;
}
#play-button, #pause-button {
padding: 10px 20px;
background-color: #fff;
border: none;
border-radius: 30px;
color: #6e8efb;
cursor: pointer;
transition: background-color 0.3s ease, color 0.3s ease;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
width: 100%;
}
#play-button:hover, #pause-button:hover {
background-color: #d4d4d4;
color: #6e8efb;
}
#progress-bar {
width: 100%;
height: 10px;
margin: 15px 0;
background-color: #fff;
border-radius: 5px;
overflow: hidden;
}
#progress {
height: 100%;
width: 0;
background-color: #a777e3;
transition: width 0.1s linear;
}
</style>
</head>
<body>
<div id="audio-player">
<label id="upload-label" for="audio-file">Upload Audio</label>
<input type="file" id="audio-file" accept="audio/*">
<div id="progress-bar">
<div id="progress"></div>
</div>
<button id="play-button">Play</button>
<button id="pause-button" style="display: none;">Pause</button>
<audio id="audio"></audio>
</div>
<script>
const audio = document.getElementById('audio');
const audioFileInput = document.getElementById('audio-file');
const playButton = document.getElementById('play-button');
const pauseButton = document.getElementById('pause-button');
const progressBar = document.getElementById('progress');
audioFileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const fileURL = URL.createObjectURL(file);
audio.src = fileURL;
playButton.style.display = 'block';
}
});
playButton.addEventListener('click', () => {
audio.play();
playButton.style.display = 'none';
pauseButton.style.display = 'block';
});
pauseButton.addEventListener('click', () => {
audio.pause();
playButton.style.display = 'block';
pauseButton.style.display = 'none';
});
audio.addEventListener('timeupdate', () => {
const progressWidth = (audio.currentTime / audio.duration) * 100;
progressBar.style.width = progressWidth + '%';
});
audio.addEventListener('ended', () => {
playButton.style.display = 'block';
pauseButton.style.display = 'none';
progressBar.style.width = '0%';
});
</script>
</body>
</html>
Explanation
- HTML Structure: We have a simple structure with a file input, play and pause buttons, and an audio element that gets its source from the uploaded file.
- CSS Styling: The player is styled with gradient colors, a circular button style, and smooth transitions upon hover for buttons.
- JavaScript:
- Handles file input change to set the audio source.
- Controls play and pause functionality with button state toggling.
- Updates a progress bar based on the current playtime of the audio.
This example should give you a sleek and interactive audio player with fundamental features. Adjust the styles or functionalities as needed to fit your requirements!