Permalink
Browse files

Do not Rely on Events to Set Audio Player Playing State

* Events sometimes fire late
* If loop option was set, the audio restarted even though it was
  paused.
  • Loading branch information...
1 parent 4534550 commit 9adb0b6dd4466ffffc11ea5d779625d8f2afbe9d @tf tf committed Jun 10, 2015
Showing with 12 additions and 8 deletions.
  1. +12 −8 vendor/assets/javascripts/audio5.min.js
@@ -546,14 +546,12 @@
* Audio play event handler. Triggered when audio starts playing.
*/
onPlay: function () {
- this.playing = true;
this.trigger('play');
},
/**
* Audio pause event handler. Triggered when audio is paused.
*/
onPause: function () {
- this.playing = false;
this.trigger('pause');
},
/**
@@ -640,12 +638,14 @@
* Play audio
*/
play: function () {
+ this.playing = true;
this.audio.play();
},
/**
* Pause audio
*/
pause: function () {
+ this.playing = false;
this.audio.pause();
},
/**
@@ -816,6 +816,7 @@
*/
play: function () {
if(!this.playing){
+ this.playing = true;
this.audio.play();
}
},
@@ -824,6 +825,7 @@
*/
pause: function () {
if(this.playing){
+ this.playing = false;
this.audio.pause();
}
},
@@ -881,24 +883,26 @@
* Audio play event handler
*/
onPlay: function () {
- this.playing = true;
this.trigger('play');
},
/**
* Audio pause event handler
*/
onPause: function () {
- this.playing = false;
this.trigger('pause');
},
/**
* Playback end event handler
*/
onEnded: function () {
- this.playing = false;
- this.trigger('ended');
- if(this.settings.loop) {
- this.play();
+ var wasPlaying = this.playing;
+
+ if (this.settings.loop && wasPlaying) {
+ this.audio.play();
+ }
+ else {
+ this.playing = false;
+ this.trigger('ended');
}
},
/**

0 comments on commit 9adb0b6

Please sign in to comment.