> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nanoforge.eu/llms.txt
> Use this file to discover all available pages before exploring further.

# @nanoforge-dev/music

[![NanoForge](https://raw.githubusercontent.com/NanoForge-dev/Engine/main/.github/logo.png)](https://github.com/NanoForge-dev)

[![npm version](https://img.shields.io/npm/v/@nanoforge-dev/music.svg?maxAge=3600)](https://www.npmjs.com/package/@nanoforge-dev/music)

[![npm downloads](https://img.shields.io/npm/dt/@nanoforge-dev/music.svg?maxAge=3600)](https://www.npmjs.com/package/@nanoforge-dev/music)

[![Tests status](https://github.com/NanoForge-dev/Engine/actions/workflows/tests.yml/badge.svg)](https://github.com/NanoForge-dev/Engine/actions/workflows/tests.yml)

[![Last commit](https://img.shields.io/github/last-commit/NanoForge-dev/Engine.svg?logo=github\&logoColor=ffffff\&path=modules%2Fmusic)](https://github.com/NanoForge-dev/Engine/commits/main/modules/music)

[![Contributors](https://img.shields.io/github/contributors/NanoForge-dev/Engine.svg?maxAge=3600\&logo=github\&logoColor=fff\&color=00c7be)](https://github.com/NanoForge-dev/Engine/graphs/contributors)

## 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.**

```sh theme={null}
npm install @nanoforge-dev/music
yarn add @nanoforge-dev/music
pnpm add @nanoforge-dev/music
bun add @nanoforge-dev/music
```

## Example usage

Register `MusicLibrary` on your client app:

```ts theme={null}
import { MusicLibrary } from "@nanoforge-dev/music";

app.use(new MusicLibrary());
```

Load and play tracks through `Context.music`:

```ts theme={null}
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

* [GitHub][source]
* [npm][npm]

## Contributing

Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
[documentation][documentation].\
See [the contribution guide][contributing] 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][discussions].

[documentation]: https://github.com/NanoForge-dev/Engine

[discussions]: https://github.com/NanoForge-dev/Engine/discussions

[source]: https://github.com/NanoForge-dev/Engine/tree/main/modules/music

[npm]: https://www.npmjs.com/package/@nanoforge-dev/music

[contributing]: https://github.com/NanoForge-dev/Engine/blob/main/.github/CONTRIBUTING.md

## Classes

* [MusicLibrary](https://docs.nanoforge.eu/references/engine/3.0.0/music/classes/MusicLibrary.md)

## Interfaces

* [MusicContextApi](https://docs.nanoforge.eu/references/engine/3.0.0/music/interfaces/MusicContextApi.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.