Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AudioRecorder.vue « AudioRecorder « NewMessageForm « components « src - github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a4119e951d62cc74ed29d44e2586550795d618d (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!--
  - @copyright Copyright (c) 2021 Marco Ambrosini <marcoambrosini@pm.me>
  -
  - @author Marco Ambrosini <marcoambrosini@pm.me>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<template>
	<div
		class="audio-recorder">
		<button
			v-if="!isRecording"
			class="audio-recorder__trigger nc-button nc-button__main"
			@click="start">
			<Microphone
				:size="16"
				title=""
				decorative />
		</button>
		<div v-else class="wrapper">
			<button
				class="audio-recorder__stop nc-button nc-button__main"
				@click="abortRecording">
				<Close
					:size="16"
					title=""
					decorative />
			</button>
			<div class="audio-recorder__info">
				<div class="recording-indicator fadeOutIn" />
				<span
					class="time">
					{{ parsedRecordTime }}</span>
			</div>
			<button
				class="audio-recorder__trigger nc-button nc-button__main"
				:class="{'audio-recorder__trigger--recording': isRecording}"
				@click="stop">
				<Check
					:size="16"
					title=""
					decorative />
			</button>
		</div>
	</div>
</template>

<script>
import Microphone from 'vue-material-design-icons/Microphone'
import Close from 'vue-material-design-icons/Close'
import Check from 'vue-material-design-icons/Check'

import { mediaDevicesManager } from '../../../utils/webrtc/index'

export default {
	name: 'AudioRecorder',

	components: {
		Microphone,
		Close,
		Check,
	},

	data() {
		return {
			// The audio stream object
			audioStream: null,
			// The media recorder which generate the recorded chunks
			mediaRecorder: null,
			// The chunks array
			chunks: [],
			// The final audio file blob
			blob: null,
			// The blob url
			URL: '',
			// Switched to true if the recording is aborted
			aborted: false,
			// recordTimer
			recordTimer: null,
			// the record timer
			recordTime: {
				minutes: 0,
				seconds: 0,
			},
		}
	},

	computed: {
		// Recording state of the mediaRecorder
		isRecording() {
			if (this.mediaRecorder) {
				return this.mediaRecorder.state === 'recording'
			} else {
				return false
			}
		},

		parsedRecordTime() {
			const seconds = this.recordTime.seconds.toString().length === 2 ? this.recordTime.seconds : `0${this.recordTime.seconds}`
			const minutes = this.recordTime.minutes.toString().length === 2 ? this.recordTime.minutes : `0${this.recordTime.minutes}`
			return `${minutes}:${seconds}`
		},
	},

	watch: {

		isRecording(newValue) {
			console.debug('isRecording', newValue)
		},
	},

	methods: {
		/**
		 * Initialize the media stream and start capturing the audio
		 */
		async start() {
			try {
				// Create new audio stream
				this.audioStream = await mediaDevicesManager.getUserMedia({
					audio: true,
				})
				// Create a mediarecorder to capture the stream
				this.mediaRecorder = new MediaRecorder(this.audioStream)
				// Add event handler to ondataAvailable
				this.mediaRecorder.ondataavailable = (e) => {
					this.chunks.push(e.data)
				}
				// Add event handler to onstop
				this.mediaRecorder.onstop = this.generateFile
				// Start the recording
				this.mediaRecorder.start()
				// Start the timer
				this.recordTimer = setInterval(() => {
					if (this.recordTime.seconds === 59) {
						this.recordTime.minutes++
						this.recordTime.seconds = 0
					}
					this.recordTime.seconds++
				}, 1000)
				// Forward an event to let the parent NewMessageForm component
				// that there's an undergoing recording operation
				this.$emit('recording', true)
				console.debug(this.mediaRecorder.state)
			} catch (exception) {
				console.debug(exception)
			}
		},

		/**
		 * Stop the mediaRecorder
		 */
		stop() {
			this.mediaRecorder.stop()
			clearInterval(this.recordTimer)
			this.$emit('recording', false)
		},

		/**
		 * Generate the file
		 */
		generateFile() {
			if (!this.aborted) {
				this.blob = new Blob(this.chunks, { 'type': 'audio/mpeg-3' })
				// Convert blob to file
				const audioFile = new File([this.blob], `${Date.now()}.mp3`)
				this.$emit('audioFile', audioFile)
				this.$emit('recording', false)
				this.URL = window.URL.createObjectURL(this.blob)
			}
			this.resetComponentData()
		},

		/**
		 * Aborts the recording operation.
		 */
		abortRecording() {
			this.aborted = true
			this.stop()
		},

		/**
		 * Resets this component to its initial state
		 */
		resetComponentData() {
			this.audioStream = null
			this.mediaRecorder = null
			this.chunks = []
			this.blob = null
			this.aborted = false
			this.isAudiorecorderActive = false
			this.recordTime = {
				minutes: 0,
				seconds: 0,
			}
		},
	},
}
</script>

<style lang="scss" scoped>

@import '../../../assets/buttons';

.audio-recorder {
	display: flex;
	// Audio record button
	&__trigger {
		&--recording {
			background-color: var(--color-success) !important;
			opacity: .8;
			color: white;
			&:hover,
			&:focus {
				opacity: 1;
			}
		}
	}

	&__stop {
		margin-left: 4px;
		background-color: var(--color-error) !important;
		color: white;
		opacity: .8;
		&:hover,
		&:focus {
			opacity: 1;
		}
	}

	&__info {
		width: 86px;
		display: flex;
		justify-content: center;
		align-items: center;
		.time {
			flex: 0 0 50px;
		}
		.recording-indicator {
			width: 16px;
			height: 16px;
			flex: 0 0 16px;
			border-radius: 8px;
			background-color: var(--color-error);
			margin: 8px;
		}
	}
}

.wrapper {
	display: flex;
}

@keyframes fadeOutIn {
	0% { opacity:1; }
	50% { opacity:.3; }
	100% { opacity:1; }
}

.fadeOutIn {
	animation: fadeOutIn 3s infinite;
}

</style>