实例介绍
【实例简介】
一个wpf做的广告程序,是否循环播放图片和视频 全屏播放功能。
【实例截图】
需要启用 Windows Media Player 功能,否则会提示【System.Runtime.InteropServices.COMException:“没有注册类】
操作步骤:控制面板>>程序>>启用或者关闭windows 功能>>选中 媒体功能>>Windows Media Player,点击确定即可
【核心代码】
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
namespace HospitalFrontTVSystem
{
public partial class MainForm : Form
{
//服务器uri
string serviceUri = ConfigurationSettings.AppSettings["constri"].ToString();
string screen = ConfigurationSettings.AppSettings["screen"].ToString();
string interval = ConfigurationSettings.AppSettings["interval"].ToString();
string deviceId = ConfigurationSettings.AppSettings["deviceId"].ToString(); //获取配置文件里的值
//缓存的资源路径
List<string> resoures = new List<string>();
//本地播放资源路径
string fileBasePath = AppDomain.CurrentDomain.BaseDirectory "playResources\\";
public MainForm()
{
InitializeComponent();
//RunAutoClass.SetAutoRun();
this.Left = Screen.AllScreens[0].Bounds.Width*Int32.Parse(screen);
this.Top = 0;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;//全屏无边框
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.axWindowsMediaPlayer1.uiMode = "none";//除去视频播放器其他功能按钮
}
private void MainForm_Load(object sender, EventArgs e)
{
pictureBox2.Left =860;
pictureBox2.Top = 540;
//timerForGetResouse.Enabled = true;//启动线程链接服务器并缓存数据
timerForImaChange.Interval = Int32.Parse(interval) * 1000;
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork = (ee, se) =>
{
timerForGetResouseFun();
};
bw.RunWorkerAsync();
}
/// <summary>
/// 线程 获取服务器数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void timerForGetResouseFun()
{
if (!Directory.Exists(fileBasePath))
{
//如果不存在就创建文件夹
Directory.CreateDirectory(fileBasePath);
}
if (serviceUri != null && deviceId!=null)
{
string serReturn = HttpClass.HRequest(serviceUri "/itf/advert/getAdvertInfo?deviceId=" deviceId);//接受服务器资料
if (serReturn != null)
{
List<string> tb = serReturn.Split(';').ToList<string>();
if (tb != null && tb.Count > 0)
{
for (int i = 0; i < tb.Count; i )
{
string[] tem = tb[i].Split(',');
if (tem != null && tem.Length > 0)
{
string filePath = tem[tem.Length - 1];
int sNum = filePath.LastIndexOf('/') 1;
int endNum = filePath.Length - sNum;
string fileName = filePath.Substring(sNum, endNum);
//首先用utf-8进行解码
fileName = System.Web.HttpUtility.UrlDecode(fileName);
//判断根目录是否已存在相同命名的文件
if (!File.Exists(fileBasePath fileName))
{
string webSerResourse = serviceUri filePath;
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.DownloadFile(webSerResourse, fileBasePath fileName);//缓存资源
}
resoures.Add(fileName);
}
}
}
else
{
MessageBox.Show("服务器没有可播放的资源");
}
}
else
{
MessageBox.Show("服务器没有可播放的资源");
}
}
else
{
MessageBox.Show("请在配置文件中完善配置信息");
}
}
int Index = 0;//播放索引
List<Image> imgs = new List<Image>();
private void timerForImaChange_Tick_Tick(object sender, EventArgs e)
{
if (resoures.Count == 0) return;
pictureBox2.Visible = false;
try
{
if (Index >= resoures.Count) //播放资源数量
{
Index = 0;
}
if (resoures[Index].Contains("jpg")||resoures[Index].Contains("JPG")||resoures[Index].Contains("png")||resoures[Index].Contains("PNG"))//播放图片
{
if (imgs.Count <= Index)
{
imgs.Add(Image.FromFile(fileBasePath resoures[Index]));
}
pictureBox1.Visible = true;
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.Image = imgs[Index];
}
else//播放视频
{
this.axWindowsMediaPlayer1.Visible = true;
this.axWindowsMediaPlayer1.Dock = DockStyle.Fill;
this.axWindowsMediaPlayer1.URL = fileBasePath resoures[Index];
this.axWindowsMediaPlayer1.stretchToFit = true;
this.axWindowsMediaPlayer1.Ctlcontrols.play();
// this.axWindowsMediaPlayer1.fullScreen = true;
timerForImaChange.Enabled = false;
}
Index ;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 当播放完视频后继续轮播图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (this.axWindowsMediaPlayer1.status == "已完成")//视频播放完后重新启动线程
{
this.axWindowsMediaPlayer1.Visible = false;
timerForImaChange.Enabled = true;
}
}
/// <summary>
/// 关闭所有线程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
timerForImaChange.Enabled = false;
}
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
}
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)