我创建一个组件说"视频播放器"并使用 Plyr.io 中的播放器。 创建用于使用 handleVideoPlayer 初始化的句柄。 使用 handlePlyrScript 加载的 Plyr 脚本。
这是我的代码:
const VideoPlayer = (props) => {
return(
<div className={styles.videoSection}>
<div className={styles.videoResponsive}>
<video poster={props.video.poster} id="player" playsInline controls>
<source src={props.video.source} type="video/mp4" />
</video>
</div>
</div>
);
}
class PlayerPage extends Component {
state = {
currentEpisode : {},
allPlayList : [],
}
// load Plyr script from cdn
handlePlyrScript = () => {
const plyrscript = document.createElement('script');
plyrscript.src = 'https://cdn.plyr.io/3.5.2/plyr.polyfilled.js';
document.body.appendChild(plyrscript);
}
// handle Video Player
handleVideoPlayer = () => {
setTimeout(() => {
const player = new Plyr('#player')
}, 500)
}
componentDidMount = () => {
console.log(this);
if(typeof(this.props.location.state) !== "undefined"){
let play_data = this.props.location.state;
this.setState({
currentEpisode : play_data
})
} else {
console.log("nothing...")
}
// call the Plyr init video tag with #player
this.handlePlyrScript()
this.handleVideoPlayer()
}
render(){
return(
<div className={style.playerPageSection}>
<div className={style.videoWrapper}>
<div className={style.videoRow}>
<div className={style.videoLeft}>
<VideoPlayer video={this.state.currentEpisode} />
</div>
<div className={style.videoRight}>
</div>
</div>
</div>
</div>
);
}
}
export default PlayerPage;
但结果没有显示带有 Plyr 风格的视频,(该视频就像带有标签的通用播放器一样。
有什么关于视频播放器的解决方案吗?
我认为您正在寻找的东西已经在这个包中制作了:https://www.npmjs.com/package/react-plyr
应该给你你想要的结果。