rein0421 commited on
Commit
ae2e16c
·
verified ·
1 Parent(s): 524d49f

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +66 -0
index.html CHANGED
@@ -78,6 +78,7 @@
78
  </button>
79
  <button class="result-button" onclick="showResults()">結果を表示</button>
80
  <script>
 
81
  let isRecording = false;
82
 
83
  function toggleRecording() {
@@ -120,5 +121,70 @@
120
  }
121
  });
122
  </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  </body>
124
  </html>
 
78
  </button>
79
  <button class="result-button" onclick="showResults()">結果を表示</button>
80
  <script>
81
+
82
  let isRecording = false;
83
 
84
  function toggleRecording() {
 
121
  }
122
  });
123
  </script>
124
+
125
+ <script>
126
+ let mode = 'False';
127
+ let mediaRecorder;
128
+ let audioChunks = [];
129
+ const startRecordingBtn = document.getElementById('recordButton');
130
+ const sendToServerBtn = document.getElementById('result-button');
131
+
132
+ // 音声録音の開始
133
+ startRecordingBtn.addEventListener('click', async () => {
134
+ if (mode){
135
+ mediaRecorder.stop();
136
+ startRecordingBtn.disabled = false;
137
+ stopRecordingBtn.disabled = true;
138
+ mode = 'False';
139
+ }
140
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
141
+ mediaRecorder = new MediaRecorder(stream);
142
+
143
+ mediaRecorder.ondataavailable = event => {
144
+ audioChunks.push(event.data);
145
+ };
146
+
147
+ mediaRecorder.onstop = () => {
148
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
149
+ const reader = new FileReader();
150
+
151
+ reader.onloadend = () => {
152
+ const base64String = reader.result.split(',')[1]; // Base64エンコードされた音声データを取得
153
+ sendToServerBtn.disabled = false;
154
+
155
+ sendToServerBtn.addEventListener('click', () => {
156
+ // 音声をバックエンドに送信
157
+ fetch('/upload_audio', {
158
+ method: 'POST',
159
+ headers: {
160
+ 'Content-Type': 'application/json',
161
+ },
162
+ body: JSON.stringify({
163
+ audio_data: base64String,
164
+ }),
165
+ })
166
+ .then(response => response.json())
167
+ .then(data => {
168
+ alert('音声がバックエンドに送信されました。');
169
+ })
170
+ .catch(error => {
171
+ console.error('エラー:', error);
172
+ });
173
+ });
174
+ };
175
+
176
+ reader.readAsDataURL(audioBlob);
177
+ };
178
+
179
+ mediaRecorder.start();
180
+ startRecordingBtn.disabled = true;
181
+ stopRecordingBtn.disabled = false;
182
+ mode = 'True';
183
+ });
184
+
185
+ // 音声録音の停止
186
+
187
+ </script>
188
+
189
  </body>
190
  </html>