AudioComponent.vue
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<template>
<view class="audio-box flex">
<view
:class="['sprite-icon',`${paused? 'icon-play' : 'icon-pause'}`]"
@click="play"
></view>
<slider
@change="sliderChange"
backgroundColor="#D8D8D8"
block-color="#2C66F7"
block-size="5"
class="progress-box"
:max="innerAudioContext[audioKey].duration||10"
:value="innerAudioContext[audioKey].currentTime||0"
/>
<view class="duration">{{currentTime}}/{{duration}}</view>
</view>
</template>
<script type='text/ecmascript-6'>
export default {
name: 'AudioComponent',
props: {
src: String,
auto: Boolean,
audioKey: String
},
data () {
return {
innerAudioContext: {},
currentTime: '00:00',
duration: '00:00',
paused: true,
progress: ''
}
},
watch: {
src: {
immediate: true,
handler (newval) {
if (newval) this.init(newval)
}
},
},
computed: {
},
destroyed () {
this.innerAudioContext[this.audioKey].destroy()
},
mounted () {
},
methods: {
init (src) {
const innerAudioContext = uni.createInnerAudioContext(),
{ audioKey } = this
innerAudioContext.src = src
innerAudioContext.sessionCategory = 'soloAmbient'
innerAudioContext.onCanplay(this.onCanplay)
innerAudioContext.onPlay(() => this.paused = false)
innerAudioContext.onPause(() => this.paused = true)
innerAudioContext.onTimeUpdate(this.onTimeUpdate)
this.innerAudioContext[audioKey] = innerAudioContext
},
onCanplay () {
this.duration = this.formatTime(this.innerAudioContext[this.audioKey].duration)
},
onTimeUpdate () {
const {
duration,
currentTime
} = this.innerAudioContext[this.audioKey],
mergedOffset = Math.ceil(currentTime)
this.progress = mergedOffset / duration * 100
this.currentTime = this.formatTime(currentTime)
},
formatTime (num) {
num = Math.floor(num);
let minutes = Math.floor(num / 60) || '0',
seconds = num % 60 || '0'
seconds = seconds < 10 ? `0${seconds}` : seconds
return `${minutes}:${seconds}`
},
play () {
if (this.paused) {
if (this.auto) this.innerAudioContext[this.audioKey].play()
this.$emit('onPlay')
}
else this.innerAudioContext[this.audioKey].pause()
},
stop () {
this.currentTime = '00:00'
this.innerAudioContext[this.audioKey].stop()
},
seek (e) {
let query = uni.createSelectorQuery()
query.select('.progress-box').boundingClientRect(data => {
const seconds = (e.changedTouches[0].clientX - data.left) / data.width * this.innerAudioContext[this.audioKey].duration
this.innerAudioContext[this.audioKey].seek(seconds)
}).exec()
},
sliderChange (e) {
this.innerAudioContext[this.audioKey].seek(e.detail.value)
}
}
}
</script>
<style scoped lang='scss'>
.audio-box {
width: toRpx(1120);
height: toRpx(80);
background-image: url('@/assets/images/common/audio-bg.webp');
background-size: 100%;
margin-bottom: toRpx(24);
box-sizing: border-box;
padding: 0 toRpx(32);
font-size: toRpx(28);
color: #333333;
.sprite-icon {
margin-right: toRpx(16);
}
.progress-box {
width: toRpx(796);
margin: 0 !important;
/deep/.uni-slider-track {
background-image: linear-gradient(90deg, #2c66f7 0%, #75bcff 100%);
}
/deep/.uni-slider-thumb {
&::after {
width: toRpx(14);
height: toRpx(14);
border: toRpx(2) solid #f4f4f4;
content: '';
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
border-radius: 100%;
box-sizing: border-box;
}
}
}
.duration {
margin-left: auto;
flex-shrink: 0;
}
}
</style>