Media Player
A fully featured media player component supporting video and audio playback with custom controls.
Installation
Prerequisites: Before installing the media player component, you'll need to update your existing shadcn/ui components to support portal containers.
DropdownMenu
function DropdownMenuContent({
className,
+ container,
sideOffset = 4,
...props
-}: React.ComponentProps<typeof DropdownMenuPrimitive.Content>) {
+}: React.ComponentProps<typeof DropdownMenuPrimitive.Content> & {
+ container?: DropdownMenuPrimitive.DropdownMenuPortalProps["container"];
+}) {
return (
- <DropdownMenuPrimitive.Portal>
+ <DropdownMenuPrimitive.Portal container={container}>Tooltip
function TooltipContent({
className,
sideOffset = 0,
+ container,
children,
...props
-}: React.ComponentProps<typeof TooltipPrimitive.Content>) {
+}: React.ComponentProps<typeof TooltipPrimitive.Content> & {
+ container?: TooltipPrimitive.TooltipPortalProps["container"];
+}) {
return (
- <TooltipPrimitive.Portal>
+ <TooltipPrimitive.Portal container={container}>After completing the prerequisite component updates, install the media player:
pnpm dlx shadcn@latest add @diceui/media-playerLayout
Import the parts, and compose them together.
import {
MediaPlayer,
MediaPlayerVideo,
MediaPlayerAudio,
MediaPlayerLoading,
MediaPlayerError,
MediaPlayerVolumeIndicator,
MediaPlayerControls,
MediaPlayerControlsOverlay,
MediaPlayerPlay,
MediaPlayerSeekBackward,
MediaPlayerSeekForward,
MediaPlayerVolume,
MediaPlayerSeek,
MediaPlayerTime,
MediaPlayerPlaybackSpeed,
MediaPlayerLoop,
MediaPlayerCaptions,
MediaPlayerFullscreen,
MediaPlayerDownload,
MediaPlayerSettings,
} from "@/components/ui/media-player";
return (
<MediaPlayer>
<MediaPlayerVideo>
<source src="..." type="video/mp4" />
</MediaPlayerVideo>
<MediaPlayerLoading />
<MediaPlayerError />
<MediaPlayerVolumeIndicator />
<MediaPlayerControls>
<MediaPlayerControlsOverlay />
<MediaPlayerPlay />
<MediaPlayerSeekBackward />
<MediaPlayerSeekForward />
<MediaPlayerVolume />
<MediaPlayerSeek />
<MediaPlayerTime />
<MediaPlayerPlaybackSpeed />
<MediaPlayerLoop />
<MediaPlayerCaptions />
<MediaPlayer.PiP />
<MediaPlayer.Fullscreen />
<MediaPlayer.Download />
</MediaPlayer.Controls>
</MediaPlayer.Root>
)Examples
Audio Player
Use the Audio component instead of Video for audio playback.
With Settings Menu
Media player with a settings menu with playback speed, caption option, and resolution selector.
HLS Playback
Media player with HLS (HTTP Live Streaming) support for adaptive bitrate streaming.
This example demonstrates using the Mux Video React package as the video element to enable HLS playback.
With Error Handling
Media player with custom error handling and retry functionality.
With Playlist
A media player example that includes a playlist, similar to music streaming applications.
API Reference
MediaPlayer
The main container component for the media player.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-controls-visible] | Present when the controls are visible. |
[data-disabled] | Present when the media player is disabled. |
[data-state] | "fullscreen" | "windowed" |
MediaPlayerVideo
The video element, integrated with the player state.
Prop
Type
MediaPlayerAudio
The audio element, integrated with the player state.
Prop
Type
MediaPlayerControls
A container for the media player controls.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the controls are disabled. |
[data-state] | "fullscreen" | "windowed" |
[data-visible] | Present when the controls are visible. |
ControlsOverlay
Displays as a subtle backdrop that improves readability for media controls.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-state] | "fullscreen" | "windowed" |
[data-visible] | Present when the controls are visible. |
MediaPlayerLoading
A loading indicator that appears when media is loading, similar to YouTube/Netflix style.
Prop
Type
MediaPlayerError
An error component that displays when media playback encounters an error, with retry and reload options.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-state] | "fullscreen" | "windowed" |
VolumeIndicator
A volume indicator that appears when the volume is changed with keyboard interactions.
Prop
Type
MediaPlayerPlay
A button to play or pause the media.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
[data-state] | "on" | "off" |
SeekBackward
A button to seek backward in the media.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
SeekForward
A button to seek forward in the media.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
MediaPlayerSeek
A slider component to seek through the media playback.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the slider is disabled. |
[data-hovering] | Present when the seek slider is being actively hovered. |
[data-slider=''] | Present on the seek slider. |
[data-slot='media-player-seek-container'] | The container for the seek slider. |
[data-slot='media-player-seek'] | The root for the seek slider. |
[data-slot='media-player-seek-buffered'] | The buffered progress indicator within the track. |
[data-slot='media-player-seek-hover-range'] | The hover preview range indicator. |
[data-slot='media-player-seek-chapter-separator'] | Chapter separators displayed on the track. |
[data-slot='media-player-seek-thumbnail'] | The thumbnail preview container in the tooltip. |
[data-slot='media-player-seek-chapter-title'] | The chapter title in the tooltip. |
[data-slot='media-player-seek-time'] | The time display in the tooltip. |
Styling
<MediaPlayer.Seek
className={
cn(
"[&_[data-slot='media-player-seek-buffered']]:bg-primary/60",
"[&_[data-slot='media-player-seek-hover-range']]:bg-primary/70",
"[&_[data-slot='media-player-seek-chapter-separator']]:w-1 [&_[data-slot='media-player-seek-chapter-separator']]:bg-muted",
"[&_[data-slot='media-player-seek-thumbnail']]:border-2 [&_[data-slot='media-player-seek-thumbnail']]:border-ring"
)
}
/>MediaPlayerVolume
A slider component to control the media volume.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the volume control is disabled. |
[data-slider=''] | Present on the volume slider. |
[data-slot='media-player-volume-container'] | The container for the volume slider. |
[data-slot='media-player-volume-trigger'] | The trigger for the volume slider. |
[data-slot='media-player-volume'] | The root for the volume slider. |
[data-state] | "on" | "off" |
MediaPlayerTime
Displays the current time, duration, or remaining time of the media.
Prop
Type
PlaybackSpeed
A select dropdown to control the media playback speed.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the select is disabled. |
MediaPlayerLoop
A button to toggle loop mode.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
[data-state] | "on" | "off" |
MediaPlayerFullscreen
A button to toggle fullscreen mode.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
[data-state] | "on" | "off" |
PiP (Picture in picture)
A button to toggle picture in picture mode (video only).
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
[data-state] | "on" | "off" |
MediaPlayerCaptions
A button to toggle captions or subtitles (video only).
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
[data-state] | "on" | "off" |
MediaPlayerDownload
A button to download the media file.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
MediaPlayerSettings
A dropdown menu with playback speed selector, caption selector, and resolution selector.
Prop
Type
Features:
- Playback speed control with customizable speeds
- Video quality/resolution selection (when available)
- Captions/subtitles control
- Organized in a clean dropdown interface
<MediaPlayer.Settings
speeds={[0.5, 0.75, 1, 1.25, 1.5, 2]}
/>| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the button is disabled. |
Accessibility
Keyboard Interactions
| Key | Description |
|---|---|
| SpaceK | Toggles play/pause. |
| ArrowRight | Seeks forward 5 seconds (video only). |
| ArrowLeft | Seeks backward 5 seconds (video only). |
| J | Seeks backward 10 seconds (video only). |
| L | Seeks forward 10 seconds (video only). |
| Shift + ArrowRight | Seeks forward 5 seconds (audio only). |
| Shift + ArrowLeft | Seeks backward 5 seconds (audio only). |
| 0123456789 | Seeks to 0%-90% of duration. |
| Home | Seeks to beginning. |
| End | Seeks to end. |
| ArrowUp | Increases volume (video only). |
| ArrowDown | Decreases volume (video only). |
| M | Toggles mute. |
| R | Toggles repeat mode. |
| F | Toggles fullscreen. |
| Escape | Exits fullscreen when in fullscreen mode. |
| > | Increases playback speed. |
| < | Decreases playback speed. |
| C | Toggles captions (video only, if available). |
| P | Toggles Picture-in-Picture. |
| D | Downloads the media file (if download button is present). |
Credits
- FASSounds - For the audio file used in basic examples.
- Pika - For the video file used in basic examples.
- Elephants Dream - Open movie by the Blender Foundation, used in settings demo under Creative Commons Attribution 2.5.
- Mux - For HLS video streaming and VTT files used in HLS and settings demos.
- Media Chrome - For VTT chapter and caption files used in examples.
- RandomMind - For "Medieval: Battle" track used in playlist demo, available under CC0 from OpenGameArt.org.
- The Lemming Shepherds - For "City Lights" track used in playlist demo.
- Picsum - For placeholder cover images used in playlist demo.