事先準備:
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);
        }
    }
}
上圖:

本文摘自 :https://blog.51cto.com/u

 APP
 APP 