Skip to content

Commit 376c673

Browse files
committed
AudioPlayer
1 parent 3216942 commit 376c673

3 files changed

Lines changed: 130 additions & 0 deletions

File tree

src/App.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<template>
22
<div id="app">
3+
<!-- <keep-alive> -->
34
<router-view />
5+
<!-- </keep-alive> -->
46
</div>
57
</template>
68

src/components/AudioPlayer.vue

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<template>
2+
<div>
3+
<h1>AudioPlayer</h1>
4+
<div>{{ src }}</div>
5+
<div>{{ duration }}</div>
6+
<div>{{ currentTime }}</div>
7+
<div>{{ status }}</div>
8+
<button @click="change">change</button>
9+
<button @click="$router.back()">back</button>
10+
</div>
11+
</template>
12+
13+
<script>
14+
const audioNodeList = [];
15+
16+
export default {
17+
name: 'AudioPlayer',
18+
props: {
19+
src: {
20+
type: String,
21+
default: '',
22+
},
23+
},
24+
data() {
25+
return {
26+
el: null,
27+
duration: 0,
28+
currentTime: 0,
29+
status: 'pause',
30+
};
31+
},
32+
computed: {},
33+
watch: {
34+
src: {
35+
immediate: true,
36+
handler() {
37+
this.init();
38+
},
39+
},
40+
},
41+
beforeDestroy() {
42+
this.resetAudio(this.el);
43+
},
44+
deactivated() {
45+
this.resetAudio(this.el);
46+
},
47+
methods: {
48+
init() {
49+
this.createAudioEl();
50+
},
51+
oncanplay() {
52+
this.duration = this.el.duration;
53+
},
54+
onplay() {
55+
this.status = 'play';
56+
},
57+
onpause() {
58+
this.status = 'pause';
59+
},
60+
onended() {
61+
this.status = 'onended';
62+
},
63+
ontimeupdate() {
64+
if (!this.el) return;
65+
this.currentTime = this.el.currentTime;
66+
},
67+
createAudioEl() {
68+
if (!this.src) return;
69+
this.el = new Audio(this.src);
70+
this.el.addEventListener('canplay', this.oncanplay);
71+
this.el.addEventListener('play', this.onplay);
72+
this.el.addEventListener('pause', this.onpause);
73+
this.el.addEventListener('ended', this.onended);
74+
this.el.addEventListener('timeupdate', this.ontimeupdate);
75+
audioNodeList.push(this.el);
76+
},
77+
resetAudio(el) {
78+
if (!el) return;
79+
el.pause();
80+
el.currentTime = 0;
81+
},
82+
closeAll() {
83+
for (const el of audioNodeList) {
84+
if (el && !el.paused) {
85+
this.resetAudio(el);
86+
}
87+
}
88+
},
89+
play() {
90+
this.closeAll();
91+
this.el.play();
92+
},
93+
pause() {
94+
this.el.pause();
95+
},
96+
change() {
97+
if (!this.el) return;
98+
this.el.paused ? this.play() : this.pause();
99+
},
100+
},
101+
};
102+
</script>
103+
104+
<style lang="scss" scoped></style>

src/views/AudioPlayerList.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div>
3+
<h1>AudioPlayerList</h1>
4+
<AudioPlayer src="static/music/world.mp3" />
5+
<AudioPlayer src="static/music/周杰伦 - 不能说的秘密(正式EP版).mp3" />
6+
</div>
7+
</template>
8+
9+
<script>
10+
import AudioPlayer from '@/components/AudioPlayer';
11+
12+
export default {
13+
name: 'AudioPlayerList',
14+
components: { AudioPlayer },
15+
mounted() {
16+
console.log('***mounted');
17+
},
18+
activated() {
19+
console.log('***activated');
20+
},
21+
};
22+
</script>
23+
24+
<style lang="scss" scoped></style>

0 commit comments

Comments
 (0)