File size: 2,251 Bytes
72ebc89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Audio Recorder</title>
</head>
<body>
    <h1>Record and Send Audio</h1>
    <button id="start-recording">Start Recording</button>
    <button id="stop-recording" disabled>Stop Recording</button>
    <button id="send-audio" disabled>Send Audio</button>
    <audio id="audio-playback" controls></audio>
    <script>
        let mediaRecorder;
        let audioChunks = [];

        document.getElementById('start-recording').addEventListener('click', async () => {
            const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
            mediaRecorder = new MediaRecorder(stream);
            mediaRecorder.ondataavailable = event => {
                audioChunks.push(event.data);
            };
            mediaRecorder.onstop = () => {
                const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
                const audioUrl = URL.createObjectURL(audioBlob);
                document.getElementById('audio-playback').src = audioUrl;
                document.getElementById('send-audio').disabled = false;
            };
            mediaRecorder.start();
            document.getElementById('start-recording').disabled = true;
            document.getElementById('stop-recording').disabled = false;
        });

        document.getElementById('stop-recording').addEventListener('click', () => {
            mediaRecorder.stop();
            document.getElementById('start-recording').disabled = false;
            document.getElementById('stop-recording').disabled = true;
        });

        document.getElementById('send-audio').addEventListener('click', () => {
            const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
            const formData = new FormData();
            formData.append('audio', audioBlob, 'audio.wav');

            fetch('/upload-audio', {
                method: 'POST',
                body: formData
            }).then(response => response.json())
              .then(data => {
                  alert('Audio processed successfully');
              });
        });
    </script>
</body>
</html>