简体   繁体   English

我的音频可视化器在 chrome 中不起作用(但在 Firefox 中起作用)

[英]My audio visualizer doesn't work in chrome (but does in firefox)

I have this audio visualizer (which I hope to get out of canvas and just use div blocks to achieve a better look).我有这个音频可视化器(我希望摆脱 canvas 并使用 div 块来获得更好的外观)。 It works in firefox but when I run it in chrome I get this error message " Failed to load resource: the server responded with a status of 404 (Not Found)" which was located at favicon.ico:1???它在 firefox 中工作,但是当我在 chrome 中运行它时,我收到此错误消息“加载资源失败:服务器响应状态为 404(未找到)”,位于 favicon.ico:1??? Not a clue what location means.不知道位置是什么意思。

''' '''

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Music Station</title>
    
    <script src="http://cwilso.github.io/AudioContext-MonkeyPatch/AudioContextMonkeyPatch.js"></script>
</head>
<body>
  <audio id="player" controls>
       <source src="Music/Pink_Floyd/WishYouWereHere/ShineOnYouCrazyDiamond(PartsI-V).mp3">
   </audio>
  
   <canvas width="500px" height="400px" background-color="#000000" id="soundCanv"></canvas>
   
   
   
    <script>
        
        window.AudioContext = window.AudioContext||window.webkitAudioContext;
        let audioCtx = new AudioContext();
      var music=document.getElementById("player");  
     var canvas=document.getElementById("soundCanv");
        var canvasCtx=canvas.getContext("2d");
        var myAudio=document.querySelector('audio');
        
        
var analyser = audioCtx.createAnalyser(); 
//    var source = audioCtx.createMediaStreamSource(music.captureStream());
            var source=audioCtx.createMediaElementSource(myAudio);
source.connect(analyser);
        analyser.connect(audioCtx.destination);

console.log("Hello");
    
    
    analyser.fftSize = 256;
var bufferLength = analyser.frequencyBinCount;
console.log(bufferLength);
var dataArray = new Uint8Array(bufferLength);

console.log("Hello There");
    
        function draw() {
            
      canvasCtx.clearRect(0, 0,canvas.width, canvas.height);

      analyser.getByteFrequencyData(dataArray);

      canvasCtx.fillStyle = 'rgb(0, 0, 0)';
      canvasCtx.fillRect(0, 0, canvas.width, canvas.height);
            
            var barWidth = (canvas.width / bufferLength) * 2.5;
var barHeight;
var x = 0;
            
                  for(var i = 0; i < bufferLength; i++) {
        barHeight = dataArray[i]/2;

        canvasCtx.fillStyle = 'rgb(' + (barHeight+100) + ',50,50)';
        canvasCtx.fillRect(x,canvas.height-barHeight/2,barWidth,barHeight);

        x += barWidth + 1;
      }
        window.requestAnimationFrame(draw);    
    };
        window.requestAnimationFrame(draw);
        draw();
    
    
    </script>
</body>
</html>

''' '''

Is there a way to get this to work in chrome?有没有办法让它在 chrome 中工作? its 2020 web audio api should have been smoothed over by now.它的 2020 web 音频 api 现在应该已经平滑了。

Chrome requires that AudioContext s must be created only from code run because of an user event. Chrome 要求AudioContext必须仅从由于用户事件而运行的代码创建。 You can solve this by wrapping your code inside a click event handler or something similar.您可以通过将代码包装在click事件处理程序或类似的东西中来解决这个问题。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Music Station</title>
    
    <script src="http://cwilso.github.io/AudioContext-MonkeyPatch/AudioContextMonkeyPatch.js"></script>
</head>
<body>
  <audio id="player" controls>
       <source src="Music/Pink_Floyd/WishYouWereHere/ShineOnYouCrazyDiamond(PartsI-V).mp3">
   </audio>
  
   <canvas width="500px" height="400px" background-color="#000000" id="soundCanv"></canvas>
   <button id="start-btn">Start</button>
   
   
    <script>

      document.getElementById("start-btn").addEventListener("click", () => {
        
        window.AudioContext = window.AudioContext||window.webkitAudioContext;
        let audioCtx = new AudioContext();
      var music=document.getElementById("player");  
     var canvas=document.getElementById("soundCanv");
        var canvasCtx=canvas.getContext("2d");
        var myAudio=document.querySelector('audio');
        
        
var analyser = audioCtx.createAnalyser(); 
//    var source = audioCtx.createMediaStreamSource(music.captureStream());
            var source=audioCtx.createMediaElementSource(myAudio);
source.connect(analyser);
        analyser.connect(audioCtx.destination);

console.log("Hello");
    
    
    analyser.fftSize = 256;
var bufferLength = analyser.frequencyBinCount;
console.log(bufferLength);
var dataArray = new Uint8Array(bufferLength);

console.log("Hello There");
    
        function draw() {
            
      canvasCtx.clearRect(0, 0,canvas.width, canvas.height);

      analyser.getByteFrequencyData(dataArray);

      canvasCtx.fillStyle = 'rgb(0, 0, 0)';
      canvasCtx.fillRect(0, 0, canvas.width, canvas.height);
            
            var barWidth = (canvas.width / bufferLength) * 2.5;
var barHeight;
var x = 0;
            
                  for(var i = 0; i < bufferLength; i++) {
        barHeight = dataArray[i]/2;

        canvasCtx.fillStyle = 'rgb(' + (barHeight+100) + ',50,50)';
        canvasCtx.fillRect(x,canvas.height-barHeight/2,barWidth,barHeight);

        x += barWidth + 1;
      }
        window.requestAnimationFrame(draw);    
    };
        window.requestAnimationFrame(draw);
        draw();
    
      });
    </script>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的脚本无法在Mozilla Firefox上运行,但可以在Chrome上运行 - My scripts doesn't work on Mozilla Firefox but does on Chrome 音频/视频DOM错误事件在Chrome / Firefox中不起作用 - Audio/Video DOM error Event doesn't work in Chrome/Firefox 我的视频实现无法在Firefox和Safari浏览器中使用; 仅镀铬 - My implementation of video doesn't work in firefox and safari; only chrome 为什么此代码在FireFox和IE中不能使用,而在Chrome中不能使用? - Why Doesn't This Code Work in FireFox and IE but does in Chrome? 为什么这个JS函数有问题? Firefox可以工作,但Chrome不能工作 - Why is wrong with this JS function? Firefox does work but Chrome doesn't JavaScript 在 Safari 中不起作用,但在 Firefox 和 Chrome 中起作用 - JavaScript doesn't work in Safari but does in Firefox and Chrome jQuery UI droppable在IE和Firefox上不起作用(但在Chrome上有效) - jQuery UI droppable doesn't work on IE and Firefox (but does on Chrome) 为什么我的可视界面在 IE 11 上不起作用,但它在边缘、chrome firefox 等中起作用。显示:flexbox 可能是问题所在? - Why my visual interface doesn't work on IE 11 but yes it does in edge, chrome firefox etc.. display:flexbox could be the problem? Onclick在Firefox中不起作用,但在Chrome中-是 - Onclick doesn't work in Firefox, but in Chrome - yes CSS calc()在Chrome和firefox上不起作用 - CSS calc() doesn't work on Chrome and firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM