From d6681a64bbdc296530fdd14cf02ba9f286efa405 Mon Sep 17 00:00:00 2001 From: Dhruv-1105 <126226693+Dhruv-1105@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:11:47 +0530 Subject: [PATCH] feat: add `onRangeStart` listener (#132) * "Added RangeCount() method to get index & string of spoken word." * "Resolved Prettier issues" --- .../community/tts/SpeakResultCallback.java | 1 + .../com/getcapacitor/community/tts/TextToSpeech.java | 6 ++++++ .../community/tts/TextToSpeechPlugin.java | 10 ++++++++++ src/definitions.ts | 11 +++++++++++ 4 files changed, 28 insertions(+) diff --git a/android/src/main/java/com/getcapacitor/community/tts/SpeakResultCallback.java b/android/src/main/java/com/getcapacitor/community/tts/SpeakResultCallback.java index 1299e12..d12fa7c 100644 --- a/android/src/main/java/com/getcapacitor/community/tts/SpeakResultCallback.java +++ b/android/src/main/java/com/getcapacitor/community/tts/SpeakResultCallback.java @@ -3,4 +3,5 @@ public interface SpeakResultCallback { void onDone(); void onError(); + void onRangeStart(int start, int end, String spokenWord); } diff --git a/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java b/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java index 77436ed..ee48c64 100644 --- a/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java +++ b/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java @@ -65,6 +65,12 @@ public void onDone(String utteranceId) { public void onError(String utteranceId) { resultCallback.onError(); } + + @Override + public void onRangeStart(String utteranceId, int start, int end, int frame) { + String spokenWord = text.substring(start, end); + resultCallback.onRangeStart(start, end, spokenWord); + } } ); diff --git a/android/src/main/java/com/getcapacitor/community/tts/TextToSpeechPlugin.java b/android/src/main/java/com/getcapacitor/community/tts/TextToSpeechPlugin.java index 20cc560..f41c8c5 100644 --- a/android/src/main/java/com/getcapacitor/community/tts/TextToSpeechPlugin.java +++ b/android/src/main/java/com/getcapacitor/community/tts/TextToSpeechPlugin.java @@ -1,5 +1,6 @@ package com.getcapacitor.community.tts; +import android.util.Base64; import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; @@ -53,6 +54,15 @@ public void onDone() { public void onError() { call.reject(ERROR_UTTERANCE); } + + @Override + public void onRangeStart(int start, int end, String spokenWord) { + JSObject ret = new JSObject(); + ret.put("start", start); + ret.put("end", end); + ret.put("spokenWord", spokenWord); + notifyListeners("onRangeStart", ret); + } }; try { diff --git a/src/definitions.ts b/src/definitions.ts index 80fd382..7e56a4e 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -1,3 +1,5 @@ +import type { PluginListenerHandle } from '@capacitor/core'; + export interface TextToSpeechPlugin { /** * Starts the TTS engine and plays the desired text. @@ -27,6 +29,15 @@ export interface TextToSpeechPlugin { * Only available for Android. */ openInstall(): Promise; + + addListener( + eventName: 'onRangeStart', + listenerFunc: (info: { + start: number; + end: number; + spokenWord: string; + }) => void, + ): Promise; } export interface TTSOptions {