using System.Speech.Synthesis; namespace EC.AutoWeightServer.IfManager.Speech { public class SpeakHelper { private static SpeechSynthesizer _curSpeech = new SpeechSynthesizer { Volume = 100,//音量 [0, 100] Rate = 0//语速 [-10, 10] }; public SpeakHelper() { } #region Start & Stop Server public static void Open(int volume = 100, int rate = 0) { _curSpeech = new SpeechSynthesizer { Volume = volume, Rate = rate }; } public static void Close() { if (!IsOpen()) return; _curSpeech.Pause(); _curSpeech.Dispose(); _curSpeech = null; } public static bool IsOpen() { return _curSpeech != null; } /// /// 开启连接 /// 音量 [0, 100] /// 语速 [-10, 10] /// /// public static void ReOpen(int volume = 100, int rate = 0) { Close(); Open(volume, rate); } public static bool CheckState(bool reOpen = false) { var state = IsOpen(); if (!state && reOpen) ReOpen(); return state; } public static void SelectVoice(VoiceGender gender, VoiceAge age) { if (!CheckState(true)) return; _curSpeech.SelectVoiceByHints(gender, age); } #endregion Start & Stop Server #region Tool Methods /// /// public static void Speak(string text) { if (!CheckState(true)) return; _curSpeech.Speak(text); } public static void SpeakAsync(string text, bool cancelAll = true) { if (!CheckState(true)) return; if (cancelAll) _curSpeech.SpeakAsyncCancelAll(); _curSpeech.SpeakAsync(text); } #endregion Tool Methods } }