From 589cf9e2bb52e3d3be84c01b37eae171eeac9c8e Mon Sep 17 00:00:00 2001 From: Maxime Ferrino Date: Mon, 25 Nov 2019 11:10:40 +0400 Subject: [PATCH] Added a stop() method, and updated README. --- README.md | 5 +++++ src/texttospeech.android.ts | 7 +++++++ src/texttospeech.ios.ts | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index f94ba50..53fd801 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ TTS.speak(speakOptions).then( - `speak(options: SpeakOptions): Promise` - start speaking with the given options - `pause(): void` - pause the speech +- `stop(): void` - stop the speech - `resume(): void` - resume the speech - `destroy(): void` - release resources for the speech synthesizer/engine @@ -104,6 +105,10 @@ let speakOptions: SpeakOptions = { - `getAvailableLanguages(): Promise>;` - returns an array of available languages (use to prevent using non-existing language/local codes) +### Android and iOS specific features + +The `pause()` is only supported on iOS. Android stops the speech and will resume from the start. Also both `pause()` and `stop()` takes one argument on iOS, indicating whether we should stop right now (`true`) or after the word being said (`false`). + ## Credits Inspired by James Montemagno's [TextToSpeech Xamarin plugin](https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/TextToSpeech) diff --git a/src/texttospeech.android.ts b/src/texttospeech.android.ts index 094ab14..1bfc36d 100644 --- a/src/texttospeech.android.ts +++ b/src/texttospeech.android.ts @@ -94,6 +94,7 @@ export class TNSTextToSpeech { /** * Interrupts the current utterance and discards other utterances in the queue. * https://developer.android.com/reference/android/speech/tts/TextToSpeech.html#stop() + * Same as stop() */ public pause() { if (this._tts && this._initialized) { @@ -101,6 +102,12 @@ export class TNSTextToSpeech { } } + public stop() { + if (this._tts && this._initialized) { + this._tts.stop(); + } + } + public resume() { // In Android there's no pause so we resume playng the last phrase... if (this._lastOptions) { diff --git a/src/texttospeech.ios.ts b/src/texttospeech.ios.ts index aeb5286..3dc5195 100644 --- a/src/texttospeech.ios.ts +++ b/src/texttospeech.ios.ts @@ -123,6 +123,12 @@ export class TNSTextToSpeech { ); } + public stop(now) { + this._speechSynthesizer.stopSpeakingAtBoundary( + now ? AVSpeechBoundary.Immediate : AVSpeechBoundary.Word + ); + } + public resume() { this._speechSynthesizer.continueSpeaking(); }