网易云audio组件的封装

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<template>
<audio class="audio-play" :src="src" ref="audio"></audio>
</template>

<script>
export default {
name: 'audio-play',
data() {
return {}
},
props: {
// 音源
src: {
default: '',
type: String
},
// 静音
muted: {
default: false,
type: Boolean
},
// 是否循环
loop: {
default: false,
type: Boolean
},
// 当前播放时间
currentTime: {
default: 0,
type: Number
},
// 音频的总时长
duration: {

},
// 是否结束
isEnded: {
default: false,
type: Boolean
},
// 用户是否在音频中移动或者跳跃到新的播放点
seeking: {
default: 1,
type: Number
},
// 是否暂停
paused: {
default: false,
type: Boolean
},
// 音量
volume: {
default: 0,
type: Number
},
/* 播放倍速:
设置或者查询音频的播放速度,
1表示正常速度,大于1表示快进,
0-1之间表示慢进,
0表示暂停(控制面板仍然是播放,仅仅是速度为0)
*/
playbackRate: {
default: 0,
type: Number
}

},
// 相关方法
// play()播放
// pause()暂停
// 事件
mounted() {
// loadstart:开始载入音频时触发
this.$refs.audio.onloadstart = () => {
this.$emit('loadstart', this.$refs.audio.duration)
}
// duracyionchange:duration属性更新时触发
this.$refs.audio.onduracyionchange = () => {
this.$emit('duracyionchange', this.$refs.audio.duration)
}
// loadeddata:音频的第一帧加载完成时触发,此时整个音频还未加载完
this.$refs.audio.onloadeddata = () => {
this.$emit('loadeddata', this.$refs.audio.duration)
}
// loadedmetadata:音频元数据加载完成时触发
this.$refs.audio.onloadedmetadata = () => {
this.$emit('loadedmetadat', this.$refs.audio.duration)
}
// progress:音屏正在加载时触发
this.$refs.audio.onprogress = () => {
this.$emit('progress', this.$refs.audio.duration)
}
// canplay :浏览器能够开始播放音频时触发
this.$refs.audio.oncanplay = () => {
// 播放
if (!this.paused) {
this.play()
} else {
this.pause()
}
this.$emit('canplay', this)
}
// canplaythrough:浏览器预计在不停下来进行缓冲的情况下,能够持续播放指定的音频时会触发
this.$refs.audio.oncanplaythrough = () => {
this.$emit('canplaythrough', this)

}
// ended播放结束
this.$refs.audio.onended = () => {
this.$emit('ended', this)

}
// error播放错误
this.$refs.audio.onerror = (err) => {
this.$emit('error', err)

}
// timeupdate时间更改
this.$refs.audio.ontimeupdate = () => {
this.$emit('timeupdate', this.$refs.audio.currentTime)
}
// volumechange音量更改
this.$refs.audio.onvolumechange = () => {
this.$emit('volumechange', this.$refs.audio.volume)
}
},
created() { },
methods: {
play() {
if (this.$refs.audio) {
this.$refs.audio.play()
}
},
pause() {
if (this.$refs.audio) {
this.$refs.audio.pause()
}
}
},
watch: {
// props传参时第一次时不会立即触发对应的watch
// 音源没法设置
// src: {
// handler(newValue, oldValue) {
// if (newValue) {
// this.$refs.audio.src = newValue
// }
// },
// immediate: true
// },
// 静音
muted: {
handler(newValue) {
if (newValue) {
this.$refs.audio.muted = newValue
}
},
immediate: true
},
// 是否循环
loop: {
handler(newValue) {
if (newValue) {
this.$refs.audio.loop = newValue
}
},
immediate: true
},
// 当前播放时间
currentTime: {
handler(newValue) {
if(newValue){
this.$refs.audio.currentTime = newValue
}
},
immediate: true
},
// 是否暂停
paused: {
handler(newValue) {
if (newValue) {
this.pause()
}
}
},
// 音量
volume: {
handler(newValue) {
if(newValue){
this.$refs.audio.volume = newValue
}
},
immediate: true
},
/* 播放倍速:
设置或者查询音频的播放速度,
1表示正常速度,大于1表示快进,
0-1之间表示慢进,
0表示暂停(控制面板仍然是播放,仅仅是速度为0)
*/
playbackRate: {
handler(newValue) {
if(newValue){
this.$refs.audio.playbackRate = newValue
}
},
immediate: true
}
},
computed: {},
components: {},
}
</script>
<style scoped></style>