Click here to Skip to main content
12,694,542 members (33,838 online)
Click here to Skip to main content
Add your own
alternative version


163 bookmarked

VLCWrapper - A Little C++-wrapper Around libvlc

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


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.


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_;
    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.
    CStatic vlcControl_;  /// This CStatic will be used as device context by libvlc.
    . . . 

Now we can initialize the VLCWrapper in OnInitDialog():

BOOL CVlcDialogDlg::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); 
    case libvlc_MediaPlayerTimeChanged:
       TRACE("VLC_EVT_TIME_CHANGED: new_time %d[ms]\n",

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);
        CString file=dlgFile.GetPathName();
        vlcPlayer_.Play();  // start media after loading....

void CVlcDialogDlg::OnBnClickedButtonPlay()


  • 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


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Bartlomiej Filipek23-Mar-13 6:20
memberBartlomiej Filipek23-Mar-13 6:20 
QuestionProblems with http access Pin
Member 959508022-Nov-12 5:44
memberMember 959508022-Nov-12 5:44 
AnswerRe: Problems with http access Pin
Member 959508022-Nov-12 22:39
memberMember 959508022-Nov-12 22:39 
GeneralRe: Problems with http access Pin
alaa hassan20-Aug-16 11:38
memberalaa hassan20-Aug-16 11:38 
GeneralRe: Problems with http access Pin
Member 128338735-Nov-16 6:07
memberMember 128338735-Nov-16 6:07 
GeneralMy vote of 2 Pin
mshahbazm11-Nov-12 3:10
membermshahbazm11-Nov-12 3:10 
QuestionCDialog and CStatic Pin
hasnain26639-Nov-12 11:27
memberhasnain26639-Nov-12 11:27 
SuggestionLoop the playback Pin
Skywalker200825-Sep-12 11:47
memberSkywalker200825-Sep-12 11:47 
Excellent! Thanks.

In my application, I need play a movie and auto-repeat the playing until the Stop button is hit. Here is how I did based on the VLCWrapper in this article:

1. Define a couple of more members in class VLCWrapperImpl:
libvlc_media_list_t* pMediaList_;
libvlc_media_list_player_t* pMdeiaListPlayer_;

2. In the VLCWrapperImpl constructor, add

pMediaList_ = libvlc_media_list_new(pVLCInstance_);
pMdeiaListPlayer_ = libvlc_media_list_player_new(pVLCInstance_);

libvlc_media_list_player_set_media_list(pMdeiaListPlayer_, pMediaList_);
libvlc_media_list_player_set_media_player(pMdeiaListPlayer_, pMediaPlayer_);
libvlc_media_list_player_set_playback_mode(pMdeiaListPlayer_, libvlc_playback_mode_loop);

3. In VLCWrapperImpl::OpenMedia(...), replace
libvlc_media_player_set_media(pMediaPlayer_, pMedia_);
libvlc_media_list_add_media(pMediaList_, pMedia_);

4. In VLCWrapperImpl::Play(), replace with
Similar changes apply in ::Pause() and ::Stop().

Just share it here in case someone wants to know.

QuestionActiver le désentrelacement Pin
jeanmarc0424-Sep-12 6:22
memberjeanmarc0424-Sep-12 6:22 
QuestionCreating project from begin Pin
aleks302-Sep-12 0:36
memberaleks302-Sep-12 0:36 
AnswerRe: Creating project from begin Pin
Member 1017777929-Jul-13 7:07
memberMember 1017777929-Jul-13 7:07 
BugProblem with chars > 127 in media path Pin
fred250629-Aug-12 6:25
memberfred250629-Aug-12 6:25 
QuestionCreating project from begin Pin
aleks3021-Aug-12 1:53
memberaleks3021-Aug-12 1:53 
QuestionHow to play a video from resources? Pin
sdiazdiaz14-Aug-12 0:28
membersdiazdiaz14-Aug-12 0:28 
AnswerRe: How to play a video from resources? Pin
huntkao15-Aug-12 23:21
memberhuntkao15-Aug-12 23:21 
QuestionSupport for Win7? Pin
habichnicht24-Jun-12 23:21
memberhabichnicht24-Jun-12 23:21 
Questionlink with 64 bit application? Pin
Alon Geri18-Mar-12 7:08
memberAlon Geri18-Mar-12 7:08 
AnswerRe: link with 64 bit application? Pin
fen23-Mar-12 0:14
memberfen23-Mar-12 0:14 
QuestionNice app - anyone have this in if so please post. thanx Pin
bwarehouse13-Mar-12 0:18
memberbwarehouse13-Mar-12 0:18 
Question.libs included in 2.0.0 Pin
Member 85397381-Mar-12 4:46
memberMember 85397381-Mar-12 4:46 
QuestionVLC 2.0 Pin
Davide Zaccanti22-Feb-12 8:57
memberDavide Zaccanti22-Feb-12 8:57 
NewslibVLC is switching to LGPL Pin
Julien Lebot5-Feb-12 23:39
memberJulien Lebot5-Feb-12 23:39 
GeneralRe: libVLC is switching to LGPL Pin
Alex Skoruppa5-Feb-12 23:57
memberAlex Skoruppa5-Feb-12 23:57 
GeneralRe: libVLC is switching to LGPL Pin
Rene Pilon12-Mar-12 5:55
memberRene Pilon12-Mar-12 5:55 
QuestionHelp with the Streaming API for VLC ... Pin
daved900014-Dec-11 8:39
memberdaved900014-Dec-11 8:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 12 Mar 2012
Article Copyright 2009 by Alex Skoruppa
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid