通用评论

index.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. this.setState({
  25. duration: event.target.duration
  26. });
  27. this.player.onended = () =>
  28. this.setState({
  29. isPlaying: false,
  30. currentDuration: 0,
  31. isLoading: false
  32. });
  33. this.player.onpause = () =>
  34. this.setState({ isPlaying: false, isLoading: false });
  35. this.player.onplaying = () =>
  36. this.setState({ isPlaying: true, isLoading: false });
  37. this.player.ontimeupdate = event =>
  38. this.setState({ currentDuration: event.target.currentTime });
  39. this.player.src = src;
  40. }
  41. }
  42. componentDidUpdate(prevProps) {
  43. if (this.props.src !== prevProps.src) {
  44. this.player.oncanplay = event =>
  45. this.setState({
  46. duration: event.target.duration
  47. });
  48. this.player.onended = () =>
  49. this.setState({
  50. isPlaying: false,
  51. currentDuration: 0,
  52. isLoading: false
  53. });
  54. this.player.onpause = () =>
  55. this.setState({ isPlaying: false, isLoading: false });
  56. this.player.onplaying = () =>
  57. this.setState({ isPlaying: true, isLoading: false });
  58. this.player.ontimeupdate = event =>
  59. this.setState({ currentDuration: event.target.currentTime });
  60. this.player.src = this.props.src;
  61. }
  62. }
  63. clickPlayOrPause(isPlaying) {
  64. if (!this.player) {
  65. return;
  66. }
  67. if (isPlaying) {
  68. this.player.pause();
  69. } else {
  70. // 如果是播放,先暂停其他的播放
  71. const playerList = document.getElementsByTagName("audio");
  72. if (playerList) {
  73. Array.from(playerList).forEach(player => {
  74. if (!player.paused) {
  75. player.pause();
  76. }
  77. });
  78. }
  79. this.player.play();
  80. this.setState({
  81. isLoading: true
  82. });
  83. }
  84. }
  85. getPlayIcon() {
  86. const { isPlaying, isLoading } = this.state;
  87. let playIconElem;
  88. if (isLoading) {
  89. playIconElem = (
  90. <img className="icon-loading" src={iconLoading} alt="iconLoading" />
  91. );
  92. } else if (isPlaying) {
  93. playIconElem = <Icon className="pause" type="pause" />;
  94. } else {
  95. playIconElem = <i className="schedule schedule-icon_image_audio" />;
  96. }
  97. return playIconElem;
  98. }
  99. render() {
  100. const { currentDuration, isPlaying, duration } = this.state;
  101. return (
  102. <div className="comment-item-speak-audio-container">
  103. {/* eslint-disable-next-line jsx-a11y/media-has-caption */}
  104. <audio
  105. ref={ref => {
  106. this.player = ref;
  107. }}
  108. style={{ display: "none" }}
  109. />
  110. <span
  111. className="icon"
  112. onClick={() => {
  113. this.clickPlayOrPause(isPlaying);
  114. }}
  115. >
  116. {this.getPlayIcon()}
  117. </span>
  118. <Slider
  119. step={0.001}
  120. className="slider"
  121. tooltipVisible={false}
  122. value={currentDuration}
  123. max={duration}
  124. onChange={value => {
  125. if (this.player) {
  126. this.player.currentTime = value;
  127. }
  128. }}
  129. />
  130. <span className="time">
  131. {dayjs
  132. .utc(dayjs.duration(currentDuration, "seconds").asMilliseconds())
  133. .format("mm:ss")}
  134. /
  135. {dayjs
  136. .utc(dayjs.duration(duration, "seconds").asMilliseconds())
  137. .format("mm:ss")}
  138. </span>
  139. </div>
  140. );
  141. }
  142. }
  143. export default AudioPlayer;