Nucleus
Media player

Media playerNew

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.

View as Markdown

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.

media-playermacOSWindowsLinuxAndroidiOSWebDesktop Gradle plugin: Not neededDesktop window runtime: Required

Linux needs GStreamer

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

ModuleArtifactWhat it holds
media-playernucleus.media-playerVideoPlayerState, VideoPlayerSurface, fullscreen, subtitles, picture-in-picture, thumbnails
audio-playernucleus.audio-playerAudioPlayer, the same engine without a picture
media-enginenucleus.media-engineMediaPlaybackState, the whole playback contract without UI, and MediaDownloads; the two players depend on it
media-hlsnucleus.media-hlsHLS streams (.m3u8)
media-dashnucleus.media-dashMPEG-DASH streams (.mpd)
media-smoothstreamingnucleus.media-smoothstreamingMicrosoft Smooth Streaming (.ism/Manifest)
media-sessionnucleus.media-sessionThe 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

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

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.

What's next

  • Video: the surface, fullscreen, subtitles, picture-in-picture and thumbnails.
  • Audio: AudioPlayer for music and podcasts.
  • Streaming: HLS, DASH, live streams, tracks and request headers.
  • Playback control: playlists, seek modes, speed, loudness and equalizer.
  • Offline: download a media and play it without network.
  • System session: lock screen, notification and media keys.
Was this page helpful?
Edit on GitHub

Last updated on