Skip to content

Overview

Cast methods hand playback off to a remote target (Chromecast, AirPlay, or W3C RemotePlayback) or pull it back to local. All state transitions emit castState events for reactive UI.

castState()

Coarse handoff state. Reflects the status of the most recently active remote-playback target. Returns CastState.UNAVAILABLE when no Cast, AirPlay, or RemotePlayback API is present in the browser.

Returns: CastState

TypeScript
import { CastState } from '@nomercy-entertainment/nomercy-player-core';

const state = player.castState();
if (state === CastState.AVAILABLE) {
showCastButton();
}

transferTo(target)

Hand playback off to a remote target, or return it to local. The target is a CastTarget — an exported string union you can import:

TypeScript
import type { CastTarget } from '@nomercy-entertainment/nomercy-player-core';
// type CastTarget = 'cast' | 'airplay' | 'remote-playback' | 'local';
TargetDescription
'cast'Chromecast / Google Cast. SDK is injected on demand if cast.autoLoad: true; otherwise the script must be on the page already
'airplay'Safari / WebKit only. Opens the playback target picker via webkitShowPlaybackTargetPicker
'remote-playback'W3C RemotePlayback API (Chrome desktop / Android)
'local'Return playback to the local element; emits castState: DISCONNECTED

Returns: Promise<void>

Events emitted: castState on every state change (CONNECTING, CONNECTED, AVAILABLE, DISCONNECTED)

Throws: BrowserPolicyError with a target-specific code when the target’s API is unavailable:

CodeCause
core:policy/castUnavailableCast SDK not loaded and autoLoad is false
core:policy/castLoadTimeoutSDK load exceeded loadTimeoutMs
core:policy/castLoadFailedSDK refused to load
core:policy/castScriptLoadFailedNetwork or CSP blocked the script
core:policy/airplayUnavailableAirPlay is WebKit-only, not this browser
core:policy/remotePlaybackUnavailableRemotePlayback API not present or no video element
core:policy/transferTargetUnknownUnrecognised target string
TypeScript
castButton.addEventListener('click', async () => {
try {
await player.transferTo('cast');
} catch (err) {
console.error('Cast unavailable:', err.message);
}
});

player.on('castState', ({ state }) => {
castButton.dataset.state = state;
});

Cast configuration

Passed to setup({ cast: { ... } }):

FieldTypeDefaultDescription
autoLoadbooleanfalseInject SDK on first transferTo('cast')
receiverApplicationIdstringGoogle default receiverCustom receiver app ID
autoJoinPolicy'origin-scoped' | 'tab-and-origin-scoped' | 'page-scoped''origin-scoped'Auto-join policy for existing sessions
resumeSavedSessionbooleantrueResume the most recent saved session
scriptUrlstringGoogle CDNOverride the SDK script URL
loadTimeoutMsnumber10000Max ms to wait for SDK load
TypeScript
player.setup({
cast: {
autoLoad: true,
receiverApplicationId: 'ABCD1234',
autoJoinPolicy: 'origin-scoped',
},
});

CastState enum

ValueMeaning
UNAVAILABLENo Cast/AirPlay/RemotePlayback APIs present
AVAILABLEAt least one device is reachable; no active session
CONNECTINGA session is being established
CONNECTEDSession is active; playback is on the remote device
DISCONNECTEDA session was active but has ended

See also