1
0

index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import videojs from 'video.js';
  4. import Assets from '@src/components/Assets';
  5. import { generateUUID } from '@src/services/Tools';
  6. function fullScreen(id) {
  7. const element = document.getElementById(id);
  8. if (element.requestFullscreen) {
  9. element.requestFullscreen();
  10. } else if (element.msRequestFullscreen) {
  11. element.msRequestFullscreen();
  12. } else if (element.mozRequestFullScreen) {
  13. element.mozRequestFullScreen();
  14. } else if (element.webkitRequestFullscreen) {
  15. element.webkitRequestFullscreen();
  16. }
  17. }
  18. function exitFullscreen() {
  19. if (document.exitFullscreen) {
  20. document.exitFullscreen();
  21. } else if (document.msExitFullscreen) {
  22. document.msExitFullscreen();
  23. } else if (document.mozCancelFullScreen) {
  24. document.mozCancelFullScreen();
  25. } else if (document.webkitExitFullscreen) {
  26. document.webkitExitFullscreen();
  27. }
  28. }
  29. let fullAgent = null;
  30. document.addEventListener('fullscreenchange', () => {
  31. if (fullAgent) fullAgent();
  32. });
  33. /* Firefox */
  34. document.addEventListener('mozfullscreenchange', () => {
  35. if (fullAgent) fullAgent();
  36. });
  37. /* Chrome, Safari and Opera */
  38. document.addEventListener('webkitfullscreenchange', () => {
  39. if (fullAgent) fullAgent();
  40. });
  41. /* IE / Edge */
  42. document.addEventListener('msfullscreenchange', () => {
  43. if (fullAgent) fullAgent();
  44. });
  45. export default class Video extends Component {
  46. constructor(props) {
  47. super(props);
  48. this.ready = false;
  49. this.state = { id: generateUUID(8), playing: false, fulling: false };
  50. }
  51. componentDidMount() {
  52. this.player = videojs(
  53. this.videoNode,
  54. {
  55. controls: false,
  56. sources: [
  57. {
  58. src: this.props.src,
  59. type: 'video/mp4',
  60. },
  61. ],
  62. },
  63. () => {
  64. this.ready = true;
  65. },
  66. );
  67. }
  68. componentWillUnmount() {
  69. if (this.player) {
  70. this.player.dispose();
  71. }
  72. }
  73. refreshTimeUpdate() {
  74. if (this.timeInterval) {
  75. clearInterval(this.timeInterval);
  76. this.timeInterval = null;
  77. }
  78. this.timeInterval = setInterval(() => {
  79. const { onTimeUpdate } = this.props;
  80. if (onTimeUpdate) onTimeUpdate(this.player.currentTime());
  81. }, this.props.duration ? this.props.duration * 1000 : 1000);
  82. }
  83. onPlay() {
  84. if (!this.ready) return;
  85. const { onPlay } = this.props;
  86. this.player.play();
  87. this.setState({ playing: true });
  88. if (onPlay) onPlay();
  89. }
  90. onPause() {
  91. if (!this.ready) return;
  92. const { onPause } = this.props;
  93. this.player.pause();
  94. this.setState({ playing: false });
  95. if (onPause) onPause();
  96. }
  97. onNext() {
  98. const { onNext } = this.props;
  99. this.player.pause();
  100. this.setState({ playing: false });
  101. if (onNext) onNext();
  102. }
  103. onSpeed(speed) {
  104. this.player.playbackRate(speed);
  105. }
  106. onFullChange() {
  107. this.setState({ fulling: !this.state.fulling });
  108. if (this.props.onFullChange) this.props.onFullChange();
  109. }
  110. onFull() {
  111. fullAgent = () => this.onFullChange();
  112. fullScreen(this.state.id);
  113. }
  114. onExitFull() {
  115. exitFullscreen();
  116. }
  117. render() {
  118. const { action = true, btnList = [], children, onAction, hideAction } = this.props;
  119. const { playing, fulling, id } = this.state;
  120. return (
  121. <div id={id} className={`video-item ${action ? 'action' : ''} ${fulling ? 'full' : ''}`}>
  122. <div className="video-wrapper">
  123. <video
  124. ref={node => {
  125. this.videoNode = node;
  126. }}
  127. />
  128. {!playing && <Assets className="play" name="play" onClick={() => this.onPlay()} />}
  129. {playing && <Assets className="stop" name="stop" onClick={() => this.onPuase()} />}
  130. </div>
  131. {!hideAction && <div className="video-bottom">
  132. <div className="progress" />
  133. {action && (
  134. <div className="action-bar">
  135. <div className="d-i-b m-r-1">
  136. {!playing && <Assets name="play2" onClick={() => this.onPlay()} />}
  137. {playing && <Assets name="stop2" onClick={() => this.onPause()} />}
  138. </div>
  139. <div className="d-i-b m-r-1">
  140. <Assets name="next2" onClick={() => this.onNext()} />
  141. </div>
  142. <div className="flex-block" />
  143. {btnList.map(btn => {
  144. if (btn.full && !fulling) return '';
  145. if (!btn.show) return '';
  146. return (
  147. <div className="d-i-b m-r-1">
  148. {btn.render ? (
  149. <div className="fix-btn d-i-b" onClick={() => {
  150. if (btn.pause) this.onPause();
  151. if (onAction) onAction(btn.key);
  152. }}>
  153. {btn.render(btn.active)}
  154. </div>
  155. ) : (<div
  156. className={`btn ${btn.active ? 'active' : ''}`}
  157. onClick={() => onAction && onAction(btn.key)}
  158. >
  159. {btn.title}
  160. </div>)}
  161. </div>
  162. );
  163. })}
  164. <div className="d-i-b m-r-1">
  165. <div className="btn">倍速</div>
  166. </div>
  167. <div className="d-i-b">
  168. {!fulling && <Assets name="full2" onClick={() => this.onFull()} />}
  169. {fulling && <Assets name="reduction2" onClick={() => this.onExitFull()} />}
  170. </div>
  171. </div>
  172. )}
  173. </div>}
  174. {children}
  175. </div>
  176. );
  177. }
  178. }