index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import React, { Component } from 'react';
  2. import { Slider } from 'antd';
  3. import './index.less';
  4. import videojs from 'video.js';
  5. import Assets from '@src/components/Assets';
  6. import { generateUUID } from '@src/services/Tools';
  7. function fullScreen(id) {
  8. const element = document.getElementById(id);
  9. if (element.requestFullscreen) {
  10. element.requestFullscreen();
  11. } else if (element.msRequestFullscreen) {
  12. element.msRequestFullscreen();
  13. } else if (element.mozRequestFullScreen) {
  14. element.mozRequestFullScreen();
  15. } else if (element.webkitRequestFullscreen) {
  16. element.webkitRequestFullscreen();
  17. }
  18. }
  19. function exitFullscreen() {
  20. if (document.exitFullscreen) {
  21. document.exitFullscreen();
  22. } else if (document.msExitFullscreen) {
  23. document.msExitFullscreen();
  24. } else if (document.mozCancelFullScreen) {
  25. document.mozCancelFullScreen();
  26. } else if (document.webkitExitFullscreen) {
  27. document.webkitExitFullscreen();
  28. }
  29. }
  30. let fullAgent = null;
  31. document.addEventListener('fullscreenchange', () => {
  32. if (fullAgent) fullAgent();
  33. });
  34. /* Firefox */
  35. document.addEventListener('mozfullscreenchange', () => {
  36. if (fullAgent) fullAgent();
  37. });
  38. /* Chrome, Safari and Opera */
  39. document.addEventListener('webkitfullscreenchange', () => {
  40. if (fullAgent) fullAgent();
  41. });
  42. /* IE / Edge */
  43. document.addEventListener('msfullscreenchange', () => {
  44. if (fullAgent) fullAgent();
  45. });
  46. export default class Video extends Component {
  47. constructor(props) {
  48. super(props);
  49. this.ready = false;
  50. this.state = { id: generateUUID(8), playing: false, fulling: false, progress: 0, speed: 1 };
  51. }
  52. componentDidMount() {
  53. this.player = videojs(
  54. this.videoNode,
  55. {
  56. controls: true,
  57. sources: [
  58. {
  59. src: this.props.src,
  60. type: 'video/mp4',
  61. },
  62. ],
  63. width: this.props.width,
  64. height: this.props.height,
  65. },
  66. () => {
  67. this.ready = true;
  68. },
  69. );
  70. }
  71. componentWillUnmount() {
  72. if (this.player) {
  73. this.player.dispose();
  74. }
  75. }
  76. clearTimeUpdate() {
  77. if (this.timeInterval) {
  78. clearInterval(this.timeInterval);
  79. this.timeInterval = null;
  80. }
  81. }
  82. refreshTimeUpdate() {
  83. if (this.timeInterval) {
  84. clearInterval(this.timeInterval);
  85. this.timeInterval = null;
  86. }
  87. this.timeInterval = setInterval(() => {
  88. const { onTimeUpdate } = this.props;
  89. if (onTimeUpdate) onTimeUpdate(this.player.currentTime());
  90. // this.setState({ progress: this.player.currentTime() * 100 / this.player.duration() });
  91. }, 1000);
  92. }
  93. onChangeProgress(value) {
  94. if (!this.ready) return;
  95. this.player.currentTime((this.player.duration() * value) / 100);
  96. this.setState({ progress: (this.player.currentTime() * 100) / this.player.duration() });
  97. }
  98. onPlay() {
  99. if (!this.ready) return;
  100. const { onPlay } = this.props;
  101. this.player.play();
  102. this.setState({ playing: true });
  103. if (onPlay) onPlay();
  104. this.refreshTimeUpdate();
  105. }
  106. onPause() {
  107. if (!this.ready) return;
  108. const { onPause } = this.props;
  109. this.player.pause();
  110. this.setState({ playing: false });
  111. if (onPause) onPause();
  112. this.clearTimeUpdate();
  113. }
  114. onNext() {
  115. const { onNext } = this.props;
  116. this.player.pause();
  117. this.clearTimeUpdate();
  118. this.setState({ playing: false });
  119. if (onNext) onNext();
  120. }
  121. onSpeed(speed) {
  122. this.player.playbackRate(speed);
  123. this.setState({ selectSpeed: false, speed });
  124. }
  125. selectSpeed(value) {
  126. this.setState({ selectSpeed: value });
  127. }
  128. onFullChange() {
  129. this.setState({ fulling: !this.state.fulling });
  130. if (this.props.onFullChange) this.props.onFullChange();
  131. }
  132. onFull() {
  133. fullAgent = () => this.onFullChange();
  134. fullScreen(this.state.id);
  135. }
  136. onExitFull() {
  137. exitFullscreen();
  138. }
  139. render() {
  140. const { btnList = [], children, onAction, hideAction } = this.props;
  141. const { playing, fulling, id, selectSpeed, speed } = this.state;
  142. return (
  143. <div id={id} className={`video-item ${!hideAction ? 'action' : ''} ${fulling ? 'full' : ''}`}>
  144. <div className="video-wrapper">
  145. <video
  146. ref={node => {
  147. this.videoNode = node;
  148. }}
  149. // vjs-fluid
  150. />
  151. {!playing && <Assets className="play" name="play" onClick={() => this.onPlay()} />}
  152. {playing && <Assets className="stop" name="stop" onClick={() => this.onPause()} />}
  153. </div>
  154. <div className="video-bottom">
  155. <div className="progress" />
  156. {/* {this.renderProgress()} */}
  157. {!hideAction && (
  158. <div className="action-bar">
  159. <div className="d-i-b m-r-1">
  160. <Assets
  161. name={!playing ? 'play2' : 'stop2'}
  162. onClick={() => (playing ? this.onPause() : this.onPlay())}
  163. />
  164. {/* {playing && <Assets name="stop2" onClick={() => this.onPause()} />} */}
  165. </div>
  166. <div className="d-i-b m-r-1">
  167. <Assets name="next2" onClick={() => this.onNext()} />
  168. </div>
  169. {/* <div className="m-r-1">{this.ready ? (formatMinuteSecond(this.player.currentTime())) : ('00:00')}</div>
  170. <div className="m-r-1">/{this.ready ? (formatMinuteSecond(this.player.duration())) : ('00:00')}</div> */}
  171. <div className="flex-block" />
  172. {btnList.map(btn => {
  173. if (btn.full && !fulling) return '';
  174. if (!btn.show) return '';
  175. return (
  176. <div className="d-i-b m-r-1">
  177. {btn.render ? (
  178. <div
  179. className="fix-btn-action d-i-b"
  180. onClick={() => {
  181. if (btn.pause) this.onPause();
  182. if (onAction) onAction(btn.key);
  183. }}
  184. >
  185. {btn.render(btn.active)}
  186. </div>
  187. ) : (<div
  188. className={`btn-action ${btn.active ? 'active' : ''}`}
  189. onClick={() => onAction && onAction(btn.key)}
  190. >
  191. {btn.title}
  192. </div>
  193. )}
  194. </div>
  195. );
  196. })}
  197. <div className="d-i-b m-r-1">
  198. <div className={`btn-action ${selectSpeed ? 'active' : ''}`} onClick={() => this.selectSpeed(!selectSpeed)}>
  199. 倍速
  200. </div>
  201. </div>
  202. <div className="d-i-b">
  203. {!fulling && <Assets name="full2" onClick={() => this.onFull()} />}
  204. {fulling && <Assets name="reduction2" onClick={() => this.onExitFull()} />}
  205. </div>
  206. </div>
  207. )}
  208. </div>
  209. {selectSpeed && (
  210. <div className="select-speed">
  211. <div className={`item ${speed === 0.5 ? 'active' : ''}`} onClick={() => this.onSpeed(0.5)}>
  212. 0.5
  213. </div>
  214. <div className={`item ${speed === 0.75 ? 'active' : ''}`} onClick={() => this.onSpeed(0.75)}>
  215. 0.75
  216. </div>
  217. <div className={`item ${speed === 1 ? 'active' : ''}`} onClick={() => this.onSpeed(1)}>
  218. 正常
  219. </div>
  220. <div className={`item ${speed === 1.25 ? 'active' : ''}`} onClick={() => this.onSpeed(1.25)}>
  221. 1.25
  222. </div>
  223. <div className={`item ${speed === 1.5 ? 'active' : ''}`} onClick={() => this.onSpeed(1.5)}>
  224. 1.5
  225. </div>
  226. <div className={`item ${speed === 2.0 ? 'active' : ''}`} onClick={() => this.onSpeed(2)}>
  227. 2.0
  228. </div>
  229. </div>
  230. )}
  231. {children}
  232. </div>
  233. );
  234. }
  235. renderProgress() {
  236. const { hideProgress } = this.props;
  237. const { progress } = this.state;
  238. return (
  239. !hideProgress && (
  240. <Slider value={progress || 0} tooltipVisible={false} onChange={value => this.onChangeProgress(value)} />
  241. )
  242. );
  243. }
  244. }