Files
dvcp_v2_wxcp_app/components/AiVideo.vue

174 lines
3.6 KiB
Vue
Raw Normal View History

2021-11-15 10:29:05 +08:00
<template>
2021-12-15 14:37:20 +08:00
<view class="imt-audio">
<view class="audio-wrapper">
<view class="audio-number">{{ format(current) }}</view>
<slider class="audio-slider" :activeColor="color" block-size="16" :value="current" :max="duration || 10"
@changing="seek=true,current=$event.detail.value" @change="audio.seek($event.detail.value)"></slider>
<view class="audio-number">{{ format(duration) }}</view>
</view>
<view class="audio-control-wrapper" :style="{color}">
2021-11-15 10:29:05 +08:00
<image
2021-12-15 14:37:20 +08:00
class="audio-control audio-control-switch"
@click="audio.paused?play():audio.pause()"
2021-12-16 18:30:17 +08:00
:src="`${$cdn}video/${paused ? 'play-icon' : 'stop-img'}.png`"/>
2021-11-15 10:29:05 +08:00
<p>{{ paused ? '点击播放' : '点击停止播放' }}</p>
2021-12-15 14:37:20 +08:00
</view>
</view>
2021-11-15 10:29:05 +08:00
</template>
<script>
2021-12-15 14:37:20 +08:00
export default {
data() {
return {
audio: uni.createInnerAudioContext(),
current: 0, //当前进度(s)
duration: 0, //总时长(s)
paused: true, //是否处于暂停状态
loading: false, //是否处于读取状态
seek: false,
}
},
props: {
src: String, //音频链接
autoplay: Boolean, //是否自动播放
continue: Boolean, //播放完成后是否继续播放下一首,需定义@next事件
control: {
type: Boolean,
default: true
}, //是否需要上一曲/下一曲按钮
color: {
type: String,
default: '#007BFF'
} //主色调
},
methods: {
//返回prev事件
prev() {
this.$emit('prev')
},
//返回next事件
next() {
this.$emit('next')
},
//格式化时长
format(num) {
return '0'.repeat(2 - String(Math.floor(num / 60)).length) + Math.floor(num / 60) + ':' + '0'.repeat(2 - String(Math.floor(num % 60)).length) + Math.floor(num % 60)
},
//点击播放按钮
play() {
this.audio.play()
this.loading = true
}
},
created() {
if (this.src) {
this.audio.src = this.src
this.autoplay && this.play()
}
this.audio.obeyMuteSwitch = false
//音频进度更新事件
this.audio.onTimeUpdate(() => {
if (!this.seek) {
this.current = this.audio.currentTime
}
if (!this.duration) {
this.duration = this.audio.duration
}
})
//音频播放事件
this.audio.onPlay(() => {
this.paused = false
this.loading = false
})
//音频暂停事件
this.audio.onPause(() => {
this.paused = true
})
//音频结束事件
this.audio.onEnded(() => {
if (this.continue) {
this.next()
} else {
this.paused = true
this.current = 0
}
})
//音频完成更改进度事件
this.audio.onSeeked(() => {
this.seek = false
})
},
beforeDestroy() {
this.audio.destroy()
},
watch: {
src(src, old) {
this.audio.src = src
this.current = 0
this.duration = 0
if (old || this.autoplay) {
this.play()
}
}
}
}
2021-11-15 10:29:05 +08:00
</script>
<style>
2021-12-15 14:37:20 +08:00
.imt-audio {
background: #fff;
2021-12-16 18:30:17 +08:00
border-radius: 20px;
2021-12-15 14:37:20 +08:00
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
.audio-wrapper {
display: flex;
align-items: center;
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
.audio-number {
2021-12-16 18:30:17 +08:00
width: 120px;
font-size: 24px;
2021-12-15 14:37:20 +08:00
line-height: 1;
color: #999999;
text-align: center;
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
.audio-slider {
flex: 1;
margin: 0;
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
.audio-control-wrapper {
2021-12-16 18:30:17 +08:00
margin-top: 40px;
2021-12-15 14:37:20 +08:00
text-align: center;
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
.audio-control-wrapper p {
color: #999999;
2021-12-16 18:30:17 +08:00
font-size: 26px;
2021-12-15 14:37:20 +08:00
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
.audio-control-wrapper image {
2021-12-16 18:30:17 +08:00
width: 128px;
height: 128px;
2021-12-15 14:37:20 +08:00
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
.audio-control {
2021-12-16 18:30:17 +08:00
font-size: 32px;
2021-12-15 14:37:20 +08:00
line-height: 1;
border-radius: 50%;
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
.audio-control-switch {
2021-12-16 18:30:17 +08:00
font-size: 40px;
margin: 0 100px;
2021-12-15 14:37:20 +08:00
}
2021-11-15 10:29:05 +08:00
2021-12-15 14:37:20 +08:00
@keyframes loading {
to {
transform: rotate(360deg);
}
}
2021-11-15 10:29:05 +08:00
</style>