Nucleus
Media player

Playback controlNew

Playlists with repeat and shuffle, exact or key-frame seeks, speed and pitch, silence skipping, loudness, an equalizer and playback stats.

View as Markdown

Everything on this page lives on the playback state. VideoPlayerState exposes playlists, seek modes and stats directly; the audio effects are on MediaPlaybackState, which you reach through AudioPlayer.playback or VideoPlayerState.playback.

val playback = audioPlayer.playback                          // an AudioPlayer
val playback = player.playback                               // a VideoPlayerState

Playlists

player.setPlaylist(
    listOf(
        MediaItem("https://example.com/01.mp3", title = "Intro", artist = "Nucleus"),
        MediaItem("https://example.com/02.mp3", title = "Chapter 1"),
    ),
)
player.repeatMode = RepeatMode.ALL      // OFF, ONE (the same as loop) or ALL
player.shuffle = true                   // each item once per pass, the current one first
player.next()
player.previous()                       // the previous item in the first 3 s, else back to the start
player.skipTo(1)
player.onItemChanged = { index -> /* currentIndex changed */ }

At the end of an item the next one starts; onPlaybackEnded only runs at the end of the last one. The next item is prepared 3 seconds before the end, so the switch is near gapless: ExoPlayer continues into it in its own playlist, the other engines swap in a session prepared ahead. A MediaItem carries the title, artist and artwork the system session shows, and its own request headers.

Seek modes

player.seekMode = SeekMode.CLOSEST_SYNC   // or SeekMode.EXACT, the default

EXACT shows the frame containing the target. CLOSEST_SYNC shows the closest key frame, without decoding the frames before it: the fast seek of a scrubbing slider. Once it lands, the position moves to that key frame.

Key-frame seeks need the engine's support: ExoPlayer has it, AVFoundation too (Nucleus finds the key frame itself, iOS 16+; an HLS stream seeks exactly), GStreamer (Nucleus finds the key frame of an MP4 file itself), and Firefox and Safari through fastSeek. Chrome and Media Foundation keep seeking exactly in CLOSEST_SYNC.

Speed and pitch

player.playbackSpeed = 1.5f
playback.preservesPitch = false     // the pitch follows the speed, as a tape would

preservesPitch is true by default: voices stay natural at any speed. Media Foundation has no such setting and keeps its own behaviour.

Skip silences

playback.skipSilence = true

Silent passages are shortened (podcasts, lectures). ExoPlayer only; it keeps a fraction of each silence and holds the position while it analyses one, then jumps past it.

Loudness and equalizer

playback.loudnessGainDb = 6f                       // 0 to MAX_LOUDNESS_GAIN_DB (20)
playback.equalizer = listOf(
    EqualizerBand(frequencyHz = 60f, gainDb = 4f),
    EqualizerBand(frequencyHz = 3_000f, gainDb = -3f),
)
playback.equalizer = emptyList()                   // flat

The loudness gain goes past volume's 1, for quiet media. Each band is a peaking filter about an octave wide at the frequency you ask for, ±15 dB at most. The processing is the same on every platform and measured on the output, identical within 0.05 dB: an audio effect of Nucleus in Media Foundation, a buffer probe in GStreamer, an MTAudioProcessingTap in AVFoundation, an AudioProcessor in ExoPlayer's audio chain, a WebAudio graph on the web. Both settings carry over from one media to the next.

Web: CORS

On the web the first effect routes the element through WebAudio, which plays media from another origin silently unless that origin allows CORS.

Stats

val stats = player.stats    // a snapshot: poll it
stats.renderedFrames        // frames shown since the media opened
stats.droppedFrames
stats.bufferedAheadMs       // media loaded past the position

Read it at each call; it is not Compose state. On iOS, AVPlayerLayer counts no frame: the frames shown are estimated from the media played at the track's frame rate.

What's next

  • Offline: download a media and play it without network.
  • System session: show the playlist in the system's media controls.
Was this page helpful?
Edit on GitHub

Last updated on