The content on this website, including videos and code examples, is for educational purposes only. All demonstrations and designs are fictional and created to illustrate coding techniques. Any resemblance to existing websites or brands is purely coincidental.
The creators and administrators of this website do not claim ownership or affiliation with any existing websites or companies. Users are encouraged to use the information responsibly for learning purposes. Liability for any misuse of the content provided is not accepted.
Below is the html code for this video.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Music Player Visualizer</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="music-player music-active-js">
<canvas class="opacity-js" id="myCanvas" width="800" height="400"></canvas>
<audio crossorigin="anonymous" id="myAudio" src=""></audio>
<div class="file-upload">
<input type="file" id="file-custom">
<label for="file-custom">
<span>Filename</span>
<button>Choose File</button>
</label>
</div>
<div class="music-player-element icon-circle-mid">
<img src="./music-player-visualizer-assets/circle.png" alt="circle" id="circle">
</div>
<h3 class="music-name" id="music-name"></h3>
<div class="music-player-element icon-play-pause play">
<img src="./music-player-visualizer-assets/play-button.png" alt="play">
</div>
<label for="progress" class="label-song-progress">Song Progress</label>
<input type="range" id="progress" value="0" class="range">
<label for="volume" class="label-volume-progress" id="volume-level">Volume: 100%</label>
<input type="range" id="volume" value="100" step="5" max="100" min="0" class="volume-range">
</div>
<script src="index.js"></script>
</body>
</html>
Below is the css code for this video.
style.css
* {
margin: 0;
padding: 0;
}
body {
height: 100vh;
width: 100%;
overflow: hidden;
}
.music-player {
position: relative;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: linear-gradient(to right, #9d00ff80, #ff313180), url("https://images.unsplash.com/photo-1531651750200-b2c2b3ddc876?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
color: #ffffff;
z-index: 5;
transition: 0.25s;
overflow: hidden;
}
#myAudio {
position: relative;
z-index: 55;
}
#myCanvas {
position: absolute;
top: 48%;
left: 49.5%;
transform: translate(-50%, -50%);
transition: 0.25s;
}
.music-player-element {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.icon-circle-mid {
height: 556px;
width: 556px;
}
.icon-circle-mid img, .icon-play-pause img {
height: 100%;
width: 100%;
}
.music-name {
position: fixed;
top: 33.3%;
right: 2.5%;
transform: translateY(-33.3%);
width: 40%;
font-size: 24px;
font-family: monospace;
}
.icon-play-pause {
height: 162px;
width: 162px;
z-index: 5;
cursor: pointer;
}
.opacity-js {
opacity: 0;
}
.animate {
animation: bouncingAnim 0.75s ease infinite;
}
@keyframes bouncingAnim {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.125);
}
}
.blinking-text {
animation: blinkingAnim 1.5s ease infinite;
}
@keyframes blinkingAnim {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.25;
}
}
.label-song-progress {
position: absolute;
top: 91%;
left: 2.5%;
transform: translate(-2.5%, -91%);
font-size: 18px;
font-family: monospace;
letter-spacing: 2px;
}
.range {
position: absolute;
top: 95%;
left: 2.5%;
transform: translate(-2.5%, -95%);
height: 15px;
width: 33.3%;
background: #00000080;
outline: none;
border-radius: 32px;
box-shadow: inset 0 0 5px #000000;
overflow: hidden;
-webkit-appearance: none;
}
.range::-webkit-slider-thumb {
height: 15px;
width: 15px;
background: #39ff14bf;
border: 3px solid #ffffff;
border-radius: 50%;
cursor: pointer;
box-shadow: -407px 0 0 400px #39ff1480;
-webkit-appearance: none;
}
.label-volume-progress {
position: absolute;
top: 91%;
right: 2.5%;
transform: translateY(-91%);
font-size: 18px;
font-family: monospace;
text-align: right;
letter-spacing: 2px;
}
.volume-range {
position: absolute;
top: 95%;
right: 2.5%;
transform: translateY(-95%);
height: 15px;
width: 33.3%;
background: #ffffff80;
outline: none;
border-radius: 32px;
box-shadow: inset 0 0 5px #ffffff;
overflow: hidden;
-webkit-appearance: none;
}
.volume-range::-webkit-slider-thumb {
height: 15px;
width: 15px;
background: #1f51ffbf;
border: 3px solid #000000;
border-radius: 50%;
box-shadow: -407px 0 0 400px #1f51ff;
cursor: pointer;
-webkit-appearance: none;
}
.file-upload [type=file] {
display: none;
}
.file-upload button {
background-color: #ff6961 !important;
border: 1px solid #ff6961;
border-top-right-radius: 16px !important;
border-top-left-radius: 16px !important;
color: #ffffff;
font-weight: 600;
pointer-events: none;
all: unset;
}
.file-upload span, .file-upload button {
padding: 4px 16px;
transition: 0.5s;
}
.file-upload {
position: absolute;
top: 2.5%;
left: 2.5%;
transform: translate(-2.5%, -2.5%);
width: 33.3%;
text-align: center;
border-radius: 16px;
font-family: monospace;
box-shadow: 0 10px 20px #00000040, 0 6px 6px #00000040;
}
.file-upload span {
border-left: 1px solid #ff6961;
border-right: 1px solid #ff6961;
border-bottom: 1px solid #ff6961;
border-bottom-left-radius: 16px;
border-bottom-right-radius: 16px;
}
.file-upload label {
display: flex;
flex-direction: column-reverse;
cursor: pointer;
box-sizing: initial;
}
.file-upload label:hover button, .file-upload label:focus-visible button {
background-color: #c23b22 !important;
border-color: #c23b22 !important;
}
.file-upload label:hover span, .file-upload label:focus-visible span {
border-color: #c23b22 !important;
background: #00000040;
}
Below is the javascript code for this video.
index.js
let canvas = document.getElementById("myCanvas");
let audio = document.getElementById("myAudio");
audio.crossOrigin = "anonymous";
let audioCtx = new AudioContext();
let source = audioCtx.createMediaElementSource(audio);
let analyser = audioCtx.createAnalyser();
let ctx = canvas.getContext("2d");
source.connect(analyser);
source.connect(audioCtx.destination);
analyser.smoothingTimeConstant = 0.75;
analyser.fftSize = 512;
let bufferLength = analyser.frequencyBinCount;
let data = new Uint8Array(bufferLength);
function draw(data) {
let gradient = ctx.createLinearGradient(0, 0, 0, 300);
gradient.addColorStop(1, "#39ff14");
ctx.fillStyle = gradient;
ctx.shadowBlur = 20;
ctx.shadowColor = "#ffffff";
let cx = canvas.width / 2;
let cy = canvas.height / 2;
let radius = 140;
let barWidth = 2;
let barHeight = 2;
let barSpacing = 7;
let maxBarNum = Math.floor((radius * 2 * Math.PI) / (barWidth + barSpacing));
let slicedPercent = Math.floor((maxBarNum * 25) / 100);
let barNum = maxBarNum - slicedPercent;
let freqJump = Math.floor(data.length / maxBarNum);
for (let i = 0; i < barNum; i++) {
let amplitude = data[i * freqJump];
let alpha = (i * 2 * Math.PI) / maxBarNum;
let beta = ((3 * 45 - barWidth) * Math.PI) / 280;
let x = 0;
let y = radius - (amplitude / 12 - barHeight);
let w = barWidth;
let h = amplitude / 6 + barHeight;
ctx.save();
ctx.translate(cx + barSpacing, cy + barSpacing);
ctx.rotate(alpha - beta);
ctx.fillRect(x, y, w, h);
ctx.restore();
}
}
function loopingFunction() {
requestAnimationFrame(loopingFunction);
analyser.getByteFrequencyData(data);
ctx.clearRect(0, 0, canvas.width, canvas.height);
draw(data);
}
loopingFunction();
let play = document.querySelector(".play");
let musicIcon = document.querySelector(".icon-play-pause img");
let circleElement = document.getElementById("circle");
let musicText = document.getElementById("music-name");
let progress = document.getElementById("progress");
let volumeBar = document.getElementById("volume");
progress.onchange = function () { audio.currentTime = progress.value; }
volumeBar.onchange = function () {
audio.volume = volumeBar.value / 100;
document.getElementById("volume-level").innerHTML = `Volume: ${volumeBar.value}%`;
}
function playMusic() {
audioCtx.resume();
canvas.classList.remove("opacity-js");
audio.ontimeupdate = function () {
progress.max = audio.duration;
progress.value = audio.currentTime;
};
if (audio.paused) {
audio.play();
circleElement.classList.add("animate");
musicText.classList.add("blinking-text");
musicIcon.src = "./music-player-visualizer-assets/pause-button.png";
} else {
audio.pause();
circleElement.classList.remove("animate");
musicText.classList.remove("blinking-text");
musicIcon.src = "./music-player-visualizer-assets/play-button.png";
}
}
play.addEventListener("click", playMusic);
audio.addEventListener("ended", function () {
audio.currentTime = 0;
circleElement.classList.remove("animate");
musicText.classList.remove("blinking-text");
musicIcon.src = "./music-player-visualizer-assets/play-button.png";
});
let input = document.querySelector("input");
input.addEventListener("change", function (e) {
let files = this.files;
audio.src = URL.createObjectURL(files[0]);
audio.load();
musicText.textContent = files[0].name;
playMusic();
});
let fileInput = Array.from(document.querySelectorAll('.file-upload [type="file"]'));
fileInput.forEach((each) => {
each.addEventListener("change", (e) => {
const path = e.target.value;
const filenameField = e.target.parentElement.querySelector("span");
const filename = path.split(/\/|\\/).pop();
filename ? filenameField.innerText = filename : filenameField.innerText = "Filename";
});
});
Thanks for visiting