Skip to content

Commit

Permalink
fix(android): ConcurrentModificationException.
Browse files Browse the repository at this point in the history
  • Loading branch information
sososdk committed Sep 18, 2023
1 parent a5368b4 commit 12748e1
Showing 1 changed file with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,13 @@ class FlutterTtsPlugin : MethodCallHandler, FlutterPlugin {
}

// Handle pending method calls (sent while TTS was initializing)
isTtsInitialized = true
for (call in pendingMethodCalls) {
call.run()
synchronized(this@FlutterTtsPlugin) {
isTtsInitialized = true
for (call in pendingMethodCalls) {
call.run()
}
pendingMethodCalls.clear()
}
pendingMethodCalls.clear()
invokeMethod("tts.init", isTtsInitialized)
} else {
Log.e(tag, "Failed to initialize TextToSpeech with status: $status")
Expand All @@ -227,9 +229,12 @@ class FlutterTtsPlugin : MethodCallHandler, FlutterPlugin {
}

// Handle pending method calls (sent while TTS was initializing)
isTtsInitialized = true
for (call in pendingMethodCalls) {
call.run()
synchronized(this@FlutterTtsPlugin) {
isTtsInitialized = true
for (call in pendingMethodCalls) {
call.run()
}
pendingMethodCalls.clear()
}
} else {
Log.e(tag, "Failed to initialize TextToSpeech with status: $status")
Expand All @@ -238,11 +243,13 @@ class FlutterTtsPlugin : MethodCallHandler, FlutterPlugin {

override fun onMethodCall(call: MethodCall, result: Result) {
// If TTS is still loading
if (!isTtsInitialized) {
// Suspend method call until the TTS engine is ready
val suspendedCall = Runnable { onMethodCall(call, result) }
pendingMethodCalls.add(suspendedCall)
return
synchronized(this@FlutterTtsPlugin) {
if (!isTtsInitialized) {
// Suspend method call until the TTS engine is ready
val suspendedCall = Runnable { onMethodCall(call, result) }
pendingMethodCalls.add(suspendedCall)
return
}
}
when (call.method) {
"speak" -> {
Expand Down Expand Up @@ -270,8 +277,10 @@ class FlutterTtsPlugin : MethodCallHandler, FlutterPlugin {
}
val b = speak(text)
if (!b) {
val suspendedCall = Runnable { onMethodCall(call, result) }
pendingMethodCalls.add(suspendedCall)
synchronized(this@FlutterTtsPlugin) {
val suspendedCall = Runnable { onMethodCall(call, result) }
pendingMethodCalls.add(suspendedCall)
}
return
}
// Only use await speak completion if queueMode is set to QUEUE_FLUSH
Expand Down

0 comments on commit 12748e1

Please sign in to comment.