通用评论

index.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. dayjs.extend(durationPlugin);
  8. dayjs.extend(utcPlugin);
  9. class AudioPlayer extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. duration: 0,
  14. currentDuration: 0,
  15. isPlaying: false
  16. };
  17. }
  18. componentDidMount() {
  19. if (this.player) {
  20. const { src } = this.props;
  21. this.player.oncanplay = event =>
  22. this.setState({ duration: event.target.duration });
  23. this.player.onended = () =>
  24. this.setState({ isPlaying: false, currentDuration: 0 });
  25. this.player.onpause = () => this.setState({ isPlaying: false });
  26. this.player.onplay = () => this.setState({ isPlaying: true });
  27. this.player.ontimeupdate = event =>
  28. this.setState({ currentDuration: event.target.currentTime });
  29. this.player.src = src;
  30. }
  31. }
  32. componentDidUpdate(prevProps) {
  33. if (this.props.src !== prevProps.src) {
  34. this.player.oncanplay = event =>
  35. this.setState({ duration: event.target.duration });
  36. this.player.onended = () =>
  37. this.setState({ isPlaying: false, currentDuration: 0 });
  38. this.player.onpause = () => this.setState({ isPlaying: false });
  39. this.player.onplay = () => this.setState({ isPlaying: true });
  40. this.player.ontimeupdate = event =>
  41. this.setState({ currentDuration: event.target.currentTime });
  42. this.player.src = this.props.src;
  43. }
  44. }
  45. clickPlayOrPause(isPlaying) {
  46. if (!this.player) {
  47. return;
  48. }
  49. if (isPlaying) {
  50. this.player.pause();
  51. } else {
  52. // 如果是播放,先暂停其他的播放
  53. const playerList = document.getElementsByTagName("audio");
  54. if (playerList) {
  55. Array.from(playerList).forEach(player => {
  56. if (!player.paused) {
  57. player.pause();
  58. }
  59. });
  60. }
  61. this.player.play();
  62. }
  63. }
  64. render() {
  65. const { currentDuration, isPlaying, duration } = this.state;
  66. return (
  67. <div className="comment-item-speak-audio-container">
  68. {/* eslint-disable-next-line jsx-a11y/media-has-caption */}
  69. <audio
  70. ref={ref => {
  71. this.player = ref;
  72. }}
  73. style={{ display: "none" }}
  74. />
  75. <span
  76. className="icon"
  77. onClick={() => {
  78. this.clickPlayOrPause(isPlaying);
  79. }}
  80. >
  81. {isPlaying ? (
  82. <Icon className="pause" type="pause" />
  83. ) : (
  84. <i className="schedule schedule-icon_image_audio" />
  85. )}
  86. </span>
  87. <Slider
  88. step={0.001}
  89. className="slider"
  90. tooltipVisible={false}
  91. value={currentDuration}
  92. max={duration}
  93. onChange={value => {
  94. if (this.player) {
  95. this.player.currentTime = value;
  96. }
  97. }}
  98. />
  99. <span className="time">
  100. {dayjs
  101. .utc(dayjs.duration(currentDuration, "seconds").asMilliseconds())
  102. .format("mm:ss")}
  103. /
  104. {dayjs
  105. .utc(dayjs.duration(duration, "seconds").asMilliseconds())
  106. .format("mm:ss")}
  107. </span>
  108. </div>
  109. );
  110. }
  111. }
  112. export default AudioPlayer;