-
Notifications
You must be signed in to change notification settings - Fork 19
/
example_2.html
202 lines (181 loc) · 7.18 KB
/
example_2.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<title>Example 2</title>
</head>
<body>
<a href="./index.html"> << back</a>
<div class="center-align">
<h1>Example 2 - <span>Version_8 </span></h1>
<p>getUserMedia() - MediaRecorder</p>
<audio controls id="audio" src=""></audio>
<br>
<button class="btn waves-effect waves-light" id="startRecording">Start</button>
<button class="btn waves-effect waves-light" id="stopRecording" disabled>Stop</button>
<br>
<a id="download" class="hide">Download Audio</a>
<br>
<button class="btn waves-effect waves-light js-code">Show Code</button>
</div>
<hr>
<pre style="font-family: GillSans, Calibri, Trebuchet, sans-serif;" class="hide"></pre>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script class="containerScript">
window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL;
let getBrowser = () => {
console.log('function getBrowser() {');
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName = navigator.appName;
var fullVersion = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
browserName = "Opera";
fullVersion = nAgt.substring(verOffset+6);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
browserName = "Microsoft Internet Explorer";
fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome"
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
browserName = "Chrome";
fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version"
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
browserName = "Safari";
fullVersion = nAgt.substring(verOffset+7);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox"
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
browserName = "Firefox";
fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) {
browserName = nAgt.substring(nameOffset,verOffset);
fullVersion = nAgt.substring(verOffset+1);
if (browserName.toLowerCase()==browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
fullVersion=fullVersion.substring(0,ix);
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
fullVersion = ''+parseFloat(navigator.appVersion);
majorVersion = parseInt(navigator.appVersion,10);
}
return browserName;
}
if (typeof MediaRecorder === 'undefined' || !navigator.getUserMedia) {
alert('MediaRecorder not supported on your browser, use Firefox 30 or Chrome 49 instead.');
}
if (getBrowser() == "Chrome") {
console.log('if Chrome');
var constraints = {"audio": true, "video": { "mandatory": { "minWidth": 640, "maxWidth": 640, "minHeight": 480,"maxHeight": 480 }, "optional": [] } }; //Chrome
} else if (getBrowser() == "Firefox") {
console.log('else Firefox');
var constraints = {audio: true, video: { width: { min: 640, ideal: 640, max: 640 }, height: { min: 480, ideal: 480, max: 480 } } }; //Firefox
} else {
console.log('else Chrome');
var constraints = {"audio": true, "video": false };//Chrome
}
let pre = document.querySelector('pre');
let shouldStop;
let stopped;
let mediaRecorder;
let audio = document.querySelector('audio');
let downloadLink = document.getElementById('download');
let startBtn = document.getElementById('startRecording');
let stopBtn = document.getElementById('stopRecording');
let codeBtn = document.querySelector('.js-code');
let handleSuccess = (stream) => {
let options;
let recordedChunks = [];
shouldStop = false;
stopped = false;
if (typeof MediaRecorder.isTypeSupported == 'function'){
if (MediaRecorder.isTypeSupported('audio/webm;codecs=opus')) {
options = {mimeType: 'audio/webm;codecs=opus'};
} else if (MediaRecorder.isTypeSupported('audio/ogg;codecs=opus')) {
options = {mimeType: 'audio/ogg;codecs=opus'};
}
console.log('mimeType: '+options.mimeType);
mediaRecorder = new MediaRecorder(stream, options);
} else {
console.log('mimeType DEFAULT');
mediaRecorder = new MediaRecorder(stream);
}
startBtn.setAttribute('disabled', true);
stopBtn.removeAttribute('disabled');
mediaRecorder.start(10);
mediaRecorder.addEventListener('dataavailable', function(e) {
if (e.data.size > 0) {
recordedChunks.push(e.data);
console.log(e.data);
}
if(shouldStop === true && stopped === false) {
console.log('Stop');
stopBtn.setAttribute('disabled', true);
startBtn.removeAttribute('disabled');
mediaRecorder.stop();
stream.getAudioTracks().forEach(function (track) {
track.stop();
});
stopped = true;
}
});
mediaRecorder.addEventListener('start', function(e) {
console.log('Started, state = ' + mediaRecorder.state);
});
mediaRecorder.addEventListener('stop', function() {
audio.src = URL.createObjectURL(new Blob(recordedChunks));
downloadLink.href = URL.createObjectURL(new Blob(recordedChunks));
downloadLink.download = 'AudioTest.wav';
downloadLink.classList.remove('hide');
});
mediaRecorder.addEventListener('warning', function(e) {
log('Warning: ' + e);
});
mediaRecorder.addEventListener('error', evt => {
console.log(evt);
reject(evt);
});
};
let errorCallback = (error) => {
alert(error);
console.log('navigator.getUserMedia error: ', error);
};
startBtn.addEventListener('click', () => {
navigator.getUserMedia({"audio": true, "video": false}, handleSuccess, errorCallback);
});
stopBtn.addEventListener('click', () => {
shouldStop = true;
});
codeBtn.addEventListener('click', () => {
pre.classList.toggle('hide');
pre.innerHTML = document.querySelector('.containerScript').innerHTML;
});
</script>
</body>
</html>