@telazer/phaser-audio-helper
v1.0.5
Published
A helper library for Phaser audio management
Downloads
44
Readme
Telazer - Phaser Audio Helper
A TypeScript utility for Phaser 3 that simplifies the loading, management, and playback of sound effects and music. With clear separation of keys and file paths, it offers an elegant API for managing game audio in a scalable way.
Installation
npm install @telazer/phaser-audio-helperKey Features
- 🔊 Easy loading and playback of SFX and music
- 🗝️ Distinct
keyandsourcemapping - 🎚️ Global volume control
- 🔁 Looping and sequence playback
- 🎲 Random playback utilities
- 🎛️ Supports playback rate variation
Getting Started
Import AudioHelper into your Phaser scene. It’s best to load sounds in a boot or preload scene.
Example Scene Integration
import { AudioHelper } from "@telazer/phaser-audio-helper";
export class InitScene extends Phaser.Scene {
preload() {
AudioHelper.loadSfx(this, [
{ key: "pop", source: "./assets/sounds/pop.mp3", volume: 1 },
]);
}
async create() {
AudioHelper.play("pop");
}
}Loading Audio
Load SFX
await AudioHelper.loadSfx(this, [
{ key: "click", source: "./assets/sounds/click.mp3", volume: 0.8 },
{ key: "jump", source: "./assets/sounds/jump.wav", volume: 1.0 },
]);Load Music
await AudioHelper.loadMusic(this, [
{ key: "bg_music", source: "./assets/music/loop.mp3", volume: 1 },
]);Playback
Play a Sound
AudioHelper.play("click");Play Music
AudioHelper.play("bg_music"); // Plays in loop modePlay on Channel (with transition)
// First call starts track A on channel "bg"
AudioHelper.playOnChannel("bg", "bg_music_A");
// Next call stops previous and starts track B
AudioHelper.playOnChannel("bg", "bg_music_B");
// Use fade transition instead of immediate stop
AudioHelper.playOnChannel("bg", "bg_music_C", { out: "fade" });
// Query current playing key on channel
const current = AudioHelper.isPlayingOnChannel("bg");
// Optionally stop current key on channel with fade
AudioHelper.stopOnChannel("bg", { out: "fade" });Stop Sound or Music
AudioHelper.stop("click");Fade Out and Stop (for transitions)
// Fade out over 800ms, then stop and restore configured volume for next play
AudioHelper.fadeOut("bg_music", 800);Advanced Playback
Set Global Volume
AudioHelper.setSoundVolume(0.5);
AudioHelper.setMusicVolume(0.8);Play with Randomized Rate
AudioHelper.playRate("jump", 0.1); // ±10% pitch variancePlay a Random Sound
AudioHelper.playRandom(["hit_1", "hit_2", "hit_3"]);Play in Sequence
AudioHelper.playSequence(["step_1", "step_2", "step_3"], 150, 30); // ±30ms varianceAPI Reference
| Method | Description |
| ----------------------------------------- | ----------------------------------- |
| loadSfx(scene, items) | Load sound effects (AudioItem[]) |
| loadMusic(scene, items) | Load music tracks (AudioItem[]) |
| play(key) | Play a sound or music by key |
| playOnChannel(channel, key, options?) | Play by channel with transition |
| isPlayingOnChannel(channel) | Get current key if playing |
| stopOnChannel(channel, options?) | Stop current channel key (fade opt) |
| isPlaying(key) | Check if a key is currently playing |
| stop(key) | Stop a sound or music by key |
| fadeOut(key, duration?) | Fade volume to 0 and stop |
| setSoundVolume(volume) | Set global volume for SFX |
| setMusicVolume(volume) | Set global volume for music |
| playRate(key, rate) | Play sound with random pitch offset |
| playRandom(keys) | Randomly play one from given keys |
| playSequence(keys, delay, randomOffset) | Play keys in sequence with timing |
AudioItem Structure
interface AudioItem {
key: string; // Internal reference key
source: string; // Path to the audio file
volume: number; // Individual volume multiplier (0–1)
}License
MIT License
Copyright (c) 2025 Telazer LLC.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.
