pull/1319/head
gedoor 3 years ago
parent 27e4d1acbf
commit 8f7d7937fa
  1. 8
      app/src/main/java/io/legado/app/model/ReadAloud.kt
  2. 15
      app/src/main/java/io/legado/app/model/ReadBook.kt
  3. 18
      app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt
  4. 24
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  5. 5
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  6. 9
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -33,18 +33,10 @@ object ReadAloud {
fun play(
context: Context,
title: String,
subtitle: String,
pageIndex: Int,
dataKey: String,
play: Boolean = true
) {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.play
intent.putExtra("title", title)
intent.putExtra("subtitle", subtitle)
intent.putExtra("pageIndex", pageIndex)
intent.putExtra("dataKey", dataKey)
intent.putExtra("play", play)
context.startService(intent)
}

@ -5,7 +5,10 @@ import com.github.liuyueyi.quick.transfer.ChineseUtils
import io.legado.app.constant.BookType
import io.legado.app.data.appDb
import io.legado.app.data.entities.*
import io.legado.app.help.*
import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp
import io.legado.app.help.ContentProcessor
import io.legado.app.help.ReadBookConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.storage.AppWebDav
import io.legado.app.model.webBook.WebBook
@ -46,6 +49,7 @@ object ReadBook : CoroutineScope by MainScope() {
ReadBook.book = book
readRecord.bookName = book.name
readRecord.readTime = appDb.readRecordDao.getReadTime(book.name) ?: 0
chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl)
durChapterIndex = book.durChapterIndex
durChapterPos = book.durChapterPos
isLocalBook = book.origin == BookType.local
@ -200,13 +204,8 @@ object ReadBook : CoroutineScope by MainScope() {
* 朗读
*/
fun readAloud(play: Boolean = true) {
val book = book
val textChapter = curTextChapter
if (book != null && textChapter != null) {
val key = IntentDataHelp.putData(textChapter)
ReadAloud.play(
appCtx, book.name, textChapter.title, durPageIndex(), key, play
)
book?.let {
ReadAloud.play(appCtx, play)
}
}

@ -15,10 +15,8 @@ import io.legado.app.service.AudioPlayService
import io.legado.app.service.BaseReadAloudService
import io.legado.app.ui.book.audio.AudioPlayActivity
import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.main.MainActivity
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.postEvent
import io.legado.app.utils.startActivity
/**
@ -87,16 +85,14 @@ class MediaButtonReceiver : BroadcastReceiver() {
postEvent(EventBus.MEDIA_BUTTON, true)
LifecycleHelp.isExistActivity(AudioPlayActivity::class.java) ->
postEvent(EventBus.MEDIA_BUTTON, true)
else -> if (AppConfig.mediaButtonOnExit || !isMediaKey || LifecycleHelp.activitySize() > 0) {
else -> if (AppConfig.mediaButtonOnExit || LifecycleHelp.activitySize() > 0 || !isMediaKey) {
if (ReadBook.book != null) {
ReadBook.readAloud()
} else {
appDb.bookDao.lastReadBook?.let {
if (!LifecycleHelp.isExistActivity(MainActivity::class.java)) {
context.startActivity<MainActivity> {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
}
context.startActivity<ReadBookActivity> {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra("readAloud", true)
ReadBook.resetData(it)
ReadBook.curTextChapter ?: ReadBook.loadContent(false)
ReadBook.readAloud()
}
}
}

@ -16,7 +16,6 @@ import androidx.media.AudioFocusRequestCompat
import io.legado.app.R
import io.legado.app.base.BaseService
import io.legado.app.constant.*
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.MediaHelp
import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook
@ -46,8 +45,6 @@ abstract class BaseReadAloudService : BaseService(),
private var mFocusRequest: AudioFocusRequestCompat? = null
private var broadcastReceiver: BroadcastReceiver? = null
private lateinit var mediaSessionCompat: MediaSessionCompat
private var title: String = ""
private var subtitle: String = ""
internal val contentList = arrayListOf<String>()
internal var nowSpeak: Int = 0
internal var readAloudNumber: Int = 0
@ -84,11 +81,9 @@ abstract class BaseReadAloudService : BaseService(),
intent?.action?.let { action ->
when (action) {
IntentAction.play -> {
title = intent.getStringExtra("title") ?: ""
subtitle = intent.getStringExtra("subtitle") ?: ""
pageIndex = intent.getIntExtra("pageIndex", 0)
pageIndex = ReadBook.durPageIndex()
newReadAloud(
intent.getStringExtra("dataKey"),
ReadBook.curTextChapter,
intent.getBooleanExtra("play", true)
)
}
@ -106,10 +101,8 @@ abstract class BaseReadAloudService : BaseService(),
}
@CallSuper
open fun newReadAloud(dataKey: String?, play: Boolean) {
dataKey?.let {
textChapter = IntentDataHelp.getData<TextChapter>(dataKey)
textChapter?.let { textChapter ->
open fun newReadAloud(textChapter: TextChapter?, play: Boolean) {
textChapter?.let {
nowSpeak = 0
readAloudNumber = textChapter.getReadLength(pageIndex)
contentList.clear()
@ -127,8 +120,7 @@ abstract class BaseReadAloudService : BaseService(),
}
}
if (play) play()
} ?: stopSelf()
} ?: stopSelf()
}
}
open fun play() {
@ -307,9 +299,9 @@ abstract class BaseReadAloudService : BaseService(),
)
else -> getString(R.string.read_aloud_t)
}
nTitle += ": $title"
var nSubtitle = subtitle
if (subtitle.isEmpty())
nTitle += ": ${ReadBook.book?.name}"
var nSubtitle = ReadBook.curTextChapter?.title
if (nSubtitle.isNullOrBlank())
nSubtitle = getString(R.string.read_aloud_s)
val builder = NotificationCompat.Builder(this, AppConst.channelIdReadAloud)
.setSmallIcon(R.drawable.ic_volume_up)

@ -8,6 +8,7 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.utils.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
@ -45,10 +46,10 @@ class HttpReadAloudService : BaseReadAloudService(),
player.release()
}
override fun newReadAloud(dataKey: String?, play: Boolean) {
override fun newReadAloud(textChapter: TextChapter?, play: Boolean) {
player.reset()
playingIndex = -1
super.newReadAloud(dataKey, play)
super.newReadAloud(textChapter, play)
}
override fun play() {

@ -51,12 +51,11 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun initBook(book: Book) {
if (ReadBook.book?.bookUrl != book.bookUrl) {
ReadBook.resetData(book)
isInitFinish = true
if (!book.isLocalBook() && ReadBook.bookSource == null) {
autoChangeSource(book.name, book.author)
return
}
ReadBook.chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl)
isInitFinish = true
if (ReadBook.chapterSize == 0) {
if (book.tocUrl.isEmpty()) {
loadBookInfo(book)
@ -194,11 +193,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
oldTocSize = it.totalChapterNum
it.changeTo(newBook)
}
ReadBook.book = newBook
ReadBook.bookSource = appDb.bookSourceDao.getBookSource(newBook.origin)
ReadBook.prevTextChapter = null
ReadBook.curTextChapter = null
ReadBook.nextTextChapter = null
ReadBook.resetData(newBook)
withContext(Main) {
ReadBook.callBack?.upContent()
}

Loading…
Cancel
Save