Skip to content

Module: src/providers/YouTube

VisionPlayer engine that connects the YouTube IFrame API and the players' media API. Emits synthetic media events and mirrors YouTube player state to the VisionPlayer state and controls the YouTube iframe element. Supports playback, pause, loop, volume mute, playback rate and seek.

Version
1.0.0
Author
Frank Kudermann - alphanull
License
MIT
Requires
DomSmith
AsyncTask
ExtendedMediaError
Looper
object
scriptLoader
Source
YouTube.js, line 23

Constructor

new YouTube(player, parent, options)

Creates an instance of the YouTube engine.

Parameters:
Name Type Description
player Player

Reference to the player instance.

parent Object

Reference to the parent component (unused for engines).

options Object

Additional options.

Parameters:
Name Type Description
apiKey symbol

Token for extended access to the player API.

Members

Holds the instance configuration for this component.

Properties
Name Type Attributes Default Description
lazyLoadLib boolean optional true

If true, load YouTube Player API on engine activation as opposed to the earlier initialisation stage.

noCookie boolean optional true

If true, use YouTube's no-cookie domain (youtube-nocookie.com) for the player iframe. Note: The API script itself must still be loaded from youtube.com. This reduces but does not eliminate cookies, and does not provide full GDPR compliance.

Type
Object
Source
YouTube.js, line 31

Reference to the main player instance.

Type
Player
Source
YouTube.js, line 40

Secret key only known to the player instance and initialized components. Used to restrict access to API methods in secure mode.

Type
symbol
Source
YouTube.js, line 47

Reference to the YouTube player instance (IFrame API).

Type
Object
Source
YouTube.js, line 53

Holds metadata information provided by media.load.

Type
Media~metaData
Source
YouTube.js, line 59

Reference to the Async Task instance. Used to handle async tasks, which can be cancelled, resolved or rejected.

Type
AsyncTask
Source
YouTube.js, line 65

Cancelable promise for script loading. Used to prevent callbacks when destroyed.

Type
Promise | null
Source
YouTube.js, line 71

Local state mirror exposed via player state API.

Type
Object
Source
YouTube.js, line 77

Loop helper for synthetic timeupdate polling.

Type
Looper | null
Source
YouTube.js, line 83

Loop helper for synthetic progress polling.

Type
Looper | null
Source
YouTube.js, line 89

Container element for the YouTube iframe.

Type
HTMLElement | null
Source
YouTube.js, line 95

Mapping of YouTube IFrame API player states.

Type
Object
Source
YouTube.js, line 647

Default state object for the YouTube engine.

Type
Object
Source
YouTube.js, line 660

Fallback empty TimeRanges-like object.

Type
Object
Source
YouTube.js, line 682

Methods

canPlay(metaData) → 'probably'|''

Checks if this engine can play the given media data, by checking if the source contains a valid YouTube video ID.

Parameters:
Name Type Description
metaData Media~metaData

The data to test.

Parameters:
Name Type Description
src string

The source URL to test.

Returns

Indicates if stream can be played.

Type 'probably' | ''
Source
YouTube.js, line 163

private #loadLib() → Promise<Object>

Loads YouTube IFrame API via CDN if not present. Uses scriptLoader for deduplication, wraps in custom Promise for API-ready callback.

Returns

Cancelable promise that resolves with the YT namespace.

Type Promise<Object>
Source
YouTube.js, line 175

private #load(metaData, optionsopt) → Promise<Media~metaData>

Loads media into the YouTube player.

Parameters:
Name Type Attributes Description
metaData Media~metaData

The media data to load.

options Object optional

Additional options.

Parameters:
Name Type Attributes Default Description
seek number optional

Seek position.

play boolean optional

Whether to play after loading.

paused boolean optional true

When true, keep paused unless play overrides.

volume number optional 1

Initial volume between 0 and 1.

muted boolean optional false

Whether to start muted.

ignoreAutoplay boolean optional

If true, ignore autoplay setting.

Returns

Resolves with current metadata once API is available.

Type Promise<Media~metaData>
Source
YouTube.js, line 252

private #loadVideo(videoId, seek, play, volume, muted)

Cues a video and primes seek/volume/mute state before playback.

Parameters:
Name Type Description
videoId string

Identifier of the YouTube video to load.

seek number

Start position in seconds for initial cue.

play boolean

Whether playback should start after cueing.

volume number

Initial volume fraction (0..1).

muted boolean

Whether audio starts muted.

Source
YouTube.js, line 320

private #onLoaded()

Syncs metadata after the player signals it is cued and emits Media API events.

Fires
media/loadedmetadata
media/ready
media/loadeddata
media/canplay
media/canplaythrough
media/progress
media/timeupdate
Source
YouTube.js, line 341

private #getMetaData() Media~metaData

Returns the current metadata.

Returns

The current metadata.

Type Media~metaData
Source
YouTube.js, line 373

private #play()

Plays the media.

Fires
media/play
Source
YouTube.js, line 379

private #pause()

Pauses the media.

Fires
media/pause
Source
YouTube.js, line 392

private #loop(loop)

Sets the loop state.

Parameters:
Name Type Description
loop boolean

Whether to loop.

Fires
media/loop
Source
YouTube.js, line 405

private #playbackRate(rate)

Sets the playback rate.

Parameters:
Name Type Description
rate number

The playback rate (0.25 to 2).

Fires
media/ratechange
Source
YouTube.js, line 419

private #seek(time)

Seeks to a specific time.

Parameters:
Name Type Description
time number

The time to seek to (in seconds).

Fires
media/seeked
Source
YouTube.js, line 434

private #volume(volume)

Sets the volume.

Parameters:
Name Type Description
volume number

The volume (0 to 1).

Fires
media/volumechange
Source
YouTube.js, line 452

private #mute(mute)

Sets the mute state.

Parameters:
Name Type Description
mute boolean

Whether to mute.

Fires
media/volumechange
Source
YouTube.js, line 465

private #updateRanges()

Recomputes buffered/played ranges from the current player state.

Source
YouTube.js, line 477

private #onStateChange(event)

Maps YouTube player states to Media API events and timers.

Parameters:
Name Type Description
event Object

State change payload from YT IFrame API.

Fires
media/play
media/playing
media/pause
media/ended
media/waiting
media/ready
Source
YouTube.js, line 507

private #getIFrameElement() → HTMLIFrameElement|null

Returns the YouTube iframe element.

Returns

The iframe element or null if not mounted.

Type HTMLIFrameElement | null
Source
YouTube.js, line 553

async enable()

Enables the YouTube engine.

Source
YouTube.js, line 558

disable()

Disables the YouTube engine.

Source
YouTube.js, line 604

destroy()

This method removes all events, subscriptions and DOM nodes created by this component.

Source
YouTube.js, line 630

private, static #toRange(start, end) → Object

Creates a minimal TimeRanges-like object for a single range.

Parameters:
Name Type Description
start number

Range start in seconds.

end number

Range end in seconds.

Returns

Range accessor.

Type Object
Source
YouTube.js, line 694

private, static #extractVideoId(src) → string|null

Extracts a YouTube video id from a URL.

Parameters:
Name Type Description
src string

Source URL.

Returns

Video id if found.

Type string | null
Source
YouTube.js, line 711