• <i id='8KdcA'><tr id='8KdcA'><dt id='8KdcA'><q id='8KdcA'><span id='8KdcA'><b id='8KdcA'><form id='8KdcA'><ins id='8KdcA'></ins><ul id='8KdcA'></ul><sub id='8KdcA'></sub></form><legend id='8KdcA'></legend><bdo id='8KdcA'><pre id='8KdcA'><center id='8KdcA'></center></pre></bdo></b><th id='8KdcA'></th></span></q></dt></tr></i><div id='8KdcA'><tfoot id='8KdcA'></tfoot><dl id='8KdcA'><fieldset id='8KdcA'></fieldset></dl></div>
      <legend id='8KdcA'><style id='8KdcA'><dir id='8KdcA'><q id='8KdcA'></q></dir></style></legend>

    1. <small id='8KdcA'></small><noframes id='8KdcA'>

      • <bdo id='8KdcA'></bdo><ul id='8KdcA'></ul>
      <tfoot id='8KdcA'></tfoot>

        由于 CORS 访问限制本地 mp3 文件,MediaElementAudio

        时间:2023-10-14

          • <bdo id='P6Saf'></bdo><ul id='P6Saf'></ul>
          • <legend id='P6Saf'><style id='P6Saf'><dir id='P6Saf'><q id='P6Saf'></q></dir></style></legend>

            <small id='P6Saf'></small><noframes id='P6Saf'>

                <i id='P6Saf'><tr id='P6Saf'><dt id='P6Saf'><q id='P6Saf'><span id='P6Saf'><b id='P6Saf'><form id='P6Saf'><ins id='P6Saf'></ins><ul id='P6Saf'></ul><sub id='P6Saf'></sub></form><legend id='P6Saf'></legend><bdo id='P6Saf'><pre id='P6Saf'><center id='P6Saf'></center></pre></bdo></b><th id='P6Saf'></th></span></q></dt></tr></i><div id='P6Saf'><tfoot id='P6Saf'></tfoot><dl id='P6Saf'><fieldset id='P6Saf'></fieldset></dl></div>
              1. <tfoot id='P6Saf'></tfoot>

                    <tbody id='P6Saf'></tbody>
                  本文介绍了由于 CORS 访问限制本地 mp3 文件,MediaElementAudioSource 输出零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下 html 页面,我正在尝试展示一个用于演示具有本地存储的 mp3 的音频可视化器的类:

                  I have the following html page that I'm trying to show a class for demonstrating an audio visualizer with an mp3 stored locally:

                  <!doctype html>
                  <html>
                  <head>
                      <header name = "Access-Control-Allow-Origin" value = "*" /> 
                      <style type = "text/css">
                              div#mp3_player{ width: 500px; height: 60px; background: #000; padding: 5px; margin: 50px auto;}
                          div#mp3_player > div > audio{ width: 500px; background: #000; float: left; }
                          div#mp3_player > canvas { width: 500px; height: 30px; background: #002D3C; float: left;}
                      </style>
                      <script>
                      //create new instance of audio 
                      var audio = new Audio();
                      audio.src = 'C:/Users/Adam/Desktop/1901.m4a';
                      audio.controls = true;
                      audio.loop = true;
                      audio.autoplay = true;
                  
                      var canvas, ctx, source, context, analyser, fbc_array, bars, bar_x, bar_width, bar_height;
                  
                      window.addEventListener("load", initMp3Player, false);
                  
                  
                      function frameLooper(){
                          window.webkitRequestAnimationFrame(frameLooper);
                          fbc_array = new Uint8Array(analyser.frequencyBinCount);
                          analyser.getByteFrequencyData(fbc_array);
                          ctx.clearRect(0, 0, canvas.width, canvas.height);
                          ctx.fillStyle = "#00CCFF";
                          bars = 100;
                          for (var i = 0; i < bars; i++){
                              bar_x = i * 3;
                              bar_width = 2;
                              bar_height = -(fbc_array[i]/2);
                              ctx.fillRect(bar_x, canvas.height, bar_width, bar_height);
                          }
                      }
                  
                      function initMp3Player(){
                          document.getElementById('audio_box').appendChild(audio);
                          context = new AudioContext();
                          analyser = context.createAnalyser();
                          canvas = document.getElementById('analyser_render');
                          ctx = canvas.getContext('2d');
                          source = context.createMediaElementSource(audio);
                          source.connect(analyser);
                          analyser.connect(context.destination);
                          frameLooper();
                      }
                  
                      </script>
                  
                  </head>
                  
                  <body>
                      <div id = "mp3_player">
                          <div id = "audio_box"></div>
                          <canvas id = "analyser_render"></canvas>
                      </div>
                  
                  </body>
                  

                  在使用脚本标签中的所有代码之前,我已经让 mp3 文件自动播放,不包括行下的代码

                  I've gotten the mp3 file to play automatically before using all of the code in the script tag excluding what's below the line

                  audio.autoplay = true;
                  

                  但是当我包含 frameLooper 函数时,我收到消息MediaElementAudioSource 由于 CORS 访问限制而输出零."既然是本地文件,有没有办法规避这个问题?

                  but when I include the frameLooper function I get the message "MediaElementAudioSource outputs zeros due to CORS access restrictions." Is there anyway to circumvent this since it's a local file?

                  推荐答案

                  在初始化Audio对象后,添加如下内容:

                  Just after initializing the Audio object, add the following:

                  audio.crossOrigin = "anonymous";
                  

                  这应该可以防止 CORS 访问限制.

                  This should prevent the CORS access restriction.

                  这篇关于由于 CORS 访问限制本地 mp3 文件,MediaElementAudioSource 输出零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:XmlHttpRequest 状态 0 而不是 IE 10 中的 401 下一篇:获取 POST 请求中的空正文

                  相关文章

                  最新文章

                  1. <tfoot id='sVw3B'></tfoot>

                    • <bdo id='sVw3B'></bdo><ul id='sVw3B'></ul>

                    <small id='sVw3B'></small><noframes id='sVw3B'>

                      <legend id='sVw3B'><style id='sVw3B'><dir id='sVw3B'><q id='sVw3B'></q></dir></style></legend>

                    1. <i id='sVw3B'><tr id='sVw3B'><dt id='sVw3B'><q id='sVw3B'><span id='sVw3B'><b id='sVw3B'><form id='sVw3B'><ins id='sVw3B'></ins><ul id='sVw3B'></ul><sub id='sVw3B'></sub></form><legend id='sVw3B'></legend><bdo id='sVw3B'><pre id='sVw3B'><center id='sVw3B'></center></pre></bdo></b><th id='sVw3B'></th></span></q></dt></tr></i><div id='sVw3B'><tfoot id='sVw3B'></tfoot><dl id='sVw3B'><fieldset id='sVw3B'></fieldset></dl></div>