1

我想在 blob 中创建一个音频对象,但我做不到。

const blobCode = () => {
    return `
    var sound      = document.createElement('audio');
    sound.id       = 'audio-player';
    sound.controls = 'controls';
    sound.src      = 'media/Blue Browne.mp3';
    sound.type     = 'audio/mpeg';
    document.body.appendChild(sound);
    class WhiteNoiseProcessor extends AudioWorkletProcessor {
    process (inputs, outputs, parameters) {
      const output = outputs[0]
      output.forEach(channel => {
        for (let i = 0; i < channel.length; i++) {
          channel[i] = Math.random() * 2 - 1
        }
      })
      return true
    }
  }
  
  registerProcessor('white-noise-processor', WhiteNoiseProcessor);`
}


const audioContext = new OfflineAudioContext(1, 128, 300000);


var blob_url = new Blob([blobCode()], {
    type: "text/javascript"
});
var blob_url_create = URL.createObjectURL(blob_url);
await audioContext.audioWorklet.addModule(blob_url_create).then(async () => {
    var wa = new AudioWorkletNode(audioContext, "white-noise-processor");
});

如果我设置 Blob 类型text/javascript,则会出现错误Uncaught ReferenceError: document is not defined

var blob_url = new Blob([blobCode()], {type: "text/javascript"});

如果我设置 Blob 类型text/html得到错误Uncaught DOMException: The user aborted a request

var blob_url = new Blob([blobCode()], {type: "text/html"});

我该如何解决这个错误?

4

0 回答 0