You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.6 KiB

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;
}
/// <summary>
/// 开启连接
/// 音量 [0, 100]
/// 语速 [-10, 10]
/// </summary>
/// <returns></returns>
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
/// <summary>
/// </summary>
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
}
}