|
| 1 | +# `<Video>` |
| 2 | + |
| 3 | +FaCC that creates a `<video>` element to play video media, re-renders on video state changes. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```jsx |
| 8 | +import {Video} from 'libreact/lib/Video'; |
| 9 | + |
| 10 | +<Video autoPlay src='http://dailym.ai/2rG7TBS'>{(actions, state) => { |
| 11 | + JSON.stringify(state, null, 4) |
| 12 | +}}</Video> |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +## Props |
| 17 | + |
| 18 | +In addition to props below also accepts all [React's media events](https://reactjs.org/docs/events.html#media-events). |
| 19 | + |
| 20 | +```tsx |
| 21 | +interface IVideoProps { |
| 22 | + src: string; |
| 23 | + autoPlay?: boolean; |
| 24 | + loop?: boolean; |
| 25 | + muted?: boolean; |
| 26 | + preload?: 'none' | 'metadata' | 'auto'; |
| 27 | + volume?: number; |
| 28 | + noJs?: React.ReactElement<any>; |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | + - `src` - required, string, video source file URL. |
| 33 | + - `autoPlay` - optional, boolean, whether to autoplay media, defaults to `false`. |
| 34 | + - `loop` - optional, boolean, whether to repeat the media when it ends, defaults to `false`. |
| 35 | + - `muted` - optional, boolean, whether to mute the media, defaults to `false`. |
| 36 | + - `preload` - optional, string, `<video>` element preload attribute. |
| 37 | + - `volume` - optional, number, media volume in `[0..1]` range, defaults to `1`. |
| 38 | + - `noJs` - optional, React element(s) to render insided the `<video>` tag. |
| 39 | + - `onMount` - optional, callback, called when component mounts, receives `IVideo` as the first argument. |
| 40 | + - `onUnmount` - optional, callback, called when component un-mounts, receives `IVideo` as the first argument. |
| 41 | + |
| 42 | + |
| 43 | +## Arguments |
| 44 | + |
| 45 | +The `children` function receives two arguments, first is the `IVideo` *actions* collection and the second |
| 46 | +is the state of the video component. |
| 47 | + |
| 48 | +```jsx |
| 49 | +<Video autoPlay src={src}>{({video, ...actions}, state) => |
| 50 | + |
| 51 | +}</Video> |
| 52 | +``` |
| 53 | + |
| 54 | +First argument is the `<Video>` component instance with the following public signature. |
| 55 | + |
| 56 | +```ts |
| 57 | +interface IVideo { |
| 58 | + el: HTMLVideoElement; |
| 59 | + video: React.ReactElement; |
| 60 | + play(); |
| 61 | + pause(); |
| 62 | + seek(time: number); |
| 63 | + volume(value: number); |
| 64 | + mute(); |
| 65 | + unmute(); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +, where |
| 70 | + |
| 71 | + - `el` - `<video>` element DOM node. |
| 72 | + - `video` - `<video>` element React node, that you have to insert in the JSX tree. |
| 73 | + |
| 74 | +The second argument is the state of the `<Video>` component with the following signature. |
| 75 | + |
| 76 | +```ts |
| 77 | +interface IVideoState { |
| 78 | + buffered?: TRange[]; |
| 79 | + time?: number; |
| 80 | + duration?: number; |
| 81 | + isPlaying?: boolean; |
| 82 | + muted?: boolean; |
| 83 | + volume?: number; |
| 84 | +} |
| 85 | + |
| 86 | +type TRange = { |
| 87 | + start: number; |
| 88 | + end: number; |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +, where |
| 93 | + |
| 94 | + - `buffered` - a list of ranges representing media intervals that have been buffered by the browser. |
| 95 | + - `time` - current time in seconds. |
| 96 | + - `duration` - total video duration in seconds. |
| 97 | + - `isPlaying` - whether the video is currently playing. |
| 98 | + - `muted` - whether `muted` attribute is set on the video element. |
| 99 | + - `volume` - current volume in range `[0..1]`. |
| 100 | + |
| 101 | + |
| 102 | +## Example |
| 103 | + |
| 104 | +Play a sample video. |
| 105 | + |
| 106 | +```jsx |
| 107 | +<Video |
| 108 | + src='http://dailym.ai/2rG7TBS' |
| 109 | + style={{ |
| 110 | + width: 400, |
| 111 | + border: '1px solid tomato' |
| 112 | + }} |
| 113 | + render={({video, play, pause, seek, volume, mute, unmute}, {isPlaying}) => |
| 114 | + <div> |
| 115 | + {video} |
| 116 | + <br /> |
| 117 | + <button onClick={() => isPlaying ? pause() : play()}> |
| 118 | + {isPlaying ? 'Pause' : 'Play'} |
| 119 | + </button> |
| 120 | + </div> |
| 121 | + } |
| 122 | +/> |
| 123 | +``` |
0 commit comments