<template>
	<view class="audio" v-if="url">
		<view class="audio-play" @click='start(audioId)'>
			<image class='audio-icon' :src='startPic' v-show='!status'></image>
			<image class='audio-icon' :src='endPic' v-show='status'></image>
		</view>
		<view class='audio-time mg18'>{{getTime(Math.round(currentTime))}}</view>
		<view class='audio-slider'>
			<slider @change='changeAudio' :activeColor='activeColor' :block-size="25" :min='0' :max='duration.toFixed(0)' :value='currentTime.toFixed(0)' :step='0.1'></slider>
		</view>
		<view class='audio-time'>{{getTime(Math.round(duration))}}</view>
	</view>
</template>

<script>
	export default {
		props: {
			url: String,
			activeColor: {
				type: String,
				default: '#FF5030'
			},
			audioId: [String,Number]
		},
		data() {
			return {
				endPic: require("@/static/hall/hear_play_icon.png"),
				startPic: require("@/static/hall/hear_suspend_icon.png"),
				context: null,
				currentTime: 0,
				duration: 100,
				status: false
			}
		},
		created() {
			this.context = uni.createInnerAudioContext();
			this.context.src = this.url;
			this.onTimeUpdate();
			this.onCanplay();
			this.onEnded();
			uni.$on('stop',(id)=> {
				if(id && id != this.audioId) {
					this.context.stop();
					this.status = false;
				} else if(!id){
					this.context.stop();
					this.status = false;
				}
			})
		},
		methods: {
			start(id) { //点击播放
				let audioId = id;
				if(this.status) {
					this.context.pause();
					this.status = !this.status;
				}else {
					uni.$emit('stop',id)
					this.context.play()
					this.status = !this.status;
				}
			},
			onCanplay() { //进入可播放状态
				this.context.onCanplay(() => {
					this.context.duration;
					setTimeout(()=>{
						this.duration = this.context.duration;
					},1000)
				})
			},
			onTimeUpdate() { //音频播放进度
				 this.context.onTimeUpdate(() => {
					 if (!Number.isFinite( this.context.duration)) {
						this.duration = this.context.currentTime + 10;
						this.currentTime = this.context.currentTime;
					 } else {
						 this.duration = this.context.duration;
						 this.currentTime = this.context.currentTime;
					 }
				 })
			},
			onEnded() { //播放结束
				this.context.onEnded(()=> {
					this.status = false;
					this.currentTime = 0;
				})
			},
			changeAudio(e) {
				let paused = this.context.paused;
				this.context.pause();
				this.context.seek(e.detail.value)
				if(!paused) {
					this.context.play();
				} 
			},
			getTime(time) {
				let m = parseInt(time / 60);
				let s = time % 60;
				return this.towNum(m) + ':' + this.towNum(s);
			},
			towNum(num) {
				if(num >= 10) {
					return num;
				}else {
					return '0' + num;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.mg18 {
		margin: 0 18rpx;
	}
	.audio {
		display: flex;
		flex-direction: row;
		align-items: center;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #757575;
		&-play {
			display: flex;
			flex-direction: row;
			align-items: center;
		}
		&-icon {
			width: 48rpx;
			height: 48rpx;
		}
		&-slider {
			flex: 1;
		}
		&-time {
			flex-shrink: 0;
		}
	}
</style>