c# winform播放MP4视频

1、在所有windows窗体中添加windows media player控件

c# winform播放MP4视频_第1张图片

c# winform播放MP4视频_第2张图片 

 c# winform播放MP4视频_第3张图片

2、拖入windows media player控件,编写代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DXProject.Core.page
{
    public partial class YrForm : DevExpress.XtraEditors.XtraForm
    {
        
        public YrForm(string path)
        {
            InitializeComponent();
            try
            {
                SetStyle(ControlStyles.UserPaint, true);
                SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
                SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
                //FileDialog open = new OpenFileDialog();
                //wxWindowsMediaplayer1是控件名
                this.axWindowsMediaPlayer1.settings.playCount = 1;//播放次数;
                //open.Filter = "*.mp4|*.mp4"; //文件格式
                //open.Title = "打开文件";
                //if (open.ShowDialog() == DialogResult.OK)
                {
                    axWindowsMediaPlayer1.URL = path;
                   
                    axWindowsMediaPlayer1.Ctlcontrols.play();//播放文件
                    axWindowsMediaPlayer1.StatusChange += windowsMediaPlay_StatusChange;
                }
            }
            catch { }
        }
        #region 解决闪烁问题
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0014) // 禁掉清除背景消息

                return;

            base.WndProc(ref m);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        #endregion
        void windowsMediaPlay_StatusChange(object sender, EventArgs e)
        {
            /*  
             * 0 Undefined Windows Media Player is in an undefined state.(未定义) 
               1 Stopped Playback of the current media item is stopped.(停止) 
               2 Paused Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location.(停留) 
               3 Playing The current media item is playing.(播放) 
               4 ScanForward The current media item is fast forwarding. 
               5 ScanReverse The current media item is fast rewinding. 
               6 Buffering The current media item is getting additional data from the server.(转换) 
               7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin.(暂停) 
               8 MediaEnded Media item has completed playback. (播放结束) 
               9 Transitioning Preparing new media item. 
               10 Ready Ready to begin playing.(准备就绪) 
               11 Reconnecting Reconnecting to stream.(重新连接) 
           */
            //判断视频是否已停止播放  
            if ((int)axWindowsMediaPlayer1.playState == 1)
            {
                //停顿2秒钟再重新播放  
                System.Threading.Thread.Sleep(100);
                //重新播放  
                //axWindowsMediaPlayer1.Ctlcontrols.play();
            }
            else if ((int)axWindowsMediaPlayer1.playState == 3)
            {
                //axWindowsMediaPlayer1.fullScreen = true;
              //  this.axWindowsMediaPlayer1.Ctlcontrols.stop();
            }
        }
        private void YrForm_Load(object sender, EventArgs e)
        {

        }

        private void YrForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //this.axWindowsMediaPlayer1.Ctlcontrols.stop();
            
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.fullScreen = true;
        }
    }
}

 

你可能感兴趣的:(c#,开发语言)