如何改变合成语音语音语言UWP

本文关键字:语音 语言 UWP 何改变 改变 | 更新日期: 2024-09-08 06:02:07

我想将文本语音语言更改为语音语言。这是我的代码:

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"的异常,但未在用户代码中进行处理。

我做错了什么?

如何改变合成语音语音语言UWP

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

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

从代码你可以检查它:

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();
}