How do I play audio and animation assets with array: true

I can’t get animation and audio played by using:

RandomAnimation.attributes.add(‘sounds’, {
type: ‘asset’,
assetType: ‘audio’,
title: ‘Sounds’,
placeholder: ‘please choose sound file’,
array: true**
});

Say if I have create 4 audios for this.sounds, I don’t know how to correctly access this.sounds[i], “i” means 0 to 3.

I tried this.sounds[i].resource but failed, It seems that I will need to add slot and get the audio played using the slot name, but I can’t find any samples for accessing array audio asset, can any one help? Thanks in advance! Truly!

Does it have to be in an audio asset array? The normal practise here is to add all the audio assets to the slots on the audio component.

Thanks for the prompt response. I am enum animation (say 4 sets) for the artists to simply mapping the same enum ID (0-3) to the audio and animation assets, that’s why I want to get the asset array working

Ok, still think it be easier to do the slots directly but it’s possible to create the slots on init via addSlot. https://developer.playcanvas.com/en/api/pc.SoundComponent.html#slots

Give each slot the string of the index number (eg ‘0’, ‘1’) as the slot name.

Now you can get the slot from the index to play or as addSlot returns the slot created, you can store that in an array that maps to the order of the audio assets.

Alternatively, you can have one slot and change the asset when you want to play the sound. The sounds can’t overlap though.

Thanks for your prompt response. I have read your api reference. However, my problem is the 1st line:

// get an asset by id
var asset = app.assets.get(10);

giving my code as an example

RandomAnimation.attributes.add(‘sounds’, {
type: ‘asset’,
assetType: ‘audio’,
title: ‘Sounds’,
placeholder: ‘please choose sound file’,
array: true**
});

say if I want to acess the 3rd elements on my sounds asset

I tried both:
var asset = app.assets.get(this.sounds[2]);

var asset = app.assets.get(this.sounds[2].resource);

both seem not working

Here’s an example of going from an asset list to the sound slots: https://playcanvas.com/editor/scene/933385

Thanks a lot for your prompt assistance. Really appreciated!!

Hello Yaustar,

Thanks for your help on the audio, it works like a charm. However, I can’t get things woks on animations as follows:

RandomAnimation.attributes.add(‘animations’, {
type: ‘asset’,
assetType: ‘animation’,
title: ‘Animations’,
array: true
});

this._animation.assets = [];

for (var j = 0; j < this.animations.length; ++j) {
    var aniClipAsset = this.animations[i];
    console.log("aniClipAsset: " + j + " - " + aniClipAsset );
    this._animation.assets.push(aniClipAsset);
}

Please help, thanks a lot!

It’s going to be a similar pattern with a slightly different function call due to the animation component to play an animation.

It plays by asset name so you can you use this.animations[someIndex].name when playing an animation with the component.

Thanks for the prompt response. the animation is now played.