@nanoforge-dev/music
v3.0.0
Published
NanoForge Engine - Music
Readme
About
@nanoforge-dev/music is NanoForge's built-in music library. It manages a collection of named HTMLAudioElement tracks exposed on Context.music and ensures only one track plays at a time — starting a new track pauses whichever one was playing.
Installation
Node.js 26 or newer is required.
npm install @nanoforge-dev/music
yarn add @nanoforge-dev/music
pnpm add @nanoforge-dev/music
bun add @nanoforge-dev/musicExample usage
Register MusicLibrary on your client app:
import { MusicLibrary } from "@nanoforge-dev/music";
app.use(new MusicLibrary());Load and play tracks through Context.music:
public override async __init(): Promise<void> {
ctx.music.load("menu", "/assets/menu.mp3");
ctx.music.load("level-1", "/assets/level-1.mp3");
}
public override async __run(ctx: Context): Promise<void> {
if (levelStarted) ctx.music.play("level-1"); // stops "menu" automatically
}Call ctx.music.mute() to toggle the muted state of every loaded track. play throws NfNotFound when the given key was never loaded.
Links
Contributing
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
documentation.
See the contribution guide if you'd like to submit a PR.
Help
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to ask questions in discussions.
