Overview
TheuseVideoPlayer hook creates a VideoPlayer instance and manages its lifecycle within a React component. It automatically handles player creation, initialization, and cleanup when the component unmounts.
Signature
Parameters
The source of the video to play. Can be:
- A
stringURL (e.g.,'https://example.com/video.mp4') - A
numberfromrequire()(e.g.,require('./video.mp4')) - A
VideoConfigobject with additional configuration - A
VideoPlayerSourceNitro object
Optional callback function to configure the player after creation. This is where you should:
- Set initial player properties (volume, muted, loop, etc.)
- Attach event listeners using
player.addEventListener() - Configure playback settings
- If
initializeOnCreationistrue(default): called when the player starts loading the source (ononLoadStartoronStatusChange) - If
initializeOnCreationisfalse: called immediately when the player is created
initializeOnCreation is true.Return Value
Returns aVideoPlayer instance that can be used to control playback and attach to a VideoView component.
Basic Usage
Simple Video Player
With Setup Callback
With VideoConfig
Advanced Usage
Event Listeners in Setup
Delayed Initialization
VideoConfig Properties
When passing aVideoConfig object as the source:
The video URI or local file from
require()HTTP headers to send with the video request
DRM configuration for protected content
Buffer configuration settings
Custom metadata (title, subtitle, description, artist, imageUri) shown in system media controls
Array of external subtitle tracks
Whether to initialize the native player immediately when created. If
false, you must call player.initialize() manually.Notes
- The hook automatically cleans up the player when the component unmounts
- The player instance is recreated if the source changes
- Event listeners added in the setup callback are automatically managed
- Changes made to the player before the setup callback runs may be overwritten (when
initializeOnCreationis true) - Use the
useEventhook for adding event listeners that depend on React state or props
See Also
- useEvent - Hook for attaching event listeners
- VideoPlayer - VideoPlayer class reference
- VideoView - Component for rendering video