createPlayer
Factory function that creates a player instance with typed store, mixins, and controller for HTML custom elements
Import
import { createPlayer, features } from '@videojs/html';Usage
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 everything needed to build a player: a pre-composed PlayerElement, typed PlayerController, context, individual mixins, and a create factory.
import { createPlayer, features, MediaElement } from '@videojs/html';
const {
PlayerElement,
PlayerController,
context,
PlayerMixin,
ProviderMixin,
ContainerMixin,
create,
} = createPlayer({ features: features.video });
// Register the pre-composed player element
customElements.define('video-player', PlayerElement); The simplest approach is to register PlayerElement directly. For custom behavior, use the individual mixins to compose your own element classes.
Examples
Basic Usage
<demo-video-player class="html-create-player-basic">
<video
src="https://stream.mux.com/lhnU49l1VGi3zrTAZhDm9LUUxSjpaPW9BL4jY25Kwo4/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>
</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,
features,
MediaElement,
selectPlayback,
} from '@videojs/html';
const { PlayerElement, PlayerController, context } = createPlayer({
features: [...features.video],
});
class VideoPlayer extends PlayerElement {
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
Overload 1
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 | () => Store | |
| ||
PlayerController | PlayerController.Constructor<Store> | |
| ||
PlayerElement | PlayerElementConstructor<Store> | |
| ||
PlayerMixin | PlayerMixin<Store> | |
| ||
ProviderMixin | ProviderMixin<Store> | |
| ||
ContainerMixin | ContainerMixin<Store> | |
| ||
Overload 2
Parameters
| Parameter | Type | Default | |
|---|---|---|---|
config* | CreatePlayerConfig<AudioFeatures> | — |
Return Value
| Property | Type | |
|---|---|---|
context | PlayerContext<Store> | |
| ||
create | () => Store | |
| ||
PlayerController | PlayerController.Constructor<Store> | |
| ||
PlayerElement | PlayerElementConstructor<Store> | |
| ||
PlayerMixin | PlayerMixin<Store> | |
| ||
ProviderMixin | ProviderMixin<Store> | |
| ||
ContainerMixin | ContainerMixin<Store> | |
| ||
Overload 3
Parameters
| Parameter | Type | Default | |
|---|---|---|---|
config* | CreatePlayerConfig<Features> | — |
Return Value
| Property | Type | |
|---|---|---|
context | PlayerContext<Store> | |
| ||
create | () => Store | |
| ||
PlayerController | PlayerController.Constructor<Store> | |
| ||
PlayerElement | PlayerElementConstructor<Store> | |
| ||
PlayerMixin | PlayerMixin<Store> | |
| ||
ProviderMixin | ProviderMixin<Store> | |
| ||
ContainerMixin | ContainerMixin<Store> | |
| ||