pull/32/head
kunfei 5 years ago
parent 0376267e27
commit c3a7f4991f
  1. 13
      app/src/main/java/io/legado/app/service/ReadAloudService.kt
  2. 32
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt

@ -36,7 +36,8 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
title: String, title: String,
subtitle: String, subtitle: String,
pageIndex: Int, pageIndex: Int,
dataKey: String dataKey: String,
play: Boolean = true
) { ) {
val readAloudIntent = Intent(context, ReadAloudService::class.java) val readAloudIntent = Intent(context, ReadAloudService::class.java)
readAloudIntent.action = "play" readAloudIntent.action = "play"
@ -44,6 +45,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
readAloudIntent.putExtra("subtitle", subtitle) readAloudIntent.putExtra("subtitle", subtitle)
readAloudIntent.putExtra("pageIndex", pageIndex) readAloudIntent.putExtra("pageIndex", pageIndex)
readAloudIntent.putExtra("dataKey", dataKey) readAloudIntent.putExtra("dataKey", dataKey)
readAloudIntent.putExtra("play", play)
context.startService(readAloudIntent) context.startService(readAloudIntent)
} }
@ -123,7 +125,10 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
title = intent.getStringExtra("title") ?: "" title = intent.getStringExtra("title") ?: ""
subtitle = intent.getStringExtra("subtitle") ?: "" subtitle = intent.getStringExtra("subtitle") ?: ""
pageIndex = intent.getIntExtra("pageIndex", 0) pageIndex = intent.getIntExtra("pageIndex", 0)
newReadAloud(intent.getStringExtra("dataKey")) newReadAloud(
intent.getStringExtra("dataKey"),
intent.getBooleanExtra("play", true)
)
} }
"pause" -> pauseReadAloud(true) "pause" -> pauseReadAloud(true)
"resume" -> resumeReadAloud() "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 { dataKey?.let {
textChapter = IntentDataHelp.getData(dataKey) as? TextChapter textChapter = IntentDataHelp.getData(dataKey) as? TextChapter
textChapter?.let { textChapter?.let {
@ -161,7 +166,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
readAloudNumber = it.getReadLength(pageIndex) readAloudNumber = it.getReadLength(pageIndex)
contentList.clear() contentList.clear()
contentList.addAll(it.getUnRead(pageIndex).split("\n")) contentList.addAll(it.getUnRead(pageIndex).split("\n"))
playTTS() if (paly) playTTS()
} ?: stopSelf() } ?: stopSelf()
} ?: stopSelf() } ?: stopSelf()
} }

@ -120,9 +120,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
override fun skipToPage(page: Int) { override fun skipToPage(page: Int) {
viewModel.durPageIndex = page viewModel.durPageIndex = page
page_view.upContent() page_view.upContent()
if (readAloudStatus == Status.PLAY) { curPageChanged()
readAloud()
}
} }
override fun skipPreChapter() { override fun skipPreChapter() {
@ -236,7 +234,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
viewModel.curTextChapter = viewModel.curTextChapter =
ChapterProvider.getTextChapter(content_text_view, bookChapter, content) ChapterProvider.getTextChapter(content_text_view, bookChapter, content)
page_view.upContent() page_view.upContent()
curChapterChange() curChapterChanged()
} }
viewModel.durChapterIndex - 1 -> { viewModel.durChapterIndex - 1 -> {
viewModel.prevTextChapter = viewModel.prevTextChapter =
@ -250,7 +248,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
private fun curChapterChange() { private fun curChapterChanged() {
viewModel.curTextChapter?.let { viewModel.curTextChapter?.let {
tv_chapter_name.text = it.title tv_chapter_name.text = it.title
tv_chapter_name.visible() tv_chapter_name.visible()
@ -259,8 +257,15 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
tv_chapter_url.visible() tv_chapter_url.visible()
} }
read_menu.upReadProgress(it.pageSize(), viewModel.durPageIndex) read_menu.upReadProgress(it.pageSize(), viewModel.durPageIndex)
if (readAloudStatus == Status.PLAY) { curPageChanged()
readAloud() }
}
private fun curPageChanged() {
when (readAloudStatus) {
Status.PLAY -> readAloud()
Status.PAUSE -> {
readAloud(false)
} }
} }
} }
@ -294,9 +299,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
override fun setPageIndex(pageIndex: Int) { override fun setPageIndex(pageIndex: Int) {
viewModel.durPageIndex = pageIndex viewModel.durPageIndex = pageIndex
viewModel.saveRead() viewModel.saveRead()
if (readAloudStatus == Status.PLAY) { curPageChanged()
readAloud()
}
} }
override fun textChapter(chapterOnDur: Int): TextChapter? { override fun textChapter(chapterOnDur: Int): TextChapter? {
@ -311,13 +314,13 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
override fun moveToNextChapter() { override fun moveToNextChapter() {
viewModel.durPageIndex = 0 viewModel.durPageIndex = 0
viewModel.moveToNextChapter() viewModel.moveToNextChapter()
curChapterChange() curChapterChanged()
} }
override fun moveToPrevChapter() { override fun moveToPrevChapter() {
viewModel.durPageIndex = viewModel.prevTextChapter?.lastIndex() ?: 0 viewModel.durPageIndex = viewModel.prevTextChapter?.lastIndex() ?: 0
viewModel.moveToPrevChapter() viewModel.moveToPrevChapter()
curChapterChange() curChapterChanged()
} }
override fun clickCenter() { override fun clickCenter() {
@ -346,7 +349,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
/** /**
* 朗读 * 朗读
*/ */
private fun readAloud() { private fun readAloud(play: Boolean = true) {
val book = viewModel.bookData.value val book = viewModel.bookData.value
val textChapter = viewModel.curTextChapter val textChapter = viewModel.curTextChapter
if (book != null && textChapter != null) { if (book != null && textChapter != null) {
@ -357,7 +360,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
book.name, book.name,
textChapter.title, textChapter.title,
viewModel.durPageIndex, viewModel.durPageIndex,
key key,
play
) )
} }
} }

Loading…
Cancel
Save