Skip to content

Commit 3494ec1

Browse files
committed
✨ add audio play/pause event
1 parent 46e1536 commit 3494ec1

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

music-player/src/components/SongPlayer.vue

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<template>
2+
<audio :src="song.songSrc" preload="auto" autoplay ref="audioPlayer" />
23
<div class="text-white">
34
<div class="flex flex-row justify-between" @click="goback">
45
<button>Back</button>
@@ -19,11 +20,11 @@
1920
<div class="flex items-center justify-center" @click="previous">
2021
<button>Previous</button>
2122
</div>
22-
<div class="flex items-center justify-center">
23+
<div class="flex items-center justify-center" @click="togglePlay">
2324
<button
2425
class="rounded-full bg-yellow-300 h-24 w-24 text-black font-bold"
2526
>
26-
Play
27+
{{ isPlaying ? "Pause" : "Play" }}
2728
</button>
2829
</div>
2930
<div class="flex items-center justify-center" @click="next">
@@ -36,6 +37,12 @@
3637

3738
<script>
3839
export default {
40+
name: "SongPlayer",
41+
data() {
42+
return {
43+
isPlaying: true,
44+
};
45+
},
3946
props: {
4047
song: {
4148
id: Number,
@@ -58,6 +65,15 @@ export default {
5865
previous() {
5966
this.$emit("previous");
6067
},
68+
togglePlay() {
69+
if (this.isPlaying) {
70+
this.$refs.audioPlayer.pause();
71+
} else {
72+
this.$refs.audioPlayer.play();
73+
}
74+
75+
this.isPlaying = !this.isPlaying;
76+
},
6177
},
6278
};
6379
</script>

0 commit comments

Comments
 (0)