2017-08-23 58 views
2

是否有可能爲TTS(如女嬰聲音)設置自定義語音?TextToSpeech的自定義語音Android

我已經試過getVoices()像下面,

if (Build.VERSION.SDK_INT >= 21) { 
    Set<Locale> localeSet = tts.getAvailableLanguages(); 
    for (Locale locale : localeSet) { 
     Log.v(TAG, locale.getDisplayName() + " - " + locale.getDisplayLanguage() + " - " + locale.getCountry()); 
     if (locale.getDisplayLanguage().equals("Tamil")) { 
      result = tts.setLanguage(locale); 
      if (result == TextToSpeech.LANG_MISSING_DATA 
        || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.e("TTS", "This Language is not supported"); 
      } else { 
       tts.setSpeechRate(0.05f); 
       //tts.setPitch(5.0f); 
       fabSpeak.setEnabled(true); 
       speakOut(); 
      } 
     } 
    } 

    Set<Voice> voices = tts.getVoices(); 
    for (Voice voice : voices) { 
     Log.v(TAG, voice.getName()); 
     if (voice.getName().equals("hi-in-x-cfn#female_2-local")) { 
      tts.setVoice(voice); 
     } 
    } 
} 

但這種聲音代替語言,所以這個代碼不讀泰米爾語了。如果我對setVoice()發表評論,那麼它會以默認的男聲朗讀泰米爾語。

我想讓這個給定的文本被這個女性的聲音讀取。可能嗎?

回答

1

這種情況的發生是因爲默認情況下,每個語音都有一個區域值,該值覆蓋您在函數上設置的區域值。我認爲你可以通過創建一個新的語音實例來覆蓋它,通過覆蓋for循環中的語言環境屬性來創建一個與你想要的設置相同的語音實例,如下所示:

for (Voice voice: voices) { 
Log.v(TAG, voice.getName()); 
if (voice.getName().equals("hi-in-x-cfn#female_2-local")) { 
    tts.setVoice(new Voice(voice.getName(), 
    locale, // YOUR LOCALE GOES HERE 
    voice.getQuality(), 
    voice.getLatency(), 
    voice.isNetworkConnectionRequired(), 
    voice.getFeatures())); 
} 
}