Player API

4 min read · Edit this page on GitHub(opens in new tab)
ts
import { createPlayer } from '@tomato-player/core';
const player = createPlayer(target, options);
target: a CSS selector, a container element, or a <video> element.

Options

OptionTypeDefaultDescription
srcSource | Source[]–URL or { src, type }. With an array, the first source a provider can play wins.
posterstring–Poster image.
titlestring–Title shown by skins and used as the accessible name and Media Session title.
tracksTextTrackInit[]–{ src, kind, label, srclang, default } captions and subtitles.
autoplaybooleanfalseTries to autoplay. If the browser blocks unmuted autoplay, it retries muted.
muted · loopbooleanfalse
playsInlinebooleantrueNeeded for inline playback on iOS.
preload'none' | 'metadata' | 'auto'browser
crossOrigin'' | 'anonymous' | 'use-credentials'–Needed for cross-origin captions and thumbnails.
volume · playbackRatenumber1Initial values.
providersProvider[][]Tried in order. Native is always the fallback.
pluginsPlugin[][]
skinSkin | nullnullnull = headless.
tokensRecord<string,string>–Design token overrides: { accent: '#0af' } → --tp-accent.
injectStylesbooleantrueSet to false for strict CSP, then import style.css files yourself.
idleTimeoutnumber2500ms of inactivity before state.idle.
i18nRecord<string,string>EnglishUI strings, see defaultStrings.
labelstringtitleAccessible name of the player region.
aspectRatiostring | false'16/9'
fullscreenFallback'native' | 'pseudo''native'iPhone has no element fullscreen. 'pseudo' keeps your skin by pinning the player to the viewport.

State (player.state)

KeyTypeNotes
source · providerSourceObject | null · string | nullActive source and provider name.
ready · started · paused · ended · waiting · seekingboolean
currentTime · durationnumberSeconds. For live streams, duration is the end of the seekable range.
buffered · seekableTimeRange[] · TimeRange | null
liveboolean
volume · muted · playbackRate
fullscreen · pip · idleboolean
qualities · qualityQuality[] · number-1 = auto (ABR).
audioTracks · audioTrackTrackInfo[] · number
textTracks · textTrackTrackInfo[] · number-1 = off.
poster · titlestring | null
errorPlayerError | null{ code, message, fatal, cause }
widthnumberContainer width (drives data-size="sm|md|lg").
Every boolean flag is also mirrored as a data-* attribute on player.el, for example data-paused, data-playing, data-waiting, data-fullscreen, data-idle, data-captions, data-error and data-size.

Methods

MethodDescription
play(): Promise<void>Swallows AbortError (play interrupted by pause/load). Rethrows NotAllowedError.
pause() · togglePlay()
seek(seconds) · seekBy(delta)Clamped to the duration, or to the seekable window for live streams.
setVolume(0..1) · setMuted(bool) · toggleMute()Raising the volume unmutes.
setPlaybackRate(rate)
setQuality(id)-1 = auto.
setAudioTrack(id) · setTextTrack(index)-1 turns captions off.
setSource(src, { poster, title, tracks })Swaps the source and tears down the previous engine. Race-safe.
enterFullscreen() · exitFullscreen() · toggleFullscreen()Container fullscreen, with fallbacks for iOS.
togglePip()Standard PiP or WebKit presentation mode.
setSkin(skin | null)Runtime skin swap.
use(plugin) · plugin(name)Add a plugin later, or get its API.
provide(key, value) · inject(key)Share capabilities between extensions.
t(key, vars)Translate a UI string.
activity()Reset the idle timer.
on · once · off · emitTyped events, see below.
destroy()Idempotent. Restores the DOM.

Events

ready, play, pause, ended, seeking (time), seeked (time), timeupdate (time), error (PlayerError), sourcechange (SourceObject), qualitychange (id), ratechange (rate), volumechange ({ volume, muted }), fullscreenchange (bool), pipchange (bool), statechange ({ state, prev }), destroy, plus any plugin:* event.
ts
const off = player.on('timeupdate', (t) => console.log(t));
off();

Error codes

CodeMeaning
NO_PROVIDERNo provider can play the source (for example, DASH without dash()).
SRC_NOT_SUPPORTEDThe browser can't decode the format or codec, or the URL 404s.
NETWORK · DECODE · MEDIA_ABORTEDNative MediaError codes.
PROVIDERFatal engine error (hls.js/dash.js). See error.cause.

Helpers

formatTime(seconds), normalizeSource(src), guessType(url), parseVtt(text), createStore(initial), Emitter, definePlugin, defineSkin, defineProvider, supportsMSE(), defaultStrings.
PreviousCreate a provider
NextUI primitives