From ec1d335c57623c01d327b6e5951d67b724069ece Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 10 Jan 2020 15:25:00 +0800 Subject: [PATCH] up --- .../legado/app/ui/rss/read/ReadRssActivity.kt | 4 +- .../app/ui/rss/read/ReadRssViewModel.kt | 53 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 951d190f7..138ede020 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -122,7 +122,9 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r } private fun readAloud() { - + webView.evaluateJavascript("document.documentElement.outerHTML") { + viewModel.readAloud(it) + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 98355d7d8..db9d7bb82 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -2,21 +2,28 @@ package io.legado.app.ui.rss.read import android.app.Application import android.content.Intent +import android.speech.tts.TextToSpeech +import android.speech.tts.UtteranceProgressListener import androidx.lifecycle.MutableLiveData import io.legado.app.App +import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssSource import io.legado.app.model.Rss import io.legado.app.model.analyzeRule.AnalyzeUrl +import kotlinx.coroutines.launch +import java.util.* -class ReadRssViewModel(application: Application) : BaseViewModel(application) { +class ReadRssViewModel(application: Application) : BaseViewModel(application), + TextToSpeech.OnInitListener { var callBack: CallBack? = null var rssSource: RssSource? = null var rssArticle: RssArticle? = null val contentLiveData = MutableLiveData() val urlLiveData = MutableLiveData() var star = false + var textToSpeech: TextToSpeech = TextToSpeech(context, this) fun initData(intent: Intent) { execute { @@ -91,6 +98,50 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) { } } + override fun onInit(status: Int) { + launch { + if (status == TextToSpeech.SUCCESS) { + textToSpeech.language = Locale.CHINA + textToSpeech.setOnUtteranceProgressListener(TTSUtteranceListener()) + } else { + toast(R.string.tts_init_failed) + } + } + } + + fun readAloud(text: String) { + + } + + override fun onCleared() { + super.onCleared() + textToSpeech.stop() + textToSpeech.shutdown() + } + + /** + * 朗读监听 + */ + private inner class TTSUtteranceListener : UtteranceProgressListener() { + + override fun onStart(s: String) { + + } + + override fun onDone(s: String) { + + } + + override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) { + super.onRangeStart(utteranceId, start, end, frame) + + } + + override fun onError(s: String) { + + } + + } interface CallBack { fun upStarMenu() }