Skip to content

Commit bca3017

Browse files
committed
#1 Cleaning up the code on the main component. Abstracting more logic into the timer component.
1 parent 54f7300 commit bca3017

File tree

3 files changed

+89
-96
lines changed

3 files changed

+89
-96
lines changed

components/CurrentTimer.vue

-28
This file was deleted.

components/Timer.vue

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<template>
2+
<p class="display-2 text-center">{{ minutes }}</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
props: {
8+
timerStatus: {
9+
type: String,
10+
default: 'stopped' // 'started'
11+
}
12+
},
13+
data() {
14+
return {
15+
elapsedTime: 0,
16+
interval: null,
17+
isRunning: false
18+
}
19+
},
20+
computed: {
21+
minutes() {
22+
let minutes = Math.floor(this.elapsedTime / 60)
23+
let seconds = this.elapsedTime - minutes * 60
24+
25+
if (minutes < 10) {
26+
minutes = '0' + minutes
27+
}
28+
if (seconds < 10) {
29+
seconds = '0' + seconds
30+
}
31+
return minutes + ':' + seconds
32+
}
33+
},
34+
watch: {
35+
timerStatus(newVal, oldVal) {
36+
if (newVal !== oldVal) {
37+
if (newVal === 'stopped') {
38+
this.stopTimer()
39+
} else {
40+
this.toggleTimer()
41+
}
42+
}
43+
}
44+
},
45+
methods: {
46+
toggleTimer() {
47+
if (this.isRunning) {
48+
clearInterval(this.interval)
49+
} else {
50+
this.interval = setInterval(this.incrementTime, 1000)
51+
}
52+
this.isRunning = !this.isRunning
53+
},
54+
incrementTime() {
55+
this.elapsedTime = +this.elapsedTime + 1
56+
},
57+
stopTimer() {
58+
// reset the count
59+
this.elapsedTime = 0
60+
// and reset the timer
61+
clearInterval(this.interval)
62+
this.isRunning = false
63+
}
64+
}
65+
}
66+
</script>

pages/index.vue

+23-68
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@
88
<v-toolbar color="primary" flat>
99
<v-toolbar-title>VoiceMail</v-toolbar-title>
1010
<v-spacer></v-spacer>
11+
12+
<!-- Start / Finish recording -->
1113
<v-btn
1214
:color="isRecording ? 'red' : 'primary darken-2'"
1315
@click="toggleRecorder"
1416
>
1517
<v-icon dark>{{
16-
isRecording ? 'mdi-pause' : 'mdi-microphone'
18+
isRecording ? 'mdi-stop' : 'mdi-microphone'
1719
}}</v-icon>
1820
</v-btn>
19-
<v-btn icon @click="stopRecording">
20-
<v-icon>mdi-stop</v-icon>
21+
22+
<!-- discard recording -->
23+
<v-btn icon :disabled="!isRecording" @click="stopRecording">
24+
<v-icon small>mdi-delete</v-icon>
2125
</v-btn>
2226
</v-toolbar>
2327
<v-card-text>
24-
<current-timer :elapsed-time="timer"></current-timer>
28+
<timer :timer-status="timerStatus"></timer>
2529

2630
<recordings-list
2731
v-model="selected"
@@ -53,21 +57,18 @@
5357
import { WebAudioRecorder } from 'web-audio-recorder-js-webpack'
5458
5559
import RecordingsList from '@/components/RecordingsList'
56-
import CurrentTimer from '@/components/CurrentTimer'
60+
import Timer from '@/components/Timer'
5761
5862
const ENCODING_TYPE = 'mp3'
5963
const ENCODE_AFTER_RECORD = true
6064
6165
export default {
6266
components: {
6367
RecordingsList,
64-
CurrentTimer
68+
Timer
6569
},
6670
data() {
6771
return {
68-
availableDevices: [],
69-
selectedDevice: null,
70-
logData: '',
7172
isRecording: false,
7273
audios: [
7374
{ isPlaying: false, duration: '01:45', volume: 50 },
@@ -80,58 +81,13 @@ export default {
8081
input: null,
8182
audioContext: null,
8283
selected: null,
83-
timer: null
84-
}
85-
},
86-
async created() {
87-
if (navigator && navigator.mediaDevices) {
88-
const mediaDevices = await navigator.mediaDevices.getUserMedia({
89-
audio: true
90-
})
91-
92-
if (mediaDevices) {
93-
const devices = await navigator.mediaDevices.enumerateDevices()
94-
95-
this.availableDevices = devices.filter(
96-
(device) => device.kind === 'audioinput'
97-
)
98-
}
84+
timerStatus: 'stopped'
9985
}
10086
},
10187
methods: {
10288
toggleRecorder() {
10389
this.isRecording = !this.isRecording
104-
this.toggleTimer()
105-
},
106-
toggleTimer() {
107-
if (this.isRunning) {
108-
clearInterval(this.interval)
109-
console.log('timer stops')
110-
} else {
111-
this.interval = setInterval(this.incrementTime, 1000)
112-
}
113-
this.isRunning = !this.isRunning
114-
},
115-
incrementTime() {
116-
this.timer = +this.timer + 1
117-
},
118-
stopTimer() {
119-
// reset the count
120-
this.timer = 0
121-
// and reset the timer
122-
clearInterval(this.interval)
123-
this.isRunning = false
124-
},
125-
inputChanged() {
126-
// console.log('input changed')
127-
if (this.getUserMediaStream) {
128-
}
129-
navigator.mediaDevices.getUserMedia({
130-
audio: true,
131-
deviceId: {
132-
exact: this.selectedDevice
133-
}
134-
})
90+
this.timerStatus = this.isRecording ? 'started' : 'stopped'
13591
},
13692
startRecording() {
13793
if (navigator.mediaDevices) {
@@ -154,7 +110,7 @@ export default {
154110
)
155111
})
156112
157-
this.log(
113+
console.warn(
158114
'getUserMedia() success, stream created, initializing WebAudioRecorder...'
159115
)
160116
@@ -172,30 +128,30 @@ export default {
172128
encoding: ENCODING_TYPE,
173129
onEncoderLoading: (recorder, encoding) => {
174130
// show "loading encoder..." display
175-
this.log('Loading ' + encoding + ' encoder...')
131+
console.warn('Loading ' + encoding + ' encoder...')
176132
},
177133
onEncoderLoaded: (recorder, encoding) => {
178134
// hide "loading encoder..." display
179-
this.log(encoding + ' encoder loaded')
135+
console.warn(encoding + ' encoder loaded')
180136
},
181137
onComplete: (recorder, blob) => {
182-
this.log('Encoding complete')
138+
console.warn('Encoding complete')
183139
const url = URL.createObjectURL(blob)
184140
this.audios.push(url)
185141
// createDownloadLink(blob, recorder.encoding);
186142
}
187143
})
188144
189145
this.recorder.setOptions({
190-
timeLimit: 300,
146+
timeLimit: 60,
191147
encodeAfterRecord: ENCODE_AFTER_RECORD,
192148
mp3: {
193149
bitRate: 160
194150
}
195151
})
196152
197153
this.recorder.startRecording()
198-
this.log('Recording started')
154+
console.warn('Recording started')
199155
this.isRecording = true
200156
})
201157
.catch((err) => {
@@ -205,8 +161,7 @@ export default {
205161
},
206162
stopRecording() {
207163
this.isRecording = false
208-
209-
this.stopTimer()
164+
this.timerStatus = 'stopped'
210165
211166
// // stop microphone access
212167
// //! can't do this, otherwise can't record further notes
@@ -216,12 +171,12 @@ export default {
216171
// // every single time a new recording is started 🤔
217172
// this.getUserMediaStream.getAudioTracks()[0].stop()
218173
219-
// // tell the recorder to finish the recording (stop recording + encode the recorded audio)
220-
// this.recorder.finishRecording()
221-
// this.log('Recording stopped')
174+
// tell the recorder to finish the recording (stop recording + encode the recorded audio)
175+
// this.recorder.finishRecording()
176+
// console.warn('Recording stopped')
222177
// },
223178
// log(event) {
224-
// this.logData += event + `<br>`
179+
// console.warnData += event + `<br>`
225180
// }
226181
}
227182
}

0 commit comments

Comments
 (0)