1
0

index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 };
  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. }
  124. onFullChange() {
  125. this.setState({ fulling: !this.state.fulling });
  126. if (this.props.onFullChange) this.props.onFullChange();
  127. }
  128. onFull() {
  129. fullAgent = () => this.onFullChange();
  130. fullScreen(this.state.id);
  131. }
  132. onExitFull() {
  133. exitFullscreen();
  134. }
  135. render() {
  136. const { btnList = [], children, onAction, hideAction } = this.props;
  137. const { playing, fulling, id } = this.state;
  138. return (
  139. <div id={id} className={`video-item ${!hideAction ? 'action' : ''} ${fulling ? 'full' : ''}`}>
  140. <div className="video-wrapper">
  141. <video
  142. ref={node => {
  143. this.videoNode = node;
  144. }}
  145. // vjs-fluid
  146. />
  147. {!playing && <Assets className="play" name="play" onClick={() => this.onPlay()} />}
  148. {playing && <Assets className="stop" name="stop" onClick={() => this.onPause()} />}
  149. </div>
  150. <div className="video-bottom">
  151. <div className="progress" />
  152. {/* {this.renderProgress()} */}
  153. {!hideAction && <div className="action-bar">
  154. <div className="d-i-b m-r-1">
  155. <Assets name={!playing ? 'play2' : 'stop2'} onClick={() => (playing ? this.onPause() : this.onPlay())} />
  156. {/* {playing && <Assets name="stop2" onClick={() => this.onPause()} />} */}
  157. </div>
  158. <div className="d-i-b m-r-1">
  159. <Assets name="next2" onClick={() => this.onNext()} />
  160. </div>
  161. {/* <div className="m-r-1">{this.ready ? (formatMinuteSecond(this.player.currentTime())) : ('00:00')}</div>
  162. <div className="m-r-1">/{this.ready ? (formatMinuteSecond(this.player.duration())) : ('00:00')}</div> */}
  163. <div className="flex-block" />
  164. {btnList.map(btn => {
  165. if (btn.full && !fulling) return '';
  166. if (!btn.show) return '';
  167. return (
  168. <div className="d-i-b m-r-1">
  169. {btn.render ? (
  170. <div className="fix-btn-action d-i-b" onClick={() => {
  171. if (btn.pause) this.onPause();
  172. if (onAction) onAction(btn.key);
  173. }}>
  174. {btn.render(btn.active)}
  175. </div>
  176. ) : (<div
  177. className={`btn-action ${btn.active ? 'active' : ''}`}
  178. onClick={() => onAction && onAction(btn.key)}
  179. >
  180. {btn.title}
  181. </div>)}
  182. </div>
  183. );
  184. })}
  185. <div className="d-i-b m-r-1">
  186. <div className="btn-action">倍速</div>
  187. </div>
  188. <div className="d-i-b">
  189. {!fulling && <Assets name="full2" onClick={() => this.onFull()} />}
  190. {fulling && <Assets name="reduction2" onClick={() => this.onExitFull()} />}
  191. </div>
  192. </div>}
  193. </div>
  194. {children}
  195. </div>
  196. );
  197. }
  198. renderProgress() {
  199. const { hideProgress } = this.props;
  200. const { progress } = this.state;
  201. return !hideProgress && <Slider value={progress || 0} tooltipVisible={false} onChange={(value) => this.onChangeProgress(value)} />;
  202. }
  203. }