index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import videojs from 'video.js';
  4. import Assets from '@src/components/Assets';
  5. export default class Video extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.ready = false;
  9. this.state = { playing: false, fulling: false };
  10. }
  11. componentDidMount() {
  12. this.player = videojs(
  13. this.videoNode,
  14. {
  15. controls: false,
  16. sources: [
  17. {
  18. src: this.props.src,
  19. type: 'video/mp4',
  20. },
  21. ],
  22. },
  23. () => {
  24. this.ready = true;
  25. },
  26. );
  27. }
  28. componentWillUnmount() {
  29. if (this.player) {
  30. this.player.dispose();
  31. }
  32. }
  33. onPlay() {
  34. if (!this.ready) return;
  35. this.player.play();
  36. this.setState({ playing: true });
  37. }
  38. onPuase() {
  39. if (!this.ready) return;
  40. this.player.pause();
  41. this.setState({ playing: false });
  42. }
  43. onSpeed(speed) {
  44. this.player.playbackRate(speed);
  45. }
  46. render() {
  47. const { action = true, btnList = [], onAction } = this.props;
  48. const { playing, fulling } = this.state;
  49. return (
  50. <div className={`video-item ${action ? 'action' : ''}`}>
  51. <div className="video-wrapper">
  52. <video
  53. ref={node => {
  54. this.videoNode = node;
  55. }}
  56. />
  57. {!playing && <Assets className="play" name="play" onClick={() => this.onPlay()} />}
  58. {playing && <Assets className="stop" name="stop" onClick={() => this.onPuase()} />}
  59. </div>
  60. <div className="video-bottom">
  61. <div className="progress" />
  62. {action && (
  63. <div className="action-bar">
  64. <div className="d-i-b m-r-1">
  65. {!playing && <Assets name="play2" onClick={() => this.onPlay()} />}
  66. {playing && <Assets name="stop2" onClick={() => this.onPuase()} />}
  67. </div>
  68. <div className="d-i-b m-r-1">
  69. <Assets name="next2" />
  70. </div>
  71. <div className="flex-block" />
  72. {btnList.map(btn => {
  73. return (
  74. <div className="d-i-b m-r-1">
  75. <div className="btn" onClick={() => onAction && onAction(btn.key)}>
  76. {btn.title}
  77. </div>
  78. </div>
  79. );
  80. })}
  81. <div className="d-i-b m-r-1">
  82. <div className="btn">倍速</div>
  83. </div>
  84. <div className="d-i-b">
  85. {!fulling && <Assets name="full2" />}
  86. {fulling && <Assets name="reduction2" />}
  87. </div>
  88. </div>
  89. )}
  90. </div>
  91. </div>
  92. );
  93. }
  94. }