Click here to Skip to main content
12,510,006 members (53,057 online)
Click here to Skip to main content
Add your own
alternative version

Stats

257.5K views
21K downloads
163 bookmarked
Posted

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
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.  

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionLoading libvlc.dll and libvlccore.dll at runtime? Pin
tHeWiZaRdOfDoS20-Dec-09 0:26
membertHeWiZaRdOfDoS20-Dec-09 0:26 
GeneralCrash due to libdirac_plugin.dll Pin
tHeWiZaRdOfDoS19-Dec-09 20:43
membertHeWiZaRdOfDoS19-Dec-09 20:43 
Generalcalled libvlc_video_take_snapshot does not work Pin
feilliang13-Oct-09 18:22
memberfeilliang13-Oct-09 18:22 
GeneralRe: called libvlc_video_take_snapshot does not work Pin
Alex Skoruppa23-Jun-10 19:41
memberAlex Skoruppa23-Jun-10 19:41 
GeneralAccess violation error in release mode Pin
kutti30-Sep-09 3:06
memberkutti30-Sep-09 3:06 
GeneralRe: Access violation error in release mode Pin
heko212-Oct-09 6:05
memberheko212-Oct-09 6:05 
Generallibvlccorelib and libvlc.lib Pin
kutti28-Sep-09 19:06
memberkutti28-Sep-09 19:06 
QuestionWhy I can not paly the video uses the demo? Pin
kent_fu27-Aug-09 17:10
memberkent_fu27-Aug-09 17:10 
AnswerRe: Why I can not paly the video uses the demo? Pin
Alex Skoruppa27-Aug-09 20:07
memberAlex Skoruppa27-Aug-09 20:07 
GeneralRe: Why I can not paly the video uses the demo? Pin
kent_fu27-Aug-09 23:39
memberkent_fu27-Aug-09 23:39 
GeneralWindow repaint problems Pin
haimen26-Aug-09 0:10
memberhaimen26-Aug-09 0:10 
GeneralRe: Window repaint problems Pin
Alex Skoruppa27-Aug-09 20:04
memberAlex Skoruppa27-Aug-09 20:04 
GeneralRe: Window repaint problems Pin
silviafranke3-Sep-09 11:58
membersilviafranke3-Sep-09 11:58 
GeneralRe: Window repaint problems [modified] Pin
haimen7-Dec-09 20:45
memberhaimen7-Dec-09 20:45 
I've found a solution that will prevent gray areas when overlapping the video with another window. All you need to do is just to add a line in the OnPaint event handler:

m_VLCScreen.RedrawWindow(NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);

Note: m_VLCScreen is the control that hosts the VLC Player.

modified on Tuesday, December 8, 2009 2:55 AM

GeneralRe: Window repaint problems Pin
Alex Skoruppa31-May-10 3:10
memberAlex Skoruppa31-May-10 3:10 
SuggestionRe: Window repaint problems [modified] Pin
tuhuo9-Aug-11 5:34
membertuhuo9-Aug-11 5:34 
GeneralPlaying mp4 Pin
yafan21-Aug-09 16:47
memberyafan21-Aug-09 16:47 
GeneralRe: Playing mp4 [modified] Pin
Alex Skoruppa21-Aug-09 21:04
memberAlex Skoruppa21-Aug-09 21:04 
GeneralInput from memory located video Pin
silviafranke20-Aug-09 21:19
membersilviafranke20-Aug-09 21:19 
GeneralRe: Input from memory located video Pin
Alex Skoruppa21-Aug-09 2:33
memberAlex Skoruppa21-Aug-09 2:33 
GeneralRe: Input from memory located video Pin
silviafranke21-Aug-09 2:58
membersilviafranke21-Aug-09 2:58 
GeneralRe: Input from memory located video Pin
Alex Skoruppa21-Aug-09 6:11
memberAlex Skoruppa21-Aug-09 6:11 
GeneralRe: Input from memory located video Pin
silviafranke21-Aug-09 6:34
membersilviafranke21-Aug-09 6:34 
GeneralRe: Input from memory located video Pin
martin_bisson27-Aug-09 2:02
membermartin_bisson27-Aug-09 2:02 
GeneralRe: Input from memory located video Pin
silviafranke3-Sep-09 11:42
membersilviafranke3-Sep-09 11:42 
Generallibvlc.lib and libvlccore.lib Pin
freelancer0120-Aug-09 8:46
memberfreelancer0120-Aug-09 8:46 
GeneralRe: libvlc.lib and libvlccore.lib Pin
Alex Skoruppa20-Aug-09 8:52
memberAlex Skoruppa20-Aug-09 8:52 
GeneralRe: libvlc.lib and libvlccore.lib [modified] Pin
freelancer0120-Aug-09 9:01
memberfreelancer0120-Aug-09 9:01 
GeneralRe: libvlc.lib and libvlccore.lib Pin
Alex Skoruppa20-Aug-09 20:12
memberAlex Skoruppa20-Aug-09 20:12 
GeneralRe: libvlc.lib and libvlccore.lib Pin
tHeWiZaRdOfDoS18-Dec-09 21:51
membertHeWiZaRdOfDoS18-Dec-09 21:51 
Generallibvlc_MediaPlayerTimeChanged doesn't seem to fire on some mpeg files Pin
yafan19-Aug-09 4:50
memberyafan19-Aug-09 4:50 
GeneralRe: libvlc_MediaPlayerTimeChanged doesn't seem to fire on some mpeg files Pin
silviafranke19-Aug-09 22:54
membersilviafranke19-Aug-09 22:54 
GeneralRe: libvlc_MediaPlayerTimeChanged doesn't seem to fire on some mpeg files Pin
silviafranke20-Aug-09 0:22
membersilviafranke20-Aug-09 0:22 
GeneralRe: libvlc_MediaPlayerTimeChanged doesn't seem to fire on some mpeg files Pin
yafan20-Aug-09 9:05
memberyafan20-Aug-09 9:05 
GeneralRe: libvlc_MediaPlayerTimeChanged doesn't seem to fire on some mpeg files Pin
yafan20-Aug-09 9:04
memberyafan20-Aug-09 9:04 
GeneralCommercial Use of GPL Code Pin
ajhampson18-Aug-09 5:22
memberajhampson18-Aug-09 5:22 
GeneralRe: Commercial Use of GPL Code Pin
Jim Crafton18-Aug-09 6:42
memberJim Crafton18-Aug-09 6:42 
GeneralRe: Commercial Use of GPL Code Pin
Alex Skoruppa18-Aug-09 7:15
memberAlex Skoruppa18-Aug-09 7:15 
GeneralThanks Pin
sinan kul17-Aug-09 21:19
membersinan kul17-Aug-09 21:19 
GeneralRe: Thanks Pin
Alex Skoruppa18-Aug-09 1:05
memberAlex Skoruppa18-Aug-09 1:05 
GeneralRe: Thanks Pin
sinan kul21-Aug-09 2:51
membersinan kul21-Aug-09 2:51 
QuestionCould you help me for getting stream in? Pin
Kalle Perala14-Aug-09 4:11
memberKalle Perala14-Aug-09 4:11 
AnswerRe: Could you help me for getting stream in? Pin
Alex Skoruppa14-Aug-09 5:39
memberAlex Skoruppa14-Aug-09 5:39 
GeneralRe: Could you help me for getting stream in? Pin
Kalle Perala14-Aug-09 20:29
memberKalle Perala14-Aug-09 20:29 
GeneralRe: Could you help me for getting stream in? Pin
Kalle Perala16-Aug-09 23:04
memberKalle Perala16-Aug-09 23:04 
GeneralRe: Could you help me for getting stream in? Pin
Alex Skoruppa17-Aug-09 10:00
memberAlex Skoruppa17-Aug-09 10:00 
GeneralRe: Could you help me for getting stream in? Pin
Member 1039945512-Apr-16 0:32
professionalMember 1039945512-Apr-16 0:32 
GeneralThanks. But your demo application doesn't play video or mp3 audio files Pin
Tage Lejon13-Aug-09 22:04
memberTage Lejon13-Aug-09 22:04 
GeneralRe: Thanks. But your demo application doesn't play video or mp3 audio files Pin
Alex Skoruppa13-Aug-09 22:16
memberAlex Skoruppa13-Aug-09 22:16 
GeneralIt works now with Video and MP4 audio, great codes! Pin
Tage Lejon14-Aug-09 0:43
memberTage Lejon14-Aug-09 0:43 

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
Web02 | 2.8.160929.1 | Last Updated 12 Mar 2012
Article Copyright 2009 by Alex Skoruppa
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid