首页 > 解决方案 > 用于 Microsoft 商店的 Javascript Windows 10 UWP 应用程序中的 decodeAudioData 失败

问题描述

欢迎任何有关此难题的帮助。我正在尝试使此代码适用于使用 Visual Studio 的 Microsoft 商店的 Windows 10 应用程序。

我正在使用来自 Github 的 sound.js

它在 Microsoft Edge、Chrome 和 Firefox 中播放声音很好(尽管由于某种原因它无法在 Internet Explorer 中播放任何声音)。在其他方面,UWP 的行为与我的测试中的 Edge 完全一样。

UWP 失败并出现错误

message "EncodingError" String
name    "EncodingError" String

我尝试了 wav 和 mp3 样本,但都因相同的错误而失败。

音频文件已添加到项目的资产列表中,所以这不是问题。

此外,UWP 能够使用 AudioContext 播放声音,因此生成音频根本不是问题 - 它能够产生正弦波、三角波、锯齿波和方波并进行攻击。但由于某种原因无法加载这些样本。

这是我正在使用的代码,它在 onFailed 函数中引发了这个编码错误。

//The `loadSound` function loads the sound file using XHR
function loadSound(o, source, loadHandler, failHandler) {
  var xhr = new XMLHttpRequest();

  //Use xhr to load the sound file.
  xhr.open("GET", source, true);
  xhr.responseType = "arraybuffer";

  //When the sound has finished loading, decode it using the
  //`decodeAudio` function (which you'll see ahead)
  xhr.addEventListener("load", decodeAudio.bind(this, o, xhr, loadHandler, failHandler)); 

  //Send the request to load the file.
  xhr.send();
}

//The `decodeAudio` function decodes the audio file for you and 
//launches the `loadHandler` when it's done
function decodeAudio(o, xhr, loadHandler, failHandler) {

  //Decode the sound and store a reference to the buffer.
  actx.decodeAudioData(
    xhr.response,
    function(buffer) {
      o.buffer = buffer;
      o.hasLoaded = true;

      //This next bit is optional, but important.
      //If you have a load manager in your game, call it here so that
      //the sound is registered as having loaded.
      if (loadHandler) {
        loadHandler();
      }
    },
    function(error) {
      if (failHandler) failHandler(o.source, error);
    }
  );
}
// ...

  //The callback function to run if an asset fails to load or decode
    onFailed: function (source, error) {
      if(do_throw_error)
      throw new Error("Audio could not be loaded: " + source);
  },

这是我试图将其变成 Windows 19 Microsoft 商店应用程序的在线节拍器- 如果您在 MIcrosoft Edge 中使用它,则会触发与 Windows 10 商店应用程序相同的代码并且它工作正常(我在获取混响可以在 Edge 中工作,但这是另一回事,对于节拍器来说不是什么大问题,虽然它会很好,但无论如何这是一个单独的问题 :) )。

谢谢!

标签: javascriptuwp

解决方案


我找到了解决方案。只是我没有添加文件。我以为我是从项目 >> 添加现有项目中获得的,但这只是将它们复制到其他文件夹之一(css 文件夹或 js 文件夹)。我需要展开以显示目录结构,然后在项目中使用包含作为解决方案: 如何在 UWP 应用程序中播放来自 JS 的声音?


推荐阅读