首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >微软商店Javascript Windows10UWP应用程序中的decodeAudioData失败

微软商店Javascript Windows10UWP应用程序中的decodeAudioData失败
EN

Stack Overflow用户
提问于 2019-03-10 02:49:56
回答 1查看 44关注 0票数 0

欢迎在这个拼图上的任何帮助。我正在尝试使用Visual Studio为Microsoft商店的Windows 10应用程序运行此代码。

我正在使用sound.js from Github

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

UWP失败并显示以下错误

代码语言:javascript
复制
message "EncodingError" String
name    "EncodingError" String

我尝试了wav和mp3示例,但都失败了,并出现了相同的错误。

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

此外,UWP能够播放声音使用AudioContext,所以它不是一个问题,生成音频-它能够使正弦波,三角形,锯齿波和方波与攻击。但由于某些原因无法加载这些样本。

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

代码语言:javascript
复制
//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);
  },

这就是online metronome that I'm trying to turn into a Windows 19 Microsoft store app -如果你在MIcrosoft Edge中使用它,那么会触发与Windows10商店应用程序相同的代码,它工作得很好(我在Edge中使用混响时遇到了问题,但这是另一回事,对于节拍器来说不是大问题,尽管这会很好,但这是一个单独的问题:)。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2019-03-11 09:49:24

我已经找到了解决方案。只是我还没有添加这些文件。我以为我已经从项目>>添加了现有项,但它只是将它们复制到了其他文件夹( css文件夹或js文件夹)中。我需要展开以显示目录结构,然后在这里使用include in project作为解决方案:How to play a sound from JS in a UWP app?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55080832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档