# Cast to AirPlay and Chromecast

Send playback to AirPlay and Google Cast devices, with availability detection and connection state.

Send playback to an Apple TV over AirPlay or a Chromecast over Google Cast while the browser stays in control.

> **Note**
>
> Using a pre-built [skin](https://videojs.org/docs/framework/react/guides/skins)? It already includes the controls shown here. You may still need the media or player setup in this guide. The component examples are for building your own player UI from individual [components](https://videojs.org/docs/framework/react/guides/ui-components).

## Installation

Install the Google Cast extension and the hls.js adapter used in the example below. AirPlay needs no extra package:

```bash
pnpm add @videojs/react @videojs/google-cast @videojs/hlsjs-video
```

## Recommended approach

Use the [GoogleCast extension](https://videojs.org/docs/framework/react/reference/components/google-cast) with a [CastButton](https://videojs.org/docs/framework/react/reference/components/cast-button), and add an [AirPlayButton](https://videojs.org/docs/framework/react/reference/components/airplay-button) for Safari. The packaged skins already include both buttons; add `GoogleCast` anywhere inside the player to enable the configurable Cast route. It works with any media the player attaches, including a plain `<Video />`.

**App.tsx**

```tsx
import { AirPlayButton, CastButton, Container, createPlayer } from '@videojs/react';
import { GoogleCast } from '@videojs/react/extensions/google-cast';
import { HlsJsVideo } from '@videojs/react/media/hlsjs-video';
import { videoFeatures } from '@videojs/react/video';

const { Player } = createPlayer({ features: videoFeatures });

export default function BasicUsage() {
  return (
    <Player>
      <Container className="media-container">
        <HlsJsVideo src="https://stream.mux.com/BV3YZtogl89mg9VcNBhhnHm02Y34zI1nlMuMQfAbl3dM.m3u8" autoPlay muted playsInline loop />
        <GoogleCast />
        <div className="media-remote-buttons">
          <AirPlayButton
            className="media-airplay-button"
            render={(props, state) => (
              <button {...props}>{state.state === 'connected' ? 'Stop AirPlay' : 'Start AirPlay'}</button>
            )}
          />
          <CastButton
            className="media-cast-button"
            render={(props, state) => (
              <button {...props}>{state.connection === 'connected' ? 'Stop casting' : 'Start casting'}</button>
            )}
          />
        </div>
      </Container>
    </Player>
  );
}
```

**App.css**

```css
.media-container {
  position: relative;
}

.media-container video {
  width: 100%;
}

.media-remote-buttons {
  position: absolute;
  right: 10px;
  bottom: 10px;
  display: flex;
  gap: 8px;
}

.media-airplay-button,
.media-cast-button {
  padding-block: 8px;
  padding-inline: 20px;
  color: black;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 9999px;
  backdrop-filter: blur(10px);
}

.media-airplay-button[data-disabled],
.media-cast-button[data-disabled] {
  cursor: not-allowed;
  opacity: 0.5;
  filter: grayscale(1);
}
```

The demo pairs the media component with the `GoogleCast` extension. Without that extension, `CastButton` drives the browser’s native Remote Playback API instead, which does not provide Video.js receiver or load-request configuration.

## How it works

The [remote playback feature](https://videojs.org/docs/framework/react/reference/api/feature-remote-playback) gives AirPlay and Google Cast one state shape:

- `remotePlaybackAvailability` is `'available'` when a remote device can be reached, `'unavailable'` when the API exists but no device is reachable, and `'unsupported'` when the browser has no remote playback route.
- `remotePlaybackState` is `'disconnected'`, `'connecting'`, or `'connected'`. The AirPlay route reports only `'disconnected'` and `'connected'`; Google Cast also reports `'connecting'`.
- `promptRemotePlayback()` opens the browser’s device picker, including during an active session. Use the picker to connect to a device or return to local playback.

AirPlay uses WebKit’s presentation APIs. Google Cast uses a sender and receiver: the browser sends the receiver a media URL and then controls playback. Your UI reads the same Video.js state for either route.

While connected, playback actions — play, pause, seek, volume — proxy to the remote device, and player state keeps reflecting the remote session. Local playback is suspended.

Control playback and read its state through the player while casting: use the built-in controls or the [store actions and state](https://videojs.org/docs/framework/react/reference/api/player-store) from `usePlayer`. Don’t call methods on the media component or listen for its events to drive or track a session.

For a custom control, call `promptRemotePlayback()` directly from the click handler and catch its promise. It rejects when no remote route exists or the browser refuses the request. A canceled Google Cast picker can also resolve without connecting, so show success only when `remotePlaybackState` is `'connected'`. The built-in buttons handle the request for you.

The Google Cast SDK loads only when the player includes the `GoogleCast` extension and runs in a Chromium browser that can cast.

## Availability and constraints

- Remote playback is asynchronous and can fail mid-session when a device or network disappears. React to `remotePlaybackState` rather than assuming a request succeeds.
- Google Cast works only in Chromium-based browsers; elsewhere its route reports `'unsupported'`.
- AirPlay works only in Safari; availability requires an AirPlay device reachable on the network.
- The remote device fetches the source and its text tracks on its own. Localhost, VPN-only, expired, or cross-origin URLs without the necessary CORS headers will fail on the receiver even if they play in the browser.
- Set `disableRemotePlayback` on the media component to opt out entirely.

## Common variations

### Custom Cast receiver

By default, Video.js sends the current source to Google’s Default Media Receiver. Set `receiver` when you have your own receiver application:

```tsx
<GoogleCast receiver="YOUR_APP_ID" />
```

Use `src` and `contentType` when the receiver should load a different source than the browser. A YouTube or Vimeo embed needs `src`: the receiver can’t play the embed itself, so without it nothing is sent to the receiver. `streamType` and `customData` let you describe that source and attach application-specific data. See the [GoogleCast reference](https://videojs.org/docs/framework/react/reference/components/google-cast) for the exact option types.

### Read connection state

Use `remotePlaybackAvailability` to decide whether to offer a route and `remotePlaybackState` to show whether it is disconnected, connecting, or connected. `CastButton` and `AirPlayButton` already apply these states to their presentation; read them directly only when another part of your app needs them.

## Troubleshooting

### The cast button doesn’t render, or renders disabled

The Cast button hides when the route is unsupported. In Chromium with no Cast device reachable, it stays visible but disabled. The AirPlay button hides unless Safari reports an AirPlay device.

### Casting starts but the device shows an error

The receiver cannot fetch the media. Confirm the source URL is reachable from the device and served with CORS headers, including caption tracks.

### Custom UI shows local playback while casting

The code reads or controls the media component directly. Read playback state from the player store and call its actions instead; only the player reflects the remote session.

### The UI freezes on “connecting”

For Google Cast, track the `promptRemotePlayback()` promise separately from `remotePlaybackState`. Clear your loading indicator when the promise settles. A canceled picker can resolve without connecting. AirPlay does not expose a `'connecting'` phase; show its connected or disconnected state instead.

## Related pages

### Components

- [CastButton](https://videojs.org/docs/framework/react/reference/components/cast-button): Accessible Cast toggle button with state reflection and keyboard support
- [AirPlayButton](https://videojs.org/docs/framework/react/reference/components/airplay-button): Accessible AirPlay toggle button that opens the WebKit playback target picker and reflects session state
- [GoogleCast](https://videojs.org/docs/framework/react/reference/components/google-cast): Options for the Google Cast extension that configures the receiver and load request

### API

- [Remote Playback](https://videojs.org/docs/framework/react/reference/api/feature-remote-playback): Remote playback state and actions for the player store
- [HlsJsVideo](https://videojs.org/docs/framework/react/reference/components/hlsjs-video): HLS video element powered by hls.js for adaptive bitrate streaming

### Guides

- [Media sources](https://videojs.org/docs/framework/react/guides/media-sources): Set what a media component plays and how its engine plays it with the structured source property

---

React documentation: https://videojs.org/docs/framework/react/llms.txt
All documentation: https://videojs.org/llms.txt
