如何更改合成语音语音UWP?

时间:2016-04-15 13:13:37

标签: c# linq text-to-speech

我想改变文字语音语言。 这是我的代码:

private async void readText(string text)
{
    var voices = SpeechSynthesizer.AllVoices;
    SpeechSynthesizer speech = new SpeechSynthesizer();
    speech.Voice = voices.First(x => x.Gender == VoiceGender.Female && x.Language.Contains("fr-FR"));
    SpeechSynthesisStream stream = await speech.SynthesizeTextToStreamAsync(text);
    mediaElement.SetSource(stream, stream.ContentType);
}

private void btnSay_Click(object sender, RoutedEventArgs e)
{
    readText(txtWhat.Text);
}

但是当我尝试运行此代码时,行中会抛出异常:

speech.Voice = voices.First(x => x.Gender == VoiceGender.Female && x.Language.Contains("fr-FR"));
  

System.Linq.dll中发生了'System.InvalidOperationException'类型的异常,但未在用户代码中处理。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

请检查您的应用程序是否有麦克风访问权限(在清单中)

<Capabilities> 
<DeviceCapability Name="microphone" /> 
</Capabilities>

您可以查看Frome代码:

bool permissionGained = await AudioCapturePermissions.RequestMicrophonePermission();
if (!permissionGained)
{
//ask user to modify settings
}

最好先检查系统中安装的语言:

var list = from a in SpeechSynthesizer.AllVoices
       where a.Language.Contains("en")
       select a;

if (list.Count() > 0)
{
synthesizer.Voice = list.Last();
}