Click here to Skip to main content
Click here to Skip to main content
Go to top

VLCWrapper - A Little C++-wrapper Around libvlc

, 12 Mar 2012
Rate this:
Please Sign up or sign in to vote.
An article on wrapping libvlc (VLC media player) in a C++-class
VLCWrapper/VLCWrapper1.jpg

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. Wink | ;)

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.  

class VLCWrapper
{
    std::auto_ptr<VLCWrapperImpl> pImpl_;
 
public:
    VLCWrapper();
    ~VLCWrapper();
    void SetOutputWindow(void* pHwnd);
    void SetEventHandler(VLCEventHandler event, void* pUserData);
    void OpenMedia(const char* pMediaPathName);
    void Play();
    void Pause();
    void Stop();
    int64_t GetLength();
    int64_t GetTime();
    void SetTime(int64_t newTime);
    void Mute(bool mute = true);
    bool GetMute();
    int  GetVolume();
    void SetVolume(int volume);
};  

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:

#include "VLCWrapper.h"

class CVlcDialogDlg : public CDialog
{
    VLCWrapper vlcPlayer_; /// Our VLCWrapper.
    .
    .
    //{{AFX_DATA(CVlcDialogDlg)
    CStatic vlcControl_;  /// This CStatic will be used as device context by libvlc.
    . . . 
    //}}AFX_DATA

Now we can initialize the VLCWrapper in OnInitDialog():

BOOL CVlcDialogDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    vlcPlayer_.SetOutputWindow((void*)vlcControl_.GetSafeHwnd()); // the CStatic will be 
							          // used for video output

    vlcPlayer_.SetEventHandler(&HandleVLCEvents, this);           // Set handler for vlc events

The handler function for VLC events:

static void HandleVLCEvents(const VLCEvent* pEvent, void* pUserData)
{
    CVlcDialogDlg* pDlg = reinterpret_cast<CVlcDialogDlg*>(pUserData); 
 
    switch(pEvent->type)
    {
    case libvlc_MediaPlayerTimeChanged:
       TRACE("VLC_EVT_TIME_CHANGED: new_time %d[ms]\n",
              pEvent->u.media_player_time_changed.new_time);
       if(pDlg)
           pDlg->UpdatePosition();
       break;
    } 
}

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

void CVlcDialogDlg::OnBnClickedButtonLoad()
{
    CFileDialog dlgFile(TRUE);
    if(dlgFile.DoModal()==IDOK)
    {
        CString file=dlgFile.GetPathName();
        vlcPlayer_.OpenMedia((LPCTSTR)file);
        vlcPlayer_.Play();  // start media after loading....
    }
}

void CVlcDialogDlg::OnBnClickedButtonPlay()
{
    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)

Share

About the Author

Alex Skoruppa
Technical Lead HERE, a Nokia Business
Germany Germany
No Biography provided

Comments and Discussions

 
Questionpimpl Pinmemberpanqnik12-Aug-14 1:11 
Questionlibvlc.dll and libvlccore.dll PinmemberMember 1090496226-Jun-14 21:28 
SuggestionIf it crashes PinmemberKarstenK21-May-14 2:49 
QuestionReduce the delay(VLC Network streaming) PinmemberMember 1052751210-Feb-14 19:04 
GeneralWin7 & vx2010 PinprofessionalAlice4720-Jan-14 3:46 
QuestionIt doesn't support playing video which's name is chinese Pinmemberftai22-Dec-13 14:48 
Questionvs2010+vlc2.1.2 crashing Pinmemberftai19-Dec-13 16:14 
AnswerRe: vs2010+vlc2.1.2 crashing Pinmemberftai19-Dec-13 18:10 
Bugget Error 1 error LNK1123: failure during conversion to COFF: PinmemberMember 934496014-Nov-13 4:39 
QuestionView the start frame only PinmemberMember 1029442825-Sep-13 2:44 
QuestionVolume and Mute PinmemberdeBoozaAgain14-Aug-13 22:38 
Questionsuccessful! PinmemberPhong Le Thanh1-Aug-13 23:20 
GeneralThanks PinmemberMaxwellG9-May-13 1:38 
Questionhelp Pinmemberangel_boom28-Mar-13 0:18 
GeneralMy vote of 5 PinmemberBartlomiej Filipek23-Mar-13 5:20 
QuestionProblems with http access PinmemberMember 959508022-Nov-12 4:44 
AnswerRe: Problems with http access PinmemberMember 959508022-Nov-12 21:39 
GeneralMy vote of 2 Pinmembermshahbazm11-Nov-12 2:10 
Not working for me
QuestionCDialog and CStatic Pinmemberhasnain26639-Nov-12 10:27 
SuggestionLoop the playback PinmemberSkywalker200825-Sep-12 10:47 
QuestionActiver le désentrelacement Pinmemberjeanmarc0424-Sep-12 5:22 
QuestionCreating project from begin Pinmemberaleks301-Sep-12 23:36 
AnswerRe: Creating project from begin PinmemberMember 1017777929-Jul-13 6:07 
BugProblem with chars > 127 in media path Pinmemberfred250629-Aug-12 5:25 
QuestionCreating project from begin Pinmemberaleks3021-Aug-12 0:53 
QuestionHow to play a video from resources? [modified] Pinmembersdiazdiaz13-Aug-12 23:28 
AnswerRe: How to play a video from resources? Pinmemberhuntkao15-Aug-12 22:21 
QuestionSupport for Win7? Pinmemberhabichnicht24-Jun-12 22:21 
Questionlink with 64 bit application? PinmemberAlon Geri18-Mar-12 6:08 
AnswerRe: link with 64 bit application? Pinmemberfen22-Mar-12 23:14 
QuestionNice app - anyone have this in vb.net? if so please post. thanx Pinmemberbwarehouse12-Mar-12 23:18 
Question.libs included in 2.0.0 PinmemberMember 85397381-Mar-12 3:46 
QuestionVLC 2.0 PinmemberDavide Zaccanti22-Feb-12 7:57 
NewslibVLC is switching to LGPL PinmemberJulien Lebot5-Feb-12 22:39 
GeneralRe: libVLC is switching to LGPL PinmemberAlex Skoruppa5-Feb-12 22:57 
GeneralRe: libVLC is switching to LGPL PinmemberRene Pilon12-Mar-12 4:55 
QuestionHelp with the Streaming API for VLC ... Pinmemberdaved900014-Dec-11 7:39 
AnswerRe: Help with the Streaming API for VLC ... Pinmemberdaved900015-Dec-11 8:21 
GeneralRe: Help with the Streaming API for VLC ... Pinmemberhuntkao15-Aug-12 23:45 
QuestionPassing options to hide title PinmemberDavide Zaccanti5-Nov-11 9:34 
AnswerRe: Passing options to hide title PinmemberLe Roy7-Nov-11 21:06 
QuestionRelease bug in Visual Studio 2003 or 2008. PinmemberMember 260660318-Sep-11 21:00 
AnswerRe: Release bug in Visual Studio 2003 or 2008. Pinmemberironfrocodeproject20-Jul-12 1:28 
Questioncropping Pinmemberlabinator21-May-11 3:42 
GeneralMy vote of 5 PinmemberHeaven20208-Mar-11 6:30 
GeneralMy vote of 4 PinmemberxComaWhitex7-Jan-11 23:43 
QuestionHow to use VLCWrapper to open video/music file from a URL? Pinmemberhuutribk20015-Jan-11 16:37 
AnswerRe: How to use VLCWrapper to open video/music file from a URL? PinmemberMarkDoubson8-Oct-11 18:21 
Questionlibvlc_new returns null PinmemberIvan616423-Oct-10 12:33 
AnswerRe: libvlc_new returns null PinmemberMember 6264827-Oct-10 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 12 Mar 2012
Article Copyright 2009 by Alex Skoruppa
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid