Problem With flutter_tts No Sound / Low Volume In iOS

2022-01-14
Flutter
Dart
development
iOS
flutter_tts

The problem

I'm using flutter_tts plugin for the text-to-speech capabilities in my travel vocabulary app, and it worked everywhere until recently - last time I tried building and deploying it, it worked everywhere except iOS on device - meaning Android emulator, Android device and iOS emulator were OK, but iOS device had no sound when trying to play the text. From searching around, some folks report low volume problem which might be relevant.

The solution

I couldn't figure out when it started and what's causing it, so it took really a lot of googling till I finally found a short thread that described a solution:

await flutterTts.setIosAudioCategory(IosTextToSpeechAudioCategory.playback, [ IosTextToSpeechAudioCategoryOptions.defaultToSpeaker ]);

It should be done after creating the FlutterTTS instance and before playing. The important parts are both IosTextToSpeechAudioCategory.playback and IosTextToSpeechAudioCategoryOptions.defaultToSpeaker - I couldn't make it work without either of those.

While I was at it, I've also added several other flags to make it more future-proof and integrated with other audio being played, as apparently the problem is in some defaults changing at some point, so the final solution looks like this:

let flutterTts = FlutterTts(); await flutterTts.setSharedInstance(true); await flutterTts.setIosAudioCategory(IosTextToSpeechAudioCategory.playback, [ IosTextToSpeechAudioCategoryOptions.allowBluetooth, IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP, IosTextToSpeechAudioCategoryOptions.mixWithOthers, IosTextToSpeechAudioCategoryOptions.defaultToSpeaker ], IosTextToSpeechAudioMode.defaultMode );