Add voice guidance
For voice guidance we can use the flutter_tts package. Other packages for TTS can also be used.
The voice instructions will come on the onTextToSpeechInstruction
callback set during navigation or simulation.
Add flutter_tts
as a dependency to the pubspec.yaml
file of the project and run the dart pub get
command to install the tts package.
We can use the onTextToSpeechInstruction
callback of the startSimulation
methods in the following way:
// instantiate FlutterTts
FlutterTts flutterTts = FlutterTts();
void simulationInstructionUpdated(NavigationInstruction instruction, Set<NavigationInstructionUpdateEvents> events) {
// handle instruction
}
void textToSpeechInstructionUpdated(String ttsInstruction) {
flutterTts.speak(ttsInstruction);
}
TaskHandler? taskHandler = NavigationService.startSimulation(
route,
null,
onNavigationInstruction: simulationInstructionUpdated,
onTextToSpeechInstruction: textToSpeechInstructionUpdated,
speedMultiplier: 2,
);
For navigation we can set the onTextToSpeechInstruction
callback in a similar way with startNavigation
.
Text to speech instructions language
The TTS instruction strings will come in the language set using the SdkSettings.setTTSLanguage
method. The snippet provided below sets the language to English. See the setting a map language for more info about the Language
class.
Language lang = languages.where((lang) => lang.languagecode == 'eng' && lang.regioncode == 'GBR').first;
SdkSettings.setTTSLanguage(lang);
See the documentation for flutter_tts for information on how to set the TTS voice, language and other options such as pitch, volume, speechRate, etc.