VideoPlayer
Browser-native video decoding with progress, buffering, chapters, captions, playback speed, full screen and picture-in-picture. Sources must use a format and codec that the target browser can play directly.
Import
import { HuiVideoPlayer } from '@nscode/handy-ui'
import type { VideoPlayerExpose, VideoPlayerSource, VideoPlayerTrack } from '@nscode/handy-ui'
Examples
Playback, chapters, captions and events
Click the video to play or pause. Double-click to toggle full screen. Captions use WebVTT; chapter markers appear after a finite duration is available.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiVideoPlayer } from '@nscode/handy-ui'
import type { VideoPlayerMarker, VideoPlayerTrack } from '@nscode/handy-ui'
const lastEvent = ref('Not played yet')
const time = ref(0)
const markers: VideoPlayerMarker[] = [
{ time: 0.5, label: 'Color change' },
{ time: 2, label: 'Closing scene' }
]
const tracks: VideoPlayerTrack[] = [
{ src: '/media/demo-captions.vtt', srclang: 'en', label: 'English', default: true }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiVideoPlayer
src="/media/demo-color.mp4"
poster="/media/demo-poster.svg"
:markers="markers"
:tracks="tracks"
:skip-seconds="1"
show-download
@play="lastEvent = 'play: playing'"
@pause="lastEvent = 'pause: paused'"
@ended="lastEvent = 'ended: finished'"
@timeupdate="time = $event"
@ratechange="lastEvent = `ratechange: ${$event}×`"
@volumechange="lastEvent = `volumechange: ${Math.round($event * 100)}%`"
@fullscreen-change="lastEvent = `fullscreen-change: ${$event}`"
@pip-change="lastEvent = `pip-change: ${$event}`"
@error="lastEvent = `error: ${$event?.code ?? 0}`"
/>
<p aria-live="polite">{{ lastEvent }}; {{ time.toFixed(1) }} seconds</p>
</HuiSpace>
</template>
Aspect ratio and object fit
aspect-ratio="auto" uses the video's intrinsic ratio. Other values accept a positive number or a ratio of positive numbers. Invalid values fall back to 16 / 9. contain keeps the whole frame, cover crops to fill, and fill stretches it.
<script setup lang="ts">
import { HuiSpace, HuiVideoPlayer } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<p>16 / 9, contain</p>
<HuiVideoPlayer src="/media/demo-color.mp4" poster="/media/demo-poster.svg" />
<p>4 / 3, cover</p>
<HuiVideoPlayer
src="/media/demo-color.mp4"
poster="/media/demo-poster.svg"
aspect-ratio="4 / 3"
object-fit="cover"
/>
<p>1 / 1, fill</p>
<HuiVideoPlayer
src="/media/demo-color.mp4"
poster="/media/demo-poster.svg"
aspect-ratio="1 / 1"
object-fit="fill"
/>
<p>auto, contain</p>
<HuiVideoPlayer
src="/media/demo-color.mp4"
poster="/media/demo-poster.svg"
aspect-ratio="auto"
/>
</HuiSpace>
</template>
Multiple sources and native controls
The browser selects a playable source in order. native-controls uses the browser's controls and omits the custom play button and control bar. Video clicks, double-clicks and keyboard behavior are handled by the browser in this mode.
<script setup lang="ts">
import { HuiSpace, HuiVideoPlayer } from '@nscode/handy-ui'
import type { VideoPlayerSource } from '@nscode/handy-ui'
const sources: VideoPlayerSource[] = [{ src: '/media/demo-color.mp4', type: 'video/mp4' }]
</script>
<template>
<HuiSpace direction="vertical" fill>
<p>Custom controls, preload=metadata</p>
<HuiVideoPlayer :src="sources" poster="/media/demo-poster.svg" preload="metadata" />
<p>Native controls, preload=none</p>
<HuiVideoPlayer
:src="sources"
poster="/media/demo-poster.svg"
native-controls
preload="none"
crossorigin="anonymous"
/>
<p>Native controls, preload=auto</p>
<HuiVideoPlayer :src="sources" native-controls preload="auto" :playsinline="false" />
</HuiSpace>
</template>
Compact layout, muted autoplay and exclusive playback
Below 480px, skip, mute, speed, captions, picture-in-picture and download move into a More menu. Menus and submenus stay inside the player's DOM. The control bar fades out during idle playback and reappears on pointer movement, touch, keyboard input or control focus. Paused and buffering media keep the controls visible.
exclusive only pauses other participating media in the same document after a real play event. Autoplay remains subject to browser policy; combine it with muted.
<script setup lang="ts">
import { HuiSpace, HuiVideoPlayer } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<div style="width: 340px; max-width: 100%">
<HuiVideoPlayer
src="/media/demo-color.mp4"
poster="/media/demo-poster.svg"
autoplay
muted
loop
exclusive
:skip-seconds="1"
:controls-hide-delay="1500"
show-download
/>
</div>
<HuiVideoPlayer
src="/media/demo-color.mp4"
poster="/media/demo-poster.svg"
exclusive
:controls-hide-delay="0"
:show-volume="false"
:show-fullscreen="false"
:show-pip="false"
:playback-rates="[]"
/>
</HuiSpace>
</template>
Methods and disabled state
Disabled media pause, lose native controls, and leave the Tab order together with internal controls. Public play, toggle, seek and full-screen entry are blocked. pause and exit from this player's own full screen remain available.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCheckbox, HuiSpace, HuiVideoPlayer } from '@nscode/handy-ui'
import type { VideoPlayerExpose } from '@nscode/handy-ui'
const player = ref<VideoPlayerExpose>()
const disabled = ref(false)
const result = ref('No method called yet')
const inspect = () => {
result.value = `Native element: ${player.value?.getMediaElement()?.tagName ?? 'not mounted'}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiCheckbox v-model="disabled" label="Disabled" />
<HuiButton :disabled="disabled" @click="player?.play()">Play</HuiButton>
<HuiButton @click="player?.pause()">Pause</HuiButton>
<HuiButton :disabled="disabled" @click="player?.toggle()">Play / pause</HuiButton>
<HuiButton :disabled="disabled" @click="player?.seek(1)">Seek to 1s</HuiButton>
<HuiButton :disabled="disabled" @click="player?.enterFullscreen()"
>Enter full screen</HuiButton
>
<HuiButton @click="player?.exitFullscreen()">Exit this player's full screen</HuiButton>
<HuiButton @click="inspect">Read native element</HuiButton>
</HuiSpace>
<HuiVideoPlayer
ref="player"
src="/media/demo-color.mp4"
poster="/media/demo-poster.svg"
:disabled="disabled"
/>
<p>{{ result }}</p>
</HuiSpace>
</template>
Slots and error retry
Use overlay for watermarks or application content. Inputs, buttons and editable content in slots do not trigger player shortcuts. controls-extra adds actions to the control bar. The error slot receives the native error and a retry function that reloads the current media.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiVideoPlayer } from '@nscode/handy-ui'
const note = ref('No scene recorded')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiVideoPlayer src="/media/demo-color.mp4" poster="/media/demo-poster.svg">
<template #overlay
><span style="position: absolute; top: 8px; right: 8px">HandyUI demo</span></template
>
<template #controls-extra
><HuiButton size="small" variant="text" @click="note = 'Current scene recorded'"
>Record</HuiButton
></template
>
</HuiVideoPlayer>
<p>{{ note }}</p>
<HuiVideoPlayer src="/media/not-found.mp4">
<template #error="{ error, retry }"
><span>Video unavailable: {{ error?.code }}</span
><HuiButton @click="retry">Reload current video</HuiButton></template
>
</HuiVideoPlayer>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| src | string | VideoPlayerSource[] | — | URL or candidate sources; new arrays with equal content do not reload |
| poster | string | — | Poster image |
| autoplay | boolean | false | Request autoplay; the browser may block it |
| muted | boolean | false | Applied initially and when externally changed; actual state follows native events |
| loop | boolean | false | Native loop |
| preload | 'none' | 'metadata' | 'auto' | 'metadata' | Preload strategy |
| aspectRatio | string | '16 / 9' | Positive ratio or auto |
| objectFit | 'contain' | 'cover' | 'fill' | 'contain' | Frame fitting |
| playbackRates | number[] | [0.5, 0.75, 1, 1.25, 1.5, 2] | Positive finite rates; empty hides the speed menu |
| tracks | VideoPlayerTrack[] | [] | Native caption sources |
| markers | VideoPlayerMarker[] | [] | Chapters; invalid or out-of-range times are ignored |
| showVolume | boolean | true | Volume controls |
| showFullscreen | boolean | true | Full-screen entry, hidden when unsupported |
| showPip | boolean | true | Picture-in-picture entry, hidden when unsupported |
| showDownload | boolean | false | Download entry; browser and server restrictions apply |
| skipSeconds | number | 0 | Positive finite skip interval |
| controlsHideDelay | number | 3000 | Idle delay; nonpositive or nonfinite values keep controls visible |
| nativeControls | boolean | false | Browser-native controls |
| exclusive | boolean | false | Exclusive playback within the same document |
| disabled | boolean | false | Pause and disable user operations |
| crossorigin | '' | 'anonymous' | 'use-credentials' | — | Forwarded to video |
| playsinline | boolean | true | Request inline playback on mobile |
Events
| Event | Parameters | Description |
|---|---|---|
| play / pause / ended | — | Corresponding real media event |
| timeupdate | (currentTime: number, duration: number) | Actual time and duration; unknown duration may be NaN / Infinity |
| ratechange | (rate: number) | Actual playback rate |
| volumechange | (volume: number, muted: boolean) | Actual volume and mute state |
| fullscreen-change | (fullscreen: boolean) | State confirmed by native full-screen events |
| pip-change | (pip: boolean) | State confirmed by native picture-in-picture events |
| error | (error: MediaError | null) | Native media error; playback-policy rejection is excluded |
TypeScript event names are fullscreenChange and pipChange; templates accept the kebab-case forms above.
Slots
| Slot | Parameters | Description |
|---|---|---|
| overlay | — | Content over the video, available with native controls too |
| controls-extra | — | Right-side custom controls, omitted in native mode |
| error | { error: MediaError | null, retry: () => void } | Custom error UI; native mode uses the browser's error UI |
Methods
| Method | Type | Description |
|---|---|---|
| play | () => Promise<void> | Request playback without predicting success; policy rejection and stale requests finish safely |
| pause | () => void | Pause |
| toggle | () => Promise<void> | Toggle using actual element state |
| seek | (seconds: number) => void | Seek within a known finite duration |
| enterFullscreen | () => Promise<void> | Full-screen root; falls back to native video full screen on supported iOS browsers |
| exitFullscreen | () => Promise<void> | Exit only full screen owned by this instance |
| getMediaElement | () => HTMLVideoElement | undefined | Read the current native element |
Types
interface VideoPlayerSource {
src: string
type?: string
}
interface VideoPlayerTrack {
src: string
srclang: string
label: string
kind?: 'subtitles' | 'captions'
default?: boolean
}
interface VideoPlayerMarker {
time: number
label?: string
}
Also exports VideoPlayerProps, VideoPlayerEmits, VideoPlayerExpose, VideoPlayerSlots and VideoPlayerErrorSlotProps.