diff --git a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt new file mode 100644 index 000000000..763d6a347 --- /dev/null +++ b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt @@ -0,0 +1,44 @@ +package io.legado.app.service.help + +import android.content.Context +import android.content.Intent +import io.legado.app.constant.Action +import io.legado.app.service.AudioPlayService + +object AudioPlay { + + fun play(context: Context, title: String?, subtitle: String, url: String, position: Int) { + val intent = Intent(context, AudioPlayService::class.java) + intent.action = Action.play + intent.putExtra("title", title) + intent.putExtra("subtitle", subtitle) + intent.putExtra("url", url) + intent.putExtra("dataKey", position) + context.startService(intent) + } + + fun pause(context: Context) { + if (AudioPlayService.isRun) { + val intent = Intent(context, AudioPlayService::class.java) + intent.action = Action.pause + context.startService(intent) + } + } + + fun resume(context: Context) { + if (AudioPlayService.isRun) { + val intent = Intent(context, AudioPlayService::class.java) + intent.action = Action.resume + context.startService(intent) + } + } + + fun stop(context: Context) { + if (AudioPlayService.isRun) { + val intent = Intent(context, AudioPlayService::class.java) + intent.action = Action.stop + context.startService(intent) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/ReadAloud.kt b/app/src/main/java/io/legado/app/service/help/ReadAloud.kt similarity index 85% rename from app/src/main/java/io/legado/app/help/ReadAloud.kt rename to app/src/main/java/io/legado/app/service/help/ReadAloud.kt index 9dd52c9a4..d2a789a5a 100644 --- a/app/src/main/java/io/legado/app/help/ReadAloud.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadAloud.kt @@ -1,4 +1,4 @@ -package io.legado.app.help +package io.legado.app.service.help import android.content.Context import android.content.Intent @@ -28,14 +28,14 @@ object ReadAloud { dataKey: String, play: Boolean = true ) { - val readAloudIntent = Intent(context, aloudClass) - readAloudIntent.action = Action.play - readAloudIntent.putExtra("title", title) - readAloudIntent.putExtra("subtitle", subtitle) - readAloudIntent.putExtra("pageIndex", pageIndex) - readAloudIntent.putExtra("dataKey", dataKey) - readAloudIntent.putExtra("play", play) - context.startService(readAloudIntent) + val intent = Intent(context, aloudClass) + intent.action = Action.play + intent.putExtra("title", title) + intent.putExtra("subtitle", subtitle) + intent.putExtra("pageIndex", pageIndex) + intent.putExtra("dataKey", dataKey) + intent.putExtra("play", play) + context.startService(intent) } fun pause(context: Context) { diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt index dc198a3a5..7f2386018 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt @@ -3,9 +3,14 @@ package io.legado.app.ui.audio import android.os.Bundle import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.Bus import io.legado.app.data.entities.BookChapter +import io.legado.app.service.help.AudioPlay import io.legado.app.utils.getViewModel +import io.legado.app.utils.observeEvent +import kotlinx.android.synthetic.main.activity_audio_play.* import kotlinx.android.synthetic.main.view_title_bar.* +import org.jetbrains.anko.sdk27.listeners.onClick class AudioPlayActivity : VMBaseActivity(R.layout.activity_audio_play), AudioPlayViewModel.CallBack { @@ -20,19 +25,29 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a } private fun initView() { - - } - - override fun loadContent() { - + fab_play_stop.onClick { + viewModel.bookData.value?.let { + viewModel.loadContent(it, viewModel.durChapterIndex) + } + } } override fun contentLoadFinish(bookChapter: BookChapter, content: String) { - + AudioPlay.play( + this, + viewModel.bookData.value?.name, + bookChapter.title, + content, + viewModel.durPageIndex + ) + viewModel.bookData.value?.let { + viewModel.loadContent(it, viewModel.durChapterIndex + 1) + } } - override fun upContent() { + override fun observeLiveBus() { + observeEvent(Bus.AUDIO_NEXT) { + } } - } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt index 48bfde649..aec1a8456 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt @@ -85,7 +85,6 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) if (changeDruChapterIndex == null) { App.db.bookChapterDao().insert(*cList.toTypedArray()) chapterSize = cList.size - callBack?.loadContent() } else { changeDruChapterIndex(cList) } @@ -113,22 +112,6 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } - private fun download(book: Book, index: Int) { - if (addLoading(index)) { - execute { - App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> - if (BookHelp.hasContent(book, chapter)) { - removeLoading(chapter.index) - } else { - download(book, chapter) - } - } ?: removeLoading(index) - }.onError { - removeLoading(index) - } - } - } - private fun download(book: Book, chapter: BookChapter) { webBook?.getContent(book, chapter, scope = this) ?.onSuccess(Dispatchers.IO) { content -> @@ -216,8 +199,6 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } interface CallBack { - fun loadContent() fun contentLoadFinish(bookChapter: BookChapter, content: String) - fun upContent() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 954ddb1eb..b2dd3dae0 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -19,7 +19,6 @@ import io.legado.app.constant.Status import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.IntentDataHelp -import io.legado.app.help.ReadAloud import io.legado.app.help.ReadBookConfig import io.legado.app.help.storage.Backup import io.legado.app.lib.dialogs.alert @@ -27,6 +26,7 @@ import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.okButton import io.legado.app.receiver.TimeElectricityReceiver import io.legado.app.service.BaseReadAloudService +import io.legado.app.service.help.ReadAloud import io.legado.app.ui.book.read.config.* import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.BG_COLOR import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.TEXT_COLOR diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index d12e89deb..daf901f60 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -11,8 +11,8 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp import io.legado.app.help.IntentDataHelp -import io.legado.app.help.ReadAloud import io.legado.app.model.WebBook +import io.legado.app.service.help.ReadAloud import io.legado.app.ui.widget.page.TextChapter import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt index 699037325..0c81d8adf 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudConfigDialog.kt @@ -13,9 +13,9 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import io.legado.app.R import io.legado.app.constant.Bus -import io.legado.app.help.ReadAloud import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService +import io.legado.app.service.help.ReadAloud import io.legado.app.ui.book.read.Help import io.legado.app.utils.getPrefString import io.legado.app.utils.postEvent diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt index e9f524dcb..4a40a53cf 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt @@ -11,8 +11,8 @@ import androidx.fragment.app.DialogFragment import io.legado.app.R import io.legado.app.constant.Bus import io.legado.app.constant.Status -import io.legado.app.help.ReadAloud import io.legado.app.service.BaseReadAloudService +import io.legado.app.service.help.ReadAloud import io.legado.app.ui.book.read.Help import io.legado.app.utils.* import kotlinx.android.synthetic.main.dialog_read_aloud.* diff --git a/app/src/main/res/layout/activity_audio_play.xml b/app/src/main/res/layout/activity_audio_play.xml index ec6bbd556..9646fd7c5 100644 --- a/app/src/main/res/layout/activity_audio_play.xml +++ b/app/src/main/res/layout/activity_audio_play.xml @@ -97,7 +97,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" - android:contentDescription="@string/read_aloud" + android:contentDescription="@string/audio_play" android:src="@drawable/ic_play_24dp" android:tint="@color/tv_text_default" app:backgroundTint="@color/background_menu" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 69829f0ab..aa36c4911 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -125,6 +125,7 @@ 朗读 正在朗读 点击打开阅读界面 + 播放 正在播放 点击打开播放界面 播放暂停