Android Google TTS:为什么langAvailable返回不支持或-2?

时间:2017-11-01 19:42:11

标签: c# android xamarin.android google-text-to-speech

我正在关注https://developer.xamarin.com/guides/android/platform_features/speech/并在langAvailable中获取单个值(默认)。 我还更新了Google TTS引擎并安装了一些语音数据。我安装了其他使用此引擎的应用程序,此应用程序具有所有语言。 那么我做错了什么? Android OS 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();

1 个答案:

答案 0 :(得分:1)

  

Android Google TTS:为什么langAvailable返回不支持或-2?

看起来谷歌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