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
1.0.0
Author
Frank Kudermann - alphanull
Frank
License
MIT
Requires
object
DomSmith
Source
ChromeCast.js, line 18

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 27

Reference to the main player instance.

Type
Player
Source
ChromeCast.js, line 37

Reference to the settings menu element.

Type
Popup
Source
ChromeCast.js, line 43

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

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

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 56

DomSmith instance for the Cast backdrop.

Type
DomSmith
Source
ChromeCast.js, line 62

DomSmith instance for the menu button.

Type
DomSmith
Source
ChromeCast.js, line 68

DomSmith instance for the controller button.

Type
DomSmith
Source
ChromeCast.js, line 74

Status information of the remote cast device.

Type
Object
Source
ChromeCast.js, line 80

Local copy of the player's functions for restoring after casting.

Properties
Name Type Description
seek function

Original media.seek function of the player.

play function

Original media.play function of the player.

pause function

Original media.pause function of the player.

volume function

Original media.volume function of the player.

mute function

Original media.mute function of the player.

load function

Original media.load function of the player.

lastSrc string

The last set media source.

Type
Object
Source
ChromeCast.js, line 101

Instance of the Cast context.

Type
Object
Source
ChromeCast.js, line 107

RemotePlayer instance for controlling the Cast device.

Type
Object
Source
ChromeCast.js, line 113

RemotePlayerController for monitoring changes to the RemotePlayer.

Type
Object
Source
ChromeCast.js, line 119

Cast session instance.

Source
ChromeCast.js, line 124

Time snapshot used for resuming playback.

Type
number
Source
ChromeCast.js, line 130

Volume state snapshot before casting.

Type
number
Source
ChromeCast.js, line 136

Flag indicating if currently active media is supported for casting.

Type
boolean
Source
ChromeCast.js, line 142

Methods

private #addScripts()

Load Cast API Scripts from Google. This is only done after the user clicks the cast button.

Source
ChromeCast.js, line 264

private #onAvailable()

Initializes the Cast context and sets up necessary event listeners.

Source
ChromeCast.js, line 276

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 301

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 316

private #onTogglePlay()

Toggles playback (play/pause).

Source
ChromeCast.js, line 327

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 344

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 396

private #switchState()

Switches the player's state and binds remote functions.

Source
ChromeCast.js, line 465

private #stopCasting(endCastSessionopt)

Stops casting and restores the player to its original state.

Parameters:
Name Type Attributes Description
endCastSession boolean optional

Indicates whether to also end the cast session.

Source
ChromeCast.js, line 535

async, private #castStopped()

Handles the stopping of casting.

Fires
media/volumechange
chromecast/stop
Source
ChromeCast.js, line 553

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 601

private #onSessionEvent(event)

Handles session events from the Cast context.

Parameters:
Name Type Description
event Object

Session event from the google lib.

Source
ChromeCast.js, line 714

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 737

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 757

private #onPlaybackRateChange()

Handles changes to the playback rate.

Listens
media/ratechange
Source
ChromeCast.js, line 779

private #enable()

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 802

private #disable()

Disables the button functionality. This method listens to media error events which cause the button to be disabled.

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

destroy()

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

Source
ChromeCast.js, line 828

Events

chromecast/start

This event is fired when chromecast was started.

Listeners
PictureInPicture#disable
VisualizerAmbient#stopLoop
Source
ChromeCast.js, line 848

chromecast/stop

This event is fired when chromecast was stopped.

Listeners
PictureInPicture#enable
VisualizerAmbient#startLoop
Source
ChromeCast.js, line 853