private async void Button_Click_F(object sender, RoutedEventArgs e) { mediaElement.Stop(); var syn = new Windows.Media.SpeechSynthesis.SpeechSynthesizer(); Windows.Media.SpeechSynthesis.SpeechSynthesisStream stm = await syn.SynthesizeTextToStreamAsync(tekst); mediaElement.SetSource(stm, stm.ContentType); mediaElement.Play(); }
private async void PlaySpeech() { try { _totalPlayTime = _totalPlayTime.Add(Setting.App.AlarmInterval); //todo //using (SpeechSynthesizer syn = new SpeechSynthesizer()) //{ // string setting = Setting.Speech.Message; // if (string.IsNullOrEmpty(setting)) // setting = SpeechSetting.DefaultMessage; // string hourMsg = ""; // if (_totalPlayTime.Hours > 0) // hourMsg = $"{_totalPlayTime.Hours}小时 {_totalPlayTime.Minutes}分"; // else // hourMsg = $"{_totalPlayTime.Minutes}分钟"; // string msg = string.Format(setting, hourMsg); // syn.Speak(msg); //} string setting = Setting.Speech.Message; string hourMsg = ""; if (_totalPlayTime.Hours > 0) { hourMsg = $"{_totalPlayTime.Hours}小时 {_totalPlayTime.Minutes}分"; } else { hourMsg = $"{_totalPlayTime.Minutes}分钟"; } string msg = string.Format(setting, hourMsg); using var synthesizer = new Windows.Media.SpeechSynthesis.SpeechSynthesizer(); using Windows.Media.SpeechSynthesis.SpeechSynthesisStream synthStream = await synthesizer.SynthesizeTextToStreamAsync(msg); using Stream stream = synthStream.AsStreamForRead(); using System.Media.SoundPlayer player = new System.Media.SoundPlayer(); player.Stream = stream; player.Play(); var icon = IoC.Get <TaskbarIcon>(); icon.ShowBalloonTip("休息提示", msg, BalloonIcon.Info); } catch (Exception ex) { logger.Error($"PlaySpeech Ex:{ex.Message}"); } }
private async void Button_Click_M(object sender, RoutedEventArgs e) { mediaElement.Stop(); var syn = new Windows.Media.SpeechSynthesis.SpeechSynthesizer(); VoiceInformation voiceInfo = (from voice in SpeechSynthesizer.AllVoices where voice.Gender == VoiceGender.Male select voice).FirstOrDefault() ?? SpeechSynthesizer.DefaultVoice; syn.Voice = voiceInfo; Windows.Media.SpeechSynthesis.SpeechSynthesisStream stm = await syn.SynthesizeTextToStreamAsync(tekst); mediaElement.SetSource(stm, stm.ContentType); mediaElement.Play(); }