简体   繁体   English

为什么此代码在Safari中有效但在Chrome中不起作用? Arrrgh

[英]Why does this code work in Safari but not Chrome? Arrrgh

Chrome v6, Safari v7 (works in v6) Chrome v6,Safari v7(适用于v6)

  if('AudioContext' in window) {

      var myAudioContext = new AudioContext();            

      const PATH = 'Sounds/',
      SOUNDS = ['Tock08'];
      var soundBuffers = {}, myNodes = {};

      function fetchSounds() {
          var request = new XMLHttpRequest();
          for (var i = 0, len = SOUNDS.length; i < len; i++) {
              request = new XMLHttpRequest();
              request._soundName = SOUNDS[i];
              request.open('GET', PATH + request._soundName + '.aiff', true);
              request.responseType = 'arraybuffer';
              request.addEventListener('load', bufferSound, false);
              request.send();
          }
      }

      function bufferSound(event) {
          var request = event.target;
          var buffer = myAudioContext.createBuffer(request.response, false);
          soundBuffers[request._soundName] = buffer;
      }
  }

  function clickSound() {
      if('AudioContext' in window ) {     //Chrome doesn't work with this or above code
          // create a new AudioBufferSourceNode
          source = myAudioContext.createBufferSource();
          source.buffer = soundBuffers['Tock08'];
          source.loop = false;
          source.connect(myNodes.volume);
          myNodes.volume.gain.value = 1;
          myNodes.volume.connect(myAudioContext.destination);
          source.noteOn(0);               // play right now (0 seconds from now)
       }
  }

In Safari, all is well. 在Safari中,一切都很好。 An array of sound buffers is created, and a call to clickSound results in a satisfying "click". 将创建一个声音缓冲区数组,并且对clickSound的调用会产生令人满意的“ click”。

In Chrome, things are different. 在Chrome中,情况有所不同。

The line: 该行:

var buffer = myAudioContext.createBuffer(request.response, false);

is flagged with 被标记为

  Uncaught SyntaxError:  An invalid or illegal string was specified.

on loading. 在加载时。

Then if I call "clickSound" I get: 然后,如果我调用“ clickSound”,则会得到:

source.buffer = soundBuffers['Tock08'];

Uncaught TypeError: Value is not of type AudioBuffer.

Does anyone know why this problem occurs? 有谁知道为什么会出现此问题?

Chrome still uses an older webkitAudioContext. Chrome仍使用较旧的webkitAudioContext。 This is how I set up context in SoundJS , and it works everywhere: 这就是我在SoundJS中设置上下文的方式 ,它可以在任何地方使用:

if (window.webkitAudioContext) {
    s.context = new webkitAudioContext();
} else if (window.AudioContext) {
    s.context = new AudioContext();
}

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个jQuery代码可以在Firefox中运行,但不适用于Chrome或Safari? - Why does this jQuery code work in Firefox but not Chrome or Safari? 为什么这个js代码不能在Chrome和Safari中运行? - Why wont this js code work in Chrome and Safari? 为什么此代码在Chrome上不起作用? - Why this code does not work on Chrome? 为什么此代码在Google Chrome浏览器中能正常运行,但是却拒绝使用Safari(在Mac和iPhone上)? - Why does this code work fine in Google Chrome, but refuses to Safari (and on the Mac and on the iPhone)? 为什么Safari / Opera无法与此JavaScript代码一起使用? - Why does Safari/Opera not work with this javascript code? 为什么此代码在IE和Safari中不起作用? - Why does this code not work in IE & Safari? 为什么我的代码在Safari或Opera中不起作用? - Why does my code not work in Safari or Opera? 为什么jquery可以在Chrome中工作,但不能在Safari中工作? - Why jquery work in Chrome but not on Safari? 为什么我的“以前”过滤器只能在Chrome中使用,而不能在Safari或Firefox中使用? - Why does my 'time ago' filter work in chrome and not safari or firefox? 为什么翻转动画无法在Chrome和Safari浏览器中使用? - Why does my flip animation not work in chrome and safari?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM