123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- using AxWMPLib;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using tool;
- namespace chutian_parking_terminal.Media
- {
- public class MediaPlayer
- {
- action_window mainForm;
- List<string> adlist { get; set; }
- List<string> imagelist { get; set; }
- PictureBox pictureBox1;
- AxWindowsMediaPlayer axWindowsMediaPlayer1;
- Label timeLab;
- Label dateLab;
- Label dateLab1;
- //广告播放线程
- Thread m_adsplay_thread;
- bool m_adsplay_condition;
- //图片播放线程
- Thread m_imageplay_thread;
- bool m_imageplay_condition;
- //时间显示线程
- Thread m_clockshow_thread;
- bool m_clockshow_condition;
- public MediaPlayer()
- {
- }
- //播放器初始化、
- public void MediaPlayerInit(action_window mainForm, ref PictureBox pictureBox1, ref AxWindowsMediaPlayer axWindowsMediaPlayer1, ref Label timeLab, ref Label dateLab, ref Label dateLab1)
- {
- this.mainForm = mainForm;
- this.pictureBox1 = pictureBox1;
- this.axWindowsMediaPlayer1 = axWindowsMediaPlayer1;
- this.timeLab = timeLab;
- this.dateLab = dateLab;
- this.dateLab1 = dateLab1;
- m_adsplay_thread = new Thread(adsplay_thread);
- m_adsplay_condition = false;
- m_imageplay_thread = new Thread(imageplay_thread);
- m_imageplay_condition = false;
- m_clockshow_thread = new Thread(clockshow_thread);
- m_clockshow_condition = false;
- }
- public void mediaplayer_run()
- {
- //启动三个线程
- m_adsplay_condition = true;
- m_adsplay_thread.Start();
- m_imageplay_condition = true;
- m_imageplay_thread.Start();
- m_clockshow_condition = true;
- m_clockshow_thread.Start();
- }
- public void mediaplayer_uninit()
- {
- //关闭三个线程
- m_adsplay_condition = false ;
- m_clockshow_condition = false;
- m_imageplay_condition = false;
- m_adsplay_thread.Abort();
- m_imageplay_thread.Abort();
- m_clockshow_thread.Abort();
- }
- /// <summary>
- /// 循环播放视频
- /// </summary>
- private void adsplay_thread()
- {
- axWindowsMediaPlayer1.uiMode = "none";
- axWindowsMediaPlayer1.enableContextMenu = false;
- axWindowsMediaPlayer1.stretchToFit = true;
- axWindowsMediaPlayer1.settings.volume = 0;
- adlist = searchResource(@"Resource", "*.mp4");
- if (adlist.Count > 0)
- {
- axWindowsMediaPlayer1.URL = "Resource\\" + adlist[0];
- PlayListener();
- }
- }
- private void imageplay_thread()
- {
- imagelist = searchResource(@"Resource", "*.jpg");
- if (imagelist.Count > 0 && m_imageplay_condition)
- {
- for (int i = 0; i <= imagelist.Count; i++)
- {
- if (!m_imageplay_condition)
- return;
- if (i == imagelist.Count) i = 0;
- try
- {
- //无法访问已释放的资源
- mainForm.Invoke(new Action(() =>
- {
- pictureBox1.Image = Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory + "/Resource/" + imagelist[i]);
- }));
- }
- catch (Exception ex)
- {
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "关闭程序后轮播图报错:" + ex.Message);
- }
- Thread.Sleep(1000);
- }
- }
- }
- private void clockshow_thread()
- {
- Task.Factory.StartNew(() =>
- {
- while (m_clockshow_condition)
- {
- try
- {
- string time = System.DateTime.Now.ToString("HH:mm:ss");
- string date = System.DateTime.Now.ToString("yyyy-MM-dd");
- string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
- string week = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];
- mainForm.Invoke(new Action(() =>
- {
- this.timeLab.Text = time;
- this.dateLab.Text = date + " " + week;
- this.dateLab1.Text = date + " " + week;
- }));
- }
- catch { }
- Thread.Sleep(1000);
- }
- });
- }
- /// <summary>
- /// 循环播放视频
- /// </summary>
- private void PlayListener()
- {
- new Thread(() =>
- {
- for (int i = 1; i <= adlist.Count; i++)
- {
- //if (!isClosed)
- if (m_adsplay_condition)
- {
- try
- {
- if (i == adlist.Count) i = 0;
- //此处会抛出异常
- if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
- {
- try
- {
- mainForm.Invoke(new Action(() =>
- {
- nextVideo(i);
- }));
- }
- catch (Exception ex)
- {
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "nextMusic进入之前抛出异常:" + ex.Message);
- }
- }
- else
- {
- i--;
- }
- }
- catch (Exception ex)
- {
- if (i != 0) { i--; }
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "没有到nextMusic就抛出异常:" + ex.Message);
- }
- }
- else
- {
- break;
- }
- Thread.Sleep(1000);
- }
- }).Start();
- }
- /// <summary>
- /// 播放下一首
- /// </summary>
- /// <param name="index">视频集的索引</param>
- private void nextVideo(int index)
- {
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "开始播放下一首");
- try
- {
- axWindowsMediaPlayer1.URL = "Resource\\" + adlist[index];
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.INFO, "播放下一首");
- }
- catch (Exception ex)
- {
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, "播放下一首失败:" + ex.Message);
- }
- //axWindowsMediaPlayer1.Ctlcontrols.play();
- }
- /// <summary>
- /// 得到指定目录中的指定后缀文件
- /// </summary>
- /// <param name="directory">项目更路径下的资源文件夹</param>
- /// <param name="suffix">后缀(如:.jpg)</param>
- /// <returns></returns>
- private List<string> searchResource(string directory, string suffix)
- {
- DirectoryInfo dir = new DirectoryInfo(directory);
- FileInfo[] fileInfo = dir.GetFiles(suffix);
- List<string> list = new List<string>();
- foreach (FileInfo item in fileInfo)
- {
- //读取文件名,包括文件后缀
- list.Add(item.Name);
- }
- return list;
- }
- ///// <summary>
- ///// 天气
- ///// </summary>
- //private void WeatherUpdate()
- //{
- // Task.Factory.StartNew(() =>
- // {
- // while (!StatusSettings.isClosed)
- // {
- // Weather.WeatherWebService weather = new Weather.WeatherWebService();
- // string[] s = new string[23];
- // s = weather.getWeatherbyCityName(TerminalSettings.City);
- // string[] a = s[10].Split(new char[2] { ':', ';' });
- // string weatherStr = s[6].Substring(5);
- // if (a.Length > 2)
- // {
- // string temperature = a[2];
- // }
- // //this.Invoke(new Action(() =>
- // //{
- // // this.weatherLab.Text = this.weatherLab.Text + weatherStr;
- // // this.temperatureLab.Text = this.temperatureLab.Text + temperature;
- // //}));
- // }
- // Thread.Sleep(1800000);
- // });
- //}
- ///// <summary>
- ///// 天气
- ///// </summary>
- //private void GetWeather()
- //{
- // Task.Factory.StartNew(() =>
- // {
- // while (!StatusSettings.isClosed)
- // {
- // try
- // {
- // string url = "http://flash.weather.com.cn/wmaps/xml/china.xml";
- // WebClient MyWebClient = new WebClient();
- // string pageData = MyWebClient.DownloadData(url);
- // string pageHtml = Encoding.UTF8.GetString(pageData);
- // XDocument doc = XDocument.Parse(pageHtml);
- // var results = from ele in doc.Descendants("city") where ele.Attribute("cityname").Value.Equals(TerminalSettings.City) select ele;
- // foreach (var result in results)
- // {
- // string weatherStr = result.Attribute("stateDetailed").Value;
- // string temperature = result.Attribute("tem2").Value + "℃";
- // try
- // {
- // this.Invoke(new Action(() =>
- // {
- // this.weatherLab.Text = "天气:" + weatherStr;
- // this.temperatureLab.Text = "气温:" + temperature;
- // }));
- // }
- // catch (Exception)
- // {
- // }
- // }
- // }
- // catch (WebException webEx)
- // {
- // Console.WriteLine(webEx.Message.ToString());
- // }
- // }
- // Thread.Sleep(3600000);
- // });
- //}
- }
- }
|