SoundJS - 通过不在IE中工作的实例播放的声音

时间:2015-05-27 09:17:17

标签: internet-explorer createjs soundjs

我在使用SoundJS的浏览器之间遇到了一些不一致的行为,即IE11对于从抽象声音实例播放感到吝啬。

以下代码适用于我测试过的所有其他浏览器,但不适用于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>

我可以通过将startSound()函数更改为此来在IE11中播放声音:

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

但这会给我的其他实现带来问题,因为每个声音都需要一个可识别的唯一实例,我可以回调它来实现补间等功能。

我有什么遗漏可以让第一种方法发挥作用吗?

1 个答案:

答案 0 :(得分:2)

好的捕获,HTMLAudioPlugin和AbstractPlugin代码中存在一个错误,如果在加载src之前创建实例,则持续时间设置不正确。在播放呼叫时,将根据持续时间检查位置,并在持续时间为NaN时检查失败。

要解决此问题,您需要在创建实例之前加载音频。

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;
}

显然,我们还会在将来的版本中修复该错误。希望有所帮助。