Skip to content

ReferencePlayer

PlayerController

Reactive controller for accessing player store state in HTML custom elements

Import

import { PlayerController } from "@videojs/html/video";

PlayerController is a reactive controller that consumes the player store from playerContext. Without a selector it returns the store instance directly (no subscription — use this for actions). With a selector it returns the selected value and subscribes to changes, triggering a host update on shallow-equal change. Access the current value via .value, which returns undefined until connected to a player.

The controller exported by each preset, or returned by createPlayer, is already bound to that player’s context, so pass only the host and optional selector. The lower-level PlayerController exported from @videojs/html accepts the context as its second argument; its constructor signatures are documented below.

import { UIElement, selectPlayback } from '@videojs/html';
import { PlayerController } from '@videojs/html/video';

class PlayButtonElement extends UIElement {
  readonly #playback = new PlayerController(this, selectPlayback);
}

The host must be a reactive controller host, such as a subclass of UIElement, so the controller can request updates.

Player context

playerContext is the context every player element provides to its descendants, whether the element comes from a preset or from createPlayer. PlayerController requests it from its host when the host connects, and the nearest ancestor player answers with its store. An element outside a player receives nothing, so value stays undefined.

Pass playerContext to the PlayerController exported from @videojs/html:

import { PlayerController, playerContext, selectPlayback, UIElement } from "@videojs/html";

class PlayButtonElement extends UIElement {
  readonly #playback = new PlayerController(this, playerContext, selectPlayback);
}

With a selector, the selector types the value. Without one, value is typed as the base player store, with no feature state or actions, because playerContext does not know the player’s features. For typed access, use the PlayerController that createPlayer returns, which is already bound to its player, or pass the context createPlayer returns, which is the same context as playerContext typed for its feature set:

import { createPlayer, PlayerController, UIElement } from "@videojs/html";
import { videoFeatures } from "@videojs/html/video";

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

class VolumeResetElement extends UIElement {
  readonly #store = new PlayerController(this, playerContext);

  reset() {
    this.#store.value?.setVolume(1);
  }
}

Examples

Basic Usage

Paused: Yes | Time: 0.0s | Volume: 100%
<demo-ctrl-player class="demo-ctrl-player">
  <media-container>
    <video src="https://stream.mux.com/BV3YZtogl89mg9VcNBhhnHm02Y34zI1nlMuMQfAbl3dM/highest.mp4" autoplay muted playsinline></video>
    <div class="panel">
      <demo-ctrl-actions class="actions">
        <button type="button" class="play">Play</button>
        <button type="button" class="pause">Pause</button>
        <button type="button" class="volume">50% Volume</button>
      </demo-ctrl-actions>
      <demo-ctrl-state class="state">
        <span class="text">Paused: Yes | Time: 0.0s | Volume: 100%</span>
      </demo-ctrl-state>
    </div>
  </media-container>
</demo-ctrl-player>

API Reference

Without Selector

new PlayerController<Store extends PlayerStore, Result = Store>(host, context)

Parameters

ParameterTypeDefaultDetails
host*object—
context*Context<symbol, Store>—

Return Value

PropertyType
valueResult | undefined
displayNamestring | undefined

With Selector

new PlayerController<Store extends PlayerStore, Result = Store>(host, context, selector)

Parameters

ParameterTypeDefaultDetails
host*object—
context*Context<symbol, Store>—
selector*object—

Return Value

PropertyType
valueResult | undefined
displayNamestring | undefined