This commit is contained in:
Horis
2023-09-25 15:07:19 +08:00
parent f4a6f8f0bb
commit f724c089e5
2 changed files with 85 additions and 63 deletions
@@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import io.legado.app.constant.AppLog
import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction
import io.legado.app.data.appDb
@@ -14,6 +15,7 @@ import io.legado.app.service.HttpReadAloudService
import io.legado.app.service.TTSReadAloudService
import io.legado.app.utils.StringUtils
import io.legado.app.utils.postEvent
import io.legado.app.utils.toastOnUi
import splitties.init.appCtx
object ReadAloud {
@@ -51,7 +53,13 @@ object ReadAloud {
intent.putExtra("play", play)
intent.putExtra("pageIndex", pageIndex)
intent.putExtra("startPos", startPos)
try {
ContextCompat.startForegroundService(context, intent)
} catch (e: Exception) {
val msg = "启动朗读服务出错\n${e.localizedMessage}"
AppLog.put(msg, e)
context.toastOnUi(msg)
}
}
fun playByEventBus(
@@ -35,6 +35,7 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import splitties.init.appCtx
import splitties.systemservices.audioManager
import splitties.systemservices.notificationManager
import splitties.systemservices.powerManager
/**
@@ -119,7 +120,7 @@ abstract class BaseReadAloudService : BaseService(),
.get()
}.onSuccess {
cover = it
upNotification()
upReadAloudNotification()
}
}
@@ -213,7 +214,7 @@ abstract class BaseReadAloudService : BaseService(),
isRun = true
pause = false
needResumeOnAudioFocusGain = false
upNotification()
upReadAloudNotification()
postEvent(EventBus.ALOUD_STATE, Status.PLAY)
}
@@ -226,7 +227,7 @@ abstract class BaseReadAloudService : BaseService(),
if (abandonFocus) {
abandonFocus()
}
upNotification()
upReadAloudNotification()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
postEvent(EventBus.ALOUD_STATE, Status.PAUSE)
ReadBook.uploadProgress()
@@ -237,7 +238,7 @@ abstract class BaseReadAloudService : BaseService(),
@CallSuper
open fun resumeReadAloud() {
pause = false
upNotification()
upReadAloudNotification()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
postEvent(EventBus.ALOUD_STATE, Status.PLAY)
}
@@ -309,7 +310,7 @@ abstract class BaseReadAloudService : BaseService(),
@Synchronized
private fun doDs() {
postEvent(EventBus.READ_ALOUD_DS, timeMinute)
upNotification()
upReadAloudNotification()
dsJob?.cancel()
dsJob = lifecycleScope.launch {
while (isActive) {
@@ -323,7 +324,7 @@ abstract class BaseReadAloudService : BaseService(),
}
}
postEvent(EventBus.READ_ALOUD_DS, timeMinute)
upNotification()
upReadAloudNotification()
}
}
}
@@ -425,11 +426,17 @@ abstract class BaseReadAloudService : BaseService(),
}
}
/**
* 更新通知
*/
override fun upNotification() {
private fun upReadAloudNotification() {
execute {
createNotification()
}.onSuccess {
notificationManager.notify(NotificationId.ReadAloudService, it.build())
}.onError {
AppLog.put("创建朗读通知出错,${it.localizedMessage}", it, true)
}
}
private fun createNotification(): NotificationCompat.Builder {
var nTitle: String = when {
pause -> getString(R.string.read_aloud_pause)
timeMinute > 0 -> getString(
@@ -484,8 +491,15 @@ abstract class BaseReadAloudService : BaseService(),
androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
builder
return builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
}
/**
* 更新通知
*/
override fun upNotification() {
execute {
createNotification()
}.onSuccess {
startForeground(NotificationId.ReadAloudService, it.build())
}.onError {