2

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)
       }
  }

Safariでは、すべて問題ありません。サウンド バッファの配列が作成され、clickSound を呼び出すと満足のいく「クリック」が得られます。

Chrome では事情が異なります。

この線:

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

フラグが立てられています

  Uncaught SyntaxError:  An invalid or illegal string was specified.

ロード中。

次に、「clickSound」を呼び出すと、次のようになります。

source.buffer = soundBuffers['Tock08'];

Uncaught TypeError: Value is not of type AudioBuffer.

この問題が発生する理由を知っている人はいますか?

4

1 に答える 1

1

Chrome はまだ古い webkitAudioContext を使用しています。これは、 SoundJSでコンテキストを設定する方法であり、どこでも機能します。

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

それが役立つことを願っています。

于 2013-10-28T15:40:25.510 に答える