Skip to content

Module: src/casting/ChromeCast

The ChromeCast component for the Media Player enables casting of media content to Chromecast devices. You can control the video either using the standard controls on the player UI or via the ChromeCast remote. Supports Subtitles, as well as poster images & more on devices that support those features.

Version
2.0.0
Author
Frank Kudermann - alphanull
Frank
License
MIT
Requires
object
DomSmith
scriptLoader
Source
ChromeCast.js, line 30

Constructor

new ChromeCast(player, parent, optionsopt)

Creates an instance of the ChromeCast component.

Parameters:
Name Type Attributes Description
player Player

Reference to the main VisionPlayer instance.

parent Controller

Reference to the parent instance.

options Object optional

Additional options.

Parameters:
Name Type Attributes Description
apiKey symbol optional

Token for extended access to the player API.

Members

Configuration for the ChromeCast component.

Properties
Name Type Attributes Default Description
showControllerButton boolean optional true

If true, a controller button is displayed.

showMenuButton boolean optional true

If true, a button in the settings menu (if available) is displayed.

lazyLoadLib boolean optional true

If true, the cast Library is only loaded after user interaction.

Type
Object
Source
ChromeCast.js, line 39

Reference to the main player instance.

Type
Player
Source
ChromeCast.js, line 49

Reference to the settings menu element.

Type
Popup
Source
ChromeCast.js, line 55

Holds tokens of subscriptions to player events, for later unsubscribe.

Type
Array<number>
Source
ChromeCast.js, line 61

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
ChromeCast.js, line 68

DomSmith instance for the Cast backdrop.

Type
DomSmith
Source
ChromeCast.js, line 74

DomSmith instance for the menu button.

Type
DomSmith
Source
ChromeCast.js, line 80

DomSmith instance for the controller button.

Type
DomSmith
Source
ChromeCast.js, line 86

Status information of the remote cast device.

Properties
Name Type Attributes Description
state string

Current player state (e.g., 'PLAYING', 'PAUSED', 'BUFFERING').

duration number

Duration of the media in seconds.

currentTime number

Current playback position in seconds.

playbackRate number

Current playback rate (1.0 = normal speed).

connected boolean

Whether a cast session is currently active.

paused boolean

Whether playback is currently paused.

activeTextTrack number

Index of the currently active subtitle track (-1 if none).

seekTo number | null

Target time for seeking (null if no seek is pending).

sessionState string optional

Current session state ('SESSION_STARTED', 'SESSION_RESUMED', 'SESSION_ENDED').

error string optional

Error message if an error occurred during casting.

noIdleEvent boolean optional

Flag to prevent idle events during media loading.

fontSize string optional

Font size for subtitles ('small', 'normal', 'big').

Type
Object
Source
ChromeCast.js, line 104

Holds metadata information provided by media.load and loaded metadata.

Type
Media~metaData
Source
ChromeCast.js, line 119

Instance of the Cast context.

Type
Object
Source
ChromeCast.js, line 125

RemotePlayer instance for controlling the Cast device.

Type
Object
Source
ChromeCast.js, line 131

RemotePlayerController for monitoring changes to the RemotePlayer.

Type
Object
Source
ChromeCast.js, line 137

Cast session instance.

Type
Object
Source
ChromeCast.js, line 143

Flag indicating if currently active media is supported for casting.

Type
boolean
Source
ChromeCast.js, line 149

Methods

canPlay(metaData) → string

Checks if the current media source is supported for casting.

Parameters:
Name Type Description
metaData Object

The media metadata.

Parameters:
Name Type Attributes Description
src string

The current media source.

mimeType string optional

The current media mime type.

Returns

'maybe' if the media source is supported, otherwise ''.

Type string
Source
ChromeCast.js, line 292

private #onMediaReady(src)

Checks if the current media source is supported for casting and enables or disables buttons accordingly.

Parameters:
Name Type Description
src string

The current media source.

Listens
media/ready
Source
ChromeCast.js, line 319

async, private #addScripts()

Load Cast API Scripts from Google. This is only done after the user clicks the cast button. Uses the centralized scriptLoader utility for deduplication and reliability.

Source
ChromeCast.js, line 329

private #onAvailable()

Initializes the Cast context and sets up necessary event listeners.

Source
ChromeCast.js, line 366

private #toggleCasting()

Toggles casting on or off based on the current status. In addition the google cast code is loaded if invoked for the first time.

Source
ChromeCast.js, line 390

private #onTogglePlay()

Toggles playback (play/pause) of the cast media. Updates the backdrop button text accordingly.

Source
ChromeCast.js, line 406

async, private #startCasting() → void

Starts casting to a Chromecast device.

Fires
notification
Returns

Returns when cast session has failed.

Type void
Source
ChromeCast.js, line 423

private #generateRequest(sourceopt) → Object

Generates a media load request for casting. Also tries to include title, poster image and subtitles, if available.

Parameters:
Name Type Attributes Description
source Object optional

The current media source.

Returns

The generated load request.

Type Object
Source
ChromeCast.js, line 479

private #stopCasting()

Stops casting and restores the player to its original state.

Source
ChromeCast.js, line 548

private #castStopped()

Handles the stopping of casting. Restores the player to its original state and switches back to the video engine.

Fires
chromecast/stop
Source
ChromeCast.js, line 566

private #load(source, options)

Loads a media file into the Chromecast player.

Parameters:
Name Type Description
source Object

The media source.

options Object

The options for the load.

Parameters:
Name Type Description
rememberState boolean

If true, the player will remember the current state of the media.

Listens
media/load
Source
ChromeCast.js, line 603

private #seek(val)

Seeks to a specific time position in the cast media.

Parameters:
Name Type Description
val number

The target time in seconds.

Source
ChromeCast.js, line 639

private #play()

Starts playback of the cast media.

Fires
media/play
Source
ChromeCast.js, line 648

private #pause()

Pauses playback of the cast media.

Fires
media/pause
Source
ChromeCast.js, line 658

private #loop(doLoop)

Disables or enables looping.

Parameters:
Name Type Description
doLoop boolean

If true, media is looping.

Fires
media/loop
Source
ChromeCast.js, line 669

private #volume(val)

Sets the volume level for the cast media.

Parameters:
Name Type Description
val number

Volume level between 0 and 1.

Fires
media/volumechange
Source
ChromeCast.js, line 681

private #mute()

Toggles mute state of the cast media.

Source
ChromeCast.js, line 690

private #getMetaData() → Media~metaData

Returns a copy of the current media metadata.

Returns

Object with current metadata.

Type Media~metaData
Source
ChromeCast.js, line 700

private #getMediaElement(apiKey) → HTMLElement

Used by components to retrieve the video element.

Parameters:
Name Type Description
apiKey symbol

Token needed to grant access in secure mode.

Returns

The element designated by the component as attachable container.

Type HTMLElement
Throws

If safe mode access was denied.

Type Error
Source
ChromeCast.js, line 708

private #onCastEvent(event)

Handles cast events.

Parameters:
Name Type Description
event Object

The cast event from the google lib.

Parameters:
Name Type Description
field string

The field that changed.

value *

The new value of the field.

Fires
chromecast/start
media/volumechange
media/timeupdate
media/pause
media/play
Source
ChromeCast.js, line 726

private #onSessionEvent(event)

Handles session events from the Cast context. Updates the remote session state based on the event type.

Parameters:
Name Type Description
event Object

Session event from the google lib.

Parameters:
Name Type Description
sessionState string

The new session state.

Source
ChromeCast.js, line 841

private #onSubtitleChange(event)

Handles changes to subtitles.

Parameters:
Name Type Description
event Object

Subtitle change event info.

Parameters:
Name Type Description
index number

Index of the selected subtitle track.

Listens
subtitles/selected
Source
ChromeCast.js, line 864

private #onFontChange(size)

Handles changes to the subtitle font size.

Parameters:
Name Type Description
size string

The new font size ('small', 'normal', 'big').

Listens
subtitles/fontsize
Source
ChromeCast.js, line 884

private #onPlaybackRateChange(rate)

Handles changes to the playback rate.

Parameters:
Name Type Description
rate number

The new playback rate.

Listens
media/ratechange
Source
ChromeCast.js, line 907

private #updateButtonVisibility()

Enables the play button functionality. This method listens to canplay events in order to restore a usable state again when the player recovered from a media error (for example by loading another file).

Listens
media/canplay
Source
ChromeCast.js, line 932

private #hideButton()

Hides the Chromecast button when casting is unavailable.

Listens
media/error
data/nomedia
Source
ChromeCast.js, line 948

enable()

Enables the Chromecast component. Subscribes to player events and sets the media state.

Source
ChromeCast.js, line 959

disable()

Disables the Chromecast component. Removes all events and subscriptions created by this component.

Source
ChromeCast.js, line 982

destroy()

Removes all events, subscriptions, and DOM nodes created by this component.

Source
ChromeCast.js, line 1007

Events

chromecast/start

This event is fired when chromecast was started.

Listeners
VisualizerAmbient#stopLoop
Source
ChromeCast.js, line 1029

chromecast/stop

This event is fired when chromecast was stopped.

Listeners
VisualizerAmbient#startLoop
Source
ChromeCast.js, line 1034