|
@@ -3,20 +3,18 @@ package io.legado.app.ui.rss.read
|
|
|
import android.app.Application
|
|
import android.app.Application
|
|
|
import android.content.Intent
|
|
import android.content.Intent
|
|
|
import android.net.Uri
|
|
import android.net.Uri
|
|
|
-import android.speech.tts.TextToSpeech
|
|
|
|
|
-import android.speech.tts.UtteranceProgressListener
|
|
|
|
|
import android.util.Base64
|
|
import android.util.Base64
|
|
|
import android.webkit.URLUtil
|
|
import android.webkit.URLUtil
|
|
|
import androidx.documentfile.provider.DocumentFile
|
|
import androidx.documentfile.provider.DocumentFile
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.viewModelScope
|
|
import androidx.lifecycle.viewModelScope
|
|
|
-import io.legado.app.R
|
|
|
|
|
import io.legado.app.base.BaseViewModel
|
|
import io.legado.app.base.BaseViewModel
|
|
|
import io.legado.app.constant.AppConst
|
|
import io.legado.app.constant.AppConst
|
|
|
import io.legado.app.data.appDb
|
|
import io.legado.app.data.appDb
|
|
|
import io.legado.app.data.entities.RssArticle
|
|
import io.legado.app.data.entities.RssArticle
|
|
|
import io.legado.app.data.entities.RssSource
|
|
import io.legado.app.data.entities.RssSource
|
|
|
import io.legado.app.data.entities.RssStar
|
|
import io.legado.app.data.entities.RssStar
|
|
|
|
|
+import io.legado.app.help.TTS
|
|
|
import io.legado.app.help.http.newCallResponseBody
|
|
import io.legado.app.help.http.newCallResponseBody
|
|
|
import io.legado.app.help.http.okHttpClient
|
|
import io.legado.app.help.http.okHttpClient
|
|
|
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
|
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
|
@@ -27,17 +25,14 @@ import java.io.File
|
|
|
import java.util.*
|
|
import java.util.*
|
|
|
|
|
|
|
|
|
|
|
|
|
-class ReadRssViewModel(application: Application) : BaseViewModel(application),
|
|
|
|
|
- TextToSpeech.OnInitListener {
|
|
|
|
|
|
|
+class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
|
|
var callBack: CallBack? = null
|
|
var callBack: CallBack? = null
|
|
|
var rssSource: RssSource? = null
|
|
var rssSource: RssSource? = null
|
|
|
var rssArticle: RssArticle? = null
|
|
var rssArticle: RssArticle? = null
|
|
|
|
|
+ var tts: TTS? = null
|
|
|
val contentLiveData = MutableLiveData<String>()
|
|
val contentLiveData = MutableLiveData<String>()
|
|
|
val urlLiveData = MutableLiveData<AnalyzeUrl>()
|
|
val urlLiveData = MutableLiveData<AnalyzeUrl>()
|
|
|
var rssStar: RssStar? = null
|
|
var rssStar: RssStar? = null
|
|
|
- var textToSpeech: TextToSpeech? = null
|
|
|
|
|
- private var ttsInitFinish = false
|
|
|
|
|
- private var ttsTextList = arrayListOf<String>()
|
|
|
|
|
|
|
|
|
|
fun initData(intent: Intent) {
|
|
fun initData(intent: Intent) {
|
|
|
execute {
|
|
execute {
|
|
@@ -192,59 +187,26 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Synchronized
|
|
@Synchronized
|
|
|
- override fun onInit(status: Int) {
|
|
|
|
|
- if (status == TextToSpeech.SUCCESS) {
|
|
|
|
|
- textToSpeech?.setOnUtteranceProgressListener(TTSUtteranceListener())
|
|
|
|
|
- ttsInitFinish = true
|
|
|
|
|
- play()
|
|
|
|
|
- } else {
|
|
|
|
|
- context.toastOnUi(R.string.tts_init_failed)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Synchronized
|
|
|
|
|
- private fun play() {
|
|
|
|
|
- if (!ttsInitFinish) return
|
|
|
|
|
- textToSpeech?.stop()
|
|
|
|
|
- ttsTextList.forEach {
|
|
|
|
|
- textToSpeech?.speak(it, TextToSpeech.QUEUE_ADD, null, "rss")
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ fun readAloud(text: String) {
|
|
|
|
|
+ if (tts == null) {
|
|
|
|
|
+ tts = TTS().apply {
|
|
|
|
|
+ setSpeakStateListener(object : TTS.SpeakStateListener {
|
|
|
|
|
+ override fun onStart() {
|
|
|
|
|
+ callBack?.upTtsMenu(true)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- fun readAloud(textArray: Array<String>) {
|
|
|
|
|
- ttsTextList.clear()
|
|
|
|
|
- ttsTextList.addAll(textArray)
|
|
|
|
|
- textToSpeech?.let {
|
|
|
|
|
- play()
|
|
|
|
|
- } ?: let {
|
|
|
|
|
- textToSpeech = TextToSpeech(context, this)
|
|
|
|
|
|
|
+ override fun onDone() {
|
|
|
|
|
+ callBack?.upTtsMenu(false)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ tts?.speak(text)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
override fun onCleared() {
|
|
override fun onCleared() {
|
|
|
super.onCleared()
|
|
super.onCleared()
|
|
|
- textToSpeech?.stop()
|
|
|
|
|
- textToSpeech?.shutdown()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 朗读监听
|
|
|
|
|
- */
|
|
|
|
|
- private inner class TTSUtteranceListener : UtteranceProgressListener() {
|
|
|
|
|
-
|
|
|
|
|
- override fun onStart(s: String) {
|
|
|
|
|
- callBack?.upTtsMenu(true)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- override fun onDone(s: String) {
|
|
|
|
|
- callBack?.upTtsMenu(false)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Deprecated("Deprecated in Java")
|
|
|
|
|
- override fun onError(s: String) {
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ tts?.clearTts()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
interface CallBack {
|
|
interface CallBack {
|