C#使用DirectShow实现简单的视频播放

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Zhangchen9091/article/details/47916299

1、C#添加DirectShow库

添加引用,选择“浏览”,添加Windows/System32/quartz.dll,引用中自动增加QuartzTypeLib。

2、代码如下:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using QuartzTypeLib;//引用DirectShow类库


namespace DirectShow
{
    public partial class FormMain : Form
    {
        //窗体样式
        public const int WS_BORDER = 0x00800000;
        public const int WS_CAPTION = 0x00C00000;
        public const int WS_CHILD = 0x40000000;
        //
        QuartzTypeLib.FilgraphManager m_FilterManager;//管理器
        QuartzTypeLib.IVideoWindow m_VideoWindow;//视频窗体
        QuartzTypeLib.IMediaEvent m_MediaEvent;//媒体事件
        QuartzTypeLib.IMediaEventEx m_MediaEventEx;//媒体事件扩展
        QuartzTypeLib.IMediaPosition m_MediaPos;//媒体当前位置
        QuartzTypeLib.IMediaControl m_MediaCtrl;//媒体控制器


        public FormMain()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            m_FilterManager = new QuartzTypeLib.FilgraphManager();//
            m_FilterManager.RenderFile("f:/768x576.avi");//设置待播放文件
            m_VideoWindow = m_FilterManager as QuartzTypeLib.IVideoWindow;//设置播放窗体
            m_VideoWindow.Owner = (int)(this.panelMain.Handle);
            m_VideoWindow.WindowStyle &= ~WS_BORDER;
            m_VideoWindow.SetWindowPosition(0, 0, panelMain.Width,panelMain.Height);
            //
            m_MediaEvent = m_FilterManager as QuartzTypeLib.IMediaEvent;//设置媒体事件
            m_MediaEventEx = m_FilterManager as QuartzTypeLib.IMediaEventEx;//设置媒体事件扩展
            m_MediaPos = m_FilterManager as QuartzTypeLib.IMediaPosition;//设置媒体位置
            m_MediaCtrl = m_FilterManager as QuartzTypeLib.IMediaControl;//设置媒体控制器
        }


        private void btnOpen_Click(object sender, EventArgs e)
        {
            m_MediaCtrl.Run();
        }


        private void btnStop_Click(object sender, EventArgs e)
        {
            m_MediaCtrl.Stop();
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页