import React from "react"; import { Slider, Icon } from "antd"; import dayjs from "dayjs"; import durationPlugin from "dayjs/plugin/duration"; import utcPlugin from "dayjs/plugin/utc"; import "./index.less"; dayjs.extend(durationPlugin); dayjs.extend(utcPlugin); class AudioPlayer extends React.Component { constructor(props) { super(props); this.state = { duration: 0, currentDuration: 0, isPlaying: false }; } componentDidMount() { if (this.player) { const { src } = this.props; this.player.oncanplay = event => this.setState({ duration: event.target.duration }); this.player.onended = () => this.setState({ isPlaying: false, currentDuration: 0 }); this.player.onpause = () => this.setState({ isPlaying: false }); this.player.onplay = () => this.setState({ isPlaying: true }); this.player.ontimeupdate = event => this.setState({ currentDuration: event.target.currentTime }); this.player.src = src; } } componentDidUpdate(prevProps) { if (this.props.src !== prevProps.src) { this.player.oncanplay = event => this.setState({ duration: event.target.duration }); this.player.onended = () => this.setState({ isPlaying: false, currentDuration: 0 }); this.player.onpause = () => this.setState({ isPlaying: false }); this.player.onplay = () => this.setState({ isPlaying: true }); this.player.ontimeupdate = event => this.setState({ currentDuration: event.target.currentTime }); this.player.src = this.props.src; } } render() { const { currentDuration, isPlaying, duration } = this.state; return (
{/* eslint-disable-next-line jsx-a11y/media-has-caption */}
); } } export default AudioPlayer;