使用SoundJS离开网页会导致Google Chrome崩溃

时间:2015-02-08 23:06:05

标签: javascript google-chrome audio soundjs

我有一个网页index.php,其中包含指向sound.php的链接。在sound.php上,使用SoundJS播放声音。

当我从sound.php导航到index.php时,Google Chrome通常会(但不总是)显示错误消息(“Aw,Snap!”): https://support.google.com/chrome/answer/95669?hl=en

我正在使用Chrome 40 for Mac OS。无论我使用链接还是浏览器的后退按钮都无关紧要。

这是我的代码:

sound.php调用一个使用SoundJS的JS函数:

<script type="text/javascript">      
  var int = [0, 7];
  prepareAudio();  
</script>

一旦删除此代码,浏览器就不会再崩溃了。

prepareAudio()位于外部文件中:

function prepareAudio() {   

  // Try WebAudio or HTMLAudio
  createjs.Sound.initializeDefaultPlugins();

  // Try flash otherwise
  if (!createjs.Sound.isReady()) {
    // Flash plug-in is not default
    createjs.FlashPlugin.swfPath = "../audio/";

    // Enable flash support
    $.getScript("../../js/flashplugin-0.6.0.min.js");

    // Prefer WebAudio over HTMLAudio. Prefer HTMLAudio over Flash.
    createjs.Sound.registerPlugins([createjs.WebAudioPlugin, createjs.HTMLAudioPlugin, createjs.FlashPlugin]);
    }

  // Get audio files
  var audioPath = "../audio/";

  var manifest = [];
  for (var i = 0; i< audioFiles.length; i++)
    manifest.push({id: audioFiles[i], src: audioPath + audioFiles[i] + ".ogg"});

  // Play audio 
  var queue = new createjs.LoadQueue(); 
  createjs.Sound.alternateExtensions = ["mp3"];
  queue.installPlugin(createjs.Sound);
  queue.addEventListener("complete", function() {playTask(int);});
  queue.loadManifest(manifest);
  createjs.Sound.registerSounds(manifest, audioPath);
}

还涉及更多代码。我用

播放声音
  createjs.Sound.play(mySound); 

Chrome和其他浏览器中的音频播放效果很好。

3 个答案:

答案 0 :(得分:3)

正如gskinner指出的那样,问题可以在其他网站上重现。它也可以在仅使用单个音频资源的页面上重现。

此问题特定于Chrome 40(或至少是最新版本的Chrome)。它并不特定于某些版本的SoundJS。

使用其他浏览器似乎是唯一的解决方案。

答案 1 :(得分:0)

当页面失去焦点时,chrome会减少页面的可用资源量。通常,这不是什么大不了的事,但是当你处理声音时,它可能会导致一些非常奇怪的行为。

您可以使用window.onblur检测页面何时失去焦点。当窗口失去焦点时,您应该尝试减少javascript正在使用的资源。如果有效,你就找到了罪魁祸首。如果没有,您应该向谷歌提交错误报告。

答案 2 :(得分:0)

似乎已在新的Chrome版本40.0.2214.115中修复,但OJay here完成的解决方法帮助我在此Chrome修复程序之前克服了此问题:

var isChrome40 = navigator && navigator.userAgent &&
navigator.userAgent.match("40.0.2214.111");
if (isChrome40) {
    createjs.Sound.registerPlugins([createjs.HTMLAudioPlugin]);
}`