VoicePlay.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 CarLocalMeter
  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(string 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 == 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. strvoiceName = strvoiceName + ".wav";
  38. string path = m_szRunPath + "\\Sound\\" + strvoiceName;
  39. int intVmci = vmciClass.VoicePlay(path);
  40. WriteLog("车牌号:" + strCarNo + " 语音播放结果( -1语音播放失败 0无音频设备 1语音播放成功 2语音播放结束):" + intVmci);
  41. }
  42. catch (Exception exp)
  43. {
  44. WriteLog("语音播放异常!" + strvoiceName + " " + exp.Message);
  45. }
  46. }
  47. private void WriteLog(string str)
  48. {
  49. // 20220925 By BourneCao 暂时屏蔽语音播放日志
  50. return;
  51. string m_szRunPath;
  52. m_szRunPath = System.Environment.CurrentDirectory;
  53. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  54. {
  55. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  56. }
  57. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  58. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  59. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  60. {
  61. Directory.CreateDirectory(strPathFile);
  62. }
  63. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\语音播放_" + strDate + ".log", true);
  64. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  65. tw.WriteLine(str);
  66. tw.WriteLine("\r\n");
  67. tw.Close();
  68. }
  69. }
  70. }