diff --git a/app/src/main/java/io/legado/app/base/BaseService.kt b/app/src/main/java/io/legado/app/base/BaseService.kt new file mode 100644 index 000000000..c67acb95c --- /dev/null +++ b/app/src/main/java/io/legado/app/base/BaseService.kt @@ -0,0 +1,21 @@ +package io.legado.app.base + +import android.app.Service +import android.content.Intent +import android.os.IBinder +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.cancel + +abstract class BaseService : Service(), CoroutineScope by MainScope() { + + override fun onBind(intent: Intent?): IBinder? { + return null + } + + + override fun onDestroy() { + super.onDestroy() + cancel() + } +} \ No newline at end of file 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 2735e54be..04f4670ee 100644 --- a/app/src/main/java/io/legado/app/service/ReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/ReadAloudService.kt @@ -1,12 +1,89 @@ package io.legado.app.service -import android.app.Service import android.content.Intent -import android.os.IBinder +import android.speech.tts.TextToSpeech +import io.legado.app.R +import io.legado.app.base.BaseService +import io.legado.app.utils.toast +import kotlinx.coroutines.launch +import java.util.* -class ReadAloudService : Service() { - override fun onBind(intent: Intent?): IBinder? { - return null +class ReadAloudService : BaseService(), TextToSpeech.OnInitListener { + + companion object { + fun paly() { + + } + + fun pause() { + + } + + fun resume() { + + } + + fun stop() { + + } + } + + private var textToSpeech: TextToSpeech? = null + private var ttsIsSuccess: Boolean = false + + override fun onCreate() { + super.onCreate() + textToSpeech = TextToSpeech(this, this) + + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + intent?.action?.let { action -> + when (action) { + "play" -> { + + } + "pause" -> { + + } + "resume" -> { + + } + "stop" -> { + + } + } + } + return super.onStartCommand(intent, flags, startId) + } + + override fun onInit(status: Int) { + launch { + if (status == TextToSpeech.SUCCESS) { + val result = textToSpeech?.setLanguage(Locale.CHINA) + if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { + toast(R.string.tts_fix) + toTTSSetting() + } else { + ttsIsSuccess = true + } + } else { + toast(R.string.tts_init_failed) + } + } + } + + private fun toTTSSetting() { + //跳转到文字转语音设置界面 + + try { + val intent = Intent() + intent.action = "com.android.settings.TTS_SETTINGS" + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK + startActivity(intent) + } catch (ignored: Exception) { + toast(R.string.tip_cannot_jump_setting_page) + } } } \ No newline at end of file