Solves NPE when the TextToSpeech engine isn't ready.
This commit is contained in:
@@ -49,10 +49,11 @@ class TextToSpeechEngine private constructor() {
|
|||||||
fun initTTS(context: Context, message: String) {
|
fun initTTS(context: Context, message: String) {
|
||||||
tts = TextToSpeech(context) {
|
tts = TextToSpeech(context) {
|
||||||
if (it == TextToSpeech.SUCCESS) {
|
if (it == TextToSpeech.SUCCESS) {
|
||||||
tts!!.language = defLanguage
|
tts?.let {
|
||||||
tts!!.setPitch(defaultPitch)
|
it.language = defLanguage
|
||||||
tts!!.setSpeechRate(defaultSpeed)
|
it.setPitch(defaultPitch)
|
||||||
tts!!.setListener(
|
it.setSpeechRate(defaultSpeed)
|
||||||
|
it.setListener(
|
||||||
onStart = {
|
onStart = {
|
||||||
onStartListener?.invoke()
|
onStartListener?.invoke()
|
||||||
},
|
},
|
||||||
@@ -71,6 +72,7 @@ class TextToSpeechEngine private constructor() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
speak(message)
|
speak(message)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
onErrorListener?.invoke(getErrorText(it))
|
onErrorListener?.invoke(getErrorText(it))
|
||||||
}
|
}
|
||||||
@@ -78,7 +80,7 @@ class TextToSpeechEngine private constructor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun speak(message: String): TextToSpeechEngine {
|
private fun speak(message: String): TextToSpeechEngine {
|
||||||
tts!!.speak(
|
tts?.speak(
|
||||||
message,
|
message,
|
||||||
TextToSpeech.QUEUE_FLUSH,
|
TextToSpeech.QUEUE_FLUSH,
|
||||||
null,
|
null,
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class TextToSpeechHelper private constructor(private val context: WeakReference<
|
|||||||
|
|
||||||
private var message: String? = null
|
private var message: String? = null
|
||||||
|
|
||||||
private var tts: TextToSpeechEngine? = null
|
private var ttsEngine: TextToSpeechEngine? = null
|
||||||
|
|
||||||
private var onStart: (() -> Unit)? = null
|
private var onStart: (() -> Unit)? = null
|
||||||
|
|
||||||
@@ -44,19 +44,19 @@ class TextToSpeechHelper private constructor(private val context: WeakReference<
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initTTS() = context.get()?.run {
|
private fun initTTS() = context.get()?.run {
|
||||||
tts = TextToSpeechEngine.getInstance()
|
ttsEngine = TextToSpeechEngine.getInstance()
|
||||||
.setOnCompletionListener { onDoneListener?.invoke() }
|
.setOnCompletionListener { onDoneListener?.invoke() }
|
||||||
.setOnErrorListener { onErrorListener?.invoke(it) }
|
.setOnErrorListener { onErrorListener?.invoke(it) }
|
||||||
.setOnStartListener { onStart?.invoke() }
|
.setOnStartListener { onStart?.invoke() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun speak(message: String): TextToSpeechHelper {
|
fun speak(message: String): TextToSpeechHelper {
|
||||||
if (tts == null) {
|
if (ttsEngine == null) {
|
||||||
initTTS()
|
initTTS()
|
||||||
}
|
}
|
||||||
this.message = message
|
this.message = message
|
||||||
|
|
||||||
tts?.initTTS(
|
ttsEngine?.initTTS(
|
||||||
appContext,
|
appContext,
|
||||||
message
|
message
|
||||||
)
|
)
|
||||||
@@ -70,8 +70,8 @@ class TextToSpeechHelper private constructor(private val context: WeakReference<
|
|||||||
*/
|
*/
|
||||||
fun highlight(): TextToSpeechHelper {
|
fun highlight(): TextToSpeechHelper {
|
||||||
if (message == null) throw Exception("Message can't be null for highlighting !! Call speak() first")
|
if (message == null) throw Exception("Message can't be null for highlighting !! Call speak() first")
|
||||||
tts?.setHighlightedMessage(message!!)
|
ttsEngine?.setHighlightedMessage(message!!)
|
||||||
tts?.setOnHighlightListener { i, i2 ->
|
ttsEngine?.setOnHighlightListener { i, i2 ->
|
||||||
onHighlightListener?.invoke(Pair(i, i2))
|
onHighlightListener?.invoke(Pair(i, i2))
|
||||||
}
|
}
|
||||||
return this
|
return this
|
||||||
@@ -86,8 +86,8 @@ class TextToSpeechHelper private constructor(private val context: WeakReference<
|
|||||||
fun destroy(
|
fun destroy(
|
||||||
action: (() -> Unit) = {}
|
action: (() -> Unit) = {}
|
||||||
) {
|
) {
|
||||||
tts?.destroy()
|
ttsEngine?.destroy()
|
||||||
tts = null
|
ttsEngine = null
|
||||||
action.invoke()
|
action.invoke()
|
||||||
INSTANCE = null
|
INSTANCE = null
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ class TextToSpeechHelper private constructor(private val context: WeakReference<
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun setLanguage(locale: Locale): TextToSpeechHelper {
|
fun setLanguage(locale: Locale): TextToSpeechHelper {
|
||||||
tts?.setLanguage(locale)
|
ttsEngine?.setLanguage(locale)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,12 +126,12 @@ class TextToSpeechHelper private constructor(private val context: WeakReference<
|
|||||||
pitch: Float = DEF_SPEECH_AND_PITCH,
|
pitch: Float = DEF_SPEECH_AND_PITCH,
|
||||||
speed: Float = DEF_SPEECH_AND_PITCH
|
speed: Float = DEF_SPEECH_AND_PITCH
|
||||||
): TextToSpeechHelper {
|
): TextToSpeechHelper {
|
||||||
tts?.setPitchAndSpeed(pitch, speed)
|
ttsEngine?.setPitchAndSpeed(pitch, speed)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
fun resetPitchAndSpeed(): TextToSpeechHelper {
|
fun resetPitchAndSpeed(): TextToSpeechHelper {
|
||||||
tts?.resetPitchAndSpeed()
|
ttsEngine?.resetPitchAndSpeed()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user