ecspresso
    Preparing search index...

    Function createAudioPlugin

    • Create an audio plugin for ECSpresso.

      Provides:

      • audioState resource for fire-and-forget SFX and music
      • audioSource component for entity-attached sounds
      • Volume hierarchy: individual * channel * master
      • playSound / stopMusic event handlers
      • soundEnded event on completion
      • Automatic cleanup on entity removal (dispose callback)

      Sounds must be preloaded through the asset pipeline (loadSound helper).

      Type Parameters

      • Ch extends string
      • G extends string = "audio"

      Parameters

      Returns Plugin<
          WorldConfigFrom<
              AudioComponentTypes<Ch>,
              AudioEventTypes<Ch>,
              AudioResourceTypes<Ch>,
          >,
          EmptyConfig,
          "audio-sync",
          G,
          never,
          "audio-sources",
      >

      const channels = defineAudioChannels({
      sfx: { volume: 1 },
      music: { volume: 0.7 },
      });

      const ecs = ECSpresso.create()
      .withAssets(a => a.add('explosion', loadSound('/sfx/boom.mp3')))
      .withPlugin(createAudioPlugin({ channels }))
      .build();

      await ecs.initialize();
      const audio = ecs.getResource('audioState');
      audio.play('explosion', { channel: 'sfx' });