VoiceMCI.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace RailLocalMeter
  10. {
  11. public class VoiceMCI
  12. {
  13. //定义API函数使用的字符串变量
  14. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  15. private string Name = "";
  16. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  17. private string durLength = "";
  18. [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
  19. private string TemStr = "";
  20. int ilong;
  21. //定义播放状态枚举变量
  22. public enum State
  23. {
  24. mPlaying = 1,
  25. mPuase = 2,
  26. mStop = 3
  27. };
  28. //结构变量
  29. public struct structMCI
  30. {
  31. public bool bMut;
  32. public int iDur;
  33. public int iPos;
  34. public int iVol;
  35. public int iBal;
  36. public string iName;
  37. public State status;
  38. };
  39. public structMCI mc = new structMCI();
  40. //取得播放文件属性
  41. public string FileName
  42. {
  43. get
  44. {
  45. return mc.iName;
  46. }
  47. set
  48. {
  49. try
  50. {
  51. TemStr = "";
  52. TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
  53. Name = Name.PadLeft(260, Convert.ToChar(" "));
  54. mc.iName = value;
  55. ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
  56. Name = GetCurrPath(Name);
  57. Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
  58. ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
  59. ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
  60. ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
  61. mc.status = State.mStop;
  62. }
  63. catch
  64. {
  65. }
  66. }
  67. }
  68. //播放
  69. public void play()
  70. {
  71. TemStr = "";
  72. TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
  73. APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
  74. mc.status = State.mPlaying;
  75. }
  76. //停止
  77. public void StopT()
  78. {
  79. TemStr = "";
  80. TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
  81. ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
  82. ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
  83. mc.status = State.mStop;
  84. }
  85. public void Puase()
  86. {
  87. TemStr = "";
  88. TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
  89. ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
  90. mc.status = State.mPuase;
  91. }
  92. private string GetCurrPath(string name)
  93. {
  94. if (name.Length < 1) return "";
  95. name = name.Trim();
  96. name = name.Substring(0, name.Length - 1);
  97. return name;
  98. }
  99. //总时间
  100. public int Duration
  101. {
  102. get
  103. {
  104. durLength = "";
  105. durLength = durLength.PadLeft(128, Convert.ToChar(" "));
  106. APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
  107. durLength = durLength.Trim();
  108. if (durLength == "") return 0;
  109. return (int)(Convert.ToDouble(durLength) / 1000f);
  110. }
  111. }
  112. //当前时间
  113. public int CurrentPosition
  114. {
  115. get
  116. {
  117. durLength = "";
  118. durLength = durLength.PadLeft(128, Convert.ToChar(" "));
  119. APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
  120. mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
  121. return mc.iPos;
  122. }
  123. }
  124. /// <summary>
  125. /// 是否有声卡
  126. /// </summary>
  127. /// <returns></returns>
  128. public bool IsExistWaveOut()
  129. {
  130. if (APIClass.waveOutGetNumDevs() != 0)
  131. {
  132. return true;//有声卡
  133. }
  134. return false;
  135. }
  136. /// <summary>
  137. /// 语音播放 -1语音播放失败 0无音频设备 1语音播放成功 2语音播放结束
  138. /// </summary>
  139. public int VoicePlay(string strPath)
  140. {
  141. try
  142. {
  143. VoiceMCI cm = new VoiceMCI();
  144. if (!cm.IsExistWaveOut())
  145. {
  146. WriteLog("未找到本机播放音频设备,无法播放");
  147. return 0;
  148. }
  149. string strvoicePath = strPath;
  150. string mp3 = strvoicePath;
  151. if (!string.IsNullOrWhiteSpace(mp3) && File.Exists(mp3))
  152. {
  153. cm.FileName = mp3;
  154. int a = cm.Duration + 1;
  155. cm.play();
  156. Application.DoEvents();
  157. DateTime dt = DateTime.Now;
  158. Task.Factory.StartNew(() =>
  159. {
  160. while (true)
  161. {
  162. //判断是否播放结束
  163. Application.DoEvents();
  164. int dur = ExecDateDiff(dt, DateTime.Now);
  165. Application.DoEvents();
  166. if (dur >= a)
  167. {
  168. CacleCls.voiceOver = true; //语音播完
  169. WriteLog("语音写入");
  170. return 2;//语音播放结束
  171. }
  172. }
  173. });
  174. }
  175. return 1;
  176. }
  177. catch (Exception exp)
  178. {
  179. WriteLog("语音播放异常!" + exp.Message);
  180. return -1;
  181. }
  182. }
  183. /// <summary>
  184. /// 计算时间差
  185. /// </summary>
  186. /// <param name="dateBegin"></param>
  187. /// <param name="dateEnd"></param>
  188. /// <returns>返回秒数</returns>
  189. public static int ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
  190. {
  191. TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
  192. TimeSpan ts2 = new TimeSpan(dateEnd.Ticks);
  193. TimeSpan ts3 = ts1.Subtract(ts2).Duration();
  194. //你想转的格式
  195. return ts3.Minutes * 60 + ts3.Seconds;
  196. }
  197. public void WriteLog(string str)
  198. {
  199. // 20220925 By BourneCao 暂时屏蔽语音播放日志
  200. return;
  201. string m_szRunPath;
  202. m_szRunPath = System.Environment.CurrentDirectory;
  203. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  204. {
  205. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  206. }
  207. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  208. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  209. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  210. {
  211. Directory.CreateDirectory(strPathFile);
  212. }
  213. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\语音播放MCI_" + strDate + ".log", true);
  214. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  215. tw.WriteLine(str);
  216. tw.WriteLine("\r\n");
  217. tw.Close();
  218. }
  219. }
  220. public class APIClass
  221. {
  222. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  223. public static extern int GetShortPathName(
  224. string lpszLongPath,
  225. string shortFile,
  226. int cchBuffer
  227. );
  228. [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
  229. public static extern int mciSendString(
  230. string lpstrCommand,
  231. string lpstrReturnString,
  232. int uReturnLength,
  233. int hwndCallback
  234. );
  235. [DllImport("winmm.dll", EntryPoint = "waveOutGetNumDevs")]
  236. public static extern int waveOutGetNumDevs();
  237. }
  238. }