-
Notifications
You must be signed in to change notification settings - Fork 0
/
demoAudioRecorder.js
118 lines (85 loc) · 3.07 KB
/
demoAudioRecorder.js
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
/* eslint-env browser */
var recorder = null
let audioPlay = true
// This example uses MediaRecorder to record from a live audio stream,
// and uses the resulting blob as a source for an audio element.
//
// The relevant functions in use are:
//
// navigator.mediaDevices.getUserMedia -> to get audio stream from microphone
// MediaRecorder (constructor) -> create MediaRecorder instance for a stream
// MediaRecorder.ondataavailable -> event to listen to when the recording is ready
// MediaRecorder.start -> start recording
// MediaRecorder.stop -> stop recording (this will generate a blob of data)
// URL.createObjectURL -> to create a URL from a blob, which we can use as audio src
function audioRecorder(stream) {
recorder = new MediaRecorder(stream)
// listen to dataavailable,
// which gets triggered whenever we have
// an audio blob available
recorder.addEventListener('dataavailable', onRecordingReady)
}
function onRecordingReady(e) {
//
// listen recording (audio play)
// just if speech is not aborted
//
if (audioPlay) {
//
// you don't want to record while playing (through loudspeakers),
// to avoid that playback audio feedback in the mic input!
//
suspendRecording()
// document.querySelector('#audiostatuscell').style.background = 'orange'
// document.querySelector('#audiostatuscell').style.color = 'black'
// document.querySelector('#audiostatus').style.background = 'orange'
// document.querySelector('#audiostatus').textContent = 'playback'
// const audio = document.getElementById('audio')
// e.data contains a blob representing the recording
// audio.src = URL.createObjectURL(e.data)
// audio.play()
resumeRecording()
//
// you want to resume recording after the audio playback
//
// audio.onended = () => {
// resumeRecording()
//console.log('recordingEnabled ' + DEFAULT_PARAMETERS_CONFIGURATION.recordingEnabled)
// }
}
}
function startRecording() {
recorder.start()
}
function stopRecording() {
// Stopping the recorder will eventually trigger the `dataavailable` event and we can complete the recording process
recorder.stop()
audioPlay = true
}
/**
* restartRecording
*
* abort and start
*/
function restartRecording() {
// https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/state
//console.log('recorder ' + recorder.state )
// need otherwise I get on Chrome the error:
// Failed to execute 'stop' on 'MediaRecorder': The MediaRecorder's state is 'inactive'.
if (recorder.state != 'inactive')
recorder.stop()
audioPlay = false
recorder.start()
}
function abortRecording() {
// Stopping the recorder will eventually trigger the `dataavailable` event and we can complete the recording process
recorder.stop()
audioPlay = false
}
// to suspend recording when the system play audio with a loudspeaker, avoiding feedback
function suspendRecording() {
DEFAULT_PARAMETERS_CONFIGURATION.recordingEnabled = false
}
function resumeRecording() {
DEFAULT_PARAMETERS_CONFIGURATION.recordingEnabled = true
}