通用评论

index.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React from "react";
  2. import { Slider, Icon } from "antd";
  3. import dayjs from "dayjs";
  4. import durationPlugin from "dayjs/plugin/duration";
  5. import utcPlugin from "dayjs/plugin/utc";
  6. import "./index.less";
  7. import iconLoading from "../../assert/loading.gif";
  8. dayjs.extend(durationPlugin);
  9. dayjs.extend(utcPlugin);
  10. class AudioPlayer extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. duration: 0,
  15. currentDuration: 0,
  16. isPlaying: false,
  17. isLoading: false
  18. };
  19. }
  20. componentDidMount() {
  21. if (this.player) {
  22. const { src } = this.props;
  23. this.player.oncanplay = event => {
  24. console.log(event, 1111);
  25. this.setState({ duration: event.target.duration });
  26. };
  27. this.player.onended = event => {
  28. console.log(event, 2222);
  29. this.setState({ isPlaying: false, currentDuration: 0 });
  30. };
  31. this.player.onpause = event => {
  32. console.log(event, 333);
  33. this.setState({ isPlaying: false });
  34. };
  35. this.player.onplay = () => {
  36. console.log(event, 5555);
  37. this.setState({ isPlaying: true, isLoading: false });
  38. };
  39. this.player.onplaying = event => {
  40. console.log(event, 4444);
  41. this.setState({ isPlaying: true, isLoading: false });
  42. };
  43. this.player.ontimeupdate = event =>
  44. this.setState({ currentDuration: event.target.currentTime });
  45. this.player.src = src;
  46. }
  47. }
  48. componentDidUpdate(prevProps) {
  49. if (this.props.src !== prevProps.src) {
  50. this.player.oncanplay = event => {
  51. console.log(event, 1111);
  52. this.setState({ duration: event.target.duration });
  53. };
  54. this.player.onended = event => {
  55. console.log(event, 2222);
  56. this.setState({ isPlaying: false, currentDuration: 0 });
  57. };
  58. this.player.onpause = event => {
  59. console.log(event, 333);
  60. this.setState({ isPlaying: false });
  61. };
  62. this.player.onplay = () => {
  63. console.log(event, 5555);
  64. this.setState({ isPlaying: true, isLoading: false });
  65. };
  66. this.player.onplaying = event => {
  67. console.log(event, 4444);
  68. this.setState({ isPlaying: true, isLoading: false });
  69. };
  70. this.player.ontimeupdate = event =>
  71. this.setState({ currentDuration: event.target.currentTime });
  72. this.player.src = this.props.src;
  73. }
  74. }
  75. clickPlayOrPause(isPlaying) {
  76. if (!this.player) {
  77. return;
  78. }
  79. if (isPlaying) {
  80. this.player.pause();
  81. } else {
  82. // 如果是播放,先暂停其他的播放
  83. const playerList = document.getElementsByTagName("audio");
  84. if (playerList) {
  85. Array.from(playerList).forEach(player => {
  86. if (!player.paused) {
  87. player.pause();
  88. }
  89. });
  90. }
  91. this.player.play();
  92. this.setState({
  93. isLoading: true
  94. });
  95. }
  96. }
  97. getPlayIcon() {
  98. const { isPlaying, isLoading } = this.state;
  99. let playIconElem;
  100. if (isLoading) {
  101. playIconElem = (
  102. <img className="icon-loading" src={iconLoading} alt="iconLoading" />
  103. );
  104. } else if (isPlaying) {
  105. playIconElem = <Icon className="pause" type="pause" />;
  106. } else {
  107. playIconElem = <i className="schedule schedule-icon_image_audio" />;
  108. }
  109. return playIconElem;
  110. }
  111. render() {
  112. const { currentDuration, isPlaying, duration } = this.state;
  113. return (
  114. <div className="comment-item-speak-audio-container">
  115. {/* eslint-disable-next-line jsx-a11y/media-has-caption */}
  116. <audio
  117. ref={ref => {
  118. this.player = ref;
  119. }}
  120. style={{ display: "none" }}
  121. />
  122. <span
  123. className="icon"
  124. onClick={() => {
  125. this.clickPlayOrPause(isPlaying);
  126. }}
  127. >
  128. {this.getPlayIcon()}
  129. </span>
  130. <Slider
  131. step={0.001}
  132. className="slider"
  133. tooltipVisible={false}
  134. value={currentDuration}
  135. max={duration}
  136. onChange={value => {
  137. if (this.player) {
  138. this.player.currentTime = value;
  139. }
  140. }}
  141. />
  142. <span className="time">
  143. {dayjs
  144. .utc(dayjs.duration(currentDuration, "seconds").asMilliseconds())
  145. .format("mm:ss")}
  146. /
  147. {dayjs
  148. .utc(dayjs.duration(duration, "seconds").asMilliseconds())
  149. .format("mm:ss")}
  150. </span>
  151. </div>
  152. );
  153. }
  154. }
  155. export default AudioPlayer;