internet-explorercreatejssoundjs

SoundJS -- sounds played via instances not working in IE


I've run into some inconsistent behaviour between browsers with SoundJS, namely that IE11 is stingy about playing from Abstract Sound Instances.

The following code works in every other browser I've tested, but not in IE11:

<html>

<head>

<script src="https://code.createjs.com/soundjs-0.6.1.min.js"></script>
<script>

var sounds = {}

function loadSounds() {
    createjs.Sound.registerSound('audio/song.mp3', 'song', 1);
    songInst = createjs.Sound.createInstance('song');
    sounds['song'] = songInst;
}

function startSound(id,v,l){
    sounds[id].play({loop:((l===true)?-1:0),volume:v});
}

</script>
</head>

<body onload="loadSounds()">
<button value="StartSound" onclick="startSound('song',1,true)">startSound</button>
</body>

</html>

I AM able to get the sound to play in IE11 by changing the startSound() function to this:

function startSound(id,v,l){
    createjs.Sound.play(id,{loop:((l===true)?-1:0),volume:v});
}

But this creates problems for the rest of my implementation, since each sound will need a uniquely-identifiable instance that I can call back to for features like volume tweening.

Is there anything I'm missing that would allow the first approach to work?


Solution

  • Good catch, there is a bug in the HTMLAudioPlugin and AbstractPlugin code that if you create an instance before the src is loaded, the duration is not set properly. On play calls, position is checked against duration and that check fails when duration is NaN.

    To work around this issue, you need to load your audio before you create the instance.

    createjs.Sound.addEventListener("fileload", createSound);
    createjs.Sound.registerSound("audio/song.mp3", "song", 1);
    
    
    function createSound(event) {
      songInst = createjs.Sound.createInstance("song");
      // this could also use event.src
      sounds['song'] = songInst;
    }
    

    Obviously we'll also fix the bug in a future release. Hope that helps.