File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ reading = false ;
2+ timer = false ;
3+ function googleSpeech ( text , rate ) {
4+ if ( ! reading ) {
5+ speechSynthesis . cancel ( ) ;
6+ if ( timer ) {
7+ clearInterval ( timer ) ;
8+ }
9+ let msg = new SpeechSynthesisUtterance ( ) ;
10+ let voices = window . speechSynthesis . getVoices ( ) ;
11+ msg . voice = voices [ 63 ] ;
12+ msg . voiceURI = 'native' ;
13+ msg . volume = 1 ; // 0 to 1
14+ msg . rate = rate ; // 0.1 to 10
15+ msg . pitch = 1 ; //0 to 2
16+ msg . text = text ;
17+ msg . lang = 'zh-CN' ;
18+
19+ msg . onerror = function ( e ) {
20+ speechSynthesis . cancel ( ) ;
21+ reading = false ;
22+ clearInterval ( timer ) ;
23+ } ;
24+
25+ msg . onpause = function ( e ) {
26+ } ;
27+
28+ msg . onboundary = function ( event ) {
29+ } ;
30+
31+ msg . onend = function ( e ) {
32+ speechSynthesis . cancel ( ) ;
33+ reading = false ;
34+ clearInterval ( timer ) ;
35+ } ;
36+
37+ speechSynthesis . onerror = function ( e ) {
38+ speechSynthesis . cancel ( ) ;
39+ reading = false ;
40+ clearInterval ( timer ) ;
41+ } ;
42+
43+ console . log ( msg ) ;
44+ speechSynthesis . speak ( msg ) ;
45+
46+ timer = setInterval ( function ( ) {
47+ if ( speechSynthesis . paused ) {
48+ speechSynthesis . resume ( ) ;
49+ }
50+
51+ } , 100 ) ;
52+
53+ reading = true ;
54+ }
55+
56+ }
You can’t perform that action at this time.
0 commit comments