SoundJS - 在audiosprite中注册4个声音

时间:2018-03-06 18:12:56

标签: javascript soundjs

我正在使用preloadJS 1.0加载SoundJS 1.0 audiosprite。音频电视分为4种声音,我称之为a,b,c和d。 (在代码的其他地方,它会随机播放4种声音中的哪一种。)

这里是基本的加载和声明代码:

 var queue = new createjs.LoadQueue();
 createjs.Sound.alternateExtensions = ["mp3"];
 queue.addEventListener("complete",handleComplete);

 //load audiosprite and define start/duration times with variables.
 queue.loadFile({src:path,data:{
     audioSprite:[
     {id:sound+"a", startTime:aStart, duration: aDur},
     {id:sound+"b", startTime:bStart, duration: bDur},
     {id:sound+"c", startTime:cStart, duration: cDur},
     {id:sound+"d", startTime:dStart, duration: dDur},
     ]
     }});

 //register the 4 audioSprite sounds as individual sound instances
    myAudio[sound+"a"] = createjs.Sound.createInstance(sound+"a");
    myAudio[sound+"b"] = createjs.Sound.createInstance(sound+"b");
    myAudio[sound+"c"] = createjs.Sound.createInstance(sound+"c");
    myAudio[sound+"d"] = createjs.Sound.createInstance(sound+"d");

当我加载声音时,我得到:Uncaught TypeError:无法读取属性' 1'为null

如果我注释掉最后3行并且只创建1个实例(例如声音+" a"),声音加载并且我没有错误,但显然我只得到4个部分中的1个

我觉得问题是它不像我这样背靠背调用createInstance。这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您需要将loadManifest用于多个文件; loadFile只加载一个文件。

 queue.loadManifest({src:path,data:{
 audioSprite:[
 {id:sound+"a", startTime:aStart, duration: aDur},
 {id:sound+"b", startTime:bStart, duration: bDur},
 {id:sound+"c", startTime:cStart, duration: cDur},
 {id:sound+"d", startTime:dStart, duration: dDur},
 ]
 }});

编辑 - 第286行的handlePreloadComplete函数看起来有点可疑,可能是一个错误https://createjs.com/docs/soundjs/files/soundjs_AbstractPlugin.js.html

p._handlePreloadComplete = function (event) {
    var src = event.target.getItem().src;
    this._audioSources[src] = event.result;
    for (var i = 0, l = this._soundInstances[src].length; i < l; i++) {
        var item = this._soundInstances[src][i];
        item.playbackResource = this._audioSources[src];
        // ToDo consider adding play call here if playstate == playfailed
        this._soundInstances[src] = null;
    }
};

迭代this._soundInstances [src]的长度,但是在第一次迭代时将其设置为null将给出你提到的错误。

也许应该是

this._soundInstances[src][i] = null;