Nucleus
Media player

System sessionNew

Show what plays on the lock screen, in the notification and in the system's media center, and answer media keys and headset buttons with media-session.

View as Markdown

media-session binds a player to the system's media controls: the item playing (title, artist, artwork), whether it plays, its position, and next / previous when the playlist has them. Their commands come back to the player: play, pause, toggle, stop, next, previous, seek.

media-sessionmacOSWindowsLinuxAndroidiOSWebDesktop Gradle plugin: RecommendedDesktop window runtime: Not neededOn the desktop, SMTC and MPRIS identify the app by the id and AUMID the plugin injects.
PlatformSystem controls
AndroidA Media3 MediaSession: notification, lock screen, headsets, Android Auto
iOSMPNowPlayingInfoCenter and MPRemoteCommandCenter
Webnavigator.mediaSession
Desktopmedia-control: SMTC on Windows, Now Playing on macOS

media-session or media-control?

media-session drives the controls from a Nucleus player by itself, on every platform. On the desktop it uses media-control, the low-level desktop module where you push the metadata and state and handle the commands yourself: use that one directly with a player of your own.

Add the dependency

build.gradle.kts
commonMain.dependencies {
    implementation("dev.nucleusframework:nucleus.media-session:3.0.0")
}

Bind a player

In Compose, for as long as the player is on screen:

MediaSessionEffect(player.playback)   // or audioPlayer.playback

Outside a composition, MediaSessionBinding(playback) binds until you close() it.

The title, artist and artwork come from the playlist's MediaItems, the title from the media's own metadata otherwise:

player.setPlaylist(
    listOf(MediaItem(url, title = "Episode 12", artist = "The Show", artworkUri = coverUrl)),
)

Android: background playback

Declare the service in your app's manifest; the session then shows a notification and playback goes on when the app leaves the screen:

AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

<application>
    <service
        android:name="dev.nucleusframework.mediaplayer.session.NucleusPlaybackService"
        android:foregroundServiceType="mediaPlayback"
        android:exported="true">
        <intent-filter>
            <action android:name="androidx.media3.session.MediaSessionService" />
        </intent-filter>
    </service>
</application>

On the desktop, SMTC and MPRIS identify the app by the id and AUMID the Nucleus Gradle plugin injects: see Media controls.

What's next

Was this page helpful?
Edit on GitHub

Last updated on