c# 实现文本读取,语音报警功能

c# 实现文本读取,语音报警功能

在测试软件功能过程中,由于需要操作硬件进行变位,但是操作完成后在去看监控软件有时间延迟,除非2个人配合,就想实现告警进行语音播报功能
实现方法1
[DllImport(“winmm.dll”)]
public static extern bool PlaySound(string pszSound, int hmod, int fdwSound);
public const int SND_FILENAME = 0x00020000;
public const int SND_ASYNC = 0x0001;

然后在需要报警的地方调用PlaySound,就可以发出声音;
缺点:必须提前录取好wav文件,且必须是wav文件,不灵活
实现方法2
网上下载文件 DotNetSpeech.dll
在引用中增加这个动态库的引用
using DotNetSpeech;//语音报警的引擎
阅读函数
private void Read(string text)
{
SpVoice sp = new SpVoice();
sp.Rate = 0;//阅读的速度,可自行调整
SpeechVoiceSpeakFlags sFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
sp.Speak(text, sFlags);
}

在需要进行报警的地方调用Read函数即可播放参数的内容;
优点,不用录制文件,直接读取文字内容;

优化:由于播放文件比文字显示要慢很多,可能出现丢语音,或者播放大量文字时对程序或系统有延迟的影响,采用线程模式
threadplaysound = new Thread(read);
threadplaysound.IsBackground = true;
threadplaysound.Start();

在需要告警的地方增加文字
llsound.Add(要增加的文字); //llsound = list

/// <summary>
  /// 语音播报 llsount的内容,由于可能未播报完成,有新soe进入引起混乱,由线程调用
  /// </summary>
  public void read()
  {
      while(true)
      {
          while(llsound.Count>0)
          {
              Read(llsound[0]);
              Thread.Sleep(500);
              llsound.RemoveAt(0);
          }
          Thread.Sleep(500);                
      }            
  }    

实验结果有效,做标记。

猜你喜欢

转载自blog.csdn.net/mainmaster/article/details/109639200