【问题标题】:Meteor DOMException: Unable to decode audio dataMeteor DOMException:无法解码音频数据
【发布时间】:2017-06-09 06:15:06
【问题描述】:

编辑:我刚刚创建了一个新的 Meteor 项目并且它工作了:D wow。但它仍然无法在我的核心项目上工作..看起来我有不同的设置。

在我的 Meteor.js 项目中,我有 4 个.mp3-文件位于public/sounds/xyz.mp3。 我将这些.mp3 加载为:

 let soundRequest = new XMLHttpRequest();
    soundRequest.open('GET', this._soundPath, true);
    soundRequest.responseType = 'arraybuffer';
    let $this = this;
    soundRequest.onload = function () {
        Core.getAudioContext().decodeAudioData(soundRequest.response, function (buffer) {
            $this.source.buffer = buffer;
            $this.source.loop = true;
            $this.source.connect($this.panner);
        });
    };
soundRequest.send();

这在google Chrome 上工作,但是当我通过meteor run android-device 构建应用程序时,我收到以下错误消息:DOMException: Unable to decode audio data 我想知道这是否是一个错误,因为加载 .png.jpg 在移动版本中工作得很好。除了meteor add crosswalk,我还没有安装任何软件包,但卸载它也无济于事。

【问题讨论】:

标签: javascript cordova audio meteor


【解决方案1】:

android 设备不支持此 Web API,但适用于 android 的 chrome 浏览器

在此链接中检查浏览器规范 https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/decodeAudioData

【讨论】:

    【解决方案2】:

    您不需要执行 http 请求来获取本地资源。您可以只参考本地网址。在 Android 设备上,路径是不同的。请参阅此代码:

      function getSound(file) {
        var sound = "/sounds/"+file;
        if (Meteor.isCordova) {
          var s;
          if (device.platform.toLowerCase() === "android") {
            sfile = cordova.file.applicationDirectory.replace('file://', '') + 'www/application/app' + sound;
          }
          else {
            sfile = cordova.file.applicationDirectory.replace('file://', '') + sound;
          }
          var s = new Media(
            sfile,
            function (success) {
              console.log("Got sound "+file+" ok ("+sfile+")");
              s.play();
            },
            function (err) {
              console.log("Get sound "+file+" ("+sfile+") failed: "+err);
            }
          );
        } else {
          var a = new Audio(sound);
          a.play();
        }
      }
    

    在设备上,它异步加载声音文件,然后播放。在浏览器中它只是同步加载和播放。

    【讨论】:

    • 嘿 Mikkel,谢谢你的回复,问题是 decodeAudio 的第一个参数需要是 arraybuffer 吗?我不明白我是如何从那里得到数组缓冲区的?
    • 您是否对声音做的不仅仅是播放它?
    • 是的,我会将它用于空间音频(网络音频 API)
    • 也许你可以使用 FileReader 来获取正确格式的文件,看这个developer.mozilla.org/en-US/docs/Web/API/FileReader/…
    • 是的,我昨天试过了,但仍然是同样的错误。这让我很困惑,因为加载 .jpg 工作正常
    猜你喜欢
    • 2017-05-16
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2017-06-09
    • 2021-02-14
    • 1970-01-01
    相关资源
    最近更新 更多