阿里云视频点播在React中的实现 发表于 2017-12-20 在原项目组件上进行了删减,保留核心代码,不一定能直接运行,需要根据情况调整代码 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import React, { Component } from 'react';import { hashHistory } from 'react-router';class CourseDetail extends Component { constructor(props) { super(props); this.state = {}; this.player = null; // 创建播放器实例变量 } componentDidMount() { // 初始化播放器, this.player = new Aliplayer({ id: 'Ali_Player', // 容器id vid: '', width: "100%", // 播放器宽度 playauth: '', cover: `${IMG_DOMAIN}${data.data.serviceCourseDto.serviceCourse.coverUrl}`, autoplay: false, rePlay: false, skinLayout:[{"name":"H5Loading","align":"cc"}, {"name":"errorDisplay","align":"tlabs","x":0,"y":0}, {"name":"infoDisplay","align":"cc"}, {"name":"controlBar","align":"blabs","x":0,"y":0,"children":[{"name":"progress","align":"tlabs","x":0,"y":0}, {"name":"timeDisplay","align":"tl","x":10,"y":24}]}] }); this.player.on('play',()=>{ if(this.state.cur_courseitem.courseItemId){ this.player.play(); } else { this.player.pause(); Toast.info('请选择章节'); } }) } handlePlay = (courseItemId) => { // 播放事件,此时获取播放凭证,也可以在挂载的时候获取播放凭证 loadVideoPalyAuth({courseItemId}).then(data => { this.setState({ cur_courseitem: { ...this.state.cur_courseitem, videoId: data.data.aliVideoPlayAuthDto.videoId, playauth: data.data.aliVideoPlayAuthDto.playAuth, courseItemId: courseItemId } },()=>{ if (this.player){ this.player.dispose(); } this.player = new Aliplayer({ id: 'Ali_Player', // 容器id vid: data.data.aliVideoPlayAuthDto.videoId, width: "100%", // 播放器宽度 playauth: data.data.aliVideoPlayAuthDto.playAuth, autoplay: false, rePlay: false, }); }); }) } render() { return ( <div> <button onClick={this.handlePlay}>播放</button> <div className="prism-player" id='Ali_Player' /> </div> ); }}export default CourseDetail;