MediaPlayer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using AxWMPLib;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using tool;
  12. namespace chutian_parking_terminal.Media
  13. {
  14. public class MediaPlayer
  15. {
  16. action_window mainForm;
  17. List<string> adlist { get; set; }
  18. List<string> imagelist { get; set; }
  19. PictureBox pictureBox1;
  20. AxWindowsMediaPlayer axWindowsMediaPlayer1;
  21. Label timeLab;
  22. Label dateLab;
  23. Label dateLab1;
  24. //广告播放线程
  25. Thread m_adsplay_thread;
  26. bool m_adsplay_condition;
  27. //图片播放线程
  28. Thread m_imageplay_thread;
  29. bool m_imageplay_condition;
  30. //时间显示线程
  31. Thread m_clockshow_thread;
  32. bool m_clockshow_condition;
  33. public MediaPlayer()
  34. {
  35. }
  36. //播放器初始化、
  37. public void MediaPlayerInit(action_window mainForm, ref PictureBox pictureBox1, ref AxWindowsMediaPlayer axWindowsMediaPlayer1, ref Label timeLab, ref Label dateLab, ref Label dateLab1)
  38. {
  39. this.mainForm = mainForm;
  40. this.pictureBox1 = pictureBox1;
  41. this.axWindowsMediaPlayer1 = axWindowsMediaPlayer1;
  42. this.timeLab = timeLab;
  43. this.dateLab = dateLab;
  44. this.dateLab1 = dateLab1;
  45. m_adsplay_thread = new Thread(adsplay_thread);
  46. m_adsplay_condition = false;
  47. m_imageplay_thread = new Thread(imageplay_thread);
  48. m_imageplay_condition = false;
  49. m_clockshow_thread = new Thread(clockshow_thread);
  50. m_clockshow_condition = false;
  51. }
  52. public void mediaplayer_run()
  53. {
  54. //启动三个线程
  55. m_adsplay_condition = true;
  56. m_adsplay_thread.Start();
  57. m_imageplay_condition = true;
  58. m_imageplay_thread.Start();
  59. m_clockshow_condition = true;
  60. m_clockshow_thread.Start();
  61. }
  62. public void mediaplayer_uninit()
  63. {
  64. //关闭三个线程
  65. m_adsplay_condition = false ;
  66. m_clockshow_condition = false;
  67. m_imageplay_condition = false;
  68. m_adsplay_thread.Abort();
  69. m_imageplay_thread.Abort();
  70. m_clockshow_thread.Abort();
  71. }
  72. /// <summary>
  73. /// 循环播放视频
  74. /// </summary>
  75. private void adsplay_thread()
  76. {
  77. axWindowsMediaPlayer1.uiMode = "none";
  78. axWindowsMediaPlayer1.enableContextMenu = false;
  79. axWindowsMediaPlayer1.stretchToFit = true;
  80. axWindowsMediaPlayer1.settings.volume = 0;
  81. adlist = searchResource(@"Resource", "*.mp4");
  82. if (adlist.Count > 0)
  83. {
  84. axWindowsMediaPlayer1.URL = "Resource\\" + adlist[0];
  85. PlayListener();
  86. }
  87. }
  88. private void imageplay_thread()
  89. {
  90. imagelist = searchResource(@"Resource", "*.jpg");
  91. if (imagelist.Count > 0 && m_imageplay_condition)
  92. {
  93. for (int i = 0; i <= imagelist.Count; i++)
  94. {
  95. if (!m_imageplay_condition)
  96. return;
  97. if (i == imagelist.Count) i = 0;
  98. try
  99. {
  100. //无法访问已释放的资源
  101. mainForm.Invoke(new Action(() =>
  102. {
  103. pictureBox1.Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory + "/Resource/" + imagelist[i]);
  104. }));
  105. }
  106. catch (Exception ex)
  107. {
  108. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "关闭程序后轮播图报错:" + ex.Message);
  109. }
  110. Thread.Sleep(1000);
  111. }
  112. }
  113. }
  114. private void clockshow_thread()
  115. {
  116. Task.Factory.StartNew(() =>
  117. {
  118. while (m_clockshow_condition)
  119. {
  120. try
  121. {
  122. string time = System.DateTime.Now.ToString("HH:mm:ss");
  123. string date = System.DateTime.Now.ToString("yyyy-MM-dd");
  124. string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  125. string week = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];
  126. mainForm.Invoke(new Action(() =>
  127. {
  128. this.timeLab.Text = time;
  129. this.dateLab.Text = date + " " + week;
  130. this.dateLab1.Text = date + " " + week;
  131. }));
  132. }
  133. catch { }
  134. Thread.Sleep(1000);
  135. }
  136. });
  137. }
  138. /// <summary>
  139. /// 循环播放视频
  140. /// </summary>
  141. private void PlayListener()
  142. {
  143. new Thread(() =>
  144. {
  145. for (int i = 1; i <= adlist.Count; i++)
  146. {
  147. //if (!isClosed)
  148. if (m_adsplay_condition)
  149. {
  150. try
  151. {
  152. if (i == adlist.Count) i = 0;
  153. //此处会抛出异常
  154. if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
  155. {
  156. try
  157. {
  158. mainForm.Invoke(new Action(() =>
  159. {
  160. nextVideo(i);
  161. }));
  162. }
  163. catch (Exception ex)
  164. {
  165. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "nextMusic进入之前抛出异常:" + ex.Message);
  166. }
  167. }
  168. else
  169. {
  170. i--;
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. if (i != 0) { i--; }
  176. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "没有到nextMusic就抛出异常:" + ex.Message);
  177. }
  178. }
  179. else
  180. {
  181. break;
  182. }
  183. Thread.Sleep(1000);
  184. }
  185. }).Start();
  186. }
  187. /// <summary>
  188. /// 播放下一首
  189. /// </summary>
  190. /// <param name="index">视频集的索引</param>
  191. private void nextVideo(int index)
  192. {
  193. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "开始播放下一首");
  194. try
  195. {
  196. axWindowsMediaPlayer1.URL = "Resource\\" + adlist[index];
  197. Log.Instance.WriteLog(LogType.PROCESS, LogFile.INFO, "播放下一首");
  198. }
  199. catch (Exception ex)
  200. {
  201. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "播放下一首失败:" + ex.Message);
  202. }
  203. //axWindowsMediaPlayer1.Ctlcontrols.play();
  204. }
  205. /// <summary>
  206. /// 得到指定目录中的指定后缀文件
  207. /// </summary>
  208. /// <param name="directory">项目更路径下的资源文件夹</param>
  209. /// <param name="suffix">后缀(如:.jpg)</param>
  210. /// <returns></returns>
  211. private List<string> searchResource(string directory, string suffix)
  212. {
  213. DirectoryInfo dir = new DirectoryInfo(directory);
  214. FileInfo[] fileInfo = dir.GetFiles(suffix);
  215. List<string> list = new List<string>();
  216. foreach (FileInfo item in fileInfo)
  217. {
  218. //读取文件名,包括文件后缀
  219. list.Add(item.Name);
  220. }
  221. return list;
  222. }
  223. ///// <summary>
  224. ///// 天气
  225. ///// </summary>
  226. //private void WeatherUpdate()
  227. //{
  228. // Task.Factory.StartNew(() =>
  229. // {
  230. // while (!StatusSettings.isClosed)
  231. // {
  232. // Weather.WeatherWebService weather = new Weather.WeatherWebService();
  233. // string[] s = new string[23];
  234. // s = weather.getWeatherbyCityName(TerminalSettings.City);
  235. // string[] a = s[10].Split(new char[2] { ':', ';' });
  236. // string weatherStr = s[6].Substring(5);
  237. // if (a.Length > 2)
  238. // {
  239. // string temperature = a[2];
  240. // }
  241. // //this.Invoke(new Action(() =>
  242. // //{
  243. // // this.weatherLab.Text = this.weatherLab.Text + weatherStr;
  244. // // this.temperatureLab.Text = this.temperatureLab.Text + temperature;
  245. // //}));
  246. // }
  247. // Thread.Sleep(1800000);
  248. // });
  249. //}
  250. ///// <summary>
  251. ///// 天气
  252. ///// </summary>
  253. //private void GetWeather()
  254. //{
  255. // Task.Factory.StartNew(() =>
  256. // {
  257. // while (!StatusSettings.isClosed)
  258. // {
  259. // try
  260. // {
  261. // string url = "http://flash.weather.com.cn/wmaps/xml/china.xml";
  262. // WebClient MyWebClient = new WebClient();
  263. // string pageData = MyWebClient.DownloadData(url);
  264. // string pageHtml = Encoding.UTF8.GetString(pageData);
  265. // XDocument doc = XDocument.Parse(pageHtml);
  266. // var results = from ele in doc.Descendants("city") where ele.Attribute("cityname").Value.Equals(TerminalSettings.City) select ele;
  267. // foreach (var result in results)
  268. // {
  269. // string weatherStr = result.Attribute("stateDetailed").Value;
  270. // string temperature = result.Attribute("tem2").Value + "℃";
  271. // try
  272. // {
  273. // this.Invoke(new Action(() =>
  274. // {
  275. // this.weatherLab.Text = "天气:" + weatherStr;
  276. // this.temperatureLab.Text = "气温:" + temperature;
  277. // }));
  278. // }
  279. // catch (Exception)
  280. // {
  281. // }
  282. // }
  283. // }
  284. // catch (WebException webEx)
  285. // {
  286. // Console.WriteLine(webEx.Message.ToString());
  287. // }
  288. // }
  289. // Thread.Sleep(3600000);
  290. // });
  291. //}
  292. }
  293. }