Skip to content
This repository was archived by the owner on May 6, 2022. It is now read-only.

Commit 339e6a2

Browse files
committed
fix(android): should use pause/resume in android; add more guards
1 parent e5ce4cc commit 339e6a2

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

android/src/main/java/com/reactnativespokestack/SpokestackModule.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class SpokestackModule(private val reactContext: ReactApplicationContext): React
6464
if (reactContext.hasActiveCatalystInstance()) {
6565
when(event.toLowerCase()) {
6666
"error" -> {
67+
Log.e(name, "Received error event with params: $params")
6768
// Reject all existing promises
6869
for ((key, promise) in promises) {
6970
promise.reject(Exception("Error in Spokestack during ${key.name}."))
@@ -98,22 +99,26 @@ class SpokestackModule(private val reactContext: ReactApplicationContext): React
9899
}
99100

100101
private fun textToSpeech(input: String, format: Int, voice: String) {
102+
if (!initialized()) {
103+
throw Exception("Spokestack must be initialized before synthesizing text")
104+
}
101105
if (format > 2 || format < 0) {
102-
throw Exception(("A format of $format is not supported. Please use an int from 0 to 2 (or use the TTSFormat enum). Refer to documentation for further details."))
106+
throw Exception("A format of $format is not supported. Please use an int from 0 to 2 (or use the TTSFormat enum). Refer to documentation for further details.")
103107
}
104108
val req = SynthesisRequest.Builder(input)
105109
.withMode(SynthesisRequest.Mode.values()[format])
106110
.withVoice(voice)
107111
.build()
108-
spokestack?.synthesize(req)
112+
spokestack!!.synthesize(req)
109113
}
110114

111115
private fun initialized(): Boolean {
112116
return spokestack != null
113117
}
114118

115119
private fun started(): Boolean {
116-
return initialized() && spokestack?.speechPipeline?.isRunning ?: false
120+
val pipeline = spokestack?.speechPipeline
121+
return initialized() && pipeline?.isRunning!! && !pipeline.isPaused
117122
}
118123

119124
private fun activated(): Boolean {
@@ -257,6 +262,7 @@ class SpokestackModule(private val reactContext: ReactApplicationContext): React
257262
}
258263
try {
259264
spokestack?.start()
265+
spokestack?.resume()
260266
promise.resolve(null)
261267

262268
// Send a start event for parity with iOS
@@ -271,7 +277,9 @@ class SpokestackModule(private val reactContext: ReactApplicationContext): React
271277
@ReactMethod
272278
fun stop(promise: Promise) {
273279
try {
274-
spokestack?.stop()
280+
// Calling stop here is more destructive than we want
281+
// and removes all references to TTS. Use pause instead.
282+
spokestack?.pause()
275283
promise.resolve(null)
276284

277285
// Send a stop event for parity with iOS
@@ -345,8 +353,12 @@ class SpokestackModule(private val reactContext: ReactApplicationContext): React
345353

346354
@ReactMethod
347355
fun classify(utterance:String, promise: Promise) {
356+
if (!initialized()) {
357+
promise.reject(Exception("Call Spokestack.initialize() before calling Spokestack.classify()."))
358+
return
359+
}
348360
promises[SpokestackPromise.CLASSIFY] = promise
349-
spokestack?.classify(utterance)
361+
spokestack!!.classify(utterance)
350362
}
351363

352364
@ReactMethod

0 commit comments

Comments
 (0)