织梦CMS - 轻松建站从此开始!

罗索

VLCWrapper - A Little C++-wrapper Around libvlc

落鹤生 发布于 2012-05-04 11:22 点击:次 
This article presents a little C++-wrapper for the libvlc-library, which is the core component of the VLC media player. I was looking for an easy way to integrate video playback in my C++ applications.
TAG:

For the demo, you need to download the actual VLC package and copy the plug-ins in the corresponding directory of the demo. You will also need to have libvlc.dll and libvlccore.dll in your path.

This package includes projects for Visual Studio 6 and Visual Studio 2010. The source package also includes the VLC headers and the libs for linking. You can find the libs on the videolan site in special ZIP files intended for developers. They are bigger (~36MB) than the normal ZIPs on the download page.

相关的源码你可以从codeproject上下载,也可以到流媒体开发论坛下载:http://bbs.rosoo.net/thread-13755-1-1.html


图片1

Introduction

This article presents a little C++-wrapper for the libvlc-library, which is the core component of the VLC media player. I was looking for an easy way to integrate video playback in my C++ applications. Because I've been using VLC for media playback for many years now, I started playing around with the VLC API. The result is a little wrapper around the libvlc-library. It provides basic media playback functionality. Since v.2.0 the libvlc-library is released under the LGPL, so you can use it in commercial applications. All sourcecode in this arcticle is licensed under the CPOL. Please excuse any bugs because this wrapper is more a quick hack than a feature complete wrapper. ;)

For testing purposes, I wrote a simple media player which uses the VLCWrapper. It's included in the example above.

Background

The most valuable source of information about the VLC API for me was the developer section on the VLC website and the documented C-headers of the VLC source. The project website is a good starting point if you plan to code something with VLC or extend the VLCWrapper.

The C++-Interface of VLCWrapper to LIBVLC

The interface declared in "VLCWrapper.h" is small and the member functions are quite self-explanatory. To get a good overlook, I stripped of the comments. Check out the sources for more information.

  1. class VLCWrapper 
  2.     std::auto_ptr<VLCWrapperImpl> pImpl_; 
  3.   
  4. public
  5.     VLCWrapper(); 
  6.     ~VLCWrapper(); 
  7.     void SetOutputWindow(void* pHwnd); 
  8.     void SetEventHandler(VLCEventHandler event, void* pUserData); 
  9.     void OpenMedia(const char* pMediaPathName); 
  10.     void Play(); 
  11.     void Pause(); 
  12.     void Stop(); 
  13.     int64_t GetLength(); 
  14.     int64_t GetTime(); 
  15.     void SetTime(int64_t newTime); 
  16.     void Mute(bool mute = true); 
  17.     bool GetMute(); 
  18.     int  GetVolume(); 
  19.     void SetVolume(int volume); 
  20. };   

Using the Code

Using the VLCWrapper is easy. The first step would be to add a new VLCWraper as a member of your window class and a CStatic for video output:

  1. #include "VLCWrapper.h" 
  2.  
  3. class CVlcDialogDlg : public CDialog 
  4.     VLCWrapper vlcPlayer_; /// Our VLCWrapper. 
  5.     . 
  6.     . 
  7.     //{{AFX_DATA(CVlcDialogDlg) 
  8.     CStatic vlcControl_;  /// This CStatic will be used as device context by libvlc. 
  9.     . . .  
  10.     //}}AFX_DATA 

Now we can initialize the VLCWrapper in OnInitDialog():

  1. BOOL CVlcDialogDlg::OnInitDialog() 
  2.     CDialog::OnInitDialog(); 
  3.  
  4.     vlcPlayer_.SetOutputWindow((void*)vlcControl_.GetSafeHwnd()); // the CStatic will be  
  5.                                       // used for video output 
  6.  
  7.     vlcPlayer_.SetEventHandler(&HandleVLCEvents, this);    // Set handler for vlc events 

The handler function for VLC events:

  1. static void HandleVLCEvents(const VLCEvent* pEvent, void* pUserData) 
  2.     CVlcDialogDlg* pDlg = reinterpret_cast<CVlcDialogDlg*>(pUserData);  
  3.   
  4.     switch(pEvent->type) 
  5.     { 
  6.     case libvlc_MediaPlayerTimeChanged: 
  7.        TRACE("VLC_EVT_TIME_CHANGED: new_time %d[ms]\n"
  8.               pEvent->u.media_player_time_changed.new_time); 
  9.        if(pDlg) 
  10.            pDlg->UpdatePosition(); 
  11.        break
  12.     }  

Now everything is in place, and you can start playing media files. E.g., the "Load" and "Play" members look like this:

  1. void CVlcDialogDlg::OnBnClickedButtonLoad() 
  2.     CFileDialog dlgFile(TRUE); 
  3.     if(dlgFile.DoModal()==IDOK) 
  4.     { 
  5.         CString file=dlgFile.GetPathName(); 
  6.         vlcPlayer_.OpenMedia((LPCTSTR)file); 
  7.         vlcPlayer_.Play();  // start media after loading.... 
  8.     } 
  9.  
  10. void CVlcDialogDlg::OnBnClickedButtonPlay() 
  11.     vlcPlayer_.Play(); 

History

  • 03/12/2012
    • Updated the project to use the VLC API 2.0.0.
    • Changed license to CPOL.
  • 09/20/2011
    • Added project files for Visual Studio 2010.
  • 09/11/2010
    • Some bugfixes and optimizations, thanks to heretic13!
    • Build the demo with VLC 1.1.4.
    • Removed demo & source links to old VLCDialog versions which required VLC < 1.1.
  • 05/29/2010
    • Updated the project to use the VLC API 1.1
    • The wrapper now uses an auto_ptr for its Pimpl. I missed deleting the Pimpl in the first versions
    • Fixed a redraw error
    • Thanks to Haim Engler for the hint on the memory leak and the redraw problem!
  • 08/20/2009
    • Pimped VlcDialog (Resizing, Icon-Buttons)
    • Striped off dependency to "StdAfx.h" in class VLCWrapper and class VLCWrapperImpl
    • Replaced a deprecated function call to libvlc; now SetOutputWindow(void*) takes a void pointer to a window handle
  • 08/14/2009
    • Some fixes in the example listings
  • 08/11/2009
    • Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Alex Skoruppa

Software Developer
VITRONIC
Germany

Member

 

(Alex Skoruppa)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201205/15980.html]
本文出处:codeproject.com 作者:Alex Skoruppa 原文
顶一下
(3)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容