我正在嘗試制作帶有歌曲可視化工具的音樂播放器。我所有的歌曲都在谷歌驅(qū)動(dòng)器上。為了制作可視化工具,我使用了這段代碼。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; }當(dāng)我嘗試運(yùn)行此代碼時(shí),出現(xiàn)此錯(cuò)誤:CORS 策略已阻止從來源“null”訪問“https://docs.google.com/uc?export=download&id=1Ifx_11yZ-FDdrO5XXseaDiipMBRZjpuB”中的音頻:不存在“Access-Control-Allow-Origin”標(biāo)頭在請(qǐng)求的資源上。我在音頻標(biāo)簽中使用了crossorigin="anonymous",我也在<header name = "Access-Control-Allow-Origin" value = "*"> head 中使用了那么為什么我會(huì)收到這個(gè)錯(cuò)誤?請(qǐng)幫我。我在互聯(lián)網(wǎng)上找不到任何解決方案。
CORS 策略已阻止從來源“null”訪問音頻:請(qǐng)求的資源上不存在
慕容森
2023-04-27 17:04:21