Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/functions/babel/trav…
Browse files Browse the repository at this point in the history
…erse-7.23.2
  • Loading branch information
kevmo314 authored Oct 20, 2023
2 parents b3045a2 + 4f299d8 commit 15f58cb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
38 changes: 38 additions & 0 deletions android/app/src/main/kotlin/com/rtirl/chat/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package com.rtirl.chat

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.speech.tts.TextToSpeech
import android.os.Build
import android.provider.Settings
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result


class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
val ttsPlugin = TextToSpeechPlugin(this)
val ttsChannel = MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"tts_plugin"
)
ttsChannel.setMethodCallHandler(ttsPlugin)
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"com.rtirl.chat/audio"
Expand Down Expand Up @@ -64,3 +75,30 @@ class MainActivity : FlutterActivity() {
super.configureFlutterEngine(flutterEngine)
}
}


class TextToSpeechPlugin(context: Context) : MethodCallHandler {
private val context: Context = context
private val tts: TextToSpeech = TextToSpeech(context) {}

override fun onMethodCall(call: MethodCall, result: Result) {
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()
}
}

fun speak(text: String) {
if (!text.isNullOrBlank()) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
}
}
}
4 changes: 4 additions & 0 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:rtchat/models/channels.dart';
import 'package:rtchat/models/layout.dart';
import 'package:rtchat/models/tts.dart';
import 'package:rtchat/models/user.dart';
// import 'package:rtchat/tts_plugin.dart';
import 'package:wakelock/wakelock.dart';
import 'dart:math' as math;

Expand Down Expand Up @@ -237,6 +238,9 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
: Icons.voice_over_off),
tooltip: AppLocalizations.of(context)!.textToSpeech,
onPressed: () {
// TODO: need to figure out how to actually
// send the text to this plugin, but manual is working
// TextToSpeechPlugin.speak("Testing this code!");
ttsModel.enabled = !ttsModel.enabled;
});
}),
Expand Down
14 changes: 14 additions & 0 deletions lib/tts_plugin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:flutter/services.dart';

class TextToSpeechPlugin {
static const MethodChannel _channel = MethodChannel('tts_plugin');

static Future<void> speak(String text) async {
try {
await _channel.invokeMethod('speak', {'text': text});
} catch (e) {
// TODO Handle the error?
// print('Error in TTSPlugin: $e');
}
}
}

0 comments on commit 15f58cb

Please sign in to comment.