@@ -136,6 +136,7 @@ function Doc(source, onEnd) {
136136 . then ( function ( ) { return source . ready } )
137137 . then ( function ( result ) { info = result } )
138138 var foundText ;
139+ const playbackState = new rxjs . BehaviorSubject ( "resumed" )
139140
140141 this . close = close ;
141142 this . play = play ;
@@ -149,6 +150,7 @@ function Doc(source, onEnd) {
149150
150151 //method close
151152 function close ( ) {
153+ playbackState . error ( { name : "CancellationException" , message : "Playback cancelled" } )
152154 return ready
153155 . catch ( function ( ) { } )
154156 . then ( function ( ) {
@@ -161,12 +163,15 @@ function Doc(source, onEnd) {
161163 async function play ( ) {
162164 if ( activeSpeech ) return activeSpeech . play ( ) ;
163165 await ready
166+ await wait ( playbackState , "resumed" )
164167 currentIndex = await source . getCurrentIndex ( )
168+ await wait ( playbackState , "resumed" )
165169 return readCurrent ( )
166170 }
167171
168172 async function readCurrent ( rewinded ) {
169173 const texts = await source . getTexts ( currentIndex ) . catch ( err => null )
174+ await wait ( playbackState , "resumed" )
170175 if ( texts ) {
171176 if ( texts . length ) {
172177 foundText = true ;
@@ -187,10 +192,12 @@ function Doc(source, onEnd) {
187192 texts = texts . map ( preprocess )
188193 if ( info . detectedLang == null ) {
189194 const lang = await detectLanguage ( texts )
195+ await wait ( playbackState , "resumed" )
190196 info . detectedLang = lang || "" ;
191197 }
192198 if ( activeSpeech ) return ;
193199 activeSpeech = await getSpeech ( texts ) ;
200+ await wait ( playbackState , "resumed" )
194201 activeSpeech . onEnd = function ( err ) {
195202 if ( err ) {
196203 if ( onEnd ) onEnd ( err ) ;
0 commit comments