Tri4 commited on
Commit
2a39528
·
verified ·
1 Parent(s): 615ea00

Update static/js/design.js

Browse files
Files changed (1) hide show
  1. static/js/design.js +21 -12
static/js/design.js CHANGED
@@ -3,8 +3,8 @@ let mediaRecorder;
3
  let recordedChunks = [];
4
 
5
  // Get elements
6
- const micContainer = document.getElementsByClassName('mic-container')[0];
7
- const circle = micContainer.getElementsByClassName('circle')[0];
8
  const outputContainer = document.getElementById('output');
9
  const audioPlayerContainer = document.getElementById('audioPlayerContainer');
10
  const audioPlayer = document.getElementById('audioPlayer');
@@ -21,21 +21,28 @@ micContainer.addEventListener('click', function () {
21
  function startRecording() {
22
  navigator.mediaDevices.getUserMedia({ audio: true })
23
  .then(function (stream) {
24
- // Create a MediaRecorder instance with MIME type 'audio/wav'
25
- mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/wav' });
26
- mediaRecorder.start();
27
-
28
  mediaRecorder.ondataavailable = function (e) {
29
  if (e.data.size > 0) {
30
  recordedChunks.push(e.data);
31
  }
32
  };
33
 
34
- // Update UI for recording
35
- circle.classList.add('active');
36
- outputContainer.textContent = 'Recording...';
37
- audioPlayerContainer.style.display = 'none'; // Hide audio player initially
38
- isRecording = true;
 
 
 
 
 
 
 
 
 
39
  })
40
  .catch(function (err) {
41
  console.error('The following error occurred: ' + err);
@@ -43,7 +50,9 @@ function startRecording() {
43
  }
44
 
45
  function stopRecording() {
46
- mediaRecorder.stop();
 
 
47
 
48
  mediaRecorder.onstop = function () {
49
  const blob = new Blob(recordedChunks, { type: 'audio/wav' });
 
3
  let recordedChunks = [];
4
 
5
  // Get elements
6
+ const micContainer = document.querySelector('.mic-container');
7
+ const circle = document.querySelector('.circle');
8
  const outputContainer = document.getElementById('output');
9
  const audioPlayerContainer = document.getElementById('audioPlayerContainer');
10
  const audioPlayer = document.getElementById('audioPlayer');
 
21
  function startRecording() {
22
  navigator.mediaDevices.getUserMedia({ audio: true })
23
  .then(function (stream) {
24
+ mediaRecorder = new MediaRecorder(stream);
25
+
 
 
26
  mediaRecorder.ondataavailable = function (e) {
27
  if (e.data.size > 0) {
28
  recordedChunks.push(e.data);
29
  }
30
  };
31
 
32
+ mediaRecorder.onstart = function () {
33
+ // Update UI for recording
34
+ circle.classList.add('active');
35
+ outputContainer.textContent = 'Recording...';
36
+ audioPlayerContainer.style.display = 'none'; // Hide audio player initially
37
+ isRecording = true;
38
+ };
39
+
40
+ mediaRecorder.onerror = function (e) {
41
+ console.error('Error recording audio:', e);
42
+ stopRecording(); // Stop recording on error
43
+ };
44
+
45
+ mediaRecorder.start();
46
  })
47
  .catch(function (err) {
48
  console.error('The following error occurred: ' + err);
 
50
  }
51
 
52
  function stopRecording() {
53
+ if (mediaRecorder && mediaRecorder.state !== 'inactive') {
54
+ mediaRecorder.stop();
55
+ }
56
 
57
  mediaRecorder.onstop = function () {
58
  const blob = new Blob(recordedChunks, { type: 'audio/wav' });