miaoda-game-audio-phaser
v0.3.2
Published
Phaser audio control for bounded sound effects, pitch variation, music transitions, and reliable ducking.
Maintainers
Readme
miaoda-game-audio-phaser
Phaser 4 audio control for named sound effects, generated short sounds, music transitions, and music ducking. Choose it when the game already uses Phaser audio and needs consistent limits and cleanup at Scene boundaries.
pnpm add miaoda-game-audio-phaserimport {AudioController} from 'miaoda-game-audio-phaser';
const audio = new AudioController(this);
audio.playSfx('coin', {group: 'pickup', maxConcurrent: 3});
audio.playMusic('exploration', {fadeInMs: 500});Preload file-based audio through Phaser as usual. The controller does not load files or decide which gameplay event should play a sound.
Generated sound effects
With Phaser's Web Audio sound manager, install short effects without files:
audio.installGeneratedSfx({
projectSeed: 'neon-runner-v2',
style: 'arcade',
sounds: [
{id: 'jump', kind: 'jump', variants: 4},
{id: 'enemy-hit', kind: 'hit', variants: 5},
],
});
audio.playGeneratedSfx('enemy-hit', {cooldownMs: 35, maxConcurrent: 3, overflow: 'stop-oldest'});Generated buffers require Web Audio; HTML5 Audio and No Audio managers cannot play them. Installed buffers are reusable across Scenes.
Effects and music
playSfx(keyOrKeys, options) supports group, cooldownMs, maxConcurrent, overflow, volume, detuneRange, and duckMusic. It returns null when suppressed, otherwise the started Phaser sound. duckMusic: true lowers music until that sound ends.
audio.crossFadeMusic('combat', {durationMs: 700, volume: 0.9});
const restore = audio.duckMusic({volume: 0.3, attackMs: 80, releaseMs: 300});
restore();
audio.setMusicVolume(0.7);
audio.setSfxMuted(false);playMusic, crossFadeMusic, and stopMusic accept fade options. Calling destroy() or allowing the Scene to shut down releases sounds, timers, and owned tweens. Already-captured sound-complete or tween callbacks become inert, so they cannot reclaim a sound twice or write a released track. Music and SFX volume/mute settings are independent.
