Constructor

MediaManager

new MediaManager(mediaElement, opt_supportedCommands, opt_id)

Parameter

mediaElement

(non-null HTMLMediaElement or non-null cast.receiver.media.Player)

The DOM media element (video/audio) or a player that implements the cast.receiver.media.Player interface. Note that the MediaManager will add listeners for 'loadedmetadata', 'error', and 'ended' HTMLMediaElement events.

opt_supportedCommands

Optional

number

A bitmask of media commands supported by the application. LOAD, PLAY, STOP, GET_STATUS must always be supported. If this value is not provided, then PAUSE, SEEK, STREAM_VOLUME, STREAM_MUTE are assumed to be supported too.

opt_id

Optional

string

The media manager ID.

Classes

Enumeration

EventType

static

string

System events dispatched by cast.receiver.MediaManager.

Value

LOAD

Fired when there is a load message.

STOP

Fired when there is a stop message.

PAUSE

Fired when there is a pause message.

SKIP_AD

Fired when there is a skip ad message.

PLAY

Fired when there is a play message.

PLAY_AGAIN

Fired when there is a play again message.

SEEK

Fired when there is a seek message.

SET_PLAYBACK_RATE

Fired when there is a set playback rate message.

SET_VOLUME

Fired when there is a set volume message.

GET_STATUS

Fired when there is a get status message.

EDIT_TRACKS_INFO

Fired when there is an edit tracks info message.

EDIT_AUDIO_TRACKS

Fired when there is an edit audio tracks message.

QUEUE_LOAD

Fired when there is a queue load message.

QUEUE_INSERT

Fired when there is a queue insert message.

QUEUE_UPDATE

Fired when there is a queue update message.

QUEUE_REMOVE

Fired when there is a queue remove message.

QUEUE_REORDER

Fired when there is a queue reorder message.

PRECACHE

Fired when there is a precache event.

PRELOAD

Fired when there is a preload event.

CANCEL_PRELOAD

Fired when there is a cancel preload event.

QUEUE_GET_ITEM_RANGE

Fired when there is a fetch item message.

QUEUE_GET_ITEMS

Fired when there is a get items info message.

QUEUE_GET_ITEM_IDS

Fired when there is a get queue ids message.

QUEUE_SHUFFLE

Fired when there is a shuffle message.

Properties

EventType

static

string

System events dispatched by cast.receiver.MediaManager.

Value

LOAD

Fired when there is a load message.

STOP

Fired when there is a stop message.

PAUSE

Fired when there is a pause message.

SKIP_AD

Fired when there is a skip ad message.

PLAY

Fired when there is a play message.

PLAY_AGAIN

Fired when there is a play again message.

SEEK

Fired when there is a seek message.

SET_PLAYBACK_RATE

Fired when there is a set playback rate message.

SET_VOLUME

Fired when there is a set volume message.

GET_STATUS

Fired when there is a get status message.

EDIT_TRACKS_INFO

Fired when there is an edit tracks info message.

EDIT_AUDIO_TRACKS

Fired when there is an edit audio tracks message.

QUEUE_LOAD

Fired when there is a queue load message.

QUEUE_INSERT

Fired when there is a queue insert message.

QUEUE_UPDATE

Fired when there is a queue update message.

QUEUE_REMOVE

Fired when there is a queue remove message.

QUEUE_REORDER

Fired when there is a queue reorder message.

PRECACHE

Fired when there is a precache event.

PRELOAD

Fired when there is a preload event.

CANCEL_PRELOAD

Fired when there is a cancel preload event.

QUEUE_GET_ITEM_RANGE

Fired when there is a fetch item message.

QUEUE_GET_ITEMS

Fired when there is a get items info message.

QUEUE_GET_ITEM_IDS

Fired when there is a get queue ids message.

QUEUE_SHUFFLE

Fired when there is a shuffle message.

customizedCloudStatusCallback

nullable function(non-null cast.receiver.media.CloudMediaStatus) returns (nullable cast.receiver.media.CloudMediaStatus or Promise containing nullable cast.receiver.media.CloudMediaStatus)

The application developer can provide a different media status for cloud sender. This hanlder will be called with a copy of the media status that is sent to all other senders, after the customizedStatusCallback handler is called. If the method returns null the media status message will not be sent

customizedStatusCallback

nullable function(non-null cast.receiver.media.MediaStatus) returns (nullable cast.receiver.media.MediaStatus or Promise containing nullable cast.receiver.media.MediaStatus)

The application developer can override this method to customize the media status that will be send to the senders (this method will be called before the media status is sent). By providing this method application developers can, for example, add custom data to the media status. The current media status will be provided as a parameter and the method should return the application-modified current status. The default behavior is to return the incoming media status. If the method returns null the media status message will not be sent (developers should be aware that if the media status is a response to a sender status request the sender will expect a response so this method should only return null it if the developer is also overriding onGetStatus).

onAbort

nullable function()

Called when the current media is terminated before reaching the end due to a sender request (for example, the current element of the queue is deleted). The default behavior is to call ResetMediaElement with idle reason. INTERRUPTED.

See also
cast.receiver.media.IdleReason

onCancelPreload

nullable function(non-null cast.receiver.MediaManager.Event) returns boolean

Called when there is a CANCEL_PRELOAD event. This can happen if the user has seek before the time that the preload is triggered or if the next item in the queue has changed and has a shorter preload time. By default is null. If implemented, the callee is supposed to return true if media will be cancelled its preloaded or false otherwise. This information will be used to send a media status message to the senders so they can update the second screen UI.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.RequestData

onEditAudioTracks

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the EDIT_AUDIO_TRACKS event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.EditAudioTracks

onEditTracksInfo

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the EDIT_TRACKS_INFO event. The default behavior is to set the tracks active/inactive as per the request.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.EditTracksInfoData

onEnded

(nullable function() or non-null function(optional non-null cast.receiver.media.IdleReason))

Called when the media ends (playback completes). The default behavior is to call ResetMediaElement with idle reason FINISHED.

See also
cast.receiver.media.IdleReason

onError

nullable function(nullable Object)

Called when there is an error not triggered by a LOAD request. The default behavior is to move to the next item in the queue or call ResetMediaElement.

onFetchItems

nullable function(non-null cast.receiver.MediaManager.Event)

Processes QUEUE_GET_ITEM_RANGE event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.FetchItemsRequestData

onGetItemsInfo

nullable function(non-null cast.receiver.MediaManager.Event)

Processes QUEUE_GET_ITEMS event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.GetItemsInfoRequestData

onGetQueueIds

nullable function(non-null cast.receiver.MediaManager.Event)

Processes QUEUE_GET_ITEM_IDS event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.RequestData

onGetStatus

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the GET_STATUS event.

onLoad

nullable function(non-null cast.receiver.MediaManager.Event)

If provided, it processes the LOAD event. The default behavior is to set the src and autoplay properties of the media element and call its load method. If provided, the currentTime property will be modified when the 'loadedmetadata' event is fired (in onMetadataLoaded) as it can only be set when the media element duration property has been set. If this method is overriden, the application developer may need to handle sendLoadError, sendLoadComplete and onMetadataLoaded. Please read the documentation of those APIs.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.LoadRequestData
cast.receiver.MediaManager#sendLoadError
cast.receiver.MediaManager#sendLoadComplete
cast.receiver.MediaManager#onMetadataLoaded

onLoadMetadataError

nullable function(non-null cast.receiver.MediaManager.LoadInfo)

Called when load has had an error, it can be overridden to handle application specific logic. The default behavior is to call resetMediaElement with idle reason ERROR and sendLoadError with error type LOAD_FAILED.

See also
cast.receiver.media.IdleReason
cast.receiver.media.ErrorType
cast.receiver.MediaManager.LoadInfo

onLocalRequestError

nullable function(non-null Object)

Called when there is an error produced by a local media request.

onMediaStatus

nullable function(non-null cast.receiver.media.MediaStatus)

Called when there is a media status message to senders. It will be null if there is no active media session. The receiver should not change the media status using this API. See cast.receiver.MediaManager#customizedStatusCallback for that use case.

onMetadataLoaded

nullable function(non-null cast.receiver.MediaManager.LoadInfo)

Called when load has completed, it can be overridden to handle application specific action. The default behavior is to set the currentTime property of the media element (if it was provided in the LOAD request), then call sendLoadComplete.

See also
cast.receiver.MediaManager.LoadInfo

onPause

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the PAUSE event. The default behavior is to call the media element's pause method and broadcast the status providing the incoming requestId.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.RequestData

onPlay

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the PLAY event. The default behavior is to call the media element's play method and broadcast the status providing the incoming requestId.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.RequestData

onPlayAgain

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the PLAY_AGAIN event. The default behavior in the IDLE state is to load the media that was last loaded by the media manager and when not in the IDLE state to seek to 0, then call the media element's play method, and broadcast the status providing the incoming requestId.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.RequestData

onPrecache

nullable function(non-null cast.receiver.MediaManager.Event)

Called when there is a PRECACHE event. By default is null. This function will be called if a PRECACHE message is received from the broadcast namespace when in background mode (before CastReceiverManager.onReady), or if a PRECACHE message is received on the media namespace (after onReady). It is the applications responsibility to deserialize the string.

onPreload

nullable function(non-null cast.receiver.MediaManager.Event) returns boolean

Called when there is a PRELOAD event. By default is null. If implemented, the callee is supposed to return true if media will be preloaded or false, otherwise. This information will be used to send a media status message to the senders so they can update the second screen UI.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.PreloadRequestData

onQueueEnded

nullable function(non-null cast.receiver.media.IdleReason, optional nullable number)

Called when the last item ends or is aborted (playback of the queue completes). Takes two params, the idleReason and optionally the requestId. The default behavior is to call ResetMediaElement with idle reason FINISHED. If the application wants to keep the media session alive after playback of the queue has been completed, the application should override this event callback with a function that modifies the queue so that there are more items to play (for example, by appending new items to the queue).

See also
cast.receiver.media.IdleReason

onQueueInsert

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the QUEUE_INSERT event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.QueueInsertRequestData

onQueueLoad

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the QUEUE_LOAD event. The default behavior is to create a media session and associate it a queue.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.QueueLoadRequestData

onQueueRemove

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the QUEUE_REMOVE event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.QueueRemoveRequestData

onQueueReorder

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the QUEUE_REORDER event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.QueueReorderRequestData

onQueueUpdate

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the QUEUE_UPDATE event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.QueueUpdateRequestData

onSeek

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the SEEK event. The default behavior is to call the media element's play or pause methods (only if required based on the current state and the resume state value of the request) and broadcast the status providing the incoming requestId.

See also
cast.receiver.media.SeekResumeState
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.SeekRequestData

onSetPlaybackRate

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the SET_PLAYBACK_RATE event. The default behavior is to update the playbackRate of the media element, and broadcast the status providing the incoming requestId. The supported playback rate by the cast platform is 0.5x - 2x. If the default behavior is being overwritten, use the updatePlaybackRate to update the mediamanager with the current playback rate.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.SetPlaybackRateRequestData

onSetVolume

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the SET_VOLUME event. The default behavior is to set volume and muted on the media element as required and broadcast the status providing the incoming requestId.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.VolumeRequestData

onShuffle

nullable function(non-null cast.receiver.MediaManager.Event)

Processes QUEUE_SHUFFLE event.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.RequestData

onSkipAd

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the SKIP_AD event. No default implementation.

See also
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.RequestData

onStop

nullable function(non-null cast.receiver.MediaManager.Event)

Processes the STOP event. The default behavior is to call resetMediaElement, with idle reason CANCELLED, and broadcast the status providing the incoming requestId.

See also
cast.receiver.media.IdleReason
cast.receiver.MediaManager.Event
cast.receiver.MediaManager.RequestData

Methods

broadcastStatus

broadcastStatus(includeMedia, opt_requestId, opt_customData, opt_includeQueueItems)

Sends a media status message to all senders (broadcast). Applications can use it when they have a custom state change. It will call cast.receiver.MediaManager.prototype.customizedStatusCallback so applications can customize the status message.

Parameter

includeMedia

boolean

Whether to include media information.

opt_requestId

Optional

number

The ID of the request that triggered the status change.

Value may be null.

opt_customData

Optional

Object

The status message application-specific custom data.

Value may be null.

opt_includeQueueItems

Optional

boolean

Whether to include queue items in the status message. By default they are not included.

getMediaInformation

getMediaInformation() returns cast.receiver.media.MediaInformation

Provides information about the media currently loaded.

See also
cast.receiver.media.MediaInformation
Returns

nullable cast.receiver.media.MediaInformation The media information.

getMediaQueue

getMediaQueue() returns cast.receiver.MediaQueue

Provides information about the media queue. To avoid unnecessary copies the underlying queue is provided. However, the media queue should only be modified using the APIs provided as otherwise some actions (evaluation of queue ended...) may not take place.

Returns

nullable cast.receiver.MediaQueue The media queue.

insertQueueItems

insertQueueItems(itemsToInsert, opt_insertBefore, opt_currentItemIndex, opt_currentItemId, opt_currentTime)

Insert items in the queue. This is an asynchronous request that will be scheduled and processed as it was requested by a sender. Typically this request will trigger a onLocalRequestError or onMediaStatus callback.

Parameter

itemsToInsert

Array of non-null cast.receiver.media.QueueItem

The list of media items to insert. Must not be null or empty. The itemId of each item must not be set (as they will be assigned by the receiver).

Value must not be null.

opt_insertBefore

Optional

number

ID of the item that will be located immediately after the inserted list. If it is not provided or it is not found, the list will be appended to the end of the queue.

Value may be null.

opt_currentItemIndex

Optional

number

Index (relative to the items array, starting with 0) of the new currentmedia Item.

Value may be null.

opt_currentItemId

Optional

number

ID of the current media Item after the insertion (if not provided, the currentItem value will be the same as before the insertion).

Value may be null.

opt_currentTime

Optional

number

Seconds since the beginning of content to start playback of the current item. If provided, this value will take precedence over the startTime value provided at the QueueItem level.

Value may be null.

load

load(loadRequestData)

Loads a media session on the receiver without involvement of a sender.

Parameter

loadRequestData

cast.receiver.MediaManager.LoadRequestData

The load request data.

Value must not be null.

loadTracksInfo

loadTracksInfo(tracksInfo) returns boolean

Allows to load tracks if load is still ongoing. This method is useful if the application wants to override onLoad but still wants to use the default behavior for tracks handling. For example, this can be useful if the application has to override onLoad to support HLS but it is using vtt closed captions. Note that if the mediaManager is no longer in the loading state the request will be ignored.

Parameter

tracksInfo

cast.receiver.media.TracksInfo

The tracks info.

Value must not be null.

Returns

boolean Whether the tracks are valid and the tracks load request was honored.

queueLoad

queueLoad(queueLoadRequestData)

Loads a queue on the receiver without involvement of a sender.

Parameter

queueLoadRequestData

cast.receiver.MediaManager.QueueLoadRequestData

Queue load request data.

Value must not be null.

removeQueueItems

removeQueueItems(itemIdsToRemove, opt_currentItemId, opt_currentTime)

Removes a list of items from the queue. If the remaining queue is empty, the media session will be terminated. This is an asynchronous request that will be scheduled and processed as it was requested by a sender. Typically this request will trigger a onLocalRequestError or onMediaStatus callback.

Parameter

itemIdsToRemove

Array of number

The list of media item IDs to remove. Must not be null or empty.

Value must not be null.

opt_currentItemId

Optional

number

ID of the current media Item after the deletion (if not provided, the currentItem value will be the same as before the deletion; if it does not exist because it has been deleted, the currentItem will point to the next logical item in the list).

Value may be null.

opt_currentTime

Optional

number

Seconds since the beginning of content to start playback of the current item. If provided, this value will take precedence over the startTime value provided at the QueueItem level.

Value may be null.

resetMediaElement

resetMediaElement(opt_idleReason, opt_broadcast, opt_requestId, opt_broadcastStatusCustomData)

Resets Media Element to IDLE state. After this call the mediaElement properties will change, paused will be true, currentTime will be zero and the src attribute will be empty. This only needs to be manually called if the developer wants to override the default behavior of onError, onStop or onEnded, for example.

Parameter

opt_idleReason

Optional

cast.receiver.media.IdleReason

The reason to be IDLE.

Value must not be null.

opt_broadcast

Optional

boolean

Whether the senders should be notified about the change (if not provided, the senders will be notified).

opt_requestId

Optional

number

If the status change is due to a sender request (for example, STOP), this is the ID of the sender request that will be added to the status message so the sender can identify it.

Value may be null.

opt_broadcastStatusCustomData

Optional

Object

If the senders should be notified, this parameter allows to set the application-specific custom data in the status message.

Value may be null.

Throws

Error If opt_broadcastStatusCustomData is provided but opt_broadcast is false.

sendError

sendError(senderId, requestId, type, opt_reason, opt_customData)

Sends an error to a specific sender.

Parameter

senderId

string

The sender ID.

requestId

number

The ID of the incoming request that caused this error.

type

cast.receiver.media.ErrorType

The error type.

Value must not be null.

opt_reason

Optional

cast.receiver.media.ErrorReason

The error reason.

Value may be null.

opt_customData

Optional

Object

The error message application-specific custom data.

Value may be null.

sendLoadComplete

sendLoadComplete(opt_customData)

Sends the new status after a LOAD message has been completed successfully. Note: Applications do not normally need to call this API. When the application overrides onLoad, it may need to manually declare that the LOAD request was successful. The default implementaion will send the new status to the sender when the video/audio element raises the 'loadedmetadata' event. The default behavior may not be acceptable in a couple scenarios:

1) When the application does not want to declare LOAD successful until, for example, 'canPlay' is raised (instead of 'loadedmetadata'). 2) When the application is not actually loading the media element (for example, if LOAD is used to load an image).

Parameter

opt_customData

Optional

Object

The status message application-specific custom data.

Value may be null.

sendLoadError

sendLoadError(opt_errorType, opt_customData)

When the application overrides onLoad, it should use this method to trigger an error response to the sender. This is typically due to application-specific verification issues.

Parameter

opt_errorType

Optional

cast.receiver.media.ErrorType

The error type, by default is assumed to be cast.receiver.media.ErrorType.LOAD_FAILED, but the application can send an INVALID_REQUEST, for example, if there is customData that does not match some criteria.

Value may be null.

opt_customData

Optional

Object

The error message application-specific custom data.

Value may be null.

See also
cast.receiver.media.ErrorType

sendStatus

sendStatus(senderId, requestId, includeMedia, opt_customData, opt_includeQueueItems)

Sends a media status message to a specific sender.

Parameter

senderId

string

The sender ID.

requestId

number

The ID of the incoming request that caused this response.

includeMedia

boolean

Whether to include media information.

opt_customData

Optional

Object

The status message application-specific custom data.

Value may be null.

opt_includeQueueItems

Optional

boolean

Whether to include queue items in the status message. By default they are not included.

setIdleReason

setIdleReason(idleReason)

Sets the IDLE reason. This allows applications that want to force the IDLE state to indicate the reason that made the player going to IDLE state (a custom error, for example). The idle reason will be sent in the next status message. NOTE: Most applications do not need to set this value, it is only needed if they want to make the player go to IDLE in special circumstances and the default idleReason does not reflect their intended behavior.

Parameter

idleReason

cast.receiver.media.IdleReason

The reason to be in the IDLE state.

Value must not be null.

setMediaElement

setMediaElement(mediaElement)

Associates a new media element or Player to the media manager.

Parameter

mediaElement

(non-null HTMLMediaElement or non-null cast.receiver.media.Player)

The DOM media element (video/audio) or a player that implements the cast.receiver.media.Player interface.

setMediaInformation

setMediaInformation(mediaInformation, opt_broadcast, opt_broadcastStatusCustomData)

Sets information about the media currently loaded. This information will be sent to the senders when they request media status.

Parameter

mediaInformation

cast.receiver.media.MediaInformation

The new media information. Use resetMediaElement to reset its value.

Value must not be null.

opt_broadcast

Optional

boolean

Whether the senders should be notified about the change (if not provided, the senders will be notified).

opt_broadcastStatusCustomData

Optional

Object

If the senders should be notified this parameter allows to set the application-specific custom data in the status message.

Value must not be null.

See also
cast.receiver.media.MediaInformation
Throws

Error If opt_broadcastStatusCustomData is provided but opt_broadcast is false.

updatePlaybackRate

updatePlaybackRate(playbackRate)

Update the current playback rate. Media manager needs to know the current playback rate to check time drift so an overwriten onSetPlaybackRate or onLoad handlers should use this function to report back the playback rate.

Parameter

playbackRate

number

The update playback rate.