VideoProvider
The VideoProvider is a component that manages the state of the video player and makes it available to all its children.
Usage
To use the VideoProvider, you need to wrap your component with it. This is usually done in the component that contains the video player.
import { VideoProvider } from 'react-native-video-toolkit';
export const VideoPlayerComponent = () => <VideoProvider>{/* Your video player content */}</VideoProvider>;Props
| Prop | Type | Default | Description |
|---|---|---|---|
config | Partial<VideoPlayerConfig> | {} | The configuration for the video player. |
theme | Partial<Theme> | {} | The theme for the video player. |
components | Partial<VideoComponents> | {} | Replacements for the sheet, sliders, spinner and press feedback the player renders. See Custom Components. |
portalHost | string | – | Name of a VideoPortalHost to open the settings sheet in. When left out, the provider renders its own host after its children. |
children | ReactNode | required | The children to render. |
VideoPortalHost
The settings sheet opens in a portal host that VideoProvider renders after its children. If the provider sits inside a screen (for example one tab of a tab navigator), the sheet is drawn inside that screen, so the navigator's tab bar or header can end up on top of it.
To open the sheet above your navigation instead, render a VideoPortalHost at the app root, after the navigator, and pass its name to portalHost. One host can serve every player in the app.
import { Slot } from 'expo-router';
import { DefaultLayout, VideoPlayer, VideoPortalHost, VideoProvider } from 'react-native-video-toolkit';
// App root, e.g. app/_layout.tsx
export default function RootLayout() {
return (
<>
<Slot />
<VideoPortalHost name="app" />
</>
);
}
// Any screen
export const PlayerScreen = () => (
<VideoProvider portalHost="app">
<VideoPlayer source={{ uri: 'https://example.com/video.mp4' }}>
<DefaultLayout />
</VideoPlayer>
</VideoProvider>
);This applies to the default sheet. A custom Sheet renders wherever its library puts it; for @gorhom/bottom-sheet,
place BottomSheetModalProvider at the app root for the same effect.
State
The VideoProvider manages the following state. Read it with useVideoSelector(), which re-renders only when the part you select changes, or useVideo(), which re-renders on every change:
| State | Type | Description | |
|---|---|---|---|
isPlaying | boolean | Whether the video is playing. | |
duration | number | The duration of the video in seconds. | |
buffering | boolean | Whether the video is buffering. | |
muted | boolean | Whether the video is muted. | |
volume | number | The volume of the video. | |
playbackRate | number | The playback rate of the video. | |
fullscreen | boolean | Whether the video is in fullscreen mode. | |
error | string | null | An error message, if any. |
config | VideoPlayerConfig | The configuration for the video player. | |
theme | Theme | The theme for the video player. | |
videoRef | React.MutableRefObject<VideoRef | null> | A ref to the video component. It's set from the first render, and its current is null until the player mounts. | |
controlsVisible | boolean | true | Whether the controls are currently visible. |
videoWrapperLayout | LayoutRectangle | The layout of the view wrapping the video component. | |
videoLayout | LayoutRectangle | The layout of the video component. | |
dimensions | { width: number; height: number } | The dimensions of the screen. |
The playback position (currentTime and playableDuration) isn't part of the state. Read it with useProgress(): it changes on every progress event, and keeping it out of the state means only the components that call useProgress() re-render.
Actions
The VideoProvider handles the following actions:
| Action | Payload | Description | |
|---|---|---|---|
INITIALIZE | { theme?: Partial<Theme>; config?: Partial<VideoPlayerConfig> } | Initializes the provider with a theme and config. | |
SET_THEME | Partial<Theme> | Sets the theme. | |
SET_CONTROLS_VISIBLE | boolean | Sets the visible state of the controls. | |
SET_VIDEO_REF | React.RefObject<any> | Sets a ref to the video component. | |
SHOW_CONTROLS | Shows the controls. | ||
HIDE_CONTROLS | Hides the controls. | ||
SEEK | number | Seeks the video to a specific time. | |
TOGGLE_PLAY_PAUSE | Toggles the play/pause state of the video. | ||
SET_VOLUME | number | Sets the volume of the video. | |
TOGGLE_MUTE | Toggles the mute state of the video. | ||
TOGGLE_FULLSCREEN | Toggles the fullscreen mode. | ||
SET_PLAYING | boolean | Sets the playing state of the video. | |
SET_DURATION | number | Sets the duration of the video. | |
SET_BUFFERING | boolean | Sets the buffering state of the video. | |
SET_ERROR | string | null | Sets an error message. |
SET_VIDEO_LAYOUT | LayoutRectangle | Sets the layout of the video component. | |
SET_DIMENSIONS | { width: number; height: number } | Sets the dimensions of the screen. |
useVideo
The useVideo hook provides access to the video state and dispatch function.
Returns
| Value | Type | Description |
|---|---|---|
state | VideoProviderState | The video state. |
dispatch | React.Dispatch<Action> | The dispatch function. |