你好我什么时候点击一个按钮我需要用印地语语言在文本块中获得一些声音。以下代码但是当我在模拟器中点击按钮时我得到以下异常。我不能了解它请帮助我摆脱这个......
private async void btnSpeak_Click(object sender, RoutedEventArgs e)
{
string filterLanguage = "";
SpeechSynthesizer speech = new SpeechSynthesizer();
filterLanguage = "hi-IN";
IEnumerable<VoiceInformation> voices = from voice in InstalledVoices.All
where voice.Language == filterLanguage
select voice;
speech.SetVoice(voices.ElementAt(0));
await speech.SpeakTextAsync(lblTranslatedText.Text);
}
我得到的例外是: -
System.ArgumentOutOfRangeException was unhandled by user code
HResult=-2146233086
Message=Specified argument was out of the range of valid values.
Parameter name: index
Source=System.Core
ParamName=index
StackTrace:
at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)
at Learn_Hindi.MainPage.<btnSpeak_Click>d__6.MoveNext()
InnerException:
答案 0 :(得分:2)
有时候你的Linq语句会返回一个空集合。在尝试获取特定元素之前,请尝试检查语音对象中是否有任何元素:
if (voices.Any())
{
speech.SetVoice(voices.ElementAt(0));
await speech.SpeakTextAsync(lblTranslatedText.Text);
}
答案 1 :(得分:0)
您应该对您的收藏进行适当的测试。发生异常是因为您尝试访问不存在的语音元素。您的模拟器上未安装您要过滤的请求语音。
IEnumerable<VoiceInformation> voices = from voice in InstalledVoices.All
where voice.Language == filterLanguage
select voice;
try {
speech.SetVoice(voices.ElementAt(0));
} catch (Exception e) {
// Show a message box or whatever to explain that the voice is not installed.
}