本文整理汇总了C#中SpeechSynthesizer.SynthesizeTextToStreamAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SpeechSynthesizer.SynthesizeTextToStreamAsync方法的具体用法?C# SpeechSynthesizer.SynthesizeTextToStreamAsync怎么用?C# SpeechSynthesizer.SynthesizeTextToStreamAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SpeechSynthesizer
的用法示例。
在下文中一共展示了SpeechSynthesizer.SynthesizeTextToStreamAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SpeakText
private async void SpeakText(object sender, RoutedEventArgs e)
{
string dialogText = null;
try
{
var voice = this.VoiceComboBox.SelectedItem as VoiceInformation;
if (voice != null)
{
var text = this.InputText.Text;
if (!string.IsNullOrWhiteSpace(text))
{
var synthesizer = new SpeechSynthesizer();
synthesizer.Voice = voice;
var audioStream = await synthesizer.SynthesizeTextToStreamAsync(text);
if (audioStream != null)
{
this.MediaPlayer.SetSource(audioStream, audioStream.ContentType);
this.MediaPlayer.Play();
}
else
{
dialogText = "Can't synthesize the text";
}
}
}
return;
}
catch (Exception ex)
{
dialogText = "Error playing audio: " + ex.Message;
}
if (!string.IsNullOrWhiteSpace(dialogText))
await new Windows.UI.Popups.MessageDialog(dialogText).ShowAsync();
}
示例2: StartTextToSpeech
static public async Task StartTextToSpeech(string text)
{
if (!string.IsNullOrEmpty(text))
{
try
{
if (_speech != null)
{
StopTextToSpeech();
}
var voice = GetSpeechVoice();
if (voice != null)
{
_speech = new SpeechSynthesizer();
_speech.Voice = voice;
SpeechSynthesisStream speechStream = await _speech.SynthesizeTextToStreamAsync(Utility.DecodeHtml(text));
_soundPlayer = new MediaElement();
_soundPlayer.SetSource(speechStream, speechStream.ContentType);
_soundPlayer.Play();
}
}
catch (Exception ex)
{
AppLogs.WriteError("SpeechServices", ex);
}
}
}
示例3: _speak_Click
private async void _speak_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
_speak.IsEnabled = false;
//
// Get selected voice
//
var selectedVoice = (VoiceInformation)_availableVoices.SelectedItem;
//
// Create synthesizer & speak
//
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.Voice = selectedVoice;
var stream = await synth.SynthesizeTextToStreamAsync(_textToSpeak.Text);
//
// Set mediaelement source to generated stream
//
_mediaElement.Stop();
_mediaElement.SetSource(stream, stream.ContentType);
_mediaElement.Play();
_speak.IsEnabled = true;
}
示例4: Speak
private async void Speak(string text)
{
synthesizer = new SpeechSynthesizer();
speechContext = ResourceContext.GetForCurrentView();
speechContext.Languages = new string[] { SpeechSynthesizer.DefaultVoice.Language };
speechResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("LocalizationTTSResources");
// If the media is playing, the user has pressed the button to stop the playback.
if (media.CurrentState.Equals(MediaElementState.Playing))
{
media.Stop();
}
else
{
try
{
SpeechSynthesisStream synthesisStream = await synthesizer.SynthesizeTextToStreamAsync(text);
media.AutoPlay = true;
media.SetSource(synthesisStream, synthesisStream.ContentType);
media.Play();
}
catch (Exception)
{
}
}
}
示例5: MediaElement
MediaElement mediaElement = new MediaElement(); // Pamięć jest przydzielana tylko raz, dynamicznie
async void speakString(string text)
{
var synth = new SpeechSynthesizer();
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(text);
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
}
示例6: Speak
public async void Speak(string text)
{
var mediaElement = new MediaElement();
var synth = new SpeechSynthesizer();
var stream = await synth.SynthesizeTextToStreamAsync(text);
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
}
示例7: SpeakText
private async void SpeakText(MediaElement audioPlayer, string textToSpeak)
{
using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
{
SpeechSynthesisStream ttsStream = await synthesizer.SynthesizeTextToStreamAsync(textToSpeak);
audioPlayer.SetSource(ttsStream, ""); // This starts the player because AutoPlay="True"
}
}
示例8: Speak
public async void Speak(MediaElement audioPlayer, SpeechMessage speech)
{
var synth = new SpeechSynthesizer();
var ttsStream = await synth.SynthesizeTextToStreamAsync(speech.Message);
audioPlayer.SetSource(ttsStream, "");
audioPlayer.CurrentStateChanged += async (object sender, Windows.UI.Xaml.RoutedEventArgs e) =>
{
await MainPage.Instance.SendResult(new ResultMessage(speech) { ResultId = (int)audioPlayer.CurrentState, Result = Enum.GetName(typeof(MediaElementState), audioPlayer.CurrentState) });
};
}
示例9: Fortune_Text_Click
private async void Fortune_Text_Click(object sender, TappedRoutedEventArgs e)
{
MediaElement media = new MediaElement();
string text = textBlockFortune.Text;
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync(text + ", God bless America");
media.SetSource(stream, stream.ContentType);
media.IsLooping = true;
media.Play();
}
示例10: Speak
public async void Speak(string text)
{
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync(text);
MediaElement media = new MediaElement();
media.AutoPlay = true;
media.SetSource(stream, stream.ContentType);
media.Play();
}
示例11: talkItButtonTap
private async void talkItButtonTap(object sender, RoutedEventArgs e)
{
SpeechSynthesizer synth = new SpeechSynthesizer();
string message = textBox.Text;
if (message != null)
{
var stream = await synth.SynthesizeTextToStreamAsync(message);
var media = new MediaElement();
media.SetSource(stream, stream.ContentType);
media.Play();
}
else
{
message = "Text box is empty";
var stream = await synth.SynthesizeTextToStreamAsync(message);
var media = new MediaElement();
media.SetSource(stream, stream.ContentType);
media.Play();
}
}
示例12: ProcessCommands
public async void ProcessCommands(SpeechRecognitionResult result)
{
string voiceCommandName = result.RulePath.First();
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
switch (voiceCommandName)
{
case "showSessionsByRoom":
var roomNumber = result.SemanticInterpretation.Properties["room"][0];
await ViewModel.FindSessionsByRoom(roomNumber);
break;
case "showSessions":
var stream = await synthesizer.SynthesizeTextToStreamAsync("There are " + ViewModel.CodecampSessions.Count + " sessions!");
AudioPlayer.SetSource(stream, string.Empty);
break;
case "findSessionsByKeyword":
string tag = result.SemanticInterpretation.Properties["tag"][0];
var sessionCount = ViewModel.CodecampSessions.Count(s => s.Tags.Contains(tag));
var findStream = await synthesizer.SynthesizeTextToStreamAsync("There are " + sessionCount + " sessions related to " + tag + "!");
AudioPlayer.SetSource(findStream, string.Empty);
break;
}
}
示例13: SpeakAsync
public async Task SpeakAsync(string text)
{
var voice = SpeechSynthesizer.AllVoices
.First(x => x.Gender.Equals(VoiceGender.Female) && x.Description.Contains("United States"));
using (var speech = new SpeechSynthesizer { Voice = voice })
{
text = string.IsNullOrWhiteSpace(text) ? "There is no text to speak." : text;
var stream = await speech.SynthesizeTextToStreamAsync(text);
var media = new MediaElement { AutoPlay = true };
media.SetSource(stream, stream.ContentType);
}
}
示例14: OnClick
async private void OnClick(object sender, RoutedEventArgs e)
{
//文本为空
if (string.IsNullOrWhiteSpace(txtToSay.Text) == true)
return;
//语音朗读
SpeechSynthesizer speech = new SpeechSynthesizer();
//将字符串生成语音
SpeechSynthesisStream stream = await speech.SynthesizeTextToStreamAsync(txtToSay.Text);
if (stream != null)
{
//MediaElement的音频源
this.mdPlayer.SetSource(stream, stream.ContentType);
}
}
示例15: Speak
// http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207057(v=vs.105).aspx
public async void Speak(string text)
{
SpeechSynthesizer synth = new SpeechSynthesizer();
try
{
var stream = await synth.SynthesizeTextToStreamAsync(text);
var mediaElement = new MediaElement();
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
}
catch (Exception pe) {
Debug.WriteLine("couldn't play voice " + pe.Message);
}
}