VoiceMCI.cs 8.3 KB

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