【问题标题】:Optimize audio for iOS Web App优化 iOS Web App 的音频
【发布时间】:2015-03-09 19:07:46
【问题描述】:

我目前正在使用带有 Cordova 框架的 Javascript 开发和测试 iOS 游戏。我正在尝试在触摸某些节点时添加声音效果。因为节点可以以任何速率重复触摸。我正在使用...

var snd = new Audio("audio/note_"+currentChain.length+".mp3");
snd.play();

这可以满足我的需要,但是当我启用这些效果时,我发现游戏会滞后。我正在处理已缩小到大约 16kb 大小的 mp3 文件,即使滞后仍然很大。

在我的情况下优化声音的最佳方法是什么?我是否因为应用程序不是原生的而受到质量限制?

谢谢!

【问题讨论】:

    标签: javascript ios cordova audio web-applications


    【解决方案1】:

    如果您使用多个文件,我建议您使用声音精灵的想法将其更改为单个文件。这个链接有更多关于它的详细信息:http://www.ibm.com/developerworks/library/wa-ioshtml5/

    根据我自己的经验,如果您没有让声音准确地播放到您想要的位置,请尝试增加文件比特率,参考:http://pupunzi.open-lab.com/2013/03/13/making-html5-audio-actually-work-on-mobile/

    【讨论】:

      【解决方案2】:

      最好的选择是预先加载它们并在需要时准备好它们。我刚刚写了一个快速的自包含闭包,我认为它会向您展示您想知道的大部分内容。

      var playSound = (function () {
              var sounds = 15, // idk how many mp3 files you have
                  loaded = new Array(sounds),
                  current = -1,
                  chain = [],
                  audio,
                  i;
      
              function incrementLoaded(index) {
                  loaded[index] = true;
              }
      
              // preloads the sound data
              for(i = 0; i < sounds; i++) {
                  audio = new Audio();
                  audio.addEventListener('canplay', incrementLoaded.bind(audio, i));
                  audio.src = 'audio/note_' + i + '.mp3';
                  chain.push(audio);
              }
      
              // this will play audio only when ready and in sequential order automatically
              // or "which" index, if supplied
              return function (which) {
                  if(typeof which === 'number') {
                      current = which;
                  } else {
                      current = (current + 1) % sounds;
                  }
      
                  // only play if loaded
                  if(loaded[current]) {
                      chain[current].pause();
                      chain[current].currentTime = 0;
                      chain[current].play();
                  }
              };
          }());
      // would play sounds in order every second
      setInterval(function () {
          playSound();
      }, 1000);
      

      【讨论】:

      • 感谢您抽出宝贵时间来做这件事。我有点理解它是如何工作的,但我想知道如何使用它。我复制了这个函数,并在我通常调用声音播放的地方粘贴了 playSound 函数调用,但我没有得到任何响应。显然我遗漏了一些东西,只是不确定是什么。
      猜你喜欢
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      相关资源
      最近更新 更多