Horis 2 лет назад
Родитель
Сommit
e0be676cca

+ 0 - 8
app/src/main/java/io/legado/app/model/ReadAloud.kt

@@ -125,14 +125,6 @@ object ReadAloud {
         }
     }
 
-    fun upTtsProgress(context: Context) {
-        if (BaseReadAloudService.isRun) {
-            val intent = Intent(context, aloudClass)
-            intent.action = IntentAction.upTtsProgress
-            ContextCompat.startForegroundService(context, intent)
-        }
-    }
-
     fun setTimer(context: Context, minute: Int) {
         if (BaseReadAloudService.isRun) {
             val intent = Intent(context, aloudClass)

+ 12 - 13
app/src/main/java/io/legado/app/model/ReadBook.kt

@@ -326,12 +326,14 @@ object ReadBook : CoroutineScope by MainScope() {
      */
     private fun curPageChanged(pageChanged: Boolean = false) {
         callBack?.pageChanged()
-        if (BaseReadAloudService.isRun && isLayoutAvailable) {
-            val scrollPageAnim = pageAnim() == 3
-            if (scrollPageAnim && pageChanged) {
-                ReadAloud.pause(appCtx)
-            } else {
-                readAloud(!BaseReadAloudService.pause)
+        curTextChapter?.let {
+            if (BaseReadAloudService.isRun && it.isCompleted) {
+                val scrollPageAnim = pageAnim() == 3
+                if (scrollPageAnim && pageChanged) {
+                    ReadAloud.pause(appCtx)
+                } else {
+                    readAloud(!BaseReadAloudService.pause)
+                }
             }
         }
         upReadTime()
@@ -343,7 +345,8 @@ object ReadBook : CoroutineScope by MainScope() {
      */
     fun readAloud(play: Boolean = true, startPos: Int = 0) {
         book ?: return
-        if (isLayoutAvailable) {
+        val textChapter = curTextChapter ?: return
+        if (textChapter.isCompleted) {
             ReadAloud.play(appCtx, play, startPos = startPos)
         }
     }
@@ -548,8 +551,6 @@ object ReadBook : CoroutineScope by MainScope() {
                             if (upContent) {
                                 callBack?.upContent(offset, resetPageOffset)
                             }
-                            curPageChanged()
-                            callBack?.contentLoadFinish()
                             available = true
                         }
                         if (upContent && isScroll) {
@@ -560,10 +561,8 @@ object ReadBook : CoroutineScope by MainScope() {
                         callBack?.onLayoutPageCompleted(index, page)
                     }
                     if (upContent) callBack?.upContent(offset, !available && resetPageOffset)
-                    if (!available) {
-                        curPageChanged()
-                        callBack?.contentLoadFinish()
-                    }
+                    curPageChanged()
+                    callBack?.contentLoadFinish()
                 }
 
                 -1 -> {

+ 12 - 16
app/src/main/java/io/legado/app/service/BaseReadAloudService.kt

@@ -36,7 +36,6 @@ import io.legado.app.model.ReadBook
 import io.legado.app.receiver.MediaButtonReceiver
 import io.legado.app.ui.book.read.ReadBookActivity
 import io.legado.app.ui.book.read.page.entities.TextChapter
-import io.legado.app.utils.LogUtils
 import io.legado.app.utils.activityPendingIntent
 import io.legado.app.utils.broadcastPendingIntent
 import io.legado.app.utils.getPrefBoolean
@@ -112,7 +111,6 @@ abstract class BaseReadAloudService : BaseService(),
     private var cover: Bitmap =
         BitmapFactory.decodeResource(appCtx.resources, R.drawable.icon_read_book)
     var pageChanged = false
-    private var ttsProgress = 0
     private var toLast = false
     var paragraphStartPos = 0
     private var readAloudByPage = false
@@ -186,7 +184,6 @@ abstract class BaseReadAloudService : BaseService(),
             IntentAction.pause -> pauseReadAloud()
             IntentAction.resume -> resumeReadAloud()
             IntentAction.upTtsSpeechRate -> upSpeechRate(true)
-            IntentAction.upTtsProgress -> upTtsProgress(ttsProgress)
             IntentAction.prevParagraph -> prevP()
             IntentAction.nextParagraph -> nextP()
             IntentAction.addTimer -> addTimer()
@@ -201,6 +198,9 @@ abstract class BaseReadAloudService : BaseService(),
             this@BaseReadAloudService.pageIndex = pageIndex
             textChapter = ReadBook.curTextChapter
             val textChapter = textChapter ?: return@execute
+            if (!textChapter.isCompleted) {
+                return@execute
+            }
             readAloudNumber = textChapter.getReadLength(pageIndex) + startPos
             readAloudByPage = getPrefBoolean(PreferKey.readAloudByPage)
             contentList = textChapter.getNeedReadAloud(0, readAloudByPage, 0)
@@ -216,13 +216,6 @@ abstract class BaseReadAloudService : BaseService(),
                 }
             }
             nowSpeak = textChapter.getParagraphNum(readAloudNumber + 1, readAloudByPage) - 1
-            if (nowSpeak < 0) {
-                LogUtils.d(TAG) {
-                    "nowSpeak:$nowSpeak readAloudNumber:$readAloudNumber isCompleted:${textChapter.isCompleted} " +
-                            "pageSize:${textChapter.pageSize} \np1:${textChapter.paragraphsInternal}\n" +
-                            "p2:${textChapter.pageParagraphsInternal}"
-                }
-            }
             if (!readAloudByPage && startPos == 0 && !toLast) {
                 pos = page.lines.first().chapterPosition -
                         textChapter.paragraphs[nowSpeak].chapterPosition
@@ -289,7 +282,6 @@ abstract class BaseReadAloudService : BaseService(),
     abstract fun upSpeechRate(reset: Boolean = false)
 
     fun upTtsProgress(progress: Int) {
-        ttsProgress = progress
         postEvent(EventBus.TTS_PROGRESS, progress)
     }
 
@@ -300,11 +292,7 @@ abstract class BaseReadAloudService : BaseService(),
             readAloudNumber -= contentList[nowSpeak].length + 1 + paragraphStartPos
             paragraphStartPos = 0
             textChapter?.let {
-                val paragraphs = if (readAloudByPage) {
-                    it.pageParagraphs
-                } else {
-                    it.paragraphs
-                }
+                val paragraphs = it.getParagraphs(readAloudByPage)
                 if (!paragraphs[nowSpeak].isParagraphEnd) readAloudNumber++
                 if (readAloudNumber < it.getReadLength(pageIndex)) {
                     pageIndex--
@@ -325,6 +313,14 @@ abstract class BaseReadAloudService : BaseService(),
             readAloudNumber += contentList[nowSpeak].length.plus(1) - paragraphStartPos
             paragraphStartPos = 0
             nowSpeak++
+            textChapter?.let {
+                val paragraphs = it.getParagraphs(readAloudByPage)
+                if (!paragraphs[nowSpeak].isParagraphEnd) readAloudNumber--
+                if (readAloudNumber >= it.getReadLength(pageIndex + 1)) {
+                    pageIndex++
+                    ReadBook.moveToNextPage()
+                }
+            }
             upTtsProgress(readAloudNumber + 1)
             play()
         } else {

+ 1 - 0
app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@@ -381,6 +381,7 @@ class HttpReadAloudService : BaseReadAloudService(),
                 exoPlayer.playWhenReady = true
                 exoPlayer.prepare()
             } else {
+                exoPlayer.clearMediaItems()
                 updateNextPos()
             }
         }

+ 2 - 4
app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@@ -276,7 +276,7 @@ class ReadBookActivity : BaseReadBookActivity(),
     override fun onPostCreate(savedInstanceState: Bundle?) {
         super.onPostCreate(savedInstanceState)
         Looper.myQueue().addIdleHandler {
-            viewModel.initData(intent) { upMenu() }
+            viewModel.initData(intent)
             false
         }
     }
@@ -902,6 +902,7 @@ class ReadBookActivity : BaseReadBookActivity(),
 
     override fun upMenuView() {
         handler.post {
+            upMenu()
             binding.readMenu.upBookView()
         }
     }
@@ -919,9 +920,6 @@ class ReadBookActivity : BaseReadBookActivity(),
             intent.removeExtra("readAloud")
             ReadBook.readAloud()
         }
-        if (BaseReadAloudService.isRun) {
-            ReadAloud.upTtsProgress(this)
-        }
         loadStates = true
     }
 

+ 22 - 13
app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt

@@ -68,9 +68,11 @@ data class TextChapter(
     val paragraphsInternal: ArrayList<TextParagraph>
         get() {
             val paragraphs = arrayListOf<TextParagraph>()
-            pages.forEach {
-                it.lines.forEach loop@{ line ->
-                    if (line.paragraphNum <= 0) return@loop
+            for (i in pages.indices) {
+                val lines = pages[i].lines
+                for (a in lines.indices) {
+                    val line = lines[a]
+                    if (line.paragraphNum <= 0) continue
                     if (paragraphs.lastIndex < line.paragraphNum - 1) {
                         paragraphs.add(TextParagraph(line.paragraphNum))
                     }
@@ -81,12 +83,15 @@ data class TextChapter(
         }
 
     val pageParagraphsInternal: List<TextParagraph>
-        get() = pages.map {
-            it.paragraphs
-        }.flatten().also {
-            it.forEachIndexed { index, textParagraph ->
-                textParagraph.num = index + 1
+        get() {
+            val paragraphs = arrayListOf<TextParagraph>()
+            for (i in pages.indices) {
+                paragraphs.addAll(pages[i].paragraphs)
+            }
+            for (i in paragraphs.indices) {
+                paragraphs[i].num = i + 1
             }
+            return paragraphs
         }
 
     /**
@@ -189,11 +194,7 @@ data class TextChapter(
         position: Int,
         pageSplit: Boolean,
     ): Int {
-        val paragraphs = if (pageSplit) {
-            if (isCompleted) pageParagraphs else pageParagraphsInternal
-        } else {
-            if (isCompleted) paragraphs else pageParagraphsInternal
-        }
+        val paragraphs = getParagraphs(pageSplit)
         paragraphs.forEach { paragraph ->
             if (position in paragraph.chapterIndices) {
                 return paragraph.num
@@ -202,6 +203,14 @@ data class TextChapter(
         return -1
     }
 
+    fun getParagraphs(pageSplit: Boolean): List<TextParagraph> {
+        return if (pageSplit) {
+            if (isCompleted) pageParagraphs else pageParagraphsInternal
+        } else {
+            if (isCompleted) paragraphs else paragraphsInternal
+        }
+    }
+
     fun getLastParagraphPosition(): Int {
         return pageParagraphs.last().chapterPosition
     }

+ 1 - 1
app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt

@@ -48,7 +48,7 @@ data class TextLine(
 ) {
 
     val columns: List<BaseColumn> get() = textColumns
-    val charSize: Int get() = textColumns.size
+    val charSize: Int get() = text.length
     val lineStart: Float get() = textColumns.firstOrNull()?.start ?: 0f
     val lineEnd: Float get() = textColumns.lastOrNull()?.end ?: 0f
     val chapterIndices: IntRange get() = chapterPosition..chapterPosition + charSize

+ 8 - 1
app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@@ -250,7 +250,14 @@ data class TextPage(
                 length++
             }
         }
-        return length + columnIndex
+        val columns = textLines[maxIndex].columns
+        for (index in 0 until columnIndex) {
+            val column = columns[index]
+            if (column is TextColumn) {
+                length += column.charData.length
+            }
+        }
+        return length
     }
 
     /**