简体   繁体   English

SoundJS-将声音分配给变量

[英]SoundJS - assigning sounds to variables

I am loading several mp3 files using loadManifest, but I am a bit confused by how I'd assign the loaded sounds to variables. 我正在使用loadManifest加载多个mp3文件,但对于如何将加载的声音分配给变量感到有些困惑。 Can I use createInstance here? 我可以在这里使用createInstance吗?

My code currently looks like this: 我的代码当前如下所示:

var myRoot = this;

var queue = new createjs.LoadQueue();
queue.addEventListener("fileload", handleFileLoad);
queue.addEventListener("complete", handleComplete);

queue.loadManifest([{ src: "media/file1.mp3", id: "sound1" },
                    { src: "media/file2.mp3", id: "sound2" },
                    { src: "media/file3.mp3", id: "sound3" }]);

function handleFileLoad(event) {    
    // assign each sound to unique variable
    myRoot.sound1 = createjs.Sound.createInstance("sound1");
    myRoot.sound2 = createjs.Sound.createInstance("sound2");
    myRoot.sound3 = createjs.Sound.createInstance("sound3");
}

function handleComplete(event) {
    // start playing sound1
    myRoot.sound1.play();
}

How can I create an instance of a sound using it's ID and assign it to a variable that I can easily access later? 如何使用其ID创建声音实例并将其分配给以后可以轻松访问的变量? Do I need to register sounds before I am able to do that? 我需要先注册声音吗?

Thank you! 谢谢!

The createjs.Sound.play(id) method returns an instance of the sound object. createjs.Sound.play(id)方法返回声音对象的实例。

So you can do: 因此,您可以执行以下操作:

myRoot.sound1 = createjs.Sound.play(id, [createjs.Sound.INTERRUPT_ANY], [delay], [offset], [loops], [volume]);

After this, you can use the reference to manipulate the sound freely. 之后,您可以使用参考来自由地操纵声音。 For example: 例如:

myRoot.sound1.volume = 0.5;
myRoot.sound1.addEventListener("complete", handleSoundComplete);
myRoot.sound1.play();
myRoot.sound1.stop();

By using this you also have several other possibilities, like saving the sound instances in an array to reuse them, or you can play them all the time by their IDs instead using a custom play method. 通过使用此方法,您还可以使用其他几种方法,例如将声音实例保存在数组中以重复使用它们,或者可以通过它们的ID始终播放它们,而不必使用自定义播放方法。

Also, if you are using the manifest loader to load the sound, you do not need to register each sound individually, since they will be registered automatically. 另外,如果您使用清单清单加载器加载声音,则无需单独注册每个声音,因为它们会自动注册。

the following line was missing, and is all that was required for that code to work: 以下行丢失了,并且该代码才能正常工作:

queue.installPlugin(createjs.Sound); 

That, and it's better to move variable assignments out to handleComplete function 这样,最好将变量分配移到handleComplete函数中

The complete working code looks like this: 完整的工作代码如下所示:

var myRoot = this;

var queue = new createjs.LoadQueue();
queue.installPlugin(createjs.Sound); 
queue.addEventListener("complete", handleComplete);

queue.loadManifest([{ src: "media/file1.mp3", id: "sound1" },
                    { src: "media/file2.mp3", id: "sound2" },
                    { src: "media/file3.mp3", id: "sound3" }]);

function handleComplete(event) {
    // assign each sound to unique variable
    myRoot.sound1 = createjs.Sound.createInstance("sound1");
    myRoot.sound2 = createjs.Sound.createInstance("sound2");
    myRoot.sound3 = createjs.Sound.createInstance("sound3");
    // start playing sound1
    myRoot.sound1.play();
}

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

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