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 adlist { get; set; } List 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(); } /// /// 循环播放视频 /// 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); } }); } /// /// 循环播放视频 /// 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(); } /// /// 播放下一首 /// /// 视频集的索引 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(); } /// /// 得到指定目录中的指定后缀文件 /// /// 项目更路径下的资源文件夹 /// 后缀(如:.jpg) /// private List searchResource(string directory, string suffix) { DirectoryInfo dir = new DirectoryInfo(directory); FileInfo[] fileInfo = dir.GetFiles(suffix); List list = new List(); foreach (FileInfo item in fileInfo) { //读取文件名,包括文件后缀 list.Add(item.Name); } return list; } ///// ///// 天气 ///// //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); // }); //} ///// ///// 天气 ///// //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); // }); //} } }