@@ -37,7 +37,6 @@ import java.io.IOException
3737import java.util.Locale
3838import kotlin.math.min
3939
40-
4140object UtilityTts {
4241
4342 private var ttsInit = false
@@ -66,18 +65,18 @@ object UtilityTts {
6665 if (status != TextToSpeech .ERROR ) {
6766 ttobjGlobal?.language = Locale .US
6867 }
68+ ttsInit = true
69+ ttobjGlobal?.setSpeechRate(Utility .readPrefInt(context, " TTS_SPEED_PREF" , 10 ) / 10.0f )
6970 }
70- ttsInit = true
71- ttobjGlobal!! .setSpeechRate(Utility .readPrefInt(context, " TTS_SPEED_PREF" , 10 ) / 10.0f )
7271 } catch (e: Exception ) {
7372 UtilityLog .handleException(e)
7473 }
7574 }
7675
7776 fun shutdownTts () {
7877 ttobjGlobal.let {
79- ttobjGlobal!! .stop()
80- ttobjGlobal!! .shutdown()
78+ ttobjGlobal? .stop()
79+ ttobjGlobal? .shutdown()
8180 }
8281 }
8382
@@ -88,7 +87,7 @@ object UtilityTts {
8887
8988 private fun initMediaPlayer (context : Context ) {
9089 mediaPlayer = MediaPlayer ()
91- mediaPlayer!! .setOnCompletionListener {
90+ mediaPlayer? .setOnCompletionListener {
9291 if (currentFile < fileCount) {
9392 playMediaPlayerFile(context, currentFile)
9493 currentFile + = 1
@@ -116,12 +115,12 @@ object UtilityTts {
116115 initTts(context)
117116 }
118117 // clear the queue of any pending objects
119- ttobjGlobal!! .stop()
118+ ttobjGlobal? .stop()
120119 if (! mpInit) {
121120 initMediaPlayer(context)
122121 }
123122 synthesizeText(context, Utility .readPref(context, " PLAYLIST_" + playlistArr[index], " " ), prodg)
124- ttobjGlobal!! .setOnUtteranceProgressListener(object : UtteranceProgressListener () {
123+ ttobjGlobal? .setOnUtteranceProgressListener(object : UtteranceProgressListener () {
125124 override fun onDone (utteranceId : String ) {
126125 if (currentFile == 0 && utteranceId.contains(prodg)) {
127126 playMediaPlayerFile(context, 0 )
@@ -149,12 +148,12 @@ object UtilityTts {
149148 initTts(context)
150149 }
151150 // clear the queue of any pending objects
152- ttobjGlobal!! .stop()
151+ ttobjGlobal? .stop()
153152 if (! mpInit) {
154153 initMediaPlayer(context)
155154 }
156155 synthesizeText(context, txt, prod)
157- ttobjGlobal!! .setOnUtteranceProgressListener(object : UtteranceProgressListener () {
156+ ttobjGlobal? .setOnUtteranceProgressListener(object : UtteranceProgressListener () {
158157 override fun onDone (utteranceId : String ) {
159158 if (currentFile == 0 && utteranceId.contains(prod)) {
160159 playMediaPlayerFile(context, 0 )
@@ -176,54 +175,31 @@ object UtilityTts {
176175
177176 private fun synthesizeText (context : Context , txtF : String , prod : String ) {
178177 if (mediaPlayer != null && mediaPlayer!! .isPlaying) {
179- mediaPlayer!! .stop()
178+ mediaPlayer? .stop()
180179 }
181180 val txt = UtilityTtsTranslations .translateAbbreviation(txtF)
182- // val myHashRender = HashMap<String, String>()
183- // val musicDir = context.getExternalFilesDir(Environment.DIRECTORY_MUSIC)
184- // val wxDir = File(musicDir, GlobalVariables.PACKAGE_NAME)
185- // if (!wxDir.exists() && !wxDir.mkdirs()) {
186- // return
187- // }
188181 val chunks = splitInChunks(Utility .fromHtml(txt))
189182 fileCount = chunks.size
190183 (0 until fileCount).forEach {
191- // myHashRender.clear()
192- // myHashRender[TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID] = it.toString() + prod
193- // val fileName = File(wxDir, FILENAME + it.toString()).absolutePath
194- // val fileName = File(context.filesDir, FILENAME + it.toString()).absolutePath
195184 val fileName = File (context.filesDir, FILENAME + it.toString())
196-
197- // ttobjGlobal!!.synthesizeToFile(chunks[it], myHashRender, fileName)
198185 val bundle = Bundle ()
199186 bundle.putString(TextToSpeech .Engine .KEY_PARAM_UTTERANCE_ID , it.toString() + prod)
200- ttobjGlobal!! .synthesizeToFile(chunks[it], bundle, fileName, it.toString() + prod)
187+ ttobjGlobal? .synthesizeToFile(chunks[it], bundle, fileName, it.toString() + prod)
201188 }
202189 }
203190
204- internal fun playMediaPlayer (status : Int ) {
205- if (status == 0 ) {
206- mediaPlayer!! .start()
207- }
208- if (status == 1 ) {
209- ttsIsPaused = if (! ttsIsPaused) {
210- mediaPlayer!! .pause()
211- true
212- } else {
213- mediaPlayer!! .start()
214- false
215- }
191+ internal fun playMediaPlayer () {
192+ ttsIsPaused = if (! ttsIsPaused) {
193+ mediaPlayer?.pause()
194+ true
195+ } else {
196+ mediaPlayer?.start()
197+ false
216198 }
217199 }
218200
219201 private fun playMediaPlayerFile (context : Context , fileNum : Int ) {
220- // val musicDir = context.getExternalFilesDir(Environment.DIRECTORY_MUSIC)
221- // val wxDir = File(musicDir, GlobalVariables.PACKAGE_NAME)
222- // if (!wxDir.exists() && !wxDir.mkdirs()) {
223- // return
224- // }
225202 mediaPlayer?.reset()
226- // val fileName = File(wxDir, FILENAME + fileNum.toString()).absolutePath
227203 val fileName = File (context.filesDir, FILENAME + fileNum.toString()).absolutePath
228204 val uri = Uri .parse(" file://$fileName " )
229205 try {
0 commit comments