Saturday, November 12, 2011

Creating Simple Video Player using VLC Active X Control in WPF


In this post I will demonstrate how to create VLC media player in WPF using VLC player library.

To use VLC player API, VLC player must be install in machine. VLC Player can be freely download at http://www.videolan.org/vlc/download-windows.html.

Follow below steps to create VLC player in WPF

1. Install VLC player. (To download VLC player use above link)
2. Create simple WPF Application in VS 2010.
3. Add reference to VLC Active X Plugin

4. Add reference to WindowsFormsIntegration Library

5. Add new custom control.

6. Drag and Drop VLC Player control from toolbar to newly created custom control

7. Write blow code to your Mainwindow.xaml file
<Window x:Class="VLCPlayerWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition  Height="0.31*"/>
        </Grid.RowDefinitions>
        <WindowsFormsHost Name="windowsFormsHost1"
                          Height="199" Grid.Row="0"
                          Grid.ColumnSpan="3"
                          HorizontalAlignment="Left"
                          VerticalAlignment="Top"
                          Width="278" />
        <Button Name="OpenButton" Content="Open"
                Grid.Row="1" Grid.Column="0"
                Width="80" Height="40"
                Click="OpenButton_Click" />
        <Button Name="PlayButton" Content="Play"
                Grid.Row="1" Grid.Column="1"
                Width="80" Height="40"
                Click="PlayButton_Click" />
        <Button Name="PauseButton" Content="Pause"
                Grid.Row="1" Grid.Column="2"
                Width="80" Height="40"
                Click="PauseButton_Click" />
    </Grid>
</Window>

8. Write below code to MainWindow.xaml.cs file
public partial class MainWindow : Window
{
    AxVLCPlugin vlc;
    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(VLCPlayer_Loaded);
    }
    void VLCPlayer_Loaded(object sender, RoutedEventArgs e)
    {
        vlc = new AxVLCPlugin();
        windowsFormsHost1.Child = vlc;
    }

    private void OpenButton_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.DefaultExt = ".avi";

        Nullable<bool> dialogResult = dlg.ShowDialog();

        if (dialogResult == true)
        {
            vlc.addTarget(dlg.FileName, null
                AXVLC.VLCPlaylistMode.VLCPlayListReplaceAndGo, 0);
            vlc.play();
        }
    }

    private void PlayButton_Click(object sender, RoutedEventArgs e)
    {
        vlc.play();
    }

    private void PauseButton_Click(object sender, RoutedEventArgs e)
    {
        if (vlc.Playing)
            vlc.pause();
    }
}

9. Run the application and see output




As demonstrated in above example, VLC ActiveX is used to created VLC player in WPF Application. WindowsFormsIntigration used to host custom control. I have created one custom contol and added VLC Active X library to it. In MainWindow, i have added WindowsFormsHost and also set VLC Player instance as child of WindowsFormsHost.
XAML
<WindowsFormsHost Name="windowsFormsHost1"
                          Height="199" Grid.Row="0"
                          Grid.ColumnSpan="3"
                          HorizontalAlignment="Left"
                          VerticalAlignment="Top"
                          Width="278" />

C#
vlc = new AxVLCPlugin();
windowsFormsHost1.Child = vlc;


I have also used Win32 FileOpenDialog box on Open button click event to open and select media file from local drive.

OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".avi";

Nullable<bool> dialogResult = dlg.ShowDialog();

if (dialogResult == true)
{
     vlc.addTarget(dlg.FileNamenull, AXVLC.VLCPlaylistMode.VLCPlayListReplaceAndGo, 0);
     vlc.play();
}


For more information about VLC ActiveX, refer to wiki.videolan.org.

Hope you liked this article. Please leave your comments/feedback in comments section of this post.

See Also - 

7 comments:

  1. Have you tried Vlc.DotNet for Wpf ?
    http://vlcdotnet.codeplex.com

    ReplyDelete
  2. Hi,
    what is vlc.addTarget? where can i get this function? may i need to add ny asembly?

    ReplyDelete
  3. Hi, Can you give me code that I can run it because when I do as you said but It didn't work, please!

    ReplyDelete
    Replies
    1. Hi,

      Sorry for delay, I have written all the code in this post.
      If still not working then you might not added UserControl. Make sure you have created UserControl with VLC Player Active X component. Now add this user control to XAML file but make sure that you add this UserControl inside WindowsFormsHost.

      Hope this will help you.

      Delete
  4. Hi:
    i have add the UserControl. but the Player ActiveX can not display.
    Thanks for you replay.

    ReplyDelete
  5. I am getting no Error : But cant't see the video playing.
    Can you give an idea what actually the problem would be.

    ReplyDelete