-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRecorder.vue
167 lines (158 loc) · 4.69 KB
/
Recorder.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
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
<template>
<v-container>
<v-row>
<v-col cols="12" class="pt-0">
<div ref="waveform"></div>
</v-col>
</v-row>
<v-row>
<v-col cols="8" class="pb-0">
<v-select v-model="deviceId"
:label="$t('note.record.device')"
:items="devices"
item-value="deviceId"
item-text="label"
:disabled="recorder.recording"
@change="onDeviceChange"
>
</v-select>
</v-col>
<v-col cols="4" class="pb-0">
<div v-show="!deviceId" class="pt-4">
<v-icon color="error" x-large>mic_off</v-icon>
</div>
<div v-show="deviceId && !recorder.recording" class="pt-4">
<v-btn @click="onStart">
<v-icon x-large color="error">fiber_manual_record</v-icon>
</v-btn>
</div>
<div v-show="recorder.recording" class="pt-4">
<v-btn @click="onPause" v-show="!recorder.paused">
<v-icon x-large>pause</v-icon>
</v-btn>
<v-btn @click="onResume" v-show="recorder.paused">
<v-icon x-large>play_arrow</v-icon>
</v-btn>
<v-btn @click="onStop">
<v-icon x-large color="error">stop</v-icon>
</v-btn>
<span class="text-center">{{formattedTime}}</span>
</div>
</v-col>
</v-row>
</v-container>
</template>
<script>
import WaveSurfer from 'wavesurfer.js'
import Microphone from 'wavesurfer.js/dist/plugin/wavesurfer.microphone'
import RecordRTC from "recordrtc";
export default {
name: "Recorder",
data() {
return {
recorder: {
recorder: null,
recording: null,
paused: null,
timeTicker: null,
time: 0,
},
deviceId: null,
devices: [],
wavesurfer: null,
}
},
computed: {
formattedTime(){
let date = new Date(0)
date.setSeconds(this.recorder.time)
return date.toISOString().substr(11, 8)
}
},
methods: {
onStart(){
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,
interact: false,
responsive: true,
cursorWidth: 0,
constraint: {audio: {deviceId: this.deviceId}},
plugins: [
Microphone.create()
]
});
this.wavesurfer.microphone.on('deviceReady', stream => {
this.recorder.recorder = RecordRTC(stream, {
type: 'audio',
})
this.recorder.recorder.startRecording()
this.recorder.recording = true
});
this.wavesurfer.microphone.on('deviceError', code => {
console.warn('Device error: ' + code);
});
this.recorder.time = 0
this.wavesurfer.microphone.start()
this.startTimeTicker()
},
onResume(){
this.wavesurfer.microphone.play()
this.recorder.recorder.resumeRecording()
this.startTimeTicker()
this.recorder.paused = false
},
onPause(){
this.wavesurfer.microphone.pause()
this.recorder.recorder.pauseRecording()
this.stopTimeTicker()
this.recorder.paused = true
},
onStop(){
this.wavesurfer.destroy()
this.wavesurfer = null
this.recorder.recorder.stopRecording(() => {
this.recorder.recorder.getDataURL(url => {
this.recorder.recorder = null
this.recorder.recording = false
this.recorder.paused = false
this.$emit('input', url)
})
})
this.stopTimeTicker()
},
onDevices(devices) {
this.devices = devices;
//if no device is selected, select the first one
if(!this.deviceId && this.devices) {
this.onDeviceChange(devices[0].deviceId)
}
},
onDeviceChange(deviceId) {
this.deviceId = deviceId
},
startTimeTicker(){
this.recorder.timeTicker = setInterval(() => {
this.recorder.time += 0.25
}, 250)
},
stopTimeTicker(){
clearInterval(this.recorder.timeTicker)
}
},
mounted() {
navigator.mediaDevices.enumerateDevices()
.then(allDevices => {
let audioInputDevices = allDevices.filter(d => d.kind === 'audioinput')
this.onDevices(audioInputDevices)
})
},
beforeDestroy() {
if(this.wavesurfer) this.wavesurfer.destroy()
}
}
</script>
<style scoped>
</style>