I am trying to make a music player with a song visualizer. All my songs are on google drive.
For making the visualizer, I am using this code.
window.addEventListener('load', () => {
canvas = document.getElementById("cnv1");
context = canvas.getContext("2d");
audioctx = new AudioContext();
WIDTH = window.innerWidth - 50;
canvas.width = WIDTH - 50;
HEIGHT = 500;
canvas.height = 500;
analyser = audioctx.createAnalyser();
analyser.fftSize = SAMPLES;
oscillator = audioctx.createOscillator();
oscillator.connect(audioctx.destination);
source = audioctx.createMediaElementSource(music);
source.connect(analyser);
source.connect(audioctx.destination);
freqArr = new Uint8Array(analyser.frequencyBinCount);
barHeight = HEIGHT;
window.requestAnimationFrame(draw);
});
function draw() {
if (!music.paused) {
bigBars = 0;
r = 0;
g = 0;
b = 255;
x = 0;
context.clearRect(0, 0, WIDTH, HEIGHT);
analyser.getByteFrequencyData(freqArr);
for (var i = 0; i < INTERVAL; i++) {
if (barHeight >= (240)) {
bigBars++;
}
let num = i;
barHeight = ((freqArr[num] - 128) * 3) + 2;
if (barHeight <= 1) {
barHeight = 2;
}
r = r + 10;
if (r > 255) {
r = 255;
}
g = g + 1;
if (g > 255) {
g = 255;
}
b = b - 2;
if (b < 0) {
b = 0;
}
context.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
context.fillRect(x, HEIGHT - barHeight, (WIDTH / INTERVAL) - 1, barHeight);
x = x + (WIDTH / INTERVAL);
}
}
window.requestAnimationFrame(draw);
}
When I am trying to run this code, I am getting this error:
Access to audio at 'https://docs.google.com/uc?export=download&id=1Ifx_11yZ-FDdrO5XXseaDiipMBRZjpuB' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have used crossorigin="anonymous" in the audio tag, and I also have used <header name = "Access-Control-Allow-Origin" value = "*">
in the head then why I am getting this error? Please help me. I couldn't find any solution on the Internet.