2

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

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

In Chrome, things are different.

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:

source.buffer = soundBuffers['Tock08'];

Uncaught TypeError: Value is not of type AudioBuffer.

Does anyone know why this problem occurs?

Ruddy
  • 9,795
  • 5
  • 45
  • 66

1 Answers1

1

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

Hope that helps.

OJay
  • 1,249
  • 7
  • 14
  • Interesting. If I add a code snippet like that but change the lines to alert("Safari") and alert("Chrome") the code reports "Safari" in both browsers. – Lou Mazzucchelli Oct 29 '13 at 00:22
  • My understanding is that Safari now supports new AudioContext and still has deprecated support for webkitAudioContext. – OJay Oct 29 '13 at 15:31