diff --git a/app/src/main/java/io/legado/app/constant/Bus.kt b/app/src/main/java/io/legado/app/constant/Bus.kt index 562da87ed..96ec93b5e 100644 --- a/app/src/main/java/io/legado/app/constant/Bus.kt +++ b/app/src/main/java/io/legado/app/constant/Bus.kt @@ -6,6 +6,7 @@ object Bus { const val ALOUD_STATE = "aloud_state" const val TTS_START = "ttsStart" const val TTS_NEXT = "ttsNextPage" + const val TTS_DS = "ttsDs" const val BATTERY_CHANGED = "batteryChanged" const val TIME_CHANGED = "timeChanged" const val READ_ALOUD_BUTTON = "readAloudButton" diff --git a/app/src/main/java/io/legado/app/service/ReadAloudService.kt b/app/src/main/java/io/legado/app/service/ReadAloudService.kt index db86d08ef..cbcd06515 100644 --- a/app/src/main/java/io/legado/app/service/ReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/ReadAloudService.kt @@ -34,6 +34,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage val tag: String = ReadAloudService::class.java.simpleName var isRun = false var textToSpeech: TextToSpeech? = null + var timeMinute: Int = 0 fun play( context: Context, @@ -101,6 +102,15 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage } } + fun setTimer(context: Context, minute: Int) { + if (isRun) { + val intent = Intent(context, ReadAloudService::class.java) + intent.action = Action.setTimer + intent.putExtra("minute", minute) + context.startService(intent) + } + } + fun clearTTS() { textToSpeech?.stop() textToSpeech?.shutdown() @@ -124,7 +134,6 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage var pause = false var title: String = "" var subtitle: String = "" - var timeMinute: Int = 0 override fun onCreate() { super.onCreate() @@ -243,6 +252,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage handler.removeCallbacks(dsRunnable) handler.postDelayed(dsRunnable, 6000) } + postEvent(Bus.TTS_DS, timeMinute) ReadAloudNotification.upNotification(this) } @@ -255,6 +265,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage handler.postDelayed(dsRunnable, 6000) } } + postEvent(Bus.TTS_DS, timeMinute) ReadAloudNotification.upNotification(this) } diff --git a/app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt b/app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt index 9189f17fd..d7e0c51ca 100644 --- a/app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt +++ b/app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt @@ -16,7 +16,10 @@ object ReadAloudNotification { fun upNotification(service: ReadAloudService) { var nTitle: String = when { service.pause -> service.getString(R.string.read_aloud_pause) - service.timeMinute in 1..60 -> service.getString(R.string.read_aloud_timer, service.timeMinute) + ReadAloudService.timeMinute in 1..60 -> service.getString( + R.string.read_aloud_timer, + ReadAloudService.timeMinute + ) else -> service.getString(R.string.read_aloud_t) } nTitle += ": ${service.title}" diff --git a/app/src/main/java/io/legado/app/ui/readbook/config/ReadAloudDialog.kt b/app/src/main/java/io/legado/app/ui/readbook/config/ReadAloudDialog.kt index 65282b558..dddda3b39 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/config/ReadAloudDialog.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/config/ReadAloudDialog.kt @@ -54,10 +54,12 @@ class ReadAloudDialog : DialogFragment() { private fun initData() { observeEvent(Bus.ALOUD_STATE) { upPlayState(it) } + observeEvent(Bus.TTS_DS) { seek_timer.progress = it } val activity = activity if (activity is ReadBookActivity) { upPlayState(activity.readAloudStatus) } + tv_timer.text = requireContext().getString(R.string.timer_m, ReadAloudService.timeMinute) cb_by_page.isChecked = requireContext().getPrefBoolean("readAloudByPage") cb_tts_follow_sys.isChecked = requireContext().getPrefBoolean("ttsFollowSys", true) seek_tts_SpeechRate.isEnabled = !cb_tts_follow_sys.isChecked @@ -92,15 +94,14 @@ class ReadAloudDialog : DialogFragment() { }) seek_timer.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - + tv_timer.text = requireContext().getString(R.string.timer_m, progress) } override fun onStartTrackingTouch(seekBar: SeekBar?) { - } override fun onStopTrackingTouch(seekBar: SeekBar?) { - + ReadAloudService.setTimer(requireContext(), seek_timer.progress) } }) }