Howler.js: Pause all & resume all sprites

Created on 26 Sep 2016  路  7Comments  路  Source: goldfire/howler.js

Hey there guys, thanks for the great library.

I am having an issue with pausing all sprites in a game on visibilitychange and then resuming the sounds when the user returns to the game.

I always get the following error:

Uncaught TypeError: Cannot read property '0' of undefined

From the this line in the play function

// Determine how long to play for and where to start playing.
var seek = sound._seek > 0 ? sound._seek : self._sprite[sprite][0] / 1000; // THIS LINE

I can't seem to understand what the issue is, I have managed to recreate the scenario in a jsfiddle however it is quite crude. Simply navigate to a different tab and then back and it breaks. Any ideas would be helpful thanks.

https://jsfiddle.net/brenwell/pqcfze7s/2/

feature

Most helpful comment

SO the above solution would be quite shit as it would destroy playback for users not using sprites who want to play the whole sound from beginning to end. So I have a better solution in case you want to incorporate it. A new unpause method.

    /**
     * Resumes all paused sounds
     * @return {Howl}
     */
    unpause: function() {

      var self = this;
      var id;
      var num = 0;

      for (var i=0; i<self._sounds.length; i++) {
        if (self._sounds[i]._paused && !self._sounds[i]._ended) {
          num++;
          id = self._sounds[i]._id;
          self.play(id)
        }
      }

      return self;
    },

All 7 comments

OOps my last jsfiddle change was not saved, 1 second I will fix it

Alright here is the correct link https://jsfiddle.net/brenwell/pqcfze7s/2/

I have built a work around http://jsfiddle.net/brenwell/pqcfze7s/5/ But I feel like that this must already be built in and its either a mistake on my side or a bug. Would love some feedback. Thanks.

I also got the functionality to work by doing the following

play: function(sprite, internal) {
  ...
        // Check if there is a single paused sound that isn't ended.
        // If there is, play that sound. If not, continue as usual.
        var num = 0;
        for (var i=0; i<self._sounds.length; i++) {
          if (self._sounds[i]._paused && !self._sounds[i]._ended) {
            num++;
            id = self._sounds[i]._id;
            self.play(id) // added this
          }
        }

        return //added this

SO the above solution would be quite shit as it would destroy playback for users not using sprites who want to play the whole sound from beginning to end. So I have a better solution in case you want to incorporate it. A new unpause method.

    /**
     * Resumes all paused sounds
     * @return {Howl}
     */
    unpause: function() {

      var self = this;
      var id;
      var num = 0;

      for (var i=0; i<self._sounds.length; i++) {
        if (self._sounds[i]._paused && !self._sounds[i]._ended) {
          num++;
          id = self._sounds[i]._id;
          self.play(id)
        }
      }

      return self;
    },

Yes @brenwell (and @goldfire), this needs to be incorporated in library immediately. This kept me from losing what little hair I have left.

I just ran into the same issue, but in a round about way. Thanks for the fix! I hope this is resolved in the library soon, the work around has stopped me from using a few features.

Was this page helpful?
0 / 5 - 0 ratings