2016-04-15 49 views
0

我想將文本更改爲語音語音語言。 這是我的代碼:如何更改合成語音語音UWP?

 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.InvalidOperationException」類型的異常出現在系統.Linq.dll但未在用戶代碼中處理。

我該怎麼做?

+1

嘗試FirstOrDefault。你確定安裝了法語嗎? 我已經使用 speech.Voice =(從Windows.Media.SpeechSynthesis.SpeechSynthesizer.AllVoices中的語音中獲得語音。語音。語言==「fr-FR」 選擇語音).First(); –

+0

你有清單功能的麥克風嗎?

+0

我還沒有安裝法語,我該怎麼做? – ktos1234

回答

2

請檢查你的應用程序有麥克風訪問授權(在清單)

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

我檢查了我已安裝的語言: foreach(語音合成器中的var voice.AllVoices) { txtVoices.Text = voice.Language; } 看來我沒有法語。 TextBox只顯示一個語言。 – ktos1234

+0

這裏是文章如何安裝語言並將其設置爲語音識別 [安裝Windows 10語音識別附加語言包](https://zulja.wordpress.com/2015/08/11/installing-additional-language-packs -for-windows-10-speech-recognition /) –

+0

謝謝,我安裝了語言,現在它可以處理我的原始代碼。 我還有一個問題,使用SpeechSynthesizer可以改變音高,音量或速度嗎?我怎樣才能做到這一點? – ktos1234