Player
import { AudioHolder, MiniPlayer, PlayerContextProvider, VideoPlayer } from "@arclockproject/common/components/Player";
For examples and live demos, see the Player guide.
AudioHolder
Component
Hidden <audio> element that plays the loaded entry's separate audio file.
Keep it mounted (e.g. next to MiniPlayer) so audio keeps playing when the
VideoPlayer is unmounted. Must be inside PlayerContextProvider.
Type parameters <metaType, listMetaType>
Props
None.
MiniPlayer
Component
Small player bar fixed to the bottom-right of the screen, with playback
controls, a progress bar and a "Resume" button that calls navigate.
Clicking it opens a larger panel, which also shows children at the bottom.
Shown while usePlayer().internal.miniPlayer is true. Must be inside
PlayerContextProvider.
Props
children?(ReactNode)
PlayerContextProvider
Component
Holds the player state shared by VideoPlayer, MiniPlayer and
AudioHolder. Put it high in the tree, e.g. around your router, so playback
survives page changes. Only its children can use the player.
metaType is the type of each entry's meta, and listMetaType the type of
playlistMeta.
Type parameters <metaType, listMetaType>
Props
-
touchHistory?((video: videoData<metaType>) => void) — Called when the player moves to another entry without opening its page, e.g. the next playlist entry while the mini player is shown. Use it to record watch history. -
navigate?((link: videoData<metaType>) => void) — Called when the player wants to open an entry's page, e.g. the mini player's "Resume" button or the next playlist entry. Use it to route. -
settings?(settings) — If passed, the player uses these settings instead of its own.usePlayer().internal.settings.setthen has no effect, so update this prop to change them. -
children?(ReactNode)
Example
<PlayerContextProvider navigate={(video) => router.push(`/watch/${video.id}`)}>
<App />
<MiniPlayer />
<AudioHolder />
</PlayerContextProvider>
VideoPlayer
Component
The main video player: 16:9 video with controls, progress bar, chapters,
heatmap, subtitles, speed, fullscreen and audio-only mode. It plays whatever
is loaded in the player state, so it can be unmounted and mounted again
without stopping (keep AudioHolder mounted for that). Must be inside
PlayerContextProvider.
Changes to videoData.id will make the player restart from 0 and play that instead.
Type parameters <metaType, listMetaType>
Props
videoData?(videoData<metaType>) — Entry to load when this player mounts or theidchanges. Leave it out to show whatever is already loaded.isPlaylist(boolean) —falseclears the current playlist, so only this entry plays.errorDisplay?(string) — Not used yet.chapters(videoChapters[]) — Chapters to mark on the progress bar.heatmap(Required<videoHeatmap>[]) — "Most watched" graph to draw above the progress bar.ScrollTriggerDistance?(number) — Not used yet. Usesettings.stickyTriggerDistanceinstead.showInlineTitle?(boolean) — Show the title over the video. It is always shown in fullscreen.
Example
<VideoPlayer videoData={{
id: "my-id-300";
title: "Cat video";
files: {
video: "/videos/cat-300.mp4";
audio: "/videos/cat-300.mp3";
subtitles: "/videos/cat-300-commentary.vtt";
thumbnail: "/videos/cat-300.png";
};
meta: { ... }; // Your data or null
}} isPlaylist={false} chapters={[]} heatmap={[]} />
videoData
Type · not exported, used by the API above
type videoData<metaType>
One video or song the player can load.
Properties
id(string) — Unique id. Loading a different id resets the progress.title(string) — Title shown in the player and mini player.files(object) — URLs of the data.video(string) — URL of the video file.audio(string | null) — URL of a separate audio file, ornullif the audio is in the video. With a separate file, audio can keep playing when the video is unmounted.subtitles(string | null) — URL of a subtitle track, ornull.thumbnail(string) — URL of the thumbnail image.
meta(null | metaType) — Your own data for this entry, ornull.
settings
Type · not exported, used by the API above
type settings
Player settings. Pass them to PlayerContextProvider's settings prop.
Properties
autoAudioOnly(boolean, defaultfalse) — Switch to audio only when the page is hidden, e.g. in a background tab.videoBackgroundBloom(boolean, defaulttrue) — Draw a soft glow of the video behind the player.autoPlay(boolean, defaultfalse) — Start playing as soon as aVideoPlayerloads a video.allowMiniPlayer(boolean, defaulttrue) — Not used yet.sticky(boolean, defaultfalse) — On small screens, keep the player stuck to the top while scrolling.stickySpacing(number, default0) — Whensticky, the gap above the player in pixels, e.g. for a navigation bar. The gap is removed while scrolling down and comes back when scrolling up.stickyTriggerDistance(number, default0) — How far to scroll, in pixels, before thestickySpacinggap changes.
videoChapters
Type · not exported, used by the API above
type videoChapters
A chapter, shown as a mark on the progress bar.
Properties
start_time(number) — Chapter start, in seconds.title(string) — Chapter name.end_time(number) — Chapter end, in seconds.
videoHeatmap
Type · not exported, used by the API above
type videoHeatmap
One segment of the "most watched" graph drawn above the progress bar.
Properties
id(string) — Unique identifier.start_time(number) — Segment start, in seconds.end_time(number) — Segment end, in seconds.value_time(number) — How much this segment is watched, relative to the other segments.
PlayerContextProps
Type · not exported, used by the API above
type PlayerContextProps<metaType, listMetaType>
What usePlayer returns. Everything is under internal, and can change
between versions.
Properties
internal(object)-
refVideo(React.RefObject<HTMLVideoElement | null>) -
refAudio(React.RefObject<HTMLAudioElement | null>) -
syncAll((time: number, playing: boolean, wasDeSync?: boolean) => void) — Seeks video and audio totime(seconds) and keeps them in sync.synchronizingistrueuntil both are ready to play.Smaller video files tend to sync better. For instance, 4k video tends to always sync with 200ms-400ms desync.
There is 0ms desync if time is set to 0.
time— Where to seek to, in seconds.playing— Play once ready. Withfalse, it does not start playing.wasDeSync— Iftruean internal error counter will go up. Once it reaches to 10 in 2 seconds, it will automatically toggle player to audio only mode. This is called by player itself when audio and video drift apart too much.
-
synchronizing(contextState<boolean>) — If the player is currently attempting to synchronize video and audio. -
playSpeed(contextState<number>) — Playback speed, where1is normal. -
volume(contextState<number>) — Volume from 0 to 1. Saved inlocalStorage. -
sourceOverride(contextState<string>) -
distortAudio(contextState<boolean>) — The passed value ofpreservesPitchto player's<audio>and<video>. -
playerData(contextState<videoData<metaType> | undefined>) — The loaded entry. Set it to play something else. -
audioOnly(contextState<boolean>) — Hide the video and play only the audio. Saved inlocalStorage. -
playing(contextState<boolean>) — Whether it is playing. Set it to play or pause. -
currentPosition(contextState<number>) — Current time, in seconds. -
maxDuration(contextState<number>) — Length of the loaded entry, in seconds. -
miniPlayer(contextState<boolean>) — WhetherMiniPlayeris shown. -
miniPlayerVisible(contextState<boolean>) -
muted(contextState<boolean>) — If video and audio are muted. Does not alter the actual volume parameter. -
videoBuffer(contextState<number>) — Available butter in seconds -
audioBuffer(contextState<number>) — Available butter in seconds -
singleLoop(contextState<boolean>) — Repeat the current entry instead of moving to the next in the playlist. -
playlistMeta(contextState<listMetaType | undefined>) — Your own data for the playlist. -
playlistContent(contextState<videoData<metaType>[]>) — Entries played one after another. Empty when not playing a playlist. -
chapters(contextState<videoChapters[]>) -
heatmap(contextState<videoHeatmap[]>) -
audioControlled(boolean) —truewhen the separate audio file drives playback, rather than the video.Note: Tested behaviour with
true. Behaviour withfalserequires testing and might not behave as expected. -
settings(contextState<settings>) — Player settings. -
touchHistory((video: videoData<metaType>) => void) — ThetouchHistoryprop ofPlayerContextProvider, or a no-op. -
navigate((link: videoData<metaType>) => void) — Thenavigateprop ofPlayerContextProvider, or a no-op. -
actionOnEnded((event: React.SyntheticEvent<HTMLAudioElement, Event>) => Promise<void>) — Event called when current play ends. -
actionOnLoadStart((event: React.SyntheticEvent<HTMLAudioElement, Event>) => void) — Event called when current play start to load. -
actionOnDurationChange((event: React.SyntheticEvent<HTMLAudioElement, Event>) => void) — Event called when duration changes. Mainly when video finally loads. -
actionOnTimeUpdate((event: React.SyntheticEvent<HTMLAudioElement, Event>, type: "audio" | "video") => void) — Event called every second when playing with who the current controller is. -
actionOnError(() => void) — General error handler -
targetControllerRef(React.RefObject<HTMLAudioElement | null> | React.RefObject<HTMLVideoElement | null>) — Ref to current controller element
-
contextState
Type · not exported, used by the API above
type contextState<k>
A piece of player state with its setter, like the pair useState returns.
Properties
value(k)set(React.Dispatch<React.SetStateAction<k>>)