简体   繁体   English

SoundJS-在audiosprite中注册4种声音

[英]SoundJS - registering 4 sounds in an audiosprite

I'm loading a SoundJS 1.0 audiosprite with preloadJS 1.0. 我正在使用preloadJS 1.0加载SoundJS 1.0音频精灵。 The audiosprite is split into 4 sounds which I'm calling a,b,c and d. 音频精灵被分为4种声音,我分别称为a,b,c和d。 (Elsewhere in the code it randomizes which of the 4 sounds to play.) (在代码的其他地方,它随机播放四种声音中的哪一种。)

Here's the basic loading and declaration code: 这是基本的加载和声明代码:

 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");

When I load the sound I get: Uncaught TypeError: Cannot read property '1' of null 当我加载声音时,我得到:Uncaught TypeError:无法读取null的属性“ 1”

If I comment out the last 3 lines and just create 1 instance (eg. sound+"a"), the sound loads and I get no error, but obviously I only get 1 of the 4 parts. 如果我注释掉最后3行并仅创建1个实例(例如sound +“ a”),则声音会加载,并且不会出现任何错误,但显然我只会得到4个部分中的1个。

I feel like the problem is it doesn't like me calling createInstance back to back like this. 我觉得问题在于它不喜欢我像这样背靠背调用createInstance。 What is the correct way to do this? 正确的方法是什么?

You need to use loadManifest for multiple files; 您需要对多个文件使用loadManifest; loadFile only loads a single file. 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},
 ]
 }});

Edit - the handlePreloadComplete function from line 286 looks a bit suspect and may be a bug https://createjs.com/docs/soundjs/files/soundjs_AbstractPlugin.js.html 编辑-第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;
    }
};

Iterating over the length of this._soundInstances[src], but then setting it to null on the first iteration will give the error you mention. 遍历this._soundInstances [src]的长度,但是在第一次迭代中将其设置为null会产生您提到的错误。

Perhaps it should be 也许应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM