通用评论

index.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. render() {
  46. const { currentDuration, isPlaying, duration } = this.state;
  47. return (
  48. <div className="comment-item-speak-audio-container">
  49. {/* eslint-disable-next-line jsx-a11y/media-has-caption */}
  50. <audio
  51. ref={ref => {
  52. this.player = ref;
  53. }}
  54. style={{ display: "none" }}
  55. />
  56. <span
  57. className="icon"
  58. onClick={() =>
  59. this.player &&
  60. (isPlaying ? this.player.pause() : this.player.play())
  61. }
  62. >
  63. {isPlaying ? (
  64. <Icon className="pause" type="pause" />
  65. ) : (
  66. <i className="schedule schedule-icon_image_audio" />
  67. )}
  68. </span>
  69. <Slider
  70. step={0.001}
  71. className="slider"
  72. tooltipVisible={false}
  73. value={currentDuration}
  74. max={duration}
  75. onChange={value => {
  76. if (this.player) {
  77. this.player.currentTime = value;
  78. }
  79. }}
  80. />
  81. <span className="time">
  82. {dayjs
  83. .utc(dayjs.duration(currentDuration, "seconds").asMilliseconds())
  84. .format("mm:ss")}
  85. /
  86. {dayjs
  87. .utc(dayjs.duration(duration, "seconds").asMilliseconds())
  88. .format("mm:ss")}
  89. </span>
  90. </div>
  91. );
  92. }
  93. }
  94. export default AudioPlayer;