- Version
- 2.0.0
- License
- MIT
- Requires
- DomSmith
- srtParser
- Source
- Subtitles.js, line 14
Constructor
new Subtitles(player, parent, optionsopt)
Creates an instance of the Subtitles component.
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
player |
Player |
The main media player instance. |
|||||||||
parent |
Popup |
Reference to the parent instance (In this case the language popup). |
|||||||||
options |
Object | optional |
Additional options.
|
Members
private #config :Object
Holds the configuration options for the Subtitles component.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
mode
|
string | optional | "custom" | "custom" mode uses the builtin subtitle engine, while "native" uses the browser engine. Note that not all Browsers may support custom engine, in this case the native fallback is used. |
allowHTML
|
string | optional | "none" | When used in conjunction with the custom engine, specifies if subtitles formatted in HTML may be used not at all ("none"), only with some basic tags ("basic") or fully ("all"). WARNING: allowing HTML may be a potential security issue and may trigger XSS attacks. Only use this if you are in full control of your subtitles! |
adaptLayout
|
boolean | optional | false | If enabled, the layout is synchronized with the UI state of controller and title, meaning that if one of these is shown, subtitles get an additional offset to prevent overlapping. Only works with custom engine. |
fontSize
|
string | optional | "medium" | The text size of the subtitles, can be "small", "medium" or "big". Only works with custom engine. |
showFontSizeControl
|
boolean | optional | true | If enabled, provide a UI to the user to change the subtitle text size. Only works with custom engine. |
showPlaceholder
|
boolean | optional | false | If enabled, display a 'not available' placeholder if no subtitles are available, otherwise completely hide the menu. |
preferredSubtitles
|
boolean | string | optional | false | If enabled, try to display subtitles: if |
- Type
- Object
- Source
- Subtitles.js, line 27
private #player :Player
Reference to the main player instance.
- Type
- Player
- Source
- Subtitles.js, line 41
private #mediaElement :HTMLMediaElement
Reference to the current media element.
- Type
- HTMLMediaElement
- Source
- Subtitles.js, line 47
private #subscriptions :Array<number>
Holds tokens of subscriptions to player events, for later unsubscribe.
- Type
- Array<number>
- Source
- Subtitles.js, line 53
private #apiKey :symbol
Secret key only known to the player instance and initialized components. Used to be able to restrict access to API methods in conjunction with secure mode.
- Type
- symbol
- Source
- Subtitles.js, line 60
private #dom :DomSmith
DomSmith for the main container used by the custom engine.
- Type
- DomSmith
- Source
- Subtitles.js, line 66
private #mode :"custom"|"native"
Stores actual subtitle mode. May be different from the configuration if the target browser does not support custom subtitles.
- Type
- "custom" | "native"
- Source
- Subtitles.js, line 72
private #preloaded :boolean
Flag indicating if subtitle was preloaded.
- Type
- boolean
- Source
- Subtitles.js, line 78
private #fontSizes :Array<string>
Allowed font size settings.
- Type
- Array<string>
- Source
- Subtitles.js, line 84
private #currentFontSize :"small"|"medium"|"big"
The current font size.
- Type
- "small" | "medium" | "big"
- Source
- Subtitles.js, line 90
private #subtitleData :Array<Subtitles~SubtitleItem>
Array holding current subtitle data.
- Type
- Array<Subtitles~SubtitleItem>
- Source
- Subtitles.js, line 96
private #renderers :Array<Object>
Stores additional renderer components (like subtitle line renderers).
- Type
- Array<Object>
- Source
- Subtitles.js, line 102
private #currentSubtitle :number
Array holding the index of the currently active subtitle.
- Type
- number
- Source
- Subtitles.js, line 108
Methods
registerRenderer(renderer)
Registers a renderer object that can handle the actual subtitle layout, like line placement etc.
| Name | Type | Description |
|---|---|---|
renderer |
Object |
The renderer to register. |
- Source
- Subtitles.js, line 148
private #onDataReady(mediaData)
Called when the media data is ready, extracts the text track info from "mediaData.text" if present.
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
mediaData |
Data~mediaItem |
Current mediaData.
|
- Listens
- data/ready
- Source
- Subtitles.js, line 160
private #onMediaReady()
Called when media is ready. If on iOS, we might preload the text tracks.
- Listens
- media/ready
- Source
- Subtitles.js, line 197
private #onSubtitleUpdate()
Called when "subtitles/update" is published, e.g. After external changes in textTracks. Rebuilds subtitles data from the current text tracks and toggles the correct track if needed.
- Listens
- subtitles/update
- Source
- Subtitles.js, line 230
private #toggleSubTitle(index) → Subtitles~SubtitleItem
Changes active subtitle display after user interaction.
| Name | Type | Description |
|---|---|---|
index |
number |
The index of the new subtitle. |
- Fires
- subtitles/active
- Returns
-
Returns the selected subtitle data entry.
- Type Subtitles~SubtitleItem
- Source
- Subtitles.js, line 283
private #loadTextTrack(srcopt, optionsopt) → Promise<{track: TextTrack, trackEle: HTMLTrackElement}>
Loads (and creates) a new text track. (see also https://developer.mozilla.org/de/docs/Web/HTML/Element/track).
| Name | Type | Attributes | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
src |
string | optional |
The URL from where the text track should be loaded. If omitted, a new blank text track is created. |
||||||||||||||||||||
options |
Object | optional |
Additional options.
|
- Returns
-
The newly created text track.
- Type Promise<{track: TextTrack, trackEle: HTMLTrackElement}>
- Throws
-
Throws if attempting to load text tracks before the loadedmetadata event.
Type Error - Source
- Subtitles.js, line 349
private #onTextTrackLoaded(event)
This handler is called if a text track (containing subtitles) was loaded. Shows the track (in native mode), or sets up an event handler for the "cuechange" event when in custom mode.
| Name | Type | Description |
|---|---|---|
event |
Event |
The original track 'load' event. |
- Source
- Subtitles.js, line 410
private #onTextTrackError(event)
This handler is called if a text track (containing subtitles) was loaded. Shows the track (in native mode), or sets up an event handler for the "cuechange" event when in custom mode.
| Name | Type | Description |
|---|---|---|
event |
Event |
The original track 'error' event. |
- Source
- Subtitles.js, line 435
private #removeTextTrackEvents(track)
Removes both 'load' and 'error' events from a given track element.
| Name | Type | Description |
|---|---|---|
track |
TextTrack |
The track to remove events from. |
- Source
- Subtitles.js, line 453
private #renderSubTitles(event)
When using the custom engine, this method listens to the cuechange event, and invokes any fitting renderer.
| Name | Type | Description |
|---|---|---|
event |
Event |
The "cuechange" event. |
- Source
- Subtitles.js, line 464
private #oniOSFullScreen(data, topic)
If we are on iOS, and using the custom engine, we need special handling when iOS enters fullscreen. As only the video, or audio tag itself can go fullscreen, we cannot display using the custom engine here. Instead we have to switch to the native engine in this case, and back to "custom" when leaving fullscreen.
| Name | Type | Description |
|---|---|---|
data |
Object |
Event data. |
topic |
string |
The event topic. |
- Listens
- fullscreen/enter
- fullscreen/leave
- Source
- Subtitles.js, line 489
private #applyFontSize(fontSize)
Applies a new font size to the subtitle container and publishes the change.
| Name | Type | Description |
|---|---|---|
fontSize |
string |
The requested font size. |
- Source
- Subtitles.js, line 523
private #translate(language) → string
Translates a language code or "off" to a user-readable label.
| Name | Type | Description |
|---|---|---|
language |
string | null |
The language code or null for "off". |
- Returns
-
A translated language string or the code if not found.
- Type string
- Source
- Subtitles.js, line 538
getElement() → HTMLElement
Returns the container element used by this component (for custom renderers to attach to).
- Returns
-
The root element of the subtitles component.
- Type HTMLElement
- Source
- Subtitles.js, line 549
private #onNoMedia()
Handles clearing state when no media is present.
- Source
- Subtitles.js, line 558
private #onEngineSet(data)
Invoked when the engine is switched.
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data |
Object |
The event data.
|
- Listens
- player/engine/set
- Source
- Subtitles.js, line 574
private #reset()
Resets the subtitles component to its initial state.
- Source
- Subtitles.js, line 614
destroy()
This method removes all events, subscriptions and DOM nodes created by this component.
- Source
- Subtitles.js, line 636
Type Definitions
SubtitleItem
Represents a single subtitle item.
| Name | Type | Description |
|---|---|---|
type
|
string | The type of subtitle track, e.g. "subtitles" or "captions". |
id
|
string | The identifier of the subtitle track (if any). |
language
|
string | The language code of this subtitle track. |
default
|
boolean | Whether this track is the default (as indicated by the media data). |
selected
|
boolean | Whether this track is currently selected or active. |
track
|
TextTrack | Object | The underlying browser TextTrack (or custom track object). |
loaded
|
boolean | True if this subtitle track has finished loading. |
- Type
- Object
- Source
- Subtitles.js, line 680
Events
subtitles/fontsize
Fired when the subtitle font size changes (custom engine).
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
fontInfo |
Object |
Object containing fontInfo.
|
- Listeners
- ChromeCast#onFontChange
- Source
- Subtitles.js, line 649
subtitles/active
Fired when a subtitle track becomes active.
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
subtitleInfo |
Object |
Object containing information about the active Subtitle.
|
- Listeners
- SubtitlesUi#onActive
- Source
- Subtitles.js, line 656
subtitles/selected
Fired when a new subtitle track was selected.
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
subtitleInfo |
Object |
Object containing information about the selected Subtitle.
|
- Listeners
- ChromeCast#onSubtitleChange
- Dash#onSubtitlesSelected
- Source
- Subtitles.js, line 665
subtitles/update
The Subtitles component listens to this event to react to outside changes to the available subtitles. Rebuilds the internal subtitle data from the current TextTracks and fires the subtitles/update event. Used mainly for external control by components as dash and hls.
- Listeners
- Subtitles#onSubtitleUpdate
- Source
- Subtitles.js, line 674