我正在关注https://developer.xamarin.com/guides/android/platform\u features/speech/并在langavailable中获取单个值(默认值)。
我也更新了google tts引擎并安装了一些语音数据。我已经安装了使用此引擎的其他应用程序,并且此应用程序具有所有语言。
我做错了什么?Android操作系统6.0
TextToSpeech textToSpeech = new TextToSpeech(this, this, "com.google.android.tts");
var langAvailable = new List<string> { "Default" };
var localesAvailable = Locale.GetAvailableLocales().ToList();
foreach (var locale in localesAvailable)
{
LanguageAvailableResult res = textToSpeech.IsLanguageAvailable(locale);
switch (res)
{
case LanguageAvailableResult.Available:
langAvailable.Add(locale.DisplayLanguage);
break;
case LanguageAvailableResult.CountryAvailable:
langAvailable.Add(locale.DisplayLanguage);
break;
case LanguageAvailableResult.CountryVarAvailable:
langAvailable.Add(locale.DisplayLanguage);
break;
}
}
langAvailable = langAvailable.OrderBy(t => t).Distinct().ToList();
最佳答案
android google tts:为什么langavailable返回不支持或-2?
似乎google tts需要一点初始化,当tts引擎正确初始化时,应该使用IsLanguageAvailable()
方法。
例如:
void TextToSpeech.IOnInitListener.OnInit(OperationResult status)
{
// if we get an error, default to the default language
if (status == OperationResult.Error)
textToSpeech.SetLanguage(Java.Util.Locale.Default);
// if the listener is ok, set the lang
if (status == OperationResult.Success)
{
langAvailable = new List<string> { "Default" };
localesAvailable = Java.Util.Locale.GetAvailableLocales().ToList();
foreach (var locale in localesAvailable)
{
LanguageAvailableResult res = textToSpeech.IsLanguageAvailable(locale);
switch (res)
{
case LanguageAvailableResult.Available:
langAvailable.Add(locale.DisplayLanguage);
break;
case LanguageAvailableResult.CountryAvailable:
langAvailable.Add(locale.DisplayLanguage);
break;
case LanguageAvailableResult.CountryVarAvailable:
langAvailable.Add(locale.DisplayLanguage);
break;
}
}
langAvailable = langAvailable.OrderBy(t => t).Distinct().ToList();
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, langAvailable);
spinLanguages.Adapter = adapter;
textToSpeech.SetLanguage(lang);
}
}
然后,当您从
Spinner
中选择语言时:spinLanguages.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
{
lang = Java.Util.Locale.GetAvailableLocales().FirstOrDefault(t => t.DisplayLanguage == langAvailable[(int)e.Id]);
textToSpeech.SetLanguage(lang);
};
Effect。
关于c# - Android Google TTS:为什么langAvailable返回不支持或-2?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47062498/