I have the following custom action to trigger text to speech:
// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:flutter_tts/flutter_tts.dart';
Future talkToMe(String text) async {
// Create a tts object
FlutterTts flutterTts = FlutterTts();
// Set properties
flutterTts.setLanguage("en-US");
flutterTts.setVoice({"name": "Karen", "locale": "en-AU"});
flutterTts.setSpeechRate(1.0);
flutterTts.setPitch(1); // 0.5 - 1.5
// Speak
flutterTts.speak(text);
}
// Set your action name, define your arguments and return parameter,
// and then add the boilerplate code using the green button on the right!
The line of code that actually triggers the app to speak is toward the bottom:
// Speak
flutterTts.speak(text);
How do I get the action to pause speaking, and then automatically continue after some amount of time? For example, I want the app to read what's in the variable "text" twice, but with a 3 second pause in the middle.
I tried this, which more or less works:
flutterTts.speak(text);
await new Future.delayed(const Duration(seconds: 3));
flutterTts.speak(text);
...but if I instead want the delay to be 0 seconds, I could do this:
flutterTts.speak(text);
await new Future.delayed(const Duration(seconds: 0));
flutterTts.speak(text);
...or remove the second line of code entirely:
flutterTts.speak(text);
flutterTts.speak(text);
When I do either, there is too much of a delay in the middle. If I define that I want no delay (0 seconds), I want the text to speech to read it all as one sentence. I'm able to achieve that effect if I write the code as:
flutterTts.speak(text + text);
...but then I don't know how to add a pause in the middle.
Any ideas?