Click here to Skip to main content
15,849,180 members
Articles / Desktop Programming / MFC

VLCWrapper - A Little C++-wrapper Around libvlc

Rate me:
Please Sign up or sign in to vote.
4.91/5 (49 votes)
12 Mar 2012CPOL3 min read 458.7K   25.8K   169   127
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. ;)

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.  

C++
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:

C++
#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():

C++
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:

C++
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:

C++
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)


Written By
Technical Lead HERE, a Nokia Business
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Could you help me for getting stream in? Pin
Alex Skoruppa14-Aug-09 6:39
Alex Skoruppa14-Aug-09 6:39 
GeneralRe: Could you help me for getting stream in? Pin
Kalle Perala14-Aug-09 21:29
Kalle Perala14-Aug-09 21:29 
GeneralRe: Could you help me for getting stream in? Pin
Kalle Perala17-Aug-09 0:04
Kalle Perala17-Aug-09 0:04 
GeneralRe: Could you help me for getting stream in? Pin
Alex Skoruppa17-Aug-09 11:00
Alex Skoruppa17-Aug-09 11:00 
GeneralRe: Could you help me for getting stream in? Pin
Member 1039945512-Apr-16 1:32
professionalMember 1039945512-Apr-16 1:32 
GeneralThanks. But your demo application doesn't play video or mp3 audio files Pin
Tage Lejon13-Aug-09 23:04
Tage Lejon13-Aug-09 23:04 
GeneralRe: Thanks. But your demo application doesn't play video or mp3 audio files Pin
Alex Skoruppa13-Aug-09 23:16
Alex Skoruppa13-Aug-09 23:16 
GeneralIt works now with Video and MP4 audio, great codes! Pin
Tage Lejon14-Aug-09 1:43
Tage Lejon14-Aug-09 1:43 
Hey Alex

Thanks a lot for your prompt explanation!

By copying DLls into a 'plugins' folder from VLC's installation,
your demo applications works well now playng videos and MP4 audio,
e.g. those of IPhone and IPod. It crashes if MP3 files are played,
however, as you have mentioend.

There are quite a few DLLs in the 'plugins': 281 pieces altogether
from VLC1.01, and they are 55.7 MB in size. I wonder if they all are
necessary for playing videos and audio on local PC, and could one
takes a subset of them for a subset of functions that VLC provides.

BR
GeneralRe: It works now with Video and MP4 audio, great codes! Pin
Alex Skoruppa14-Aug-09 2:54
Alex Skoruppa14-Aug-09 2:54 
GeneralAddition of one not does make MP3 file playable, it's nice Pin
Tage Lejon14-Aug-09 4:18
Tage Lejon14-Aug-09 4:18 
GeneralRe: Addition of one not does make MP3 file playable, it's nice Pin
Alex Skoruppa14-Aug-09 5:20
Alex Skoruppa14-Aug-09 5:20 
GeneralMore about unicode file names issues Pin
Tage Lejon14-Aug-09 13:01
Tage Lejon14-Aug-09 13:01 
GeneralRe: More about unicode file names issues Pin
Alex Skoruppa15-Aug-09 2:16
Alex Skoruppa15-Aug-09 2:16 
GeneralWhich version of VLC Pin
KL Chin13-Aug-09 17:16
KL Chin13-Aug-09 17:16 
GeneralRe: Which version of VLC Pin
Alex Skoruppa13-Aug-09 20:41
Alex Skoruppa13-Aug-09 20:41 

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.