useMedia returns the current HTMLMediaElement registered with the player, or null if no media element has mounted yet. Use it to interact directly with the native media API when you need capabilities that aren’t exposed through the store.
Must be called within a Player.Provider. The media element becomes available after a <video> or <audio> component mounts inside the provider tree.
Parameters
This hook takes no arguments.Return value
The registered
HTMLMediaElement (HTMLVideoElement or HTMLAudioElement), or null if no media element has been registered yet.The element is available after the media component (such as a <video> wrapped with useMediaRegistration) mounts inside the provider. It becomes null again if the media component unmounts.Usage
Basic usage
Guarding against null
The media element isnull before the <video> or <audio> component mounts. Always guard against null before calling media methods:
Standalone import
Also available as a standalone import from@videojs/react. The behavior is identical — no typing difference.
Registering a custom media element
To connect a custom<video> or <audio> element (rather than using a built-in component), use useMediaRegistration. This hook returns the setter that Player.Provider uses to track the active media element.
Rules
- Must be called inside a
Player.Provider. - Returns
nulluntil a media element is registered inside the provider tree. - Re-renders the component when the media element mounts or unmounts.