# playerjs-video

Video component that plays any embed implementing the player.js protocol, such as Mux Player, Gumlet, FrameRate, Livid, Bunny Stream, and Streamable

[player.js](https://github.com/embedly/player.js/blob/master/SPEC.rst) is a `postMessage` protocol for controlling embedded players, not a video host. Any embed that implements its receiver side plays through this one component: Mux Player’s iframe embed, Gumlet, FrameRate, Livid, Bunny Stream, Streamable, and others. Commands are posted to the embed directly, so no third-party script loads.

## Import

```bash
pnpm add @videojs/playerjs-video
```

```ts
import '@videojs/html/media/playerjs-video';
```

Or load it from the [CDN](../../guides/cdn.md):

```html
<script type="module" src="https://cdn.jsdelivr.net/npm/@videojs/cdn@10.6.0/media/playerjs-video.js"></script>
```

## Load a source

`src` takes the embed URL of any player.js receiver: an absolute `http` or `https` URL, with protocol-relative URLs (`//host/…`) read as `https`. Nothing inspects the URL to decide whether it speaks player.js; choosing this component is the opt-in. A `src` that isn’t an `http(s)` URL fails with `MEDIA_ERR_SRC_NOT_SUPPORTED`, and an embed that doesn’t implement the protocol never reports ready, so the element stays at `readyState` 0.

A `playerjs` query parameter in `src` is removed before the embed loads. It’s how [LazyVideo](./lazy-video.md) and [`resolveAdapterType`](../api/resolve-adapter-type.md) tell a player.js embed apart, so one `src` works for both.

```html
<playerjs-video src="https://play.gumlet.io/embed/64bfb0913ed6e5096d66dc1e"></playerjs-video>
```

## Feature detection

Embeds implement the spec to different degrees. Each one lists the methods and events it supports in its `ready` message, and every command is checked against that list:

- A missing getter leaves its value at the default, such as a `duration` of `NaN`. Some embeds implement a getter but can’t answer it yet: Streamable reports no duration until playback starts.
- A missing setter is a no-op; the value stays wherever the embed has it.

`supports(type, name)` reports whether the embed advertised a method or event. `type` is `'method'` or `'event'`, and `name` is one player.js name or an array of names that must all be supported. It returns `false` until the embed reports ready.

The `loadcomplete` event marks the point where the answer is meaningful:

```ts
const video = document.querySelector('playerjs-video');

video.addEventListener('loadcomplete', () => {
  const canSeek = video.supports('method', 'setCurrentTime');
});
```

## Controls

Without `controls`, the element ignores pointer events, so a [player skin](../../guides/skins.md) above it receives every click. With `controls`, the embed’s own chrome takes pointer input.

player.js itself can’t hide an embed’s chrome, but for the services it recognizes, the element does it through the embed URL, as far as each service allows:

| Service | Without `controls` |
| --- | --- |
| [Mux Player](https://www.mux.com/docs/guides/player?utm_source=videojs&utm_campaign=vjs10&utm_content=docs-content) (`player.mux.com`) | Nothing: its controls are hidden with the `--controls` CSS property, which the iframe doesn’t expose. Under a skin its control bar still stays out of sight, since the frame never gets the hover that reveals it, but its center play button shows while paused |
| [Gumlet](https://docs.gumlet.com/video/embed-stream) | `disable_player_controls=true`: only the large center play button stays |
| FrameRate | `show_controls=0` and `initial_play_btn=0`: no control bar and no large start button |
| [Livid](https://support.livid.com/article/46-advanced-embedding-parameters) | `controls=0`: no chrome at all, the play button included. Livid applies it to videos on Pro and Premium accounts only |
| [Bunny Stream](https://bunny.net/docs/stream/embedding) | No parameter hides the control bar, so it’s shrunk instead: `compactControls=true`, `showSpeed=false`, `showHeatmap=false`, `chromecast=false`, `disableAirPlay=true` |
| Streamable | `nocontrols=1`: no chrome at all |

Other services get their URL untouched; pass whatever they offer under [`source.engine.playerJs`](#sourceengineplayerjs).

## URL parameters

For the recognized services, `autoplay`, `muted` (either `muted` or `defaultMuted`), `loop`, and `preload` are written onto the embed URL in the service’s own spelling, and turned off explicitly where the service defaults to on: Gumlet and Streamable loop, and Bunny Stream autoplays. Livid and Mux Player also take `preload` as the element’s own value. The embed reads its URL once, so changing these later reaches the embed over the protocol (`autoplay`, `muted`, `loop`) or not until the next source (`controls`, `preload`); rebuilding the frame for them would restart the video. The exception is an embed still waiting for play under `preload="none"`, which setting `autoplay`, `controls`, or another `preload` builds.

Every other parameter a service documents works too, in either of two places. Written into `src`, a parameter is kept as written, even over one the element would derive. Under `source.engine.playerJs`, it overrides both. A parameter a service reads from the URL hash, like Livid’s start time `#t=25`, goes in `src`.

```html
<playerjs-video src="https://player.mediadelivery.net/embed/759/eb1c4f77-0cda-46be-b47d-1118ad7c2ffe?t=30s&captions=en"></playerjs-video>
```

## Behavior

- `autoplay`, `muted` (seeded by `defaultMuted`), and `loop` are also applied over the protocol once the embed reports ready, which is how they reach services the element doesn’t recognize. An embed without `setLoop` is looped by replaying it on `ended`.
- `volume` (0 to 1, sent as the spec’s 0 to 100), `muted`, and seeking through `currentTime` use the spec’s methods.
- `playbackRate` reaches only embeds that advertise `setPlaybackRate`, an extension to the spec that Gumlet implements.
- player.js reports no buffering, so a started embed counts as playing.
- No text tracks: `textTracks` stays empty.
- The protocol has no fullscreen command, so the element’s own `requestFullscreen()` fullscreens the iframe.
- `poster` and `playsInline` are stored but have no effect on the embed. `preload` reaches Livid and Mux Player as is and Bunny Stream as `preload=false` when it’s `"none"`, and no other service.
- With `preload="none"`, nothing is fetched from the embed’s host, on any service until playback is requested: the embed is built on `play()` and starts playing as it loads. `autoplay` or `controls` builds it at once, since each asks to play or shows the embed’s own play button. Until then the frame is empty, without the service’s thumbnail, so give the player a poster.
- An embed `error` with code 1 surfaces as `MEDIA_ERR_SRC_NOT_SUPPORTED`. Codes 2 and 3 mean a command wasn’t supported; they’re ignored, with a warning in development. Any other code is a fatal error carrying the embed’s message.

## Examples

### Basic Usage

**index.html**

```html
<playerjs-video class="playerjs-video" src="https://play.gumlet.io/embed/64bfb0913ed6e5096d66dc1e" controls></playerjs-video>
```

**index.css**

```css
.playerjs-video {
  width: 100%;
  aspect-ratio: 16 / 9;
}
```

**index.ts**

```ts
import '@videojs/html/media/playerjs-video';
```

## API Reference

### Attributes

These attributes configure the embedded media adapter:

| Attribute | Type | Default |
| --- | --- | --- |
| `autoplay` | `boolean` | `false` |
| `controls` | `boolean` | `false` |
| `loop` | `boolean` | `false` |
| `muted` | `boolean` | `false` |
| `playsinline` | `boolean` | `true` |
| `poster` | `string` | `''` |
| `preload` | `MediaPreloadType` | `'metadata'` |
| `src` | `string` | `''` |

### Properties

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `autoplay` | `boolean` | `false` | |
| `buffered` | `TimeRangeLike` | — | Read-only. |
| `controls` | `boolean` | `false` | |
| `currentSrc` | `string` | — | Read-only. |
| `currentTime` | `number` | — | |
| `defaultMuted` | `boolean` | `false` | |
| `duration` | `number` | — | Read-only. |
| `ended` | `boolean` | — | Read-only. |
| `engine` | `Window \| null` | — | Read-only. The embed's window. player.js publishes no player object; commands are posted to the frame. Null until rendered. |
| `error` | `MediaError \| null` | — | Read-only. |
| `isFullscreen` | `boolean` | — | Read-only. |
| `loop` | `boolean` | `false` | |
| `muted` | `boolean` | `false` | |
| `paused` | `boolean` | — | Read-only. |
| `playbackRate` | `number` | — | Reaches the embed only where it advertises `setPlaybackRate`, an extension to the spec. |
| `played` | `{ length: number; start(index: number): number; end(index: number): number }` | — | Read-only. |
| `playsInline` | `boolean` | `true` | |
| `poster` | `string` | `''` | |
| `preload` | `MediaPreloadType` | `'metadata'` | |
| `readyState` | `number` | — | Read-only. |
| `seekable` | `TimeRangeLike` | — | Read-only. |
| `seeking` | `boolean` | — | Read-only. |
| `source` | `{ src?: string; engine?: PlayerJsSourceEngineConfig } \| null` | `null` | Embed URL in `src`, extra URL parameters under `engine.playerJs`. Re-derives `src`; equal sources skip reload. |
| `src` | `string` | `''` | |
| `textTracks` | `TextTrackListLike` | — | Read-only. Unsupported: player.js has no text track surface. |
| `volume` | `number` | — | |

### Engine options

#### `source.engine.playerJs`

player.js standardizes the messages an embed answers, not its URL, so there are no shared parameters. Every key under `source.engine.playerJs` is written onto the embed URL’s query as given, for whichever service serves it — anything in [Gumlet’s](https://docs.gumlet.com/video/embed-stream), [Bunny Stream’s](https://bunny.net/docs/stream/embedding), or [Livid’s](https://support.livid.com/article/46-advanced-embedding-parameters) parameter lists, for example. Values are stringified (`true` becomes `'true'`), an array repeats the parameter once per value, and `null` removes a parameter the URL would otherwise carry. These override both `src` and the parameters the element derives from its props. Changing them reloads the embed. [Media Sources](../../guides/media-sources.md) covers how engine options fit into a structured source.

`referrerPolicy` never reaches the URL, and `<playerjs-video>` doesn’t apply it to its iframe either; it takes effect only in the React component.

Prefer the `autoplay`, `loop`, `muted`, `controls`, and `preload` props over their URL spellings: the props also reach the embed over the protocol and keep the element’s state in step with it.

```ts
const video = document.querySelector('playerjs-video');
video.controls = true;
video.source = {
  src: 'https://play.gumlet.io/embed/64bfb0913ed6e5096d66dc1e',
  engine: { playerJs: { player_color: '#ff0000', disabled_player_control: ['cast', 'pip'] } },
};
```

| Option | Type | Description |
| --- | --- | --- |
| `referrerPolicy` | `ReferrerPolicy \| undefined` | `referrerpolicy` for the embed iframe. Not written to the URL. |

### Methods

Supports these media methods: `exitFullscreen`, `load`, `pause`, `play`, `requestFullscreen`, `supports`.

### Events

Implements these standard media events through the embedded player: `durationchange`, `emptied`, `ended`, `error`, `loadedmetadata`, `loadstart`, `pause`, `play`, `playing`, `progress`, `ratechange`, `seeked`, `seeking`, `timeupdate`, `volumechange`.

Also emits these Video.js-specific events:

| Event | Description |
| --- | --- |
| `loadcomplete` | |
| `sourcechange` | Fired when `source` changes, either directly or by resolving a new `src`. Read `source` for the new value. |