import {Component} from '@tarojs/taro' import {Image, View} from '@tarojs/components' import {BaseProps} from 'types/BaseProps' import Audio from "../../utils/Audio" import Loading from './loading.svg' import config from '../../utils/config' interface State { tips: { text: string, state: string }, time: { currentTime: number, duration: number, schedule: number } } const audio = new Audio() export default class AudioPlayer extends Component { state = { tips: { text: 'Loading', state: 'h2w__audio--loading' }, time: { currentTime: 0, duration: 1, schedule: 0 } } componentWillMount(): void { const {data} = this.props audio.create(data) audio.eventPlay = () => { this.setState({tips: {state: 'h2w__audio--play', text: 'Playing'}}) } audio.eventCanplay = () => { this.setState({tips: {state: 'h2w__audio--readyed', text: 'Readyed'}}) } audio.eventTimeUpdate = function (duration, currentTime) { this.setState({ time: { currentTime: currentTime, duration: duration, schedule: Math.round(audio.currentTime) / Math.round(audio.duration) * 100 + '%' } }) } audio.eventPause = () => { this.setState({tips: {state: 'h2w__audio--pause', text: 'Pause'}}); } audio.eventStop = () => { this.setState({tips: {state: 'h2w__audio--end', text: 'End'}}); } } componentWillUnmount(): void { audio.destroy() } playAndPause = () => { if (audio.status === 'update' || audio.status === 'play') { audio.pause(); } else { audio.play(); } } options = { addGlobalClass: true } render() { const {data} = this.props const {tips, time} = this.state const className = config.classPrefix + 'h2w__audio ' + (tips && tips.state ? (config.classPrefix + tips.state) : (config.classPrefix + 'h2w__audio--loading')) return ( {tips.text || 'Error'} {data.attr.name} {data.attr.author} {time.currentTime || '00:00:00'} / {time.duration || '00:00:00'} ) } }