为什么在Safari但不是Chrome浏览code的工作? Arrrgh [英] Why does this code work in Safari but not Chrome? Arrrgh

查看:162
本文介绍了为什么在Safari但不是Chrome浏览code的工作? Arrrgh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

铬V6,V7的Safari(V6中的作品)

Chrome v6, Safari v7 (works in 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)
       }
  }

在Safari浏览器,一切都很好。声音缓冲区的数组被创建,并调用clickSound在一个令人满意的点击的结果。

In Safari, all is well. An array of sound buffers is created, and a call to clickSound results in a satisfying "click".

在Chrome中,情况就不同了。

In Chrome, things are different.

行:

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

被标记以

  Uncaught SyntaxError:  An invalid or illegal string was specified.

上加载。

那么,如果我称之为clickSound我得到:

Then if I call "clickSound" I get:

source.buffer = soundBuffers['Tock08'];

Uncaught TypeError: Value is not of type AudioBuffer.

有谁知道为什么会出现这个问题呢?

Does anyone know why this problem occurs?

推荐答案

Chrome仍然会使用旧的webkitAudioContext。这是我如何设置背景下 SoundJS ,和它的作品随处可见:

Chrome still uses an older webkitAudioContext. This is how I set up context in SoundJS, and it works everywhere:

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

希望有所帮助。

这篇关于为什么在Safari但不是Chrome浏览code的工作? Arrrgh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆