-
Notifications
You must be signed in to change notification settings - Fork 19
/
example_1.html
91 lines (84 loc) · 3.22 KB
/
example_1.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
<!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 1</title>
</head>
<body>
<a href="./index.html"> << back</a>
<div class="center-align">
<h1>Example 1 - <span>Version_8 </span></h1>
<p>getUserMedia() - AudioContext() - <a href="https://github.com/mattdiamond/Recorderjs">RecorderJS</a> - exportWAV()</p>
<audio controls autoplay></audio>
<br>
<button class="btn waves-effect waves-light js-start">Start</button>
<button class="btn waves-effect waves-light js-stop" disabled>Stop</button>
<br>
<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="./recorder.js"></script>
<script src="https://cdn.webrtc-experiment.com/MediaStreamRecorder.js"> </script>
<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">
let recorder;
let context;
let audio = document.querySelector('audio');
let startBtn = document.querySelector('.js-start');
let stopBtn = document.querySelector('.js-stop');
let codeBtn = document.querySelector('.js-code');
let pre = document.querySelector('pre');
window.URL = window.URL || window.webkitURL;
/**
* Detecte the correct AudioContext for the browser
* */
window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
let onFail = function(e) {
alert('Error '+e);
console.log('Rejected!', e);
};
let onSuccess = function(s) {
console.log('Recording...');
let tracks = s.getTracks();
startBtn.setAttribute('disabled', true);
stopBtn.removeAttribute('disabled');
context = new AudioContext();
let mediaStreamSource = context.createMediaStreamSource(s);
recorder = new Recorder(mediaStreamSource);
recorder.record();
stopBtn.addEventListener('click', ()=>{
console.log('Stop Recording...');
stopBtn.setAttribute('disabled', true);
startBtn.removeAttribute('disabled');
recorder.stop();
tracks.forEach(track => track.stop());
recorder.exportWAV(function(s) {
audio.src = window.URL.createObjectURL(s);
});
});
}
startBtn.addEventListener('click', ()=>{
if (navigator.getUserMedia) {
/**
* ask permission of the user for use microphone or camera
*/
navigator.getUserMedia({audio: true}, onSuccess, onFail);
} else {
console.warn('navigator.getUserMedia not present');
}
});
codeBtn.addEventListener('click', () => {
pre.classList.toggle('hide');
pre.innerHTML = document.querySelector('.containerScript').innerHTML;
});
</script>
</body>
</html>