Skip to content

Commit

Permalink
Add playerror event for when mobile HTML5 audio is unable to play
Browse files Browse the repository at this point in the history
Fixes #774
  • Loading branch information
goldfire committed Aug 18, 2017
1 parent b1ea550 commit 92dc9d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -187,6 +187,8 @@ Whether or not to enable the `withCredentials` flag on XHR requests used to fetc
Fires when the sound is loaded.
#### onloaderror `Function`
Fires when the sound is unable to load. The first parameter is the ID of the sound (if it exists) and the second is the error message/code.
#### onplayerror `Function`
Fires when the sound is unable to play. The first parameter is the ID of the sound and the second is the error message/code.
#### onplay `Function`
Fires when the sound begins playing. The first parameter is the ID of the sound.
#### onend `Function`
Expand Down
28 changes: 21 additions & 7 deletions src/howler.core.js
Expand Up @@ -487,6 +487,7 @@
self._onfade = o.onfade ? [{fn: o.onfade}] : [];
self._onload = o.onload ? [{fn: o.onload}] : [];
self._onloaderror = o.onloaderror ? [{fn: o.onloaderror}] : [];
self._onplayerror = o.onplayerror ? [{fn: o.onplayerror}] : [];
self._onpause = o.onpause ? [{fn: o.onpause}] : [];
self._onplay = o.onplay ? [{fn: o.onplay}] : [];
self._onstop = o.onstop ? [{fn: o.onstop}] : [];
Expand Down Expand Up @@ -758,15 +759,28 @@
node.muted = sound._muted || self._muted || Howler._muted || node.muted;
node.volume = sound._volume * Howler.volume();
node.playbackRate = sound._rate;
node.play();

// Setup the new end timer.
if (timeout !== Infinity) {
self._endTimers[sound._id] = setTimeout(self._ended.bind(self, sound), timeout);
}
// Mobile browsers will throw an error if this is called without user interaction.
try {
node.play();

if (!internal) {
self._emit('play', sound._id);
// If the node is still paused, then we can assume there was a playback issue.
if (node.paused) {
self._emit('playerror', sound._id, 'Playback was unable to start. This is most commonly an issue ' +
'on mobile devices where playback was not within a user interaction.');
return;
}

// Setup the new end timer.
if (timeout !== Infinity) {
self._endTimers[sound._id] = setTimeout(self._ended.bind(self, sound), timeout);
}

if (!internal) {
self._emit('play', sound._id);
}
} catch (err) {
self._emit('playerror', sound._id, err);
}
};

Expand Down

0 comments on commit 92dc9d6

Please sign in to comment.