Using the VLC ActiveX control in WPF

How to integrate the powerful VLC media player into your WPF application

Prerequisites:

  • Windows version of VLC Media Player with the ActiveX component installed.
  • Microsoft Visual Studio 2010 professional / Microsoft Visual C# 2010 Express (Don’t know if this method works on older versions).

Instructions:

  1. If you are not adding this to a current project then create a Visual C# > Windows > WPF Application.
  2. Add a User Control to your project (Project > Add New Item > Visual C# > User Control).
  3. Open the Toolbox (if you can’t see this: View > Toolbox).
  4. Right Click anywhere on the Toolbox then click on Choose Items. (This may take a while to open)
  5. Navigate to the COM Components tab, find and check “VideoLAN VLC ActiveX plugin v1” (axvlc.dell) and press OK.
  6. Now in your Toolbox you should see “VideoLAN VLC ActiveX plugin v1”. Double click on it and set its Dock Property to “Fill”
  7. Now save an build your project (Build > Build Solution.. or press F6)
  8. In the Solution Explorer under References you should now see “AXVLC” and “AxAXVLC”. if you don’t add them manually by right clicking on References, go into your projects debug folder and add “AxInterop.AXVLC.dll” and “Interop.AXVLC.dll”.
  9. Right clicking on References in the Solution Explorer, then Add Reference; Browse to the .NET Tab and find and select “WindowsFormsIntegration”, then press OK.
  10. Now in MainWindow.xaml (or where ever you want to add it to), Add a name to your grid (or the container)… i named mine “grid1”.
  11. From the toolbox add a WindowsFormsHost to the grid, and name it. I named mine “WFH1”  (see Figure 1)
  12. Now right click on your main window and click on View Code.
  13. Declare “AxAXVLC.AxVLCPlugin” as a global variable. (AxAXVLC.AxVLCPlugin vlcPlayer = new AxAXVLC.AxVLCPlugin();
  14. in your default constructor add the player object as the child of the WindowsFormsHost (WFH1.Child = vlcPlayer;)
  15. And that’s it! you wont see anything when you run the program, but the player is there, i will post a code snippet below so you can get a file to open and play in it (Figure 2).
  16. See http://wiki.videolan.org/ActiveX for instructions on how to use the VLC media player ActiveX Plugin.

UPDATE 16-OCT-2011: (List of Fields/Methods/Events of the Plug-in)
Here are a list of fields/methods/events that should allow you to have a lot of control over the plug-in:
Fields

  • bool AutoLoop { get; set; }
  • bool AutoPlay { get; set; }
  • bool CtlVisible { get; set; }
  • int Length { get; }
  • string MRL { get; set; }
  • bool Playing { get; }
  • int PlaylistCount { get; }
  • int PlaylistIndex { get; }
  • float Position { get; set; }
  • int Time { get; set; }
  • string VersionInfo { get; }
  • int Volume { get; set; }

Events

  • MediaPlayerBackward;
  • MediaPlayerBuffering;
  • MediaPlayerEncounteredError;
  • MediaPlayerEndReached;
  • MediaPlayerForward;
  • MediaPlayerNothingSpecial;
  • MediaPlayerOpening;
  • MediaPlayerPausableChanged;
  • MediaPlayerPaused;
  • MediaPlayerPlaying;
  • MediaPlayerPositionChanged;
  • MediaPlayerSeekableChanged;
  • MediaPlayerStopped;
  • MediaPlayerTimeChanged;
  • pauseEvent;
  • playEvent;
  • stopEvent;

Methods

  • void addTarget(string uri, object options, VLCPlaylistMode mode, int position);
  • void AttachInterfaces();
  • void CreateSink();
  • void DetachSink();
  • void fullscreen();
  • object getVariable(string name);
  • void pause();
  • void play();
  • void playFaster();
  • void playlistClear();
  • void playlistNext();
  • void playlistPrev();
  • void playSlower();
  • void setVariable(string name, object value);
  • void shuttle(int seconds);
  • void stop();
  • void toggleMute();
Figuare 1

Example of further useage:

I just added a button to my application to open allow the user to select a file and play it in the player:

[Updated 08 Oct 2012, courtesy of bielb89 from YouTube]

VLC V2.x.x.x +You will need to add “file:///” +before the Url for the file.

Figure 2.. Click on the more button to see a written version of the code.. [This Picture is outdated]
using System.Windows;
using System.Windows.Forms;

namespace WPFVLC
{
    ///
<summary> /// Interaction logic for MainWindow.xaml
 /// </summary>
    public partial class MainWindow : Window
    {
        AxAXVLC.AxVLCPlugin vlcPlayer = new AxAXVLC.AxVLCPlugin();
        public MainWindow()
        {
            InitializeComponent();
            WFH1.Child = vlcPlayer;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string mrl = "";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            if (ofd.FileName != "")
            {
                mrl = ofd.FileName;
                vlcPlayer.addTarget("file:///" +mrl, null, AXVLC.VLCPlaylistMode.VLCPlayListReplaceAndGo, 0);
                vlcPlayer.play();
            }
        }
    }
}

29 thoughts on “Using the VLC ActiveX control in WPF

    1. @ANAS
      As far as I’m aware, VLC doesn’t allow command line args for security reasons.
      Here are a list of fields/methods/events that should allow you to have a lot of control over the plug-in:
      Fields
      bool AutoLoop { get; set; }
      bool AutoPlay { get; set; }
      bool CtlVisible { get; set; }
      int Length { get; }
      string MRL { get; set; }
      bool Playing { get; }
      int PlaylistCount { get; }
      int PlaylistIndex { get; }
      float Position { get; set; }
      int Time { get; set; }
      string VersionInfo { get; }
      int Volume { get; set; }

      Events
      MediaPlayerBackward;
      MediaPlayerBuffering;
      MediaPlayerEncounteredError;
      MediaPlayerEndReached;
      MediaPlayerForward;
      MediaPlayerNothingSpecial;
      MediaPlayerOpening;
      MediaPlayerPausableChanged;
      MediaPlayerPaused;
      MediaPlayerPlaying;
      MediaPlayerPositionChanged;
      MediaPlayerSeekableChanged;
      MediaPlayerStopped;
      MediaPlayerTimeChanged;
      pauseEvent;
      playEvent;
      stopEvent;
      Methods
      void addTarget(string uri, object options, VLCPlaylistMode mode, int position);
      void AttachInterfaces();
      void CreateSink();
      void DetachSink();
      void fullscreen();
      object getVariable(string name);
      void pause();
      void play();
      void playFaster();
      void playlistClear();
      void playlistNext();
      void playlistPrev();
      void playSlower();
      void setVariable(string name, object value);
      void shuttle(int seconds);
      void stop();
      void toggleMute();

      -Zahid

  1. @Nav

    Its pretty simple.
    To get the position to a slider, add a timer and put this in its tick event:
    void seekSliderUpdater_Tick(object sender, EventArgs e)
    {
    if (mediaPlayer_vlc.Playing)
    {
    slider_Seek.Value = mediaPlayer_vlc.Time;
    }
    }

    To seek the video using the slider, add this to the slider’s value Changed property:

    slider_Seek.ValueChanged += (s, e) =>
    {
    if (mediaPlayer_vlc.Playing == false)
    {
    mediaPlayer_vlc.Position = (float)(slider_Seek.Value / slider_Seek.Maximum);
    }
    };

      1. Can you please give complete code for add seekbar in c# for vlc 1.1.7 using c# winforms

  2. I have two questions. I’ve added v2 of the player and followed the example. Can open video using my C# app no problem. First question, how do I add a play control to my player? Second question, I want to play the video faster…so I created a button that triggers vlc.playFaster(), only problem is it only works once (i.e. if I click it multiple times, it doesn’t get faster). How do get the video to play 4x or 6x faster…and then back to normal speed if necessary?

    thank you.

    1. for the play button, put in [yourVlCPlayerName].play();

      As for the play faster/slower problem, i dont think there is a way around it. the ACTIVE X plugin isn’t meant to be a fully functional player.

  3. Hi, great post, very helpful. Have one problem, I’m trying to get the video to go fullscreen when it is double clicked. I just cant get it to work. I put in the vlcplayer.fullscreen(); but it does nothing, any help please?

    1. Read the updated post it may help:

      [Updated 08 Oct 2012, courtesy of bielb89 from YouTube]

      VLC V2.x.x.x +You will need to add “”file:///” +” before the Url for the file.

  4. Hello, we are students from Belgium and working on timecoding in VLC.

    we are trying to play a video and to get it to work. But the video wont play. I put “file:///” in front of it, but still wont play it.

    We also used a direct link instead of using a OpenFileDialog.

    Is it maybe possible to send us your project in mail? We really could use it.

    Thanks in advance,

    Dries

  5. I am not able to find Active X (COM Components) on Windows 8, with Visual Studio 2012.
    VLC Player Version 2.0.5

    Can you please help in this regard.

  6. HI,
    thanks for this great tut. I’m wondering how can i implement an event to this. for example I need a function to be called when I get the player playing. Can you help me with that?

    1. I did some research on this, this is because these functions were never fully implemented. The best you could do is use workarounds and make your own events which will attempt to mimic the video player’s events.

  7. hi ..
    can I use it without installing vlc on the target machine ?
    can’t I just distribute the activeX components with my application without the need to install vlc on each machine I want to run my app on ?
    thanks in advance

  8. Hi Zahid,

    I really like how you set out and explained how to embed vlc, and I am finding it is working very well. My main problem is being able to see what audio and subtitle tracks are available. I have been trying to use the GetVariable(“audio-track”) method, but seem to be doing it incorrectly. Do you have any advice? I want to provide an option to the user to enable them to select which track to play.

  9. Please tell me how to Run Videos from VLC using C# Windows Forms Application. I have written a code but VLC is not showing any video after it gets invoked.

Leave a reply to Zahid Cancel reply