FrameworkStyle

PlayerController

Reactive controller for accessing player store state in HTML custom elements

Import

import { PlayerController } from '@videojs/html';

Usage

PlayerController is a reactive controller that consumes the player store from context. It has two overloads:

Without selector — returns the store instance directly. Does NOT subscribe to changes. Use this to access store actions.

import { PlayerController, MediaElement, playerContext } from '@videojs/html';

class VolumeControl extends MediaElement {
  #player = new PlayerController(this, playerContext);

  handleClick() {
    this.#player.value?.setVolume(0.5);
  }
}

With selector — returns the selected value and subscribes to changes. Triggers a host update when the selected value changes (using shallow equality).

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

class PlayButton extends MediaElement {
  #playback = new PlayerController(this, context, selectPlayback);

  render() {
    const playback = this.#playback.value;
    // playback is the selected state slice
  }
}

Access the current value via the .value getter, which returns undefined if the controller is not yet connected to a provider.

Examples

Basic Usage

Paused: Yes | Time: 0.0s | Volume: 100%
<demo-ctrl-player class="html-player-controller-basic">
    <video
        src="https://stream.mux.com/lhnU49l1VGi3zrTAZhDm9LUUxSjpaPW9BL4jY25Kwo4/highest.mp4"
        autoplay
        muted
        playsinline
    ></video>
    <div class="html-player-controller-basic__panel">
        <demo-ctrl-actions class="html-player-controller-basic__actions">
            <button class="action-play">Play</button>
            <button class="action-pause">Pause</button>
            <button class="action-volume">50% Volume</button>
        </demo-ctrl-actions>
        <demo-ctrl-state class="html-player-controller-basic__state">
            <span class="state-text">Paused: Yes | Time: 0.0s | Volume: 100%</span>
        </demo-ctrl-state>
    </div>
</demo-ctrl-player>

API Reference

Overload 1

Parameters

Parameter Type Default
host* PlayerControllerHost
context* PlayerContext<Store>

Return Value

Property Type
value Result | undefined

Overload 2

Parameters

Parameter Type Default
host* PlayerControllerHost
context* PlayerContext<Store>
selector* Selector<InferStoreState<Store>, Result>

Return Value

Property Type
value Result | undefined
VideoJS