- Version
- 1.0.0
- License
- MIT
- Requires
- DomSmith
- srtParser
- Menu
- Source
- Subtitles.js, line 17
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 30
private #player :Player
Reference to the main player instance.
- Type
- Player
- Source
- Subtitles.js, line 44
private #parent :Popup
Reference to the parent component.
- Type
- Popup
- Source
- Subtitles.js, line 50
private #mediaElement :HTMLMediaElement
Reference to the current media element.
- Type
- HTMLMediaElement
- Source
- Subtitles.js, line 56
private #subscriptions :Array<number>
Holds tokens of subscriptions to player events, for later unsubscribe.
- Type
- Array<number>
- Source
- Subtitles.js, line 62
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 69
private #dom :DomSmith
DomSmith for the main container used by the custom engine.
- Type
- DomSmith
- Source
- Subtitles.js, line 75
private #menu :Menu
private #fontMenu :Menu
Reference to the font menu.
- Type
- Menu
- Source
- Subtitles.js, line 87
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 93
private #preloaded :boolean
Flag indicating if subtitle was preloaded.
- Type
- boolean
- Source
- Subtitles.js, line 99
private #fontSizes :Array<string>
Allowed font size settings.
- Type
- Array<string>
- Source
- Subtitles.js, line 105
private #currentFontSize :"small"|"medium"|"big"
The current font size.
- Type
- "small" | "medium" | "big"
- Source
- Subtitles.js, line 111
private #subtitleData :Array<Subtitles~SubtitleItem>
Array holding current subtitle data.
- Type
- Array<Subtitles~SubtitleItem>
- Source
- Subtitles.js, line 117
private #renderers :Array<Object>
Stores additional renderer components (like subtitle line renderers).
- Type
- Array<Object>
- Source
- Subtitles.js, line 123
private #currentSubtitle :number
Array holding the index of the currently active subtitle.
- Type
- number
- Source
- Subtitles.js, line 129
private #currentCues :Map<any, {ele: HTMLElement, renderer: Object}>
Map storing the currently active cues for rendering.
- Type
- Map<any, {ele: HTMLElement, renderer: Object}>
- Source
- Subtitles.js, line 135
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 220
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 232
private #onMediaReady()
Called when media is ready. If on iOS, we might preload the text tracks.
- Listens
- media/ready
- Source
- Subtitles.js, line 286
private #onSubtitleUpdate()
Called when "subtitles/update" is published, e.g. After external changes in textTracks. Rebuilds the menu and toggles the correct track if needed.
- Listens
- subtitles/update
- Source
- Subtitles.js, line 319
async, 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/selected
- Returns
-
Returns the selected subtitle data entry.
- Type Subtitles~SubtitleItem
- Source
- Subtitles.js, line 361
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 421
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 482
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 505
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 521
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 532
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 569
private #changeFontSize(index, item)
Changes the font size of the custom subtitles.
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
index |
number |
The index of the new fontsize. |
||||||
item |
Object |
The selected item.
|
- Fires
- subtitles/fontsize
- Source
- Subtitles.js, line 606
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 620
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 631
destroy()
This method removes all events, subscriptions and DOM nodes created by this component.
- Source
- Subtitles.js, line 640
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 688
Events
subtitles/fontsize
Fired when the user changes the subtitle font size (custom engine).
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
fontInfo |
Object |
Object containing fontInfo.
|
- Listeners
- ChromeCast#onFontChange
- Source
- Subtitles.js, line 665
subtitles/selected
Fired when the user changes the displayed subtitle.
| Name | Type | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
subtitleInfo |
Object |
Object containing information about the selected Subtitle.
|
- Listeners
- ChromeCast#onSubtitleChange
- Dash#onSubtitlesSelected
- Source
- Subtitles.js, line 672
subtitles/update
The Subtitles component listens to this event to react to outside changes to the available subtitles. Rebuilds the menu according to the current TextTracks. Used mainly for external control by components as dash and hls.
- Listeners
- Subtitles#onSubtitleUpdate
- Source
- Subtitles.js, line 682