.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> WPF >> Post New Resource Bookmark and Share

 Subscribe to Articles

Add Windows media Player to WPF Application

Posted By :Diptimaya Patra      Posted Date :30/05/2010   Points :25   Category: WPF    URL: http://dpatra.blogspot.com

Add Windows media Player to WPF Application. In this article we will see how we can use Windows Media Player's COM and its ActiveX Control in WPF.
 


Creating a WPF Project

Fire up Visual Studio 2008, create a Blank Visual Studio Solution, and name it as WPFCOM.

Clipboard01.gif

Let's add a WPF Application project into the solution and name it as WMPInWPF.

Clipboard02.gif

Let's forget that we have added the WPF Application to the project for some time.

Now let's add a Windows Forms Control Library, and name it as MediaLibrary.

Clipboard03.gif

Now we need to follow exact steps:

1) Add Reference to "wmp.dll" in COM tab.

Clipboard04.gif

After you have added the dll into the MediaLibrary project the following structure is modified.

Clipboard05.gif

Now build the project and add the component to the Toolbar, by Right clicking on the Toolbox and select Add Tab, give name as COM Controls.

Clipboard06.gif

As you see above add dll by selecting Choose Items, select the same DLL as you have imported to the project that is, wmp.dll

Clipboard07.gif

After you press OK on the above dialog box you would see that the Windows Media Player Control is added to the Toolbox.

Clipboard08.gif

As you see in above figure we have Windows Media Player control, just drag and drop in the User Control's designer surface.

You would see something similar to the following figure.

Clipboard09.gif

Now we would make it Dock in the center so that it would fill in properly in the User Control.

Clipboard10.gif

And it would fill the User Control as displayed below:

Clipboard11.gif

Now another important step is to build the project again.

As soon as you build the project the related DLL's are created in the BIN folder of the project:

Clipboard12.gif

Now we achieved successfully getting these DLL's and designing the User Control.

Now before proceeding further let's just change the name of the UserControl to something useful, such as WMPControl.

Clipboard13.gif

Now we need to have the project reference in the WPF Application project.

Clipboard14.gif

And most importantly the Browse through the output folder of the UserControl project, and the following DLL.

Clipboard15.gif

Now that you have added it, you need to add two more DLL's such as System.Windows.Forms and WindowsFormsIntegration. As displayed below:

Clipboard16.gif

Now that we have everything ready to proceed for the design, let's build the solution once again to check everything in place.

So solution was built successfully; now we will design our WPF Application. Let's do this in the following way:

We will have a Browse Button that will add a folder to the application and playable media files onto a ListBox. When ListBox item is selected the media will change and it should start playing.

Clipboard17.gif

As you see in above figure we have successfully added a Button and a ListBox in the Leftside of the Window, Now as you can see above in Green Border we would place our UserControl.

But, before that we need refer some assemblies in the XAML so that the UserControl would be accessible in XAML. Let's do it.

Clipboard18.gif

Code view of the above image
<Window x:Class="WMPInWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:windowForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    xmlns:activeXControl="clr-namespace:AxWMPLib;assembly=AxInterop.WMPLib"
    Title="Windows Media Player In WPF" Height="378" Width="544">
    <Grid Background="Black">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="134*" />
            <ColumnDefinition Width="388*" />
        </Grid.ColumnDefinitions>
        <Button Height="23" x:Name="btnBrowse" 
                Click="btnBrowse_Click" Width="100" 
                Content="Browse" Margin="0,3,0,0" 
                VerticalAlignment="Top"/>
        <ListBox Margin="0,32,0,0" Background="{x:Null}" 
                 SelectionChanged="MediaChanged"
                 x:Name="lbMediaFiles" Foreground="White" />
        <WindowsFormsHost x:Name="winFormsHost" Grid.Column="1">
            <activeXControl:AxWindowsMediaPlayer x:Name="activeXMediaPlayer"/>
        </WindowsFormsHost>
        
    </Grid>
</Window>

Now that we have the UserControl adde to the XAML we can proceed towards adding the control inside the Grid.

Clipboard19.gif

code view of the above image
<Window x:Class="WMPInWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:windowForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    xmlns:activeXControl="clr-namespace:AxWMPLib;assembly=AxInterop.WMPLib"
    Title="Windows Media Player In WPF" Height="378" Width="544">
    <Grid Background="Black">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="134*" />
            <ColumnDefinition Width="388*" />
        </Grid.ColumnDefinitions>
        <Button Height="23" x:Name="btnBrowse" 
                Click="btnBrowse_Click" Width="100" 
                Content="Browse" Margin="0,3,0,0" 
                VerticalAlignment="Top"/>
        <ListBox Margin="0,32,0,0" Background="{x:Null}" 
                 SelectionChanged="MediaChanged"
                 x:Name="lbMediaFiles" Foreground="White" />
        <WindowsFormsHost x:Name="winFormsHost" Grid.Column="1">
            <activeXControl:AxWindowsMediaPlayer x:Name="activeXMediaPlayer"/>
        </WindowsFormsHost>
        
    </Grid>
</Window>

The design would look like the following:

Clipboard20.gif

Now let's do some coding for the SelectionChanged event of the ListBox and the Button Click of the Browse Button.

Add the following code for Browsing a folder and list the media files in the ListBox.

Clipboard21.gif

code view of the above image
List<string> mediaFileList;
string mediaFolder = string.Empty;

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
    mediaFileList = new List<string>();
    System.Windows.Forms.FolderBrowserDialog fbd = new 
        System.Windows.Forms.FolderBrowserDialog();
    if (fbd.ShowDialog()!= System.Windows.Forms.DialogResult.Cancel)
    {
        mediaFolder = fbd.SelectedPath;
        DirectoryInfo dir = new DirectoryInfo(mediaFolder);
        foreach (FileInfo file in dir.GetFiles("*.*", SearchOption.AllDirectories))
        {
            if (file.Extension == ".wmv" || file.Extension == ".avi")
            {
                mediaFileList.Add(file.Name);
            }
        }

        if (mediaFileList != null)
        {
            lbMediaFiles.ItemsSource = null;
            lbMediaFiles.ItemsSource = mediaFileList;
        }
    }
}

Now on SelectionChanged event handler of the ListBox we would open the media selected. Add the following code:

Clipboard22.gif

code view of the above image
private void MediaChanged(object sender, SelectionChangedEventArgs e)
{
    if (lbMediaFiles.SelectedIndex != -1)
    {
        AxWMPLib.AxWindowsMediaPlayer axWmp =
           winFormsHost.Child as AxWMPLib.AxWindowsMediaPlayer;
        activeXMediaPlayer.URL = mediaFolder + 
                                 "\\" + 
                                 lbMediaFiles.SelectedItem.ToString();
    }
}

That's it. Let's run the application to have test ride.

Clipboard23.gif

The above figure is the FolderBrowserDialog, opened when clicked for Browse.

Clipboard24.gif

The media files are selected on the left hand side after Folder Path selection.

Clipboard25.gif

When the media item is selected from the ListBox, it would start playing.

You can also download the sample project used in the above example.

Hope this article helps.



Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses
Author: Nikhil Kumar         Company URL: http://fast-get.com
Posted Date: 31/05/2010

nice explain... step by step ...
perfect article.

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend