In this post
I will demonstrate how to create VLC media player in WPF using VLC player
library.
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.FileName, null, 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 -