add read_aloud.png and soundmanager files
[bookreader.git] / BookReader / BookReader.js
index fd08029..cfd52a2 100644 (file)
@@ -3815,7 +3815,7 @@ BookReader.prototype.ttsStartCB = function (data) {
     this.ttsChunks = data;
     this.ttsHilites = [];
     
-    //deal with first page being blank
+    //deal with the page being blank
     if (0 == data.length) {
         console.log('first page is blank!');
         if(this.ttsAdvance(true)) {
@@ -3892,6 +3892,13 @@ BookReader.prototype.ttsLoadChunk = function (page, pos, string) {
 
 // ttsNextChunk()
 //______________________________________________________________________________
+// I've split this function into two parts: ttsNextChunk and ttsNextChunkPhase2.
+// This is to make the 2-page flip behavior nicer, but makes the code much 
+// more complicated.
+// If a page flip is necessary, ttsAdvance() will return false so Phase2 isn't
+// called. Instead, this.animationFinishedCallback is set, so that Phase2
+// continues after animation is finished.
+
 BookReader.prototype.ttsNextChunk = function () {
     console.log(this);
     console.log(this.ttsPosition);
@@ -3899,11 +3906,31 @@ BookReader.prototype.ttsNextChunk = function () {
     if (-1 != this.ttsPosition) {
         soundManager.destroySound('chunk'+this.ttsIndex+'-'+this.ttsPosition);    
     }
-    
-    
+
+    //remove old hilights
+    $(this.ttsHilites).remove();
+    this.ttsHilites = [];    
+        
     var moreToPlay = this.ttsAdvance();
     
-    if (!moreToPlay) return;
+    if (moreToPlay) {
+        this.ttsNextChunkPhase2();
+    }    
+    
+    //This function is called again when ttsPlay() has finished playback.
+    //If the next chunk of text has not yet finished loading, ttsPlay()
+    //will start polling until the next chunk is ready.
+}
+
+// ttsNextChunkPhase2()
+//______________________________________________________________________________
+// page flip animation has now completed
+BookReader.prototype.ttsNextChunkPhase2 = function () {
+    if (0 == this.ttsChunks.length) {
+        console.log('ttsNextChunk2: ttsChunks.length is zero.. hacking...');
+        this.ttsStartCB(this.ttsChunks);
+        return;
+    }
     
     console.log('next chunk is ');
     console.log(this.ttsPosition);    
@@ -3918,7 +3945,6 @@ BookReader.prototype.ttsNextChunk = function () {
     this.ttsPrefetchAudio();
     
     this.ttsPlay();
-    
 }
 
 // ttsAdvance()
@@ -3945,11 +3971,14 @@ BookReader.prototype.ttsAdvance = function (starting) {
                 this.ttsChunks = this.ttsNextChunks;
                 this.ttsNextChunks = null;
                 if ((this.ttsIndex != this.twoPage.currentIndexL) && (this.ttsIndex != this.twoPage.currentIndexR)) {
+                    this.animationFinishedCallback = this.ttsNextChunkPhase2;
                     this.next();
+                    return false;
+                } else {
+                    return true;
                 }
-                return true;
             } else {
-                console.log('ttsNextChunks is null');
+                console.log('ttsAdvance: ttsNextChunks is null');
                 return false; 
             }
         }
@@ -3978,7 +4007,11 @@ BookReader.prototype.ttsPrefetchAudio = function () {
         console.log('preloading chunk 0 from next page, index='+(this.ttsIndex+1))
         if (null != this.ttsNextChunks) {
             console.log(this.ttsNextChunks);
-            this.ttsLoadChunk(this.ttsIndex+1, 0, this.ttsNextChunks[0][0]);        
+            if (0 != this.ttsNextChunks.length) {
+                this.ttsLoadChunk(this.ttsIndex+1, 0, this.ttsNextChunks[0][0]);        
+            } else {
+                console.log('prefetchAudio(): ttsNextChunks is zero length!');
+            }
         } else {
             console.log('ttsNextChunks is null, not preloading next page');
             this.ttsBuffering = true;
@@ -3996,10 +4029,6 @@ BookReader.prototype.ttsPlay = function () {
     console.log('chunk = ' + chunk);
     console.log(this.ttsChunks);
 
-    //remove old hilights
-    $(this.ttsHilites).remove();
-    this.ttsHilites = [];
-
     //add new hilights
     if (2 == this.mode) {
         this.ttsHilite2UP(chunk);