简体   繁体   中英

Flutter TTS (Text to Speech) - Is it possible & how to get language code for iOS voices?

I am working on a Flutter app that uses the TTS library.

The available voice objects returned from flutterTts.getVoices() is device/OS specific.

In the app, for a given TTS language, I would like to give users the option to select from a set of available voices for that language.

Functional Objective: Solved for Android

When running Flutter TTS on Android, the list of voices returned are strings that indicate which language-code that a voice corresponds to. For example:

  • for language-code en-US
  • there is: en-US-language , en-us-x-sfg#male_1-local

This built-in meta data allows to create a Map<String, List<String>> with the following type of key/value pairs:

  • key : en-us
  • value : ['en-US-language', 'en-us-x-sfg#male_1-local']

Then having this map, when user selects say English from the list of available languages, there can be a drop down list like so:

Map<String, String> labelToVoiceMap = {
   'Voice1' : 'en-US-language',
   'Voice2' : 'en-us-x-sfg#male_1-local'
}

Question

When running Flutter TTS on iOS, the list of voices returned is just human names.

  • For example: Aaron , Fred

Is there a way in Flutter to determine what the associated language is for a particular voice returned from the flutterTts object?

Here is a post - unrelated to Flutter - describing the voice objects on an iOS device, that shows there is a field/property for language/locale code: How to get a list of ALL voices on iOS 9?

eg: [AVSpeechSynthesisVoice 0x28266fb40] Language: en-US, Name: Fred, Quality: Default [com.apple.speech.synthesis.voice.Fred]

Option 1: .. any way to get at that in Flutter?

~~ ~~~~~ ~~~~~ ~~~~~ ~~~~~ ~~~

Option 2: Alternatively - for a given name on iOS (eg, 'Fred' ), if that voice is available on the current device, is there some official listing that guarantees what the associated language code will be for that voice name?

To do exactly what you're trying to do, what really needs to happen is Flutter TTS needs to add a new method: getVoicesForLocale(String locale).

Or you or someone would need to write a separate Flutter package that does this one task.

But this would be extremely complicated to do, or at least to do well. For example, you have presumably only handled one Android TTS engine so far.

You're probably not going to like this answer, but your best bet for predictable cross platform TTS is to use a single cloud service, probably Google cloud tts.

To answer your last question, I'm pretty darn sure each IOS voice has a unique name and will always have the same associated language... so assuming you really do have the Android side handled, you could hard code the relationships in your app, but you'd have to handle the IOS side differently in that you can't just reflect all installed voices to the user like on Android because what if Apple adds new voices? You would have to filter them to known voices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM