React autoplay video
Ngày đăng:
10/02/2022
Trả lời:
0
Lượt xem:
171
Video.js and ReactJS integrationHere are a couple ReactJS player implementations. Show React Functional Component and useEffect Exampleimport React from "react"; import videojs from "video.js"; import "video.js/dist/video-js.css"; export const VideoJS = ( props ) => { const videoRef = React.useRef(null); const playerRef = React.useRef(null); const { options, onReady } = props; React.useEffect(() => { // make sure Video.js player is only initialized once if (!playerRef.current) { const videoElement = videoRef.current; if (!videoElement) return; const player = playerRef.current = videojs(videoElement, options, () => { console.log("player is ready"); onReady && onReady(player); }); } else { // you can update player here [update player through props] // const player = playerRef.current; // player.autoplay(options.autoplay); // player.src(options.sources); } }, [options, videoRef]); // Dispose the Video.js player when the functional component unmounts React.useEffect(() => { const player = playerRef.current; return () => { if (player) { player.dispose(); playerRef.current = null; } }; }, [playerRef]); return (You can then use it like this: (see options guide for option information) Rest of app here
Rest of app here
>
);
}
React Class Component ExampleIt just instantiates the Video.js player on componentDidMount and destroys it on componentWillUnmount. You can then use it like this: (see options guide for option information) const videoJsOptions = { autoplay: true, controls: true, sources: [{ src: '/path/to/video.mp4', type: 'video/mp4' }] } returnUsing a React Component as a Video JS Component/** * EpisodeList.js * * This is just a plain ol' React component. * the vjsComponent methods, player methods etc. are available via * the vjsComponent prop (`this.props.vjsComponent`) */ import React, { Component, PropTypes } from 'react'; class EpisodeList extends Component { render() { return ({this.props.body} |