-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
90 lines (78 loc) · 2.57 KB
/
main.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
function callTest() {
const div = document.getElementById("serverResponse");
const text = div.innerHTML;
const myReq = JSON.stringify({ "text": text });
// NOTE: Fetch does not currently work on IE! We will need a polyfill (i think that's what it is called) to be compatible.
fetch('http://localhost:3000/', {
method: "POST", body: myReq, headers: { "Content-Type": "application/json" }
})
.then(function(response) {
return response.json();
})
.then(function(myJson) {
div.innerHTML = myJson.text;
});
}
function handleSubmit(e){
e.preventDefault();
const text = document.translate.textvalue.value;
const answertext = document.getElementById('translateResponse');
const answer = answertext.innerHTML;
const myReq = JSON.stringify({"name": text, "value":answer});
fetch('http://localhost:3000/translate', {
method: "POST", body: myReq, headers: { "Content-Type": "application/json" }
}).then(function(response){
return response.json();
}).then(function(myJson){
console.log(answer);
answertext.innerHTML = myJson.value;
})
}
const audioSelect = document.querySelector('select#audioSource');
function hasGetUserMedia() {
return !!(navigator.mediaDevices &&
navigator.mediaDevices.getUserMedia);
}
if (hasGetUserMedia()) {
// Good to go!
console.log("It works!!");
} else {
alert('getUserMedia() is not supported by your browser');
}
navigator.mediaDevices.enumerateDevices()
.then(gotDevices).then(getStream).catch(handleError);
audioSelect.onchange = getStream;
function gotDevices(deviceInfos) {
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
const option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'audioinput') {
option.text = deviceInfo.label ||
'microphone ' + (audioSelect.length + 1);
audioSelect.appendChild(option);
} else {
console.log('Found another kind of device: ', deviceInfo);
}
}
}
function getStream() {
if (window.stream) {
window.stream.getTracks().forEach(function(track) {
track.stop();
});
}
const constraints = {
audio: {
deviceId: {exact: audioSelect.value}
}
};
navigator.mediaDevices.getUserMedia(constraints).
then(gotStream).catch(handleError);
}
function gotStream(stream) {
window.stream = stream; // make stream available to console
}
function handleError(error) {
console.error('Error: ', error);
}