Skip to content

Commit

Permalink
Do not Rely on Events to Set Audio Player Playing State
Browse files Browse the repository at this point in the history
* Events sometimes fire late
* If loop option was set, the audio restarted even though it was
  paused.
  • Loading branch information
tf committed Jun 29, 2015
1 parent 4534550 commit 9adb0b6
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions vendor/assets/javascripts/audio5.min.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -546,14 +546,12 @@
* Audio play event handler. Triggered when audio starts playing. * Audio play event handler. Triggered when audio starts playing.
*/ */
onPlay: function () { onPlay: function () {
this.playing = true;
this.trigger('play'); this.trigger('play');
}, },
/** /**
* Audio pause event handler. Triggered when audio is paused. * Audio pause event handler. Triggered when audio is paused.
*/ */
onPause: function () { onPause: function () {
this.playing = false;
this.trigger('pause'); this.trigger('pause');
}, },
/** /**
Expand Down Expand Up @@ -640,12 +638,14 @@
* Play audio * Play audio
*/ */
play: function () { play: function () {
this.playing = true;
this.audio.play(); this.audio.play();
}, },
/** /**
* Pause audio * Pause audio
*/ */
pause: function () { pause: function () {
this.playing = false;
this.audio.pause(); this.audio.pause();
}, },
/** /**
Expand Down Expand Up @@ -816,6 +816,7 @@
*/ */
play: function () { play: function () {
if(!this.playing){ if(!this.playing){
this.playing = true;
this.audio.play(); this.audio.play();
} }
}, },
Expand All @@ -824,6 +825,7 @@
*/ */
pause: function () { pause: function () {
if(this.playing){ if(this.playing){
this.playing = false;
this.audio.pause(); this.audio.pause();
} }
}, },
Expand Down Expand Up @@ -881,24 +883,26 @@
* Audio play event handler * Audio play event handler
*/ */
onPlay: function () { onPlay: function () {
this.playing = true;
this.trigger('play'); this.trigger('play');
}, },
/** /**
* Audio pause event handler * Audio pause event handler
*/ */
onPause: function () { onPause: function () {
this.playing = false;
this.trigger('pause'); this.trigger('pause');
}, },
/** /**
* Playback end event handler * Playback end event handler
*/ */
onEnded: function () { onEnded: function () {
this.playing = false; var wasPlaying = this.playing;
this.trigger('ended');
if(this.settings.loop) { if (this.settings.loop && wasPlaying) {
this.play(); this.audio.play();
}
else {
this.playing = false;
this.trigger('ended');
} }
}, },
/** /**
Expand Down

0 comments on commit 9adb0b6

Please sign in to comment.