zoukankan      html  css  js  c++  java
  • C#使用DirectShow播放视频文件 [转]

    原文网址:http://blog.csdn.net/openzpc/article/details/48442751

    最近在开发一个视频播放软件,主要要求就是循环播放多个视频文件,并且要求两个视频文件切换时,不能有黑屏现象发生。

    无论是使用Winform的Mediaplayer控件还是WPF的MediaElement控件,在一个视频播放完毕切换到另一个视频时,都会有一个短暂的黑屏情况,于是我就把目光放在了DirectShow上面。下面说一下如何使用DirectShow进行视频播放。

    1.quartz.dll和Tlbimp.exe

    使用DirectShow,需要用到C:WindowsSystem32文件夹下的quartz.dll文件,但此文件并不能直接在Visual Studio中使用,需要通过Tlbimp.exe(类型库导入程序),将quartz.dll转化为互操作程序集。这里只需要执行以下命令 

    [csharp] view plain copy
    1. tlbimp c:windowssystem32quartz.dll /out:QuartzTypeLib.dll  

    这个命令并不是直接在cmd中执行,而是在Vs的开发工具中。


    执行结果如下

     

    2.相关代码

    首先创建FilgraphManager等相关的实例

    [csharp] view plain copy
    1. private const int WM_APP = 0x8000;  
    2. private const int WM_GRAPHNOTIFY = WM_APP + 1;  
    3. FilgraphManager _filGraphManager = null;  
    4. private IBasicVideo _basicVideo = null;  
    5. IVideoWindow _videoWindow = null;  
    6. IMediaEvent _mediaEvent = null;  
    7. IMediaEventEx _mediaEventEx = null;  
    8. IMediaPosition _mediaPosition = null;  
    9. IMediaControl _mediaControl = null;  

    加载视频文件

    [csharp] view plain copy
    1. private void LoadVideo(string fileName)  
    2. {  
    3.     _filGraphManager = new FilgraphManager();  
    4.     _filGraphManager.RenderFile(fileName);  
    5.     _basicVideo = _filGraphManager as IBasicVideo;  
    6.     try  
    7.     {  
    8.         _videoWindow = _filGraphManager as IVideoWindow;  
    9.         _videoWindow.Owner = (int) PlVideo.Handle;  
    10.         _videoWindow.WindowStyle = 0x40000000;  
    11.         _videoWindow.SetWindowPosition(PlVideo.ClientRectangle.Left, PlVideo.ClientRectangle.Top,  
    12.             PlVideo.ClientRectangle.Width, PlVideo.ClientRectangle.Height);  
    13.   
    14.     }  
    15.     catch (Exception)  
    16.     {  
    17.   
    18.         throw;  
    19.     }  
    20.   
    21.     _mediaEvent = _filGraphManager as IMediaEvent;  
    22.     _mediaEventEx = _filGraphManager as IMediaEventEx;  
    23.     _mediaEventEx.SetNotifyWindow((int) this.Handle, WM_GRAPHNOTIFY, 0);  
    24.     _mediaPosition = _filGraphManager as IMediaPosition;  
    25.     _mediaControl = _filGraphManager as IMediaControl;  
    26. }  


    视频播放只需要调用FilgraphManager实例中的Run方法即可

    3.类和接口的说明

    FilgraphManager 用于建立和控制graph的对象

    RenderFile方法用于加载指定文件,Run,Stop,Pause方法对filters进行控制

    IBasicVideo 接口  设置video的属性,如宽高,比特率等内容,通过owner属性设置video在哪个控件上显示

    IMediaEvent 接口  获取事件通知的接口  IMediaEventEx是前者的扩展

    IMediaPosition 接口  用于获取stream中的位置 get_CurrentPosition方法可以获取当前位置,当需要进度条显示视频播放位置时,可以使用此方法

    IMediaControl 接口  这个根据名称就可以知道用途

  • 相关阅读:
    解决undefined reference to `__poll_chk@GLIBC_2.16' 错误
    交叉编译总结 libosscore.a libcurl.a libmysqlclient.a
    APUE环境配置
    UDT中epoll对CLOSE状态的处理
    查看ld搜索路径
    linux shell 比较文件夹内容 diff
    交互式makefile
    linux shell取文本最后一行
    linux 查看静态库,动态库是32位还是64位
    python学习day4之路
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/7691163.html
Copyright © 2011-2022 走看看