File tree Expand file tree Collapse file tree 3 files changed +86
-0
lines changed
86-Speech Recognition Interface Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 " />
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
6+ < title > Speech Recognition Interface</ title >
7+ < link rel ="stylesheet " href ="styles.css " />
8+ </ head >
9+ < body >
10+ < div class ="container ">
11+ < h1 > Speech Recognition Interface</ h1 >
12+ < p id ="output "> Your transcribed speech will appear here.</ p >
13+ < button id ="startBtn "> Start Listening</ button >
14+ </ div >
15+ < script src ="script.js "> </ script >
16+ </ body >
17+ </ html >
Original file line number Diff line number Diff line change 1+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
2+ const startBtn = document . getElementById ( "startBtn" ) ;
3+ const output = document . getElementById ( "output" ) ;
4+
5+ // Check if the browser supports the Web Speech API
6+ if ( "SpeechRecognition" in window || "webkitSpeechRecognition" in window ) {
7+ const recognition = new ( window . SpeechRecognition ||
8+ window . webkitSpeechRecognition ) ( ) ;
9+
10+ recognition . continuous = true ;
11+ recognition . interimResults = true ;
12+
13+ recognition . onstart = ( ) => {
14+ startBtn . innerText = "Listening..." ;
15+ } ;
16+
17+ recognition . onresult = ( event ) => {
18+ const transcript = Array . from ( event . results )
19+ . map ( ( result ) => result [ 0 ] . transcript )
20+ . join ( "" ) ;
21+
22+ output . innerText = transcript ;
23+ } ;
24+
25+ recognition . onend = ( ) => {
26+ startBtn . innerText = "Start Listening" ;
27+ } ;
28+
29+ recognition . onerror = ( event ) => {
30+ console . error ( "Speech recognition error:" , event . error ) ;
31+ } ;
32+
33+ startBtn . addEventListener ( "click" , ( ) => {
34+ if ( recognition . running ) {
35+ recognition . stop ( ) ;
36+ } else {
37+ recognition . start ( ) ;
38+ }
39+ } ) ;
40+ } else {
41+ output . innerText =
42+ "Speech recognition is not supported in this browser." ;
43+ startBtn . disabled = true ;
44+ }
45+ } ) ;
Original file line number Diff line number Diff line change 1+ body {
2+ font-family : "Arial" , sans-serif;
3+ margin : 0 ;
4+ padding : 0 ;
5+ display : flex;
6+ align-items : center;
7+ justify-content : center;
8+ height : 100vh ;
9+ background-color : # f4f4f4 ;
10+ }
11+
12+ .container {
13+ text-align : center;
14+ }
15+
16+ button {
17+ padding : 10px 20px ;
18+ font-size : 16px ;
19+ cursor : pointer;
20+ background-color : # 3498db ;
21+ color : # fff ;
22+ border : none;
23+ border-radius : 5px ;
24+ }
You can’t perform that action at this time.
0 commit comments