VoicePlay.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace RailLocalMeter
  8. {
  9. public class VoicePlay
  10. {
  11. private string m_szRunPath = Environment.CurrentDirectory;
  12. private VoiceMCI vmciClass = new VoiceMCI();
  13. /// <summary>
  14. /// 语音播放
  15. /// </summary>
  16. /// <param name="strvoiceName"></param>
  17. /// <param name="strPntName">车牌号</param>
  18. public void GetVoicePlay(VoiceEnum strvoiceName, string strCarNo)
  19. {
  20. try
  21. {
  22. WriteLog("车牌号:" + strCarNo + " 当前准备播放:" + strvoiceName);
  23. //判断是否存在语音,存在则语音播报
  24. bool isHaveVoice = false;
  25. foreach (string str in CacleCls.voiceInfo)
  26. {
  27. if (strvoiceName.ToString() == str)
  28. {
  29. isHaveVoice = true;
  30. break;
  31. }
  32. }
  33. if (!isHaveVoice) return; //不存在语音则不进行后面播放操作
  34. if (!CacleCls.voiceOver) return;//语音未播完
  35. CacleCls.voiceOver = false;
  36. WriteLog("车牌号:" + strCarNo + " 语音播放:" + strvoiceName);
  37. string path = $"{m_szRunPath}\\Sound\\{strvoiceName}.wav";
  38. int intVmci = vmciClass.VoicePlay(path);
  39. WriteLog("车牌号:" + strCarNo + " 语音播放结果( -1语音播放失败 0无音频设备 1语音播放成功 2语音播放结束):" + intVmci);
  40. }
  41. catch (Exception exp)
  42. {
  43. WriteLog("语音播放异常!" + strvoiceName + " " + exp.Message);
  44. }
  45. }
  46. private void WriteLog(string str)
  47. {
  48. // 20220925 By BourneCao 暂时屏蔽语音播放日志
  49. return;
  50. string m_szRunPath;
  51. m_szRunPath = System.Environment.CurrentDirectory;
  52. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  53. {
  54. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  55. }
  56. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  57. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  58. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  59. {
  60. Directory.CreateDirectory(strPathFile);
  61. }
  62. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\语音播放_" + strDate + ".log", true);
  63. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  64. tw.WriteLine(str);
  65. tw.WriteLine("\r\n");
  66. tw.Close();
  67. }
  68. }
  69. public enum VoiceEnum
  70. {
  71. 车辆未停到位,
  72. 开始计量,
  73. 车辆超重,
  74. 计量完成,
  75. 洁净重量过小,
  76. 仪表未归零,
  77. 两次计量重量接近,
  78. 车号未识别,
  79. 程序处理异常
  80. }
  81. }