# Libgdx Audio Lifecycle

> Use when writing libGDX Java/Kotlin code involving audio (Sound, Music, Gdx.audio). Use when debugging audio not playing, audio format issues, or platform-specific audio behavior in libGDX.

- **Type:** Skill
- **Install:** `agentstack add skill-kyu-n-gdx-claude-skills-libgdx-audio-lifecycle`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [kyu-n](https://agentstack.voostack.com/s/kyu-n)
- **Installs:** 0
- **Category:** [Content & Media](https://agentstack.voostack.com/c/content-and-media)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [kyu-n](https://github.com/kyu-n)
- **Source:** https://github.com/kyu-n/gdx-claude-skills/tree/master/skills/libgdx-audio-lifecycle

## Install

```sh
agentstack add skill-kyu-n-gdx-claude-skills-libgdx-audio-lifecycle
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# libGDX Audio

Quick reference for libGDX audio APIs. Covers Sound, Music, platform gotchas, and common audio patterns.

## Audio: Sound vs Music

| | Sound | Music |
|---|---|---|
| **Loading** | Fully into memory | Streamed from disk |
| **Use for** | Short effects ( {
    // Called when music finishes playing
});

bgm.dispose();                                 // MUST call when done
```

In non-trivial projects, prefer loading audio via `AssetManager` rather than raw `Gdx.audio.newMusic()`. AssetManager handles disposal via `unload()` and supports async loading.

**Gotchas:**
- **OnCompletionListener does NOT fire when looping is true.** If you need looping + completion callback, set looping=false and restart in the listener.
- libGDX **automatically pauses/resumes Music** on Android pause/resume. You do NOT need to manually pause music in `ApplicationListener.pause()`. Doing so is redundant.
- Only one OnCompletionListener per Music instance (last set wins).
- `setPosition()` is unreliable on some Android devices for MP3 — seeking uses bitrate estimation and can be off by several seconds. OGG seeking is more accurate, but OGG is unsupported on iOS. For cross-platform seeking accuracy, consider WAV (large files) or accept MP3 imprecision.

## Common Patterns

### Shared Music Across Screens

Store Music in the Game class, not in individual Screens. Dispose only in `Game.dispose()`.

```java
public class MyGame extends Game {
    public Music bgm;

    @Override
    public void create() {
        bgm = Gdx.audio.newMusic(Gdx.files.internal("theme.mp3")); // use MP3 for iOS compatibility
        bgm.setLooping(true);
        bgm.play();
        setScreen(new MenuScreen(this));
    }

    @Override
    public void dispose() {
        bgm.dispose();
        getScreen().dispose();
    }
}
```

## Platform Differences

| Behavior | Desktop (LWJGL3) | Android | iOS (RoboVM) |
|---|---|---|---|
| OGG support | Yes | Yes | **No** |
| Music auto-pause | On minimize only | Yes | Yes |
| Sound size limit | None | **<1MB PCM** | None |
| MP3 seeking accuracy | Good | **Unreliable** | Good |

## Common Mistakes

1. **Manually pausing Music in pause()** — libGDX does this automatically. Redundant code confuses readers.
2. **Using Sound for long audio** — Sound loads entirely into memory. Use Music for anything over a few seconds.
3. **Expecting OnCompletionListener with looping** — It won't fire. Use looping=false + manual restart.
4. **Using OGG on iOS** — Will fail silently or crash. Use WAV/MP3.
5. **Playing Sound in create() on Android** — Audio system may not be ready. Defer to first `render()` frame.
6. **Relying on Music.setPosition() accuracy with MP3 on Android** — Seeking can be off by seconds. Use OGG for accuracy (but not on iOS).
7. **Checking Sound.play() return value for failure detection** — Behavior is backend-dependent. Don't rely on it.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [kyu-n](https://github.com/kyu-n)
- **Source:** [kyu-n/gdx-claude-skills](https://github.com/kyu-n/gdx-claude-skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-kyu-n-gdx-claude-skills-libgdx-audio-lifecycle
- Seller: https://agentstack.voostack.com/s/kyu-n
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
