createPlayer
Factory function that creates a player instance with typed store, mixins, and controller for HTML custom elements
createPlayer is the entry point for setting up a Video.js player with HTML custom elements. It accepts a configuration object with a features array and returns a typed PlayerController , context, ProviderMixin, ContainerMixin, and a create store factory.
import { createPlayer, MediaElement, selectPlayback } from '@videojs/html';
import { videoFeatures } from '@videojs/html/video';
const { ProviderMixin, ContainerMixin, PlayerController, context } = createPlayer({
features: videoFeatures,
});
// Provider element: owns the store, provides context to descendants
class VideoPlayer extends ProviderMixin(MediaElement) {}
customElements.define('video-player', VideoPlayer);
// Control element with selector
class PlayButton extends MediaElement {
#playback = new PlayerController(this, context, selectPlayback);
} PlayerController, ProviderMixin, and ContainerMixin are intended for controller hosts and custom-element composition. Keep wiring minimal in utility examples, and build concrete elements where needed.
Use split elements (recommended for reusable skins/layouts):
import { createPlayer, MediaElement } from '@videojs/html';
import { videoFeatures } from '@videojs/html/video';
const { ProviderMixin, ContainerMixin } = createPlayer({ features: videoFeatures });
class PlayerRoot extends ProviderMixin(MediaElement) {}
class PlayerRegion extends ContainerMixin(MediaElement) {}Use a single composed element when the same element should both own the store and attach media:
import { createPlayer, MediaElement } from '@videojs/html';
import { videoFeatures } from '@videojs/html/video';
const { ProviderMixin, ContainerMixin } = createPlayer({ features: videoFeatures });
class ComposedPlayer extends ProviderMixin(ContainerMixin(MediaElement)) {}Examples
Basic Usage
<demo-video-player class="html-create-player-basic">
<media-container>
<video
slot="media"
src="https://stream.mux.com/BV3YZtogl89mg9VcNBhhnHm02Y34zI1nlMuMQfAbl3dM/highest.mp4"
autoplay
muted
playsinline
></video>
<demo-play-toggle class="html-create-player-basic__button">
<span class="show-when-paused">Play</span>
<span class="show-when-playing">Pause</span>
</demo-play-toggle>
</media-container>
</demo-video-player>
.html-create-player-basic {
position: relative;
}
.html-create-player-basic video {
width: 100%;
}
.html-create-player-basic__button {
padding-block: 8px;
position: absolute;
bottom: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(10px);
color: black;
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 9999px;
padding-inline: 20px;
cursor: pointer;
}
.html-create-player-basic__button .show-when-paused {
display: none;
}
.html-create-player-basic__button .show-when-playing {
display: none;
}
.html-create-player-basic__button[data-paused] .show-when-paused {
display: inline;
}
.html-create-player-basic__button:not([data-paused]) .show-when-playing {
display: inline;
}
import {
applyElementProps,
applyStateDataAttrs,
createButton,
createPlayer,
MediaElement,
selectPlayback,
} from '@videojs/html';
import { videoFeatures } from '@videojs/html/video';
import '@videojs/html/media/container';
const { ProviderMixin, PlayerController, context } = createPlayer({
features: videoFeatures,
});
class VideoPlayer extends ProviderMixin(MediaElement) {
static readonly tagName = 'demo-video-player';
}
class PlayToggle extends MediaElement {
static readonly tagName = 'demo-play-toggle';
readonly #player = new PlayerController(this, context, selectPlayback);
#disconnect: AbortController | null = null;
override connectedCallback(): void {
super.connectedCallback();
this.#disconnect = new AbortController();
const buttonProps = createButton({
onActivate: () => {
const state = this.#player.value;
if (!state) return;
state.paused ? state.play() : state.pause();
},
isDisabled: () => !this.#player.value,
});
applyElementProps(this, buttonProps, this.#disconnect.signal);
}
override disconnectedCallback(): void {
super.disconnectedCallback();
this.#disconnect?.abort();
this.#disconnect = null;
}
protected override update(): void {
super.update();
const state = this.#player.value;
if (!state) return;
applyStateDataAttrs(this, state, { paused: 'data-paused', ended: 'data-ended' });
}
}
customElements.define(VideoPlayer.tagName, VideoPlayer);
customElements.define(PlayToggle.tagName, PlayToggle);
API Reference
Video
Creates a player factory with typed store, mixins, and controller.
Parameters
| Parameter | Type | Default | |
|---|---|---|---|
config* | CreatePlayerConfig<VideoFeatures> | — | |
| |||
Return Value
| Property | Type | |
|---|---|---|
context | PlayerContext<Store> | |
| ||
create | function | |
| ||
PlayerController | PlayerController.Constructor<Store> | |
| ||
ProviderMixin | ProviderMixin<Store> | |
| ||
ContainerMixin | ContainerMixin<Store> | |
| ||
Audio
Creates a player factory for audio media.
Parameters
| Parameter | Type | Default | |
|---|---|---|---|
config* | CreatePlayerConfig<AudioFeatures> | — | |
| |||
Return Value
| Property | Type | |
|---|---|---|
context | PlayerContext<Store> | |
| ||
create | function | |
| ||
PlayerController | PlayerController.Constructor<Store> | |
| ||
ProviderMixin | ProviderMixin<Store> | |
| ||
ContainerMixin | ContainerMixin<Store> | |
| ||
Generic
Creates a player factory with custom features.
Parameters
| Parameter | Type | Default | |
|---|---|---|---|
config* | CreatePlayerConfig<Features> | — | |
| |||
Return Value
| Property | Type | |
|---|---|---|
context | PlayerContext<Store> | |
| ||
create | function | |
| ||
PlayerController | PlayerController.Constructor<Store> | |
| ||
ProviderMixin | ProviderMixin<Store> | |
| ||
ContainerMixin | ContainerMixin<Store> | |
| ||