|
|
@ -35,7 +35,9 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), |
|
|
|
val contentLiveData = MutableLiveData<String>() |
|
|
|
val contentLiveData = MutableLiveData<String>() |
|
|
|
val urlLiveData = MutableLiveData<AnalyzeUrl>() |
|
|
|
val urlLiveData = MutableLiveData<AnalyzeUrl>() |
|
|
|
var star = false |
|
|
|
var star = false |
|
|
|
var textToSpeech: TextToSpeech = TextToSpeech(context, this) |
|
|
|
var textToSpeech: TextToSpeech? = null |
|
|
|
|
|
|
|
private var ttsInitFinish = false |
|
|
|
|
|
|
|
private var ttsText = "" |
|
|
|
|
|
|
|
|
|
|
|
fun initData(intent: Intent) { |
|
|
|
fun initData(intent: Intent) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
@ -143,28 +145,42 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
override fun onInit(status: Int) { |
|
|
|
override fun onInit(status: Int) { |
|
|
|
launch { |
|
|
|
if (status == TextToSpeech.SUCCESS) { |
|
|
|
if (status == TextToSpeech.SUCCESS) { |
|
|
|
textToSpeech?.language = Locale.CHINA |
|
|
|
textToSpeech.language = Locale.CHINA |
|
|
|
textToSpeech?.setOnUtteranceProgressListener(TTSUtteranceListener()) |
|
|
|
textToSpeech.setOnUtteranceProgressListener(TTSUtteranceListener()) |
|
|
|
ttsInitFinish = true |
|
|
|
} else { |
|
|
|
play() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
launch { |
|
|
|
toast(R.string.tts_init_failed) |
|
|
|
toast(R.string.tts_init_failed) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
private fun play() { |
|
|
|
|
|
|
|
if (!ttsInitFinish) return |
|
|
|
|
|
|
|
textToSpeech?.stop() |
|
|
|
|
|
|
|
ttsText.split("\n", " ", " ").forEach { |
|
|
|
|
|
|
|
textToSpeech?.speak(it, TextToSpeech.QUEUE_ADD, null, "rss") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun readAloud(text: String) { |
|
|
|
fun readAloud(text: String) { |
|
|
|
textToSpeech.stop() |
|
|
|
ttsText = text |
|
|
|
text.split("\n", " ", " ").forEach { |
|
|
|
textToSpeech?.let { |
|
|
|
textToSpeech.speak(it, TextToSpeech.QUEUE_ADD, null, "rss") |
|
|
|
play() |
|
|
|
|
|
|
|
} ?: let { |
|
|
|
|
|
|
|
textToSpeech = TextToSpeech(context, this) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onCleared() { |
|
|
|
override fun onCleared() { |
|
|
|
super.onCleared() |
|
|
|
super.onCleared() |
|
|
|
textToSpeech.stop() |
|
|
|
textToSpeech?.stop() |
|
|
|
textToSpeech.shutdown() |
|
|
|
textToSpeech?.shutdown() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|