Skip to content

Commit

Permalink
feat: add a speak method to the android native channel (fixes #1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessielw committed Oct 19, 2023
1 parent e4e40fc commit 719619a
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions android/app/src/main/kotlin/com/rtirl/chat/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,21 @@ class MainActivity : FlutterActivity() {
flutterEngine.dartExecutor.binaryMessenger,
"tts_plugin"
)
ttsChannel.setMethodCallHandler(ttsPlugin)
// ttsChannel.setMethodCallHandler(ttsPlugin)
ttsChannel.setMethodCallHandler { call, result ->
when (call.method) {
"speak" -> {
val text = call.argument<String>("text")
if (!text.isNullOrBlank()) {
ttsPlugin.speak(text)
result.success(true)
} else {
result.error("INVALID_ARGUMENT", "Text is empty or null", null)
}
}
else -> result.notImplemented()
}
}
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"com.rtirl.chat/audio"
Expand Down Expand Up @@ -82,16 +96,23 @@ class TextToSpeechPlugin(context: Context) : MethodCallHandler {
private val tts: TextToSpeech = TextToSpeech(context) {}

override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "speak") {
val text = call.argument<String>("text")
if (!text.isNullOrBlank()) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
result.success(true)
} else {
result.error("INVALID_ARGUMENT", "Text is empty or null", null)
when (call.method) {
"speak" -> {
val text = call.argument<String>("text")
if (!text.isNullOrBlank()) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
result.success(true)
} else {
result.error("INVALID_ARGUMENT", "Text is empty or null", null)
}
}
} else {
result.notImplemented()
else -> result.notImplemented()
}
}

fun speak(text: String) {
if (!text.isNullOrBlank()) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
}
}
}
}

0 comments on commit 719619a

Please sign in to comment.