createSoundGroup
Create a named sound group. A group can cap how many of its sounds play at once and give its members shared play options, such as a lower volume for all effects.
createSoundGroup(groupName: string, options?: { maxInstances?: number; playOptions?: PlayOptions }): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
groupName | string | required | Name of the group. Used by every other group method. |
options | { maxInstances?: number; playOptions?: PlayOptions } | {} | Settings for the group, see below. |
Options
| Property | Type | Default | Description |
|---|---|---|---|
maxInstances | number | no limit | The most sounds the group holds at once. When a new one joins a full group, the oldest is stopped and leaves. |
playOptions | PlayOptions | none | Options merged into every sound that joins. They win over the sound's own options. Options passed to play() win over them for a plain sound, but not for an overlapping instance, which joins the group after its options are set. |
Returns
Nothing.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('laser', '/audio/laser.mp3');
// At most 3 lasers at once, all at half volume
soundHub.createSoundGroup('effects', {
maxInstances: 3,
playOptions: { volume: 0.5 },
});
// Play straight into the group with the groupId play option
soundHub.play('laser', { overlap: true, groupId: 'effects' });
soundHub.play('laser', { overlap: true, groupId: 'effects' });
soundHub.play('laser', { overlap: true, groupId: 'effects' });
soundHub.play('laser', { overlap: true, groupId: 'effects' }); // stops the first one
Good to know
- A name that is already taken is ignored with a debug log. The existing group keeps its settings.
- There are two ways in:
addToSoundGroup, or thegroupIdplay option. Withoverlap, each new instance ('laser:1','laser:2') joins the group, which is what makesmaxInstancesa voice limit. - Playing with a
groupIdthat does not exist does not play the sound at all. Only a debug log says why. - No event is dispatched.
See also
addToSoundGroup: put a loaded sound in the group.getGroup: look at what the group holds.removeSoundGroup: stop everything in the group and delete it.soundGroup: overview of sound groups.