ReferencePlayer
useMedia
Hook to access the media attached to the nearest Player
useMedia returns the media attached to the nearest Player, or null until a media component mounts inside it.
Import
createPlayer also returns useMedia alongside Player and usePlayer:
Both forms are the same hook and return the same Media | null type. Unlike usePlayer, the createPlayer form doesn’t add typing from your features.
Usage
Call useMedia from a component rendered inside Player. Calling it outside Player throws, because it reads the nearest Player’s context.
The component re-renders when a different media attaches or the current one detaches, not when the media’s own properties change. To render from playback state, such as paused or currentTime, select it with usePlayer.
The Media object
Media is the player’s handle on whatever plays the content. What that handle is depends on the media component:
VideoandAudioattach the rendered native<video>or<audio>element.- Components that front a playback engine or an embed, such as
HlsVideo,DashVideo, orYouTubeVideo, attach an object that implements the media contract on top of that engine.
Because media vary this much, the Media type guarantees only a play() method and the addEventListener, removeEventListener, and dispatchEvent event methods. Everything else, like pausing, seeking, volume, or text tracks, is a capability that a given media may or may not have. Narrow Media with the media capability guards before using those members.
Examples
Basic usage
Read the attached media’s source and video dimensions, narrowing with isMediaSourceCapable and isMediaVideoDimensionsCapable first.