Skip to content

Commit

Permalink
Merge pull request #437 from playcanvas/feature/ie-audio-fix
Browse files Browse the repository at this point in the history
Fix audio halting loading in IE
  • Loading branch information
vkalpias committed Nov 16, 2015
2 parents 2997914 + 383c58d commit 29a0624
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/audio/audio_sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ pc.extend(pc, function () {

var Sound;

// checks if user is running IE
var ie = function () {
var ua = window.navigator.userAgent;

var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}

var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}

return false;
};

if (pc.AudioManager.hasAudioContext()) {
Sound = function (manager, url, success, error) {
this.buffer = null;
Expand Down Expand Up @@ -46,14 +66,27 @@ pc.extend(pc, function () {
console.warn(pc.string.format('Audio format for {0} not supported', url));
success(this);
} else {
var isIE = ie();
// audio needs to be added to the DOM for IE
if (isIE)
document.body.appendChild(this.audio);

this.audio.oncanplaythrough = function () {
// remove from DOM no longer necessary
if (isIE)
document.body.removeChild(this.audio);

if (!this.isLoaded) {
this.isLoaded = true;
success(this);
}
}.bind(this);

this.audio.onerror = function () {
// remove from DOM no longer necessary
if (isIE)
document.body.removeChild(this.audio);

// continue loading through error
success(this);
};
Expand Down

0 comments on commit 29a0624

Please sign in to comment.