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
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:
import type { CastTarget } from '@nomercy-entertainment/nomercy-player-core';
// type CastTarget = 'cast' | 'airplay' | 'remote-playback' | 'local';
| Target | Description |
|---|---|
'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:
| Code | Cause |
|---|---|
core:policy/castUnavailable | Cast SDK not loaded and autoLoad is false |
core:policy/castLoadTimeout | SDK load exceeded loadTimeoutMs |
core:policy/castLoadFailed | SDK refused to load |
core:policy/castScriptLoadFailed | Network or CSP blocked the script |
core:policy/airplayUnavailable | AirPlay is WebKit-only, not this browser |
core:policy/remotePlaybackUnavailable | RemotePlayback API not present or no video element |
core:policy/transferTargetUnknown | Unrecognised target string |
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: { ... } }):
| Field | Type | Default | Description |
|---|---|---|---|
autoLoad | boolean | false | Inject SDK on first transferTo('cast') |
receiverApplicationId | string | Google default receiver | Custom receiver app ID |
autoJoinPolicy | 'origin-scoped' | 'tab-and-origin-scoped' | 'page-scoped' | 'origin-scoped' | Auto-join policy for existing sessions |
resumeSavedSession | boolean | true | Resume the most recent saved session |
scriptUrl | string | Google CDN | Override the SDK script URL |
loadTimeoutMs | number | 10000 | Max ms to wait for SDK load |
player.setup({
cast: {
autoLoad: true,
receiverApplicationId: 'ABCD1234',
autoJoinPolicy: 'origin-scoped',
},
});
CastState enum
| Value | Meaning |
|---|---|
UNAVAILABLE | No Cast/AirPlay/RemotePlayback APIs present |
AVAILABLE | At least one device is reachable; no active session |
CONNECTING | A session is being established |
CONNECTED | Session is active; playback is on the remote device |
DISCONNECTED | A session was active but has ended |
See also
- Device Detection:
isTv,isMobile,device - Events Reference:
castState - Enums:
CastState