# System session

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.

`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.

| Platform | System controls |
| -------- | --------------- |
| Android | A Media3 `MediaSession`: notification, lock screen, headsets, Android Auto |
| iOS | `MPNowPlayingInfoCenter` and `MPRemoteCommandCenter` |
| Web | `navigator.mediaSession` |
| Desktop | [`media-control`](/docs/os/media-control): SMTC on Windows, Now Playing on macOS |

`media-session` drives the controls from a Nucleus player by itself, on every platform. On the
desktop it uses [`media-control`](/docs/os/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

```kotlin title="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:

```kotlin
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 `MediaItem`s, the title from the media's
own metadata otherwise:

```kotlin
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:

```xml title="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](/docs/os/media-control).

## What's next

- [Playback control](/docs/media/playback): the playlist the controls step through.
- [Media controls](/docs/os/media-control): the desktop module underneath, usable on its own.
