Check if sound correctly loaded

Hi,

I am actually playing my sound from URL with this function :

SoundController.prototype.playSound = function(audio) {

    this.entity.sound.removeSlot('currentSound');

    var soundAsset = new pc.Asset("soundName.ogg", "audio", { url: audio }); 
    this.app.assets.add(soundAsset);


    this.entity.sound.addSlot('currentSound', {
        asset: soundAsset
    });
    this.entity.sound.play('currentSound');
};

It work fine but, sometime, sound isn’t loaded. So, I want to know if sound is loaded or not.

Is there any way to check this ? And if it’s not loaded restart the call to the server ?

Hi @antoinebao,

Update your code to play the sound after it has finished loading:

SoundController.prototype.playSound = function(audio) {

    this.entity.sound.removeSlot('currentSound');

    var soundAsset = new pc.Asset("soundName.ogg", "audio", { url: audio }); 

    soundAsset.ready(() => {

       this.entity.sound.addSlot('currentSound', {
           asset: soundAsset
       });
       this.entity.sound.play('currentSound');
    });

    this.app.assets.add(soundAsset);
};

https://developer.playcanvas.com/en/api/pc.Asset.html#ready