Monday, August 30, 2010

Include and Use VLC Player in C#

In this post I will show how to use the VLC Player in C#. This is in my opinion the best open source media player, so we can use it to play any audio or video files in our C# application.
For the VLC Player there exists an ActiveX component, which comes by as a .dll file.
So when downloading VLC, it is important to check the point "ActiveX component" (by default, this is anyway activated though).
Now an ActiveX control is installed during installation and registered on the computer. This control now can already be included in .Net. For that right click on the toolbox, select Add Elements, which prompts a windows to add new controls to open. We now switch to the tab COM controls and activate there VideoLAN VLC ActiveX Plugin v1.
If this entry is not availabe, the .dll file has to be registered manually, the right file is axvlc.dll in the VideoLAN folder.
ActiveX controls can be registered via regsvr32. Suppose the .dll file sits in C:\Program Files (x86)\VideoLAN\VLC\axvlc.dll. Then we can register it with executing regsvr32 "C:\Program Files (x86)\VideoLAN\VLC\axvlc.dll".
If the VLC control was successfully added to the toolbox, it can be added like any other to the form.

To play an arbitrary video or audiofile, the multimedia object first has to be added to Playlist, which we do with the function addTarget(). This expects 4 parameters, the first one describes the path to the object to be played. As second parameter play options can be set (as for example the width-to-height ratio). The third parameter is the playing mode and the fourth the position in the current playlist.
To play the file finally, the function play() has to be called.
pause() stops the playing, with a repeated call of play() the video / audiofile is continued from the same time.
The volume of the audio signal can be set by the parameter Volume, here 0 is no sound, 100 full volume.
The following example plays the video "Matrix" on my desktop with 50% volume.
addTarget() does not get any options, the playing mode should be self explainining and -666 as last parameter identifies the last position in the playlist.
Attention: In newer versions of the VLC player one has to append a "file:///" in front of the file path, otherwise the file does not play!

axVLCPlugin1.Volume = 50;
axVLCPlugin1.addTarget(@"C:\Users\User\Desktop\matrix.mp4"null, AXVLC.VLCPlaylistMode.VLCPlayListAppendAndGo, -666);
axVLCPlugin1.play();


Edit About this post I created a little video on youtube (in German): http://www.youtube.com/watch?v=Bh4qsHrk910

5 comments:

  1. Dear Oliver
    I am capturing video on a Camera and Using RTSP to run this video on VLC player Live. But when my video stops VLC get stucks. I have to manually reset the VLC whenever the video is ready again. I wan to build an Window based application which controls the Start and Stop of the VLC player before and after video is stuck or ready. Please tell me how can I do this in C#.

    Thanks
    Hammad Saif

    ReplyDelete
    Replies
    1. Use the C# classes provided by
      http://vlcdotnet.codeplex.com/

      Delete
    2. Hi, thanks for the advice, exactly about that I posted a couple days ago! (http://csharp-tricks-en.blogspot.de/2014/10/videolan-dotnet.html).
      And yeah, it's recommended to use this now, so hope your program works with it (although I don't quite get it I have to say).

      Delete
  2. Muhammad hamad saifJanuary 29, 2015 at 8:38 PM

    I am able to run vlc activex plugin in my Visual C# windows forms application. But I am facing problems to play streamed video after it is paused in a timer. Please tell me why does't video plays using play() after hitting pause().

    ReplyDelete
  3. I'm unable to create release using axvlcplugin2.
    Debug is working fine.

    ReplyDelete