# Media player

Play video and audio with one Compose API on Windows, macOS, Linux, Android, iOS and the web, with HLS, DASH, offline playback and the system's media controls.

The media modules play video and audio in Compose with the platform's own decoders: Media
Foundation on Windows, AVFoundation on macOS and iOS, GStreamer on Linux, Media3 ExoPlayer on
Android and the browser's `<video>` on the web. You write against one state object; every platform behaves the
same, to the millisecond and to the frame.

On Linux the player uses the system's GStreamer, 1.20 or later, with its base, good, bad and
libav plugins (`gstreamer1.0-plugins-*`, `gstreamer1.0-libav` on Debian and Ubuntu). It is not
bundled: without it, opening a media reports an error.

## Pick the modules

| Module | Artifact | What it holds |
| ------ | -------- | ------------- |
| `media-player` | `nucleus.media-player` | `VideoPlayerState`, `VideoPlayerSurface`, fullscreen, subtitles, picture-in-picture, thumbnails |
| `audio-player` | `nucleus.audio-player` | `AudioPlayer`, the same engine without a picture |
| `media-engine` | `nucleus.media-engine` | `MediaPlaybackState`, the whole playback contract without UI, and `MediaDownloads`; the two players depend on it |
| `media-hls` | `nucleus.media-hls` | HLS streams (`.m3u8`) |
| `media-dash` | `nucleus.media-dash` | MPEG-DASH streams (`.mpd`) |
| `media-smoothstreaming` | `nucleus.media-smoothstreaming` | Microsoft Smooth Streaming (`.ism/Manifest`) |
| `media-session` | `nucleus.media-session` | The system's media controls: lock screen, notification, Now Playing, SMTC |

Streaming formats are modules of their own, as in Media3: an app that plays no HLS carries none of
it. Add a format's module and the players open it; nothing else to configure.

## Add the dependency

```kotlin title="build.gradle.kts"
kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("dev.nucleusframework:nucleus.media-player:3.0.0")
            // Optional: the formats you stream and the system controls
            implementation("dev.nucleusframework:nucleus.media-hls:3.0.0")
            implementation("dev.nucleusframework:nucleus.media-session:3.0.0")
        }
    }
}
```

The modules are Kotlin Multiplatform: the same artifacts cover the JVM desktop, Android, iOS
and the web (Wasm and JS).

## Play a video

```kotlin
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import dev.nucleusframework.mediaplayer.VideoPlayerSurface
import dev.nucleusframework.mediaplayer.rememberVideoPlayerState

@Composable
fun Player() {
    val player = rememberVideoPlayerState()
    LaunchedEffect(Unit) {
        player.openUri("https://example.com/movie.mp4")
    }
    VideoPlayerSurface(player, Modifier.fillMaxSize())
}
```

`rememberVideoPlayerState` releases the player when it leaves the composition. The state is
plain Compose state: `isPlaying`, `currentTime`, `duration`, `sliderPos`, `isLoading` and
`error` recompose your controls as they change.

## One behaviour on every platform

Each behaviour is a written rule, checked against every real engine by the same conformance
scenarios: frame-numbered fixtures read back from the screen, exact state traces and latency
budgets. A few examples of what that pins down:

- A seek shows **the frame whose interval contains the target**, not the next key frame, and
  the position reads the target exactly from the call on.
- Paused, the position is the start of the frame on screen.
- `onPlaybackEnded` runs once per end; with `loop`, `onRestart` runs instead.
- A missing source, an HTTP error, a refused connection and an undecodable file map to the same
  `VideoPlayerError` subclass everywhere.

Where an engine cannot follow a rule, the scenario says so and the page lists it: see
[Platform support](/docs/media/platform-support).

- `MediaPlaybackState` (`media-engine`) holds every behaviour. A platform only implements an
  internal engine interface: a session that opens a URI, plays, pauses, seeks and reports.
- Every public call is safe from any thread. It updates what you read at once and records the
  intent; a serialized context brings the engine to it. A storm of calls costs a few engine
  calls and the last intent wins.
- On the desktop, frames reach Compose without a CPU copy: Media Foundation renders into a
  shared Direct3D 11 texture and AVFoundation into an IOSurface, both drawn by a
  [`TextureView`](/docs/native-content/texture-view). On the web, each frame of a `<video>` that
  never enters the page is copied into a WebGL texture (Compose 1.13).
- No AWT anywhere: the fullscreen player is a Nucleus window in real fullscreen.

## What's next

- [Video](/docs/media/video): the surface, fullscreen, subtitles, picture-in-picture and thumbnails.
- [Audio](/docs/media/audio): `AudioPlayer` for music and podcasts.
- [Streaming](/docs/media/streaming): HLS, DASH, live streams, tracks and request headers.
- [Playback control](/docs/media/playback): playlists, seek modes, speed, loudness and equalizer.
- [Offline](/docs/media/offline): download a media and play it without network.
- [System session](/docs/media/system-session): lock screen, notification and media keys.
