Skip to content

Commit

Permalink
IMPROVEMENT detection of sound loaded and ready to play.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignacio Calderon committed Apr 3, 2014
1 parent ad03223 commit 3fc6450
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/base/utils/Audio.js
Expand Up @@ -34,7 +34,7 @@ Quark.inherit(Audio, Quark.EventDispatcher);
*/
Audio.prototype.load = function()
{
this._element.addEventListener("progress", this._evtHandler, false);
this._element.addEventListener("canplay", this._evtHandler, false);
this._element.addEventListener("ended", this._evtHandler, false);
this._element.addEventListener("error", this._evtHandler, false);
try{
Expand All @@ -49,25 +49,13 @@ Audio.prototype.load = function()
*/
Audio.prototype._evtHandler = function(e)
{
if(e.type == "progress")
if(e.type == "canplay")
{
var i = 0, buffered = 0, ranges = e.target.buffered;
if(ranges && ranges.length > 0)
{
for (i = ranges.length - 1; i >= 0; i--)
{
buffered = (ranges.end(i) - ranges.start(i));
}
}
var percent = buffered / e.target.duration;
if(percent >= 1)
{
this._element.removeEventListener("progress", this._evtHandler);
this._element.removeEventListener("error", this._evtHandler);
this._loaded = true;
this.dispatchEvent({type:"loaded", target:this});
if(this.autoPlay) this.play();
}
this._element.removeEventListener("canplay", this._evtHandler);
this._element.removeEventListener("error", this._evtHandler);
this._loaded = true;
this.dispatchEvent({type:"loaded", target:this});
if(this.autoPlay) this.play();
}else if(e.type == "ended")
{
this.dispatchEvent({type:"ended", target:this});
Expand Down

0 comments on commit 3fc6450

Please sign in to comment.