mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
263177cc6e
commit
346c725e19
@ -14,7 +14,6 @@ import android.support.v4.media.session.MediaSessionCompat
|
|||||||
import android.support.v4.media.session.PlaybackStateCompat
|
import android.support.v4.media.session.PlaybackStateCompat
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.media.AudioFocusRequestCompat
|
import androidx.media.AudioFocusRequestCompat
|
||||||
import com.google.android.exoplayer2.ExoPlayer
|
|
||||||
import com.google.android.exoplayer2.PlaybackException
|
import com.google.android.exoplayer2.PlaybackException
|
||||||
import com.google.android.exoplayer2.Player
|
import com.google.android.exoplayer2.Player
|
||||||
import com.google.android.exoplayer2.SimpleExoPlayer
|
import com.google.android.exoplayer2.SimpleExoPlayer
|
||||||
@ -25,6 +24,7 @@ import io.legado.app.constant.EventBus
|
|||||||
import io.legado.app.constant.IntentAction
|
import io.legado.app.constant.IntentAction
|
||||||
import io.legado.app.constant.Status
|
import io.legado.app.constant.Status
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
|
import io.legado.app.data.entities.Book
|
||||||
import io.legado.app.data.entities.BookChapter
|
import io.legado.app.data.entities.BookChapter
|
||||||
import io.legado.app.help.ExoPlayerHelper
|
import io.legado.app.help.ExoPlayerHelper
|
||||||
import io.legado.app.help.IntentHelp
|
import io.legado.app.help.IntentHelp
|
||||||
@ -54,7 +54,7 @@ class AudioPlayService : BaseService(),
|
|||||||
|
|
||||||
private lateinit var audioManager: AudioManager
|
private lateinit var audioManager: AudioManager
|
||||||
private lateinit var mFocusRequest: AudioFocusRequestCompat
|
private lateinit var mFocusRequest: AudioFocusRequestCompat
|
||||||
private lateinit var exoPlayer: ExoPlayer
|
private lateinit var exoPlayer: SimpleExoPlayer
|
||||||
private var title: String = ""
|
private var title: String = ""
|
||||||
private var subtitle: String = ""
|
private var subtitle: String = ""
|
||||||
private var mediaSessionCompat: MediaSessionCompat? = null
|
private var mediaSessionCompat: MediaSessionCompat? = null
|
||||||
@ -135,6 +135,7 @@ class AudioPlayService : BaseService(),
|
|||||||
exoPlayer.setMediaSource(mediaSource)
|
exoPlayer.setMediaSource(mediaSource)
|
||||||
exoPlayer.playWhenReady = true
|
exoPlayer.playWhenReady = true
|
||||||
exoPlayer.prepare()
|
exoPlayer.prepare()
|
||||||
|
exoPlayer.seekTo(position.toLong())
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
it.printStackTrace()
|
it.printStackTrace()
|
||||||
toastOnUi("$url ${it.localizedMessage}")
|
toastOnUi("$url ${it.localizedMessage}")
|
||||||
@ -163,7 +164,6 @@ class AudioPlayService : BaseService(),
|
|||||||
pause = false
|
pause = false
|
||||||
if (!exoPlayer.isPlaying) {
|
if (!exoPlayer.isPlaying) {
|
||||||
exoPlayer.play()
|
exoPlayer.play()
|
||||||
exoPlayer.seekTo(position.toLong())
|
|
||||||
}
|
}
|
||||||
upPlayProgress()
|
upPlayProgress()
|
||||||
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
|
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
|
||||||
@ -177,11 +177,7 @@ class AudioPlayService : BaseService(),
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun adjustProgress(position: Int) {
|
private fun adjustProgress(position: Int) {
|
||||||
if (exoPlayer.isPlaying) {
|
exoPlayer.seekTo(position.toLong())
|
||||||
exoPlayer.seekTo(position.toLong())
|
|
||||||
} else {
|
|
||||||
this.position = position
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun upSpeed(adjust: Float) {
|
private fun upSpeed(adjust: Float) {
|
||||||
@ -205,8 +201,13 @@ class AudioPlayService : BaseService(),
|
|||||||
}
|
}
|
||||||
Player.STATE_READY -> {
|
Player.STATE_READY -> {
|
||||||
// 准备好
|
// 准备好
|
||||||
AudioPlay.status = Status.PLAY
|
if (exoPlayer.playWhenReady) {
|
||||||
postEvent(EventBus.AUDIO_STATE, Status.PLAY)
|
AudioPlay.status = Status.PLAY
|
||||||
|
postEvent(EventBus.AUDIO_STATE, Status.PLAY)
|
||||||
|
} else {
|
||||||
|
AudioPlay.status = Status.PAUSE
|
||||||
|
postEvent(EventBus.AUDIO_STATE, Status.PAUSE)
|
||||||
|
}
|
||||||
postEvent(EventBus.AUDIO_SIZE, exoPlayer.duration)
|
postEvent(EventBus.AUDIO_SIZE, exoPlayer.duration)
|
||||||
upPlayProgress()
|
upPlayProgress()
|
||||||
AudioPlay.saveDurChapter(exoPlayer.duration)
|
AudioPlay.saveDurChapter(exoPlayer.duration)
|
||||||
@ -277,8 +278,11 @@ class AudioPlayService : BaseService(),
|
|||||||
upPlayProgressJob?.cancel()
|
upPlayProgressJob?.cancel()
|
||||||
upPlayProgressJob = launch {
|
upPlayProgressJob = launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
saveProgress()
|
AudioPlay.book?.let {
|
||||||
postEvent(EventBus.AUDIO_PROGRESS, exoPlayer.currentPosition)
|
it.durChapterPos = exoPlayer.currentPosition.toInt()
|
||||||
|
postEvent(EventBus.AUDIO_PROGRESS, it.durChapterPos)
|
||||||
|
saveProgress(it)
|
||||||
|
}
|
||||||
delay(1000)
|
delay(1000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -286,7 +290,7 @@ class AudioPlayService : BaseService(),
|
|||||||
|
|
||||||
private fun loadContent() = with(AudioPlay) {
|
private fun loadContent() = with(AudioPlay) {
|
||||||
durChapter?.let { chapter ->
|
durChapter?.let { chapter ->
|
||||||
if (addLoading(durChapterIndex)) {
|
if (addLoading(chapter.index)) {
|
||||||
val book = AudioPlay.book
|
val book = AudioPlay.book
|
||||||
val bookSource = AudioPlay.bookSource
|
val bookSource = AudioPlay.bookSource
|
||||||
if (book != null && bookSource != null) {
|
if (book != null && bookSource != null) {
|
||||||
@ -330,19 +334,16 @@ class AudioPlayService : BaseService(),
|
|||||||
* 加载完成
|
* 加载完成
|
||||||
*/
|
*/
|
||||||
private fun contentLoadFinish(chapter: BookChapter, content: String) {
|
private fun contentLoadFinish(chapter: BookChapter, content: String) {
|
||||||
if (chapter.index == AudioPlay.durChapterIndex) {
|
if (chapter.index == AudioPlay.book?.durChapterIndex) {
|
||||||
subtitle = chapter.title
|
subtitle = chapter.title
|
||||||
url = content
|
url = content
|
||||||
play()
|
play()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveProgress() {
|
private fun saveProgress(book: Book) {
|
||||||
execute {
|
execute {
|
||||||
AudioPlay.book?.let {
|
appDb.bookDao.upProgress(book.bookUrl, book.durChapterPos)
|
||||||
AudioPlay.durChapterPos = exoPlayer.currentPosition.toInt()
|
|
||||||
appDb.bookDao.upProgress(it.bookUrl, AudioPlay.durChapterPos)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,8 +22,6 @@ object AudioPlay {
|
|||||||
var book: Book? = null
|
var book: Book? = null
|
||||||
var durChapter: BookChapter? = null
|
var durChapter: BookChapter? = null
|
||||||
var inBookshelf = false
|
var inBookshelf = false
|
||||||
var durChapterIndex = 0
|
|
||||||
var durChapterPos = 0
|
|
||||||
var bookSource: BookSource? = null
|
var bookSource: BookSource? = null
|
||||||
val loadingChapters = arrayListOf<Int>()
|
val loadingChapters = arrayListOf<Int>()
|
||||||
|
|
||||||
@ -37,18 +35,19 @@ object AudioPlay {
|
|||||||
upDurChapter(it)
|
upDurChapter(it)
|
||||||
}
|
}
|
||||||
durChapter?.let {
|
durChapter?.let {
|
||||||
val intent = Intent(context, AudioPlayService::class.java)
|
context.startService<AudioPlayService> {
|
||||||
intent.action = IntentAction.play
|
action = IntentAction.play
|
||||||
context.startService(intent)
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun upDurChapter(book: Book) {
|
fun upDurChapter(book: Book) {
|
||||||
durChapter = appDb.bookChapterDao.getChapter(book.bookUrl, durChapterIndex)
|
durChapter = appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex)
|
||||||
postEvent(EventBus.AUDIO_SUB_TITLE, durChapter?.title ?: "")
|
postEvent(EventBus.AUDIO_SUB_TITLE, durChapter?.title ?: "")
|
||||||
postEvent(EventBus.AUDIO_SIZE, durChapter?.end?.toInt() ?: 0)
|
postEvent(EventBus.AUDIO_SIZE, durChapter?.end?.toInt() ?: 0)
|
||||||
postEvent(EventBus.AUDIO_PROGRESS, durChapterPos)
|
postEvent(EventBus.AUDIO_PROGRESS, book.durChapterPos)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun pause(context: Context) {
|
fun pause(context: Context) {
|
||||||
@ -96,8 +95,8 @@ object AudioPlay {
|
|||||||
fun skipTo(context: Context, index: Int) {
|
fun skipTo(context: Context, index: Int) {
|
||||||
Coroutine.async {
|
Coroutine.async {
|
||||||
book?.let { book ->
|
book?.let { book ->
|
||||||
durChapterIndex = index
|
book.durChapterIndex = index
|
||||||
durChapterPos = 0
|
book.durChapterPos = 0
|
||||||
durChapter = null
|
durChapter = null
|
||||||
saveRead(book)
|
saveRead(book)
|
||||||
play(context)
|
play(context)
|
||||||
@ -111,8 +110,8 @@ object AudioPlay {
|
|||||||
if (book.durChapterIndex <= 0) {
|
if (book.durChapterIndex <= 0) {
|
||||||
return@let
|
return@let
|
||||||
}
|
}
|
||||||
durChapterIndex--
|
book.durChapterIndex = book.durChapterIndex - 1
|
||||||
durChapterPos = 0
|
book.durChapterPos = 0
|
||||||
durChapter = null
|
durChapter = null
|
||||||
saveRead(book)
|
saveRead(book)
|
||||||
play(context)
|
play(context)
|
||||||
@ -125,8 +124,8 @@ object AudioPlay {
|
|||||||
if (book.durChapterIndex >= book.totalChapterNum) {
|
if (book.durChapterIndex >= book.totalChapterNum) {
|
||||||
return@let
|
return@let
|
||||||
}
|
}
|
||||||
durChapterIndex++
|
book.durChapterIndex = book.durChapterIndex + 1
|
||||||
durChapterPos = 0
|
book.durChapterPos = 0
|
||||||
durChapter = null
|
durChapter = null
|
||||||
saveRead(book)
|
saveRead(book)
|
||||||
play(context)
|
play(context)
|
||||||
@ -142,8 +141,6 @@ object AudioPlay {
|
|||||||
fun saveRead(book: Book) {
|
fun saveRead(book: Book) {
|
||||||
book.lastCheckCount = 0
|
book.lastCheckCount = 0
|
||||||
book.durChapterTime = System.currentTimeMillis()
|
book.durChapterTime = System.currentTimeMillis()
|
||||||
book.durChapterIndex = durChapterIndex
|
|
||||||
book.durChapterPos = durChapterPos
|
|
||||||
Coroutine.async {
|
Coroutine.async {
|
||||||
appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex)?.let {
|
appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex)?.let {
|
||||||
book.durChapterTitle = it.title
|
book.durChapterTitle = it.title
|
||||||
|
@ -54,7 +54,7 @@ class AudioPlayActivity :
|
|||||||
}
|
}
|
||||||
private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
|
private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
|
||||||
it?.let {
|
it?.let {
|
||||||
if (it.first != AudioPlay.durChapterIndex) {
|
if (it.first != AudioPlay.book?.durChapterIndex) {
|
||||||
AudioPlay.skipTo(this, it.first)
|
AudioPlay.skipTo(this, it.first)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -233,7 +233,6 @@ class AudioPlayActivity :
|
|||||||
binding.tvAllTime.text = progressTimeFormat.format(it.toLong())
|
binding.tvAllTime.text = progressTimeFormat.format(it.toLong())
|
||||||
}
|
}
|
||||||
observeEventSticky<Int>(EventBus.AUDIO_PROGRESS) {
|
observeEventSticky<Int>(EventBus.AUDIO_PROGRESS) {
|
||||||
AudioPlay.durChapterPos = it
|
|
||||||
if (!adjustProgress) binding.playerProgress.progress = it
|
if (!adjustProgress) binding.playerProgress.progress = it
|
||||||
binding.tvDurTime.text = progressTimeFormat.format(it.toLong())
|
binding.tvDurTime.text = progressTimeFormat.format(it.toLong())
|
||||||
}
|
}
|
||||||
|
@ -27,9 +27,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
|
|||||||
book?.let { book ->
|
book?.let { book ->
|
||||||
titleData.postValue(book.name)
|
titleData.postValue(book.name)
|
||||||
coverData.postValue(book.getDisplayCover())
|
coverData.postValue(book.getDisplayCover())
|
||||||
durChapterIndex = book.durChapterIndex
|
durChapter = appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex)
|
||||||
durChapterPos = book.durChapterPos
|
|
||||||
durChapter = appDb.bookChapterDao.getChapter(book.bookUrl, durChapterIndex)
|
|
||||||
upDurChapter(book)
|
upDurChapter(book)
|
||||||
bookSource = appDb.bookSourceDao.getBookSource(book.origin)
|
bookSource = appDb.bookSourceDao.getBookSource(book.origin)
|
||||||
if (durChapter == null) {
|
if (durChapter == null) {
|
||||||
@ -111,14 +109,13 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
|
|||||||
chapters: List<BookChapter>
|
chapters: List<BookChapter>
|
||||||
) {
|
) {
|
||||||
execute {
|
execute {
|
||||||
AudioPlay.durChapterIndex = BookHelp.getDurChapter(
|
book.durChapterIndex = BookHelp.getDurChapter(
|
||||||
book.durChapterIndex,
|
book.durChapterIndex,
|
||||||
oldTocSize,
|
oldTocSize,
|
||||||
book.durChapterTitle,
|
book.durChapterTitle,
|
||||||
chapters
|
chapters
|
||||||
)
|
)
|
||||||
book.durChapterIndex = AudioPlay.durChapterIndex
|
book.durChapterTitle = chapters[book.durChapterIndex].title
|
||||||
book.durChapterTitle = chapters[AudioPlay.durChapterIndex].title
|
|
||||||
appDb.bookDao.update(book)
|
appDb.bookDao.update(book)
|
||||||
appDb.bookChapterDao.insert(*chapters.toTypedArray())
|
appDb.bookChapterDao.insert(*chapters.toTypedArray())
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ buildscript {
|
|||||||
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
|
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:7.0.1'
|
classpath 'com.android.tools.build:gradle:7.0.2'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
classpath 'de.timfreiheit.resourceplaceholders:placeholders:0.4'
|
classpath 'de.timfreiheit.resourceplaceholders:placeholders:0.4'
|
||||||
classpath 'de.undercouch:gradle-download-task:4.1.2'
|
classpath 'de.undercouch:gradle-download-task:4.1.2'
|
||||||
|
Loading…
Reference in New Issue
Block a user