通过远程位置(Opera)的URL加载音频文件?

时间:2017-07-24 18:18:23

标签: javascript opera codepen audiocontext

也许有人知道为什么codepend不会从URL加载音频文件? (我没有专业代码,所以我不能直接上传文件到笔)。

我有这个Audio" loader"在我的计划中实施:

// Audio loader implementation.
window.onload = init;
let context;
let bufferLoader;
let greenBuffer = null;
let redBuffer = null;
let blueBuffer = null;
let yellowBuffer = null;
let dohBuffer = null;
let woohooBuffer = null;
let excellentBuffer = null;
let superDohBuffer = null;
// Buffer loader class taken from https://www.html5rocks.com/en/tutorials/webaudio/intro/
function BufferLoader(context, urlList, callback) {
  this.context = context;
  this.urlList = urlList;
  this.onload = callback;
  this.bufferList = new Array();
  this.loadCount = 0;
}

BufferLoader.prototype.loadBuffer = function(url, index) {
  // Load buffer asynchronously
  let request = new XMLHttpRequest();
  request.open("GET", url, true);
  request.responseType = "arraybuffer";

  let loader = this;

  request.onload = function() {
    // Asynchronously decode the audio file data in request.response
    loader.context.decodeAudioData(
      request.response,
      function(buffer) {
        if (!buffer) {
          alert('error decoding file data: ' + url);
          return;
        }
        loader.bufferList[index] = buffer;
        if (++loader.loadCount == loader.urlList.length)
          loader.onload(loader.bufferList);
      },
      function(error) {
        console.error('decodeAudioData error', error);
      }
    );
  }

  request.onerror = function() {
    alert('BufferLoader: XHR error');
  }

  request.send();
}

BufferLoader.prototype.load = function() {
  for (let i = 0; i < this.urlList.length; ++i)
  this.loadBuffer(this.urlList[i], i);
}
function init() {
    try {
        // Fix up for prefixing
        window.AudioContext = window.AudioContext||window.webkitAudioContext;
        context = new AudioContext();
    }
    catch(e) {
        alert('Web Audio API is not supported in this browser');
    }
    bufferLoader = new BufferLoader(
        context,
        [
          'https://cors-anywhere.herokuapp.com/https://s3.amazonaws.com/freecodecamp/simonSound1.mp3',
          'https://cors-anywhere.herokuapp.com/https://s3.amazonaws.com/freecodecamp/simonSound2.mp3',
          'https://cors-anywhere.herokuapp.com/https://s3.amazonaws.com/freecodecamp/simonSound3.mp3',
          'https://cors-anywhere.herokuapp.com/https://s3.amazonaws.com/freecodecamp/simonSound4.mp3',
          'https://cors-anywhere.herokuapp.com/http://www.springfieldfiles.com/sounds/homer/doh.mp3',
          'https://cors-anywhere.herokuapp.com/http://www.springfieldfiles.com/sounds/homer/woohoo.mp3',
          'https://cors-anywhere.herokuapp.com/http://springfieldfiles.com/sounds/burns/excellnt.mp3',
          'https://cors-anywhere.herokuapp.com/http://www.springfieldfiles.com/sounds/homer/doheth.mp3',
        ],
        setBuffers
        );

      bufferLoader.load();
}

function setBuffers(bufferList){
    greenBuffer = bufferList[0];
    redBuffer = bufferList[1];
    blueBuffer = bufferList[2];
    yellowBuffer = bufferList[3];
    dohBuffer = bufferList[4];
    woohooBuffer = bufferList[5];
    excellentBuffer = bufferList[6];
    superDohBuffer = bufferList[7];
}

如果我在本地使用此代码(不在codepen上),它可以正常工作。它加载这些文件后来我可以按我想要的方式播放这些音频文件。但是,如果我在codepen上运行它,它会抛出这个(注意我还在https://cors-anywhere.herokuapp.com/前面加载了绕过CORS的URL):

console_runner-079c09a….js:1 decodeAudioData error DOMException: Unable to decode audio data
(anonymous) @ console_runner-079c09a….js:1
(anonymous) @ pen.js:80
index.html:1 Uncaught (in promise) DOMException: Unable to decode audio data
index.html:1 Uncaught (in promise) DOMException: Unable to decode audio data

可在此处查看完整笔:https://codepen.io/andriusl/pen/proxKj

更新 这似乎与浏览器有关。 AudioContext无法与Opera浏览器一起使用,因此这个问题更倾向于浏览器而不是代码本身。

0 个答案:

没有答案