事先准备:
1、DirectX 两个必备的dll文件http://download.csdn.net/detail/yao__shun__yu/5981799
2、下载解码器FinalCodecs1.13.0719.exe【找不到的加我群5307397】
代码块:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using Microsoft.DirectX.AudioVideoPlayback; namespace ScreenServer { public partial class VideoPlayer : Form { [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hPos, int x, int y, int cx, int cy, uint nflags); #region private Member //add by fuzhengwei 2013年8月21日 //临时存放视频播放路径 private static string videoUrlBuffer = null; //add by fuzhengwei 2013年8月21日 //记录播放时长 private static long duration; //add by fuzhengwei 2013年8月21日 //打开过滤 private readonly string filterText = "Video Files (*.avi; *.mov; *.mpg; *.mpeg; *.ts; *.wmv; *.vob; *.dat; *.rm; *.rmvb; *.flv)|*.avi; *.mov; *.mpg; *.mpeg; *.ts; *.wmv; *.vob; *.dat; *.rm; *.rmvb; *.flv"; //add by fuzhengwei 2013年8月21日 //Video private Video myVideo = null; #endregion public VideoPlayer() { InitializeComponent(); //窗口置顶 IntPtr HWND_TOPMOST = new IntPtr(-1); SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, 0x0001 | 0x0002); //窗口最大化 this.WindowState = System.Windows.Forms.FormWindowState.Maximized; } //add by fuzhengwei 2013年8月21日 //打开视频 private void openVideoToolStripMenuItem_Click(object sender, EventArgs e) { openVideoFileDialog.InitialDirectory = Application.StartupPath; openVideoFileDialog.Title = "Open Video File"; openVideoFileDialog.Filter = filterText; if(openVideoFileDialog.ShowDialog() == DialogResult.OK) { videoUrlBuffer = openVideoFileDialog.FileName; myVideo = new Video(videoUrlBuffer); myVideo.Owner = videoPanel; myVideo.Play(); duration = (int)myVideo.Duration; //调用显示时间函数 showTimeInPanel(duration); videoTimer.Enabled = true; } } //add by fuzhengwei 2013年8月21日 //播放视频 private void playVideoToolStripMenuItem_Click(object sender, EventArgs e) { if (null == videoUrlBuffer) { MessageBox.Show("请点击\"打开\"选择视频路径!"); } else { videoTimer.Enabled = true; showTimeInPanel(duration); myVideo.Play(); } } //add by fuzhengwei 2013年8月21日 //暂停视频 private void plauseToolStripMenuItem_Click(object sender, EventArgs e) { //暂停timer videoTimer.Enabled = false; myVideo.Pause(); } //add by fuzhengwei 2013年8月21日 //停止视频 private void stopToolStripMenuItem_Click(object sender, EventArgs e) { //暂停timer videoTimer.Enabled = false; //显示时间清零 showTimeInPanel(0); //显示时间清零 conShowTimeInPanel(0); myVideo.Stop(); } //add by fuzhengwei 2013年8月21日 //时间显示在屏幕上 private void showTimeInPanel(long durateTime) { videoTime.Text = ConvertTimeToString(durateTime); } //add by fuzhengwei 2013年8月21日 //每隔一毫秒显示一次时间在panel上 private void conShowTimeInPanel(long durateTime) { runTimelabel.Text = ConvertTimeToString(durateTime); } //add by fuzhengwei 2013年8月21日 //格式化时间 private string ConvertTimeToString(double time) { string timeFormat = "00:00:00"; int hours = 0; int minutes = 0; int seconds = 0; checked { hours = (int)time / 3600; minutes = ((int)time - (hours * 3600)) / 60; seconds = (int)time - hours * 3600 - minutes * 60; } timeFormat = string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds); return timeFormat; } //add by fuzhengwei 2013年8月21日 //循环时间事件 private void videoTimer_Tick(object sender, EventArgs e) { conShowTimeInPanel((int)myVideo.CurrentPosition); } } }