I am trying to load a sound from a URL at runtime (I am not able to know in advance the URL of the sound), but I cannot make it work.
This is what I do in my addSound function:
Primitives.addSound = function(app, entity, soundUrl)
{
entity.addComponent("sound");
var soundAsset = new pc.Asset(soundUrl, "audio", { url: soundUrl });
app.assets.load(soundAsset);
var onAssetLoad = function()
{
entity.sound.addSlot('MySoundSlot', {
loop: true,
autoPlay: true,
asset: soundAsset,
});
entity.sound.positional = true;
};
soundAsset.once('load', onAssetLoad);
}
The onLoadAsset function is called, the sound is loaded (I can see in the console the length, frequency, etc. of the audio file) but no sound is played.
However, if I use an asset that is in the project asset list, it works instead:
Primitives.addSound = function(app, entity)
{
entity.addComponent("sound");
var existingAsset = app.assets.get(12887060);
entity.sound.addSlot('MySoundSlot', {
loop: true,
autoPlay: true,
asset: existingAsset,
});
entity.sound.positional = true;
}
What am I doing wrong???
Isn’t it possible to load a sound from a URL at runtime???