# Audio

Play music, podcasts and the sound of any media with AudioPlayer, on the platform's decoders, without decoding a picture.

`AudioPlayer` plays audio with the platform's own decoders: no extra codec library. MP3, AAC
(M4A), FLAC and WAV play on every platform. It runs on the same engine as the video player, so
playlists, streaming formats, offline playback and the system's media controls work the same.

## Add the dependency

```kotlin title="build.gradle.kts"
commonMain.dependencies {
    implementation("dev.nucleusframework:nucleus.audio-player:3.0.0")
}
```

## Play a file

```kotlin
val player = AudioPlayer()
player.play("https://example.com/episode.mp3")
player.pause()
player.seekTo(90_000)       // milliseconds
player.setVolume(0.8f)
player.setRate(1.5f)
player.release()            // when you are done
```

In Compose, `rememberAudioPlayerLiveState()` gives a remembered player and a snapshot of its
state (`state`, `position`, `duration`, `volume`) that recomposes as it plays, and releases the
player on dispose:

```kotlin
val live = rememberAudioPlayerLiveState()
Text("${live.position / 1000} s")
Button(onClick = { live.player.play(url) }) { Text("Play") }
```

`state` is `IDLE`, `BUFFERING`, `PLAYING` or `PAUSED`. Errors arrive in `error` and through
`setOnErrorListener`, called once per error.

## The sound of a video

`AudioPlayer` plays the sound of a video file without decoding its picture: ExoPlayer disables
its video track, Media Foundation runs an audio-only engine, GStreamer builds no video branch, AVFoundation disables the item's
video tracks, the web uses an `<audio>` element.
A podcast published as video costs no video decoder.

## The full engine

`player.playback` is the `MediaPlaybackState` underneath. Everything the
[playback control](/docs/media/playback) page describes is there: playlists, seek modes,
silence skipping, loudness and equalizer, stats.

```kotlin
player.playback.skipSilence = true
player.playback.loudnessGainDb = 6f
```

## What's next

- [Playback control](/docs/media/playback): playlists, speed, silences, loudness and equalizer.
- [System session](/docs/media/system-session): lock screen, notification and media keys.
- [Offline](/docs/media/offline): keep episodes on the device.
