File size: 1,801 Bytes
5454980
93a35f3
5454980
93a35f3
5454980
93a35f3
 
 
5454980
 
 
93a35f3
 
 
 
5454980
 
93a35f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612f193
5454980
 
 
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>
<head>
    <title>Simple Speech Demo</title>
    <style>
        body { font-family: sans-serif; padding: 20px; }
        button { padding: 10px 20px; margin: 10px 0; }
        #status { margin: 10px 0; }
    </style>
</head>
<body>
    <h1>Simple Speech Demo</h1>
    <button id="start">Start Listening</button>
    <div id="status">Ready</div>
    <div id="output"></div>

    <script>
        if (!('webkitSpeechRecognition' in window)) {
            alert('Speech recognition not supported');
        } else {
            const recognition = new webkitSpeechRecognition();
            const startButton = document.getElementById('start');
            const status = document.getElementById('status');
            const output = document.getElementById('output');

            recognition.continuous = false;
            recognition.interimResults = false;

            startButton.onclick = () => {
                try {
                    recognition.start();
                    status.textContent = 'Listening...';
                    startButton.disabled = true;
                } catch (e) {
                    console.error(e);
                    status.textContent = 'Error: ' + e.message;
                }
            };

            recognition.onresult = (event) => {
                const text = event.results[0][0].transcript;
                output.textContent = 'You said: ' + text;
            };

            recognition.onend = () => {
                status.textContent = 'Ready';
                startButton.disabled = false;
            };

            recognition.onerror = (event) => {
                status.textContent = 'Error: ' + event.error;
                startButton.disabled = false;
            };
        }
    </script>
</body>
</html>