Spaces:
Running
Running
Update index.html:Send audio every 10 seconds
Browse files- index.html +44 -30
index.html
CHANGED
@@ -73,15 +73,12 @@
|
|
73 |
<button class="result-button" id="resultButton" onclick="showResults()">結果を表示</button>
|
74 |
|
75 |
<script>
|
76 |
-
|
77 |
-
function tmp(){
|
78 |
-
console.log("10秒経ったよ");
|
79 |
-
}
|
80 |
-
|
81 |
-
|
82 |
let isRecording = false;
|
83 |
let mediaRecorder;
|
84 |
let audioChunks = [];
|
|
|
|
|
|
|
85 |
async function toggleRecording() {
|
86 |
const recordButton = document.getElementById('recordButton');
|
87 |
const recordIcon = document.getElementById('recordIcon');
|
@@ -93,35 +90,25 @@
|
|
93 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
94 |
mediaRecorder = new MediaRecorder(stream);
|
95 |
audioChunks = [];
|
|
|
96 |
mediaRecorder.ondataavailable = event => {
|
97 |
if (event.data.size > 0) {
|
98 |
audioChunks.push(event.data);
|
99 |
}
|
100 |
};
|
101 |
mediaRecorder.onstop = () => {
|
102 |
-
|
103 |
-
|
104 |
-
reader.onloadend = () => {
|
105 |
-
const base64String = reader.result.split(',')[1]; // Base64 エンコードされた音声データ
|
106 |
-
// サーバーへ音声データを送信
|
107 |
-
fetch('/upload_audio', {
|
108 |
-
method: 'POST',
|
109 |
-
headers: {
|
110 |
-
'Content-Type': 'application/json',
|
111 |
-
},
|
112 |
-
body: JSON.stringify({ audio_data: base64String }),
|
113 |
-
})
|
114 |
-
.then(response => response.json())
|
115 |
-
.then(data => {
|
116 |
-
alert('音声がバックエンドに送信されました。');
|
117 |
-
})
|
118 |
-
.catch(error => {
|
119 |
-
console.error('エラー:', error);
|
120 |
-
});
|
121 |
-
};
|
122 |
-
reader.readAsDataURL(audioBlob);
|
123 |
};
|
|
|
124 |
mediaRecorder.start();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
} catch (error) {
|
126 |
console.error('マイクへのアクセスに失敗しました:', error);
|
127 |
isRecording = false;
|
@@ -131,14 +118,44 @@
|
|
131 |
// 録音停止
|
132 |
isRecording = false;
|
133 |
recordButton.classList.remove('recording');
|
|
|
134 |
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
|
135 |
mediaRecorder.stop();
|
136 |
}
|
137 |
}
|
138 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
function showResults() {
|
140 |
window.location.href = 'feedback.html';
|
141 |
}
|
|
|
142 |
// Chart.js の初期化
|
143 |
const ctx = document.getElementById('speechChart').getContext('2d');
|
144 |
const speechChart = new Chart(ctx, {
|
@@ -163,9 +180,6 @@
|
|
163 |
}
|
164 |
}
|
165 |
});
|
166 |
-
|
167 |
-
setInterval(tmp,10000);
|
168 |
-
|
169 |
</script>
|
170 |
</body>
|
171 |
</html>
|
|
|
73 |
<button class="result-button" id="resultButton" onclick="showResults()">結果を表示</button>
|
74 |
|
75 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
let isRecording = false;
|
77 |
let mediaRecorder;
|
78 |
let audioChunks = [];
|
79 |
+
let isRecordingStopped = false; //自動停止を管理する
|
80 |
+
let recordingInterval;
|
81 |
+
|
82 |
async function toggleRecording() {
|
83 |
const recordButton = document.getElementById('recordButton');
|
84 |
const recordIcon = document.getElementById('recordIcon');
|
|
|
90 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
91 |
mediaRecorder = new MediaRecorder(stream);
|
92 |
audioChunks = [];
|
93 |
+
previousAudioChunks = [];
|
94 |
mediaRecorder.ondataavailable = event => {
|
95 |
if (event.data.size > 0) {
|
96 |
audioChunks.push(event.data);
|
97 |
}
|
98 |
};
|
99 |
mediaRecorder.onstop = () => {
|
100 |
+
sendAudioChunks([...audioChunks]);
|
101 |
+
audioChunks = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
};
|
103 |
+
|
104 |
mediaRecorder.start();
|
105 |
+
|
106 |
+
recordingInterval = setInterval(() => {
|
107 |
+
if(isRecording && !isRecordingStopped){
|
108 |
+
isRecordingStopped = true;
|
109 |
+
mediaRecorder.stop();
|
110 |
+
}
|
111 |
+
},10000);
|
112 |
} catch (error) {
|
113 |
console.error('マイクへのアクセスに失敗しました:', error);
|
114 |
isRecording = false;
|
|
|
118 |
// 録音停止
|
119 |
isRecording = false;
|
120 |
recordButton.classList.remove('recording');
|
121 |
+
clearInterval(recordingInterval);
|
122 |
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
|
123 |
mediaRecorder.stop();
|
124 |
}
|
125 |
}
|
126 |
}
|
127 |
+
|
128 |
+
function sendAudioChunks(chunks){
|
129 |
+
const audioBlob = new Blob(chunks, { type: 'audio/wav' });
|
130 |
+
const reader = new FileReader();
|
131 |
+
reader.onloadend = () => {
|
132 |
+
const base64String = reader.result.split(',')[1]; // Base64 エンコードされた音声データ
|
133 |
+
// サーバーへ音声データを送信
|
134 |
+
fetch('/upload_audio', {
|
135 |
+
method: 'POST',
|
136 |
+
headers: {
|
137 |
+
'Content-Type': 'application/json',
|
138 |
+
},
|
139 |
+
body: JSON.stringify({ audio_data: base64String }),
|
140 |
+
})
|
141 |
+
.then(response => response.json())
|
142 |
+
.then(data => {
|
143 |
+
alert('音声がバックエンドに送信されました。');
|
144 |
+
})
|
145 |
+
.catch(error => {
|
146 |
+
console.error('エラー:', error);
|
147 |
+
});
|
148 |
+
};
|
149 |
+
reader.readAsDataURL(audioBlob);
|
150 |
+
|
151 |
+
isRecordingStopped = false;
|
152 |
+
mediaRecorder.start();
|
153 |
+
}
|
154 |
+
|
155 |
function showResults() {
|
156 |
window.location.href = 'feedback.html';
|
157 |
}
|
158 |
+
|
159 |
// Chart.js の初期化
|
160 |
const ctx = document.getElementById('speechChart').getContext('2d');
|
161 |
const speechChart = new Chart(ctx, {
|
|
|
180 |
}
|
181 |
}
|
182 |
});
|
|
|
|
|
|
|
183 |
</script>
|
184 |
</body>
|
185 |
</html>
|