KitePlayerWorker

A player that runs in a Web Worker, so the page's own thread stays free while it opens, decodes and draws (#100).

val player = KitePlayerWorker.start(canvas)
player.open(MediaItem("https://example.com/movie.mp4"))
button.onclick = { player.play() }

The worker holds the engine, the codec module, the libass module and the readers. The page keeps only what a worker cannot have: the audio device, whose sound goes from the worker to the device without passing through the page, and the canvas, whose drawing is handed to the worker. Frames, sound and subtitles never cross between the two threads; commands and state do.

An http, https or blob item plays here, read with range requests that only a worker may make. The page's own KitePlayer cannot open one. A relative address, of an item or of one of its subtitles, is read against the page's.

The same calls as KitePlayer

Every member below has the name, the parameters, the defaults and the meaning of the KitePlayer member of the same name, and a call that fails throws what KitePlayer would throw: PlaybackException, IllegalArgumentException, IllegalStateException or UnsupportedOperationException. state, progress, stats and events carry what the worker's player publishes, and state.media and state.queue hold the items this facade was given rather than copies of them.

Two differences come from the thread between the page and the player:

What does not cross

  • subtitleCues: the worker draws the subtitles on the canvas itself.

  • coverArt: the media session of the page's own player shows it, and a worker has none.

  • position() and audioClock(): read progress instead. transportMark and awaitClose.

  • inspect, scanAudio, captureFrame, thumbnailAt, recording, memento and restore.

  • Attaching or detaching a renderer or an audio tap, and setExternalClock.

  • An item, or an external subtitle or a thumbnail file, with its own reader: it is refused with PlaybackError.ConfigurationInvalid. Give it an address instead.

  • A PlayerConfig: the worker builds its player on the default one.

The page's own KitePlayer is unchanged and stays the web default.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val events: SharedFlow<PlayerEvent>

What happened in the player. A worker that dies sends PlayerEvent.Failed from here, and a setter the player refused sends PlaybackWarning.CommandRefused.

Link copied to clipboard
val progress: StateFlow<Progress>

The position and how far ahead the worker has read, as the worker last sent them.

Link copied to clipboard
val state: StateFlow<PlayerSnapshot>

The player's state, as the worker last sent it. Position is not in it; see progress.

Link copied to clipboard
val stats: StateFlow<PlaybackStats>

Diagnostics, as the worker last sent them, once a second by default.

Functions

Link copied to clipboard

Loads a subtitle file, selects it, and returns its id once it is showing. source needs an address: one with its own reader is refused with PlaybackError.ConfigurationInvalid.

Link copied to clipboard
suspend fun addToQueue(item: MediaItem, index: Int? = null)

Inserts one item. See the list overload.

suspend fun addToQueue(items: List<MediaItem>, index: Int? = null)

Inserts items at index, or at the end when index is null.

Link copied to clipboard
fun chapterAt(position: Duration): Chapter?

The chapter whose span holds position, or null: at or after its start and before its end, and the last such chapter when spans overlap, the rule KitePlayer.chapterAt follows.

Link copied to clipboard
suspend fun clearQueue()

Removes every queue item except the one playing.

Link copied to clipboard
open override fun close()

Asks the worker to close the player and end, and returns at once. The worker ends when the player has closed. closeAndAwait waits for that.

Link copied to clipboard
suspend fun closeAndAwait()

close, returning once the worker has closed the player and ended.

Link copied to clipboard
suspend fun diagnosticsDump(): String

The worker player's diagnostics dump.

Link copied to clipboard
suspend fun moveInQueue(from: Int, to: Int)

Moves the queue item at from so that it sits at to.

Link copied to clipboard
suspend fun next()

Opens the next queue item, keeping the play or pause intent.

Link copied to clipboard
suspend fun nextChapter()

Seeks to the start of the next chapter. The worker reads the position, so it is exact.

Link copied to clipboard
suspend fun open(media: MediaItem)

Opens media and returns once the first frame is ready and the player is paused on it.

Link copied to clipboard
suspend fun openQueue(items: List<MediaItem>, startIndex: Int = 0)

Opens items as the queue, starting at startIndex, as KitePlayer.openQueue does.

Link copied to clipboard
fun pause()

Pauses playback.

Link copied to clipboard

Picture in picture for the canvas this player draws on, or null when it was started without one or this browser has neither feature KitePlayerPictureInPicture uses. The canvas belongs to the worker, and both features still carry its frames: the document window takes the page's canvas element, and the video element's window plays a live capture of it. Its play and pause buttons play and pause this player. Call start from the viewer's click, as there.

Link copied to clipboard
fun play()

Starts or resumes playback. Call it from a user gesture's own handler: the browser starts the page's audio device only there, and this starts it before it tells the worker.

Link copied to clipboard
suspend fun previous()

Opens the previous queue item, keeping the play or pause intent.

Link copied to clipboard
suspend fun previousChapter()

Seeks to the start of this chapter, or the one before within its first three seconds.

Link copied to clipboard
suspend fun reloadExternalSubtitle(track: TrackId, encoding: String? = null)

Reads an external subtitle track's file again in encoding, or decided from its bytes for null, as KitePlayer.reloadExternalSubtitle does, and returns once the new reading shows.

Link copied to clipboard
suspend fun removeFromQueue(index: Int)

Removes the queue item at index.

Link copied to clipboard
fun requestSeek(to: Duration, mode: SeekMode = SeekMode.KeyframeThenRefine)

Asks for a seek and returns at once, for a seek bar being dragged.

Link copied to clipboard
suspend fun seek(to: Duration, mode: SeekMode = SeekMode.Precise)

Seeks to to and returns once the worker has landed there, as KitePlayer.seek does.

Link copied to clipboard
suspend fun seekToChapter(index: Int)

Seeks to the start of chapter index.

Link copied to clipboard
suspend fun selectProgram(number: Int?)

Plays the channel numbered number of the tracks' programmes, or lets the player choose with null.

Link copied to clipboard

Shows a second subtitle track at the top of the picture, or clears it with null.

Link copied to clipboard
suspend fun selectTrack(kind: TrackKind, track: TrackId?): TrackChange

Selects a track, or deselects the kind with a null track, and says what happened.

Link copied to clipboard
suspend fun selectVariant(index: Int?)

Plays the variant at index of the tracks' variants, or lets the player choose with null.

Link copied to clipboard
fun setAbLoop(a: Duration?, b: Duration? = null)

Arms or clears the A-B loop.

Link copied to clipboard

Delays the sound against the picture.

Link copied to clipboard
fun setBalance(value: Float)

Sets the stereo balance.

Link copied to clipboard

Raises or lowers the dialogue in a downmix, in decibels (#442).

Link copied to clipboard
fun setDuckLevel(level: Float)

Lowers the sound by level without touching the volume.

Link copied to clipboard

Sets the ten-band equaliser.

Link copied to clipboard

Draws only the forced pictures of a Blu-ray or DVD subtitle track, or every picture again.

Link copied to clipboard

Sets how HDR video reaches the screen.

Link copied to clipboard

Chooses which keyframe a SeekMode.Keyframe seek lands on; see KitePlayer.setKeyframeChoice.

Link copied to clipboard
fun setLoop(mode: LoopMode)

Sets what happens at the end of the media.

Link copied to clipboard
fun setMarkers(markers: List<Marker>)

Sets the positions to announce with PlayerEvent.MarkerReached.

Link copied to clipboard
fun setMuted(value: Boolean)

Silences the sound without losing the volume.

Link copied to clipboard

Turns the night mode on or off (#442).

Link copied to clipboard
fun setPitch(semitones: Double)

Moves the pitch by semitones without changing the speed (#465).

Link copied to clipboard

Chooses whether setSpeed keeps pitch.

Link copied to clipboard

Sets how much work the renderer spends on the picture.

Link copied to clipboard
fun setShuffle(enabled: Boolean, seed: Long? = null)

Plays the queue in a shuffled order, or in the order it was given.

Link copied to clipboard

Shortens the silent stretches of a podcast or an audiobook (#429).

Link copied to clipboard
fun setSleepTimer(timer: SleepTimer?, fade: Duration = KitePlayer.DEFAULT_SLEEP_FADE)

Stops playback later, fading the sound down first. Null cancels an armed timer.

Link copied to clipboard
fun setSpeed(value: Double)

Sets the playback rate.

Link copied to clipboard

Sets what the two front speakers play (#462).

Link copied to clipboard

Shifts subtitle timing. Positive shows cues later.

Link copied to clipboard

Moves the subtitles up the screen.

Link copied to clipboard

Keeps subtitles inside the safe area of the output.

Link copied to clipboard

Scales subtitle text over the authored size.

Link copied to clipboard

Overrides the authored subtitle style, or clears the override with null.

Link copied to clipboard

Sets the live picture controls.

Link copied to clipboard
fun setVideoEnabled(enabled: Boolean)

Parks or resumes video decoding in place.

Link copied to clipboard

Sets how the picture occupies the canvas.

Link copied to clipboard

Sets the framing controls.

Link copied to clipboard
fun setViewport(width: Int, height: Int, scale: Float)

Sizes the canvas's drawing buffer to width by height CSS pixels at scale device pixels each, as VideoRenderer.setViewport does. The canvas belongs to the worker, so the page sets its size through here rather than on the element.

Link copied to clipboard
fun setVolume(value: Float)

Sets the volume.

Link copied to clipboard
suspend fun stepFrame(direction: StepDirection = StepDirection.Forward)

Steps a paused player by exactly one decoded frame.

Link copied to clipboard
suspend fun stop()

Stops playback and closes what was open. The player stays usable.

Link copied to clipboard
suspend fun supportBundle(): String

The worker player's support bundle.

Link copied to clipboard

The worker player's last warnings, oldest first.