-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAudio.vue
96 lines (92 loc) · 2.42 KB
/
Audio.vue
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
<template>
<div>
<div ref="waveform" class="wave pb-1"></div>
<v-btn block @click="onPlay" v-show="!audio.playing" small>
<v-icon class="pr-5" small>play_circle_outline</v-icon>
<span>{{formattedCurrentTime}}</span> / <span>{{formattedDuration}}</span>
</v-btn>
<v-btn block @click="onPause" v-show="audio.playing" small>
<v-icon class="pr-5" small>pause_circle_outline</v-icon>
<span>{{formattedCurrentTime}}</span> / <span>{{formattedDuration}}</span>
</v-btn>
</div>
</template>
<script>
import WaveSurfer from 'wavesurfer.js'
export default {
name: "Audio",
props: {
value: {
type: String,
required: true,
}
},
data(){
return {
audio: {
playing: null,
currentTime: null,
duration: null,
},
wavesurfer: null
}
},
methods: {
onPlay(){
this.wavesurfer.play()
},
onPause(){
this.wavesurfer.pause()
},
format(duration){
let date = new Date(0)
date.setSeconds(duration)
return date.toISOString().substr(11, 8)
}
},
computed: {
formattedCurrentTime(){
return this.format(this.audio.currentTime)
},
formattedDuration(){
return this.format(this.audio.duration)
},
},
mounted() {
let curTheme = this.$vuetify.theme.isDark ? this.$vuetify.theme.themes.dark : this.$vuetify.theme.themes.light
this.wavesurfer = WaveSurfer.create({
container: this.$refs.waveform,
waveColor: curTheme.primary,
progressColor: curTheme.secondary,
cursorColor: curTheme.accent,
height: 100,
cursorWidth: 3,
responsive: true
})
this.wavesurfer.on('ready', () => {
this.audio.duration = this.wavesurfer.getDuration()
})
this.wavesurfer.on('play', () => {
this.audio.playing = true
})
this.wavesurfer.on('pause', () => {
this.audio.playing = false
})
this.wavesurfer.on('audioprocess', () => {
this.audio.currentTime = this.wavesurfer.getCurrentTime()
})
this.wavesurfer.on('seek', () => {
this.audio.currentTime = this.wavesurfer.getCurrentTime()
})
this.wavesurfer.load(this.value)
},
beforeDestroy() {
this.wavesurfer.destroy()
}
}
</script>
<style scoped>
.wave {
cursor: text;
}
</style>