Get available locales for text to speech (TTS)

前端 未结 4 1841
臣服心动
臣服心动 2020-12-04 17:02

I\'m working on a text-to-speech implementation of a flashcard program. Text in different languages should be read out. In order to do this properly the user has to select t

4条回答
  •  一个人的身影
    2020-12-04 17:36

    Someone else has done the hard work, at http://kaviddiss.com/2012/08/12/android-text-to-speech-languages/

    To save you time, here's their code extract

    TextToSpeech tts = ...
    // let's assume tts is already inited at this point:
    Locale[] locales = Locale.getAvailableLocales();
    List localeList = new ArrayList();
    for (Locale locale : locales) {
        int res = tts.isLanguageAvailable(locale);
        if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
            localeList.add(locale);
        }
    }
    // at this point the localeList object will contain
    // all available languages for Text to Speech
    

    The results depend on which TTS engine has been selected. For instance, one of my phones includes both the Pico-TTS and Google-text-to-speech engines.

    Q-Smart (Vietnamese Phone with Google TTS as selected engine)

    D/SpeakRepeatedly( 3979): Engine Google Text-to-speech Engine:com.google.android.tts
    D/SpeakRepeatedly( 3979): Engine Pico TTS:com.svox.pico
    D/SpeakRepeatedly( 3979): German (Germany):German:de_DE
    D/SpeakRepeatedly( 3979): English (United Kingdom):English:en_GB
    D/SpeakRepeatedly( 3979): English (United States):English:en_US
    D/SpeakRepeatedly( 3979): English (United States,Computer):English:en_US_POSIX
    D/SpeakRepeatedly( 3979): Spanish (Spain):Spanish:es_ES
    D/SpeakRepeatedly( 3979): French (France):French:fr_FR
    D/SpeakRepeatedly( 3979): Italian (Italy):Italian:it_IT
    D/SpeakRepeatedly( 3979): Portuguese (Brazil):Portuguese:pt_BR
    D/SpeakRepeatedly( 3979): Portuguese (Portugal):Portuguese:pt_PT
    

    And with Pico selected

    D/SpeakRepeatedly( 4837): Engine Google Text-to-speech Engine:com.google.android.tts
    D/SpeakRepeatedly( 4837): Engine Pico TTS:com.svox.pico
    D/SpeakRepeatedly( 4837): German (Germany):German:de_DE
    D/SpeakRepeatedly( 4837): English (United Kingdom):English:en_GB
    D/SpeakRepeatedly( 4837): English (United States):English:en_US
    D/SpeakRepeatedly( 4837): English (United States,Computer):English:en_US_POSIX
    D/SpeakRepeatedly( 4837): Spanish (Spain):Spanish:es_ES
    D/SpeakRepeatedly( 4837): French (France):French:fr_FR
    D/SpeakRepeatedly( 4837): Italian (Italy):Italian:it_IT
    

    Note: Portuguese isn’t listed in the TTS Settings UI. When I select Portuguese programmatically in my app it speaks with a Portuguese accent! FWIW here's my code to select Portuguese (it accepts both Brazilian and Portuguese locales).

    if (locale.getDisplayName().startsWith("Portuguese")) {
        Log.i(SPEAK_REPEATEDLY, "Setting Locale to: " + locale.toString());
        tts.setLanguage(locale);
        }
    }
    

提交回复
热议问题