File tree Expand file tree Collapse file tree 1 file changed +6
-0
lines changed Expand file tree Collapse file tree 1 file changed +6
-0
lines changed Original file line number Diff line number Diff line change 1616
1717 recognition . interimResults = true ;
1818
19+ // 创建p便签,附加到DOM树中
1920 let p = document . createElement ( 'p' ) ;
2021 const words = document . querySelector ( '.words' ) ;
2122 words . appendChild ( p ) ;
2223
24+ // 监听recognition的result事件,获取到语音输入的文字
2325 recognition . addEventListener ( 'result' , ( e ) => {
2426 const results = Array . from ( e . results )
2527 . map ( result => result [ 0 ] )
2628 . map ( result => result . transcript )
2729 . join ( '' ) ;
2830
31+ // 可以动态的将其中的某一个词语换掉
2932 const poopScript = results . replace ( / g o o d / gi, '👍' ) ;
3033 p . textContent = poopScript ;
3134
35+ // 如果当前一段输入结束了,也就是有停顿,就会新建一个p便签
3236 if ( e . results [ 0 ] . isFinal ) {
3337 p = document . createElement ( 'p' ) ;
3438 words . appendChild ( p ) ;
3539 }
3640 } ) ;
3741
42+ // 监听recognition的end事件,当前输入结束后,再次开始,使其一直处于输入状态
3843 recognition . addEventListener ( 'end' , recognition . start ) ;
3944
45+ // 开启recognition
4046 recognition . start ( ) ;
4147
4248
You can’t perform that action at this time.
0 commit comments