C#编写的Windows应用程序,框架为.NET5.0,实现PC端与PLC通讯,PLC发送数据给PC端,PC端做判断并立即输出语音,并且一直循环该语音,直到PLC端发出无异常的消息。在处理PLC发来的消息时,如果该语音的内容与前一条相同,则不发发声,如果不同,则立即打断正在循环的语音,进行发声。
由于语音的循环时间是可变动的,在更改定时器定时时间后,语音输出会时有时无(已经测试过,定时器会按时触发),奇怪的是初始定时器延时时间为10秒,在缩小定时时间后,语音输出未见异常,但是增大定时时间后,语音就会时有时无,不会在定时达到后准时输出语音。请问怎么解决?
定时器定时触发的函数:
public void SpeakOut(object source, System.Timers.ElapsedEventArgs e)
{
t1.Stop(); //先关闭定时器
if (oldTime != Time)
{
oldTime = Time;
t1.Interval = Time * 1000;
}
if (EnableCycle)
{
//MessageBox.Show(spVoice.Status.RunningState.ToString());
if (spVoice.Status.RunningState == SpeechRunState.SRSEDone) //'RunningState=2表示当前有声音输出
{
spVoice.Speak(oldSrecv, SpeechVoiceSpeakFlags.SVSFlagsAsync);//表示异步执行语音输出
//spVoice.Speak(oldSrecv, SpeechVoiceSpeakFlags.SVSFDefault);
//Thread.Sleep(20000);
//MessageBox.Show(oldSrecv);
}
}
t1.Start(); //执行完毕后再开启器
}
PC端处理完PLC发送的数据,所触发的语音:
public string TextJst(string LineText, string oldLineText)
{
if (string.IsNullOrEmpty(LineText))
{
//oldSrecvZA = "";
}
else
{
if (LineText.Contains("正常"))
{
if (string.IsNullOrEmpty(oldLineText))
{
oldLineText = "";
}
else
{
oldLineText = "";
spVoice.Speak(LineText, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
//MessageBox.Show(oldLineText+"1");
//字符串长度为两个字符为长度1,一个汉字为长度1
API(LineText.Substring(0, LineText.Length - 3) + "线恢复正常");
}
}
else
{
if (string.IsNullOrEmpty(oldLineText))
{
spVoice.Speak(LineText, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
//MessageBox.Show(oldLineText + "2");
API(LineText);
oldLineText = LineText;
}
else
{
if (oldLineText == LineText)
LineText = "";
else
{
spVoice.Speak(LineText, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
//MessageBox.Show(oldLineText + "3");
API(LineText);
oldLineText = LineText;
}
}
}
}
return oldLineText;
}
定时触发的语音输出,使用SVSFPurgeBeforeSpeak会解决此问题,可能是异步发声的方式有问题,比如阻塞等,希望大神指点。