From c3a7f4991f489ce1990c3b8f537326d9c63f688c Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 26 Aug 2019 08:47:33 +0800 Subject: [PATCH] up --- .../io/legado/app/service/ReadAloudService.kt | 13 +++++--- .../app/ui/readbook/ReadBookActivity.kt | 32 +++++++++++-------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/ReadAloudService.kt b/app/src/main/java/io/legado/app/service/ReadAloudService.kt index 7ae517880..2dca6a24a 100644 --- a/app/src/main/java/io/legado/app/service/ReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/ReadAloudService.kt @@ -36,7 +36,8 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage title: String, subtitle: String, pageIndex: Int, - dataKey: String + dataKey: String, + play: Boolean = true ) { val readAloudIntent = Intent(context, ReadAloudService::class.java) readAloudIntent.action = "play" @@ -44,6 +45,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage readAloudIntent.putExtra("subtitle", subtitle) readAloudIntent.putExtra("pageIndex", pageIndex) readAloudIntent.putExtra("dataKey", dataKey) + readAloudIntent.putExtra("play", play) context.startService(readAloudIntent) } @@ -123,7 +125,10 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage title = intent.getStringExtra("title") ?: "" subtitle = intent.getStringExtra("subtitle") ?: "" pageIndex = intent.getIntExtra("pageIndex", 0) - newReadAloud(intent.getStringExtra("dataKey")) + newReadAloud( + intent.getStringExtra("dataKey"), + intent.getBooleanExtra("play", true) + ) } "pause" -> pauseReadAloud(true) "resume" -> resumeReadAloud() @@ -153,7 +158,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage } } - private fun newReadAloud(dataKey: String?) { + private fun newReadAloud(dataKey: String?, paly: Boolean) { dataKey?.let { textChapter = IntentDataHelp.getData(dataKey) as? TextChapter textChapter?.let { @@ -161,7 +166,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage readAloudNumber = it.getReadLength(pageIndex) contentList.clear() contentList.addAll(it.getUnRead(pageIndex).split("\n")) - playTTS() + if (paly) playTTS() } ?: stopSelf() } ?: stopSelf() } diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt index 2c2ff3def..424fec041 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt @@ -120,9 +120,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea override fun skipToPage(page: Int) { viewModel.durPageIndex = page page_view.upContent() - if (readAloudStatus == Status.PLAY) { - readAloud() - } + curPageChanged() } override fun skipPreChapter() { @@ -236,7 +234,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea viewModel.curTextChapter = ChapterProvider.getTextChapter(content_text_view, bookChapter, content) page_view.upContent() - curChapterChange() + curChapterChanged() } viewModel.durChapterIndex - 1 -> { viewModel.prevTextChapter = @@ -250,7 +248,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea } } - private fun curChapterChange() { + private fun curChapterChanged() { viewModel.curTextChapter?.let { tv_chapter_name.text = it.title tv_chapter_name.visible() @@ -259,8 +257,15 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea tv_chapter_url.visible() } read_menu.upReadProgress(it.pageSize(), viewModel.durPageIndex) - if (readAloudStatus == Status.PLAY) { - readAloud() + curPageChanged() + } + } + + private fun curPageChanged() { + when (readAloudStatus) { + Status.PLAY -> readAloud() + Status.PAUSE -> { + readAloud(false) } } } @@ -294,9 +299,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea override fun setPageIndex(pageIndex: Int) { viewModel.durPageIndex = pageIndex viewModel.saveRead() - if (readAloudStatus == Status.PLAY) { - readAloud() - } + curPageChanged() } override fun textChapter(chapterOnDur: Int): TextChapter? { @@ -311,13 +314,13 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea override fun moveToNextChapter() { viewModel.durPageIndex = 0 viewModel.moveToNextChapter() - curChapterChange() + curChapterChanged() } override fun moveToPrevChapter() { viewModel.durPageIndex = viewModel.prevTextChapter?.lastIndex() ?: 0 viewModel.moveToPrevChapter() - curChapterChange() + curChapterChanged() } override fun clickCenter() { @@ -346,7 +349,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea /** * 朗读 */ - private fun readAloud() { + private fun readAloud(play: Boolean = true) { val book = viewModel.bookData.value val textChapter = viewModel.curTextChapter if (book != null && textChapter != null) { @@ -357,7 +360,8 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea book.name, textChapter.title, viewModel.durPageIndex, - key + key, + play ) } }