howler.js icon indicating copy to clipboard operation
howler.js copied to clipboard

Howler not playing sounds on iOS 5

Open arjunmehta opened this issue 13 years ago • 4 comments

var sound = new Howl({
  urls: ['/static/misc/snare.mp3'],
  autoplay: false,
  loop: false,
  onload:function(){
        div.prepend($("<code>").text("SOUND LOADED"));
        console.log("SOUND LOADED");
        div.prepend($("</br>"));
  },
  onend: function() {
    console.log('Finished!');
  }
});

//jQuery click event to get the sound playing
$('#whatever').click(function(){
    sound.play();
});

Will not play the sound. Tested in Chrome and Safari on iOS 5. Works as expected, wonderfully, in iOS 6 (Chrome and Safari). Used the console and no errors are thrown. Nothing logged to the console on load (as attempted above). No clue on what is going at all.

However, using a standard constructor for an HTML5 Audio object like below works as expected! var sound= new Audio('/static/misc/beep.mp3'); $('#whatever').click(function(){ sound.play(); });

Would LOVE to use Howler sprites and have it work on iOS 5 (and other OSes) as well. Just calling out the issue.

arjunmehta avatar Aug 15 '13 14:08 arjunmehta

Did you try setting buffer: true to force HTML5 Audio? There's no official support for iOS 5, but the HTML5 Audio portion technically should work.

goldfire avatar Sep 12 '13 07:09 goldfire

Hey James,

I just tried that and it's still not playing, nor triggering any of the listeners (ie. onload, onend). Even moreso, when I force using HTML5 Audio by setting "buffer: true", the audio no longer works on iOS6 either! Have you fully tested your HTML5 native audio functionality in iOS 6? Maybe I'm doing something wrong?

Could you confirm that when you set buffer:true on an iOS device sound no longer plays?

My guess is that it has something to do with the manual human input required to initialize audio on iOS and something is interrupting or not transferring the "click" event to the Audio object so that iOS will allow it to be played.

arjunmehta avatar Sep 15 '13 19:09 arjunmehta

Okay something else super weird: If I "Add to homescreen", in iOS 6 on my iPhone5, with buffer:true (ie. forcing HTML5 Audio I presume?) it works fine. But still not on iOS5.

This makes very little sense why HTML5 Audio would behave differently in the two modes (As a web app, vs within Safari/Chrome) but perhaps knowing the answer to this might help explain what's going on?

arjunmehta avatar Sep 15 '13 21:09 arjunmehta

In case anyone's still curious about this, I stumbled across this thread when I experienced the same issue (trying to use Howler audio sprites in a game that needs to run in iOS5.x Safari). The problem is that in Safari on iOS, HTML5 media doesn't support preload="auto":

The preload attribute is supported in Safari 5.0 and later. Safari on iOS never preloads.

...and also won't respond to node.load(); unless triggered by a user interaction:

In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not.

In iOS<6 (where webaudio is not available) if you instantiate your Howls in some non-user-initiated startup routine, Howl._loaded will never get set to "true" because the audio will not preload. This will block playing later on in Howl.prototype.play when it checks:

if (!self._loaded) {
  self.on('load', function() {
    self.play(sprite, callback);
  });

  return self;
}

A simple workaround would be to instantiate the Howls in a user-triggered event:

var myHowl;

function playHowlSprite(name) {
    if(!myHowl) {
        myHowl = new Howl(myJSON);
    };
    myHowl.play(name);
};

...or to check for iOS5 and in that case set self._loaded = true; in Howl.prototype.load:

Howl.prototype = {
  /**
   * Load an audio file.
   * @return {Howl}
   */
  load: function() {
    var self = this,
      url = null;

    // if no audio is available, quit immediately
    if (noAudio) {
      self.on('loaderror');
      return;
    }

    if([some test for iOS5]) self._loaded = true;
    .
    .
    .

Disclaimer: I'm not actually sure why the second workaround works. When I look at the code, setting self._loaded = true; should still block when the code arrives at the node.readyState check below:

if (node.readyState === 4) {
  node.id = soundId;
  node.currentTime = pos || 0.01;
  node.muted = Howler._muted;
  node.volume = self._volume * Howler.volume();
  setTimeout(function() { node.play(); }, 0);
} else {
  self._clearEndTimer(timerId);

  (function(){
    var sound = self,
      playSprite = sprite,
      fn = callback,
      newNode = node;
    var listener = function() {
      sound.play(playSprite, fn);

      // clear the event listener
      newNode.removeEventListener('canplaythrough', listener, false);
    };
    newNode.addEventListener('canplaythrough', listener, false);
  })();

  return self;
}

With no preloading, node.readyState should always be 0, but something, somewhere is triggering a valid user-initiated load, and I can't figure out what. Nevertheless, it seems to work on iOS5.0 and 5.1 (tested in simulator and on physical devices).

wonderfool avatar Dec 02 '13 18:12 wonderfool