Reputation: 207
I would like to change text to speech voice langauge. This is my code:
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);
}
But when I try to run this code, there is exception thrown in line:
speech.Voice = voices.First(x => x.Gender == VoiceGender.Female && x.Language.Contains("fr-FR"));
An exception of type 'System.InvalidOperationException' occurred in System.Linq.dll but was not handled in user code.
What do I do wrong?
Upvotes: 2
Views: 2477
Reputation: 2679
Please check is your application has microphone access grants (in manifest)
<Capabilities>
<DeviceCapability Name="microphone" />
</Capabilities>
Frome code you can check it with:
bool permissionGained = await AudioCapturePermissions.RequestMicrophonePermission();
if (!permissionGained)
{
//ask user to modify settings
}
And better check first is language installed in system:
var list = from a in SpeechSynthesizer.AllVoices
where a.Language.Contains("en")
select a;
if (list.Count() > 0)
{
synthesizer.Voice = list.Last();
}
Upvotes: 4