Embedded Windows Media Player in WPF

Source: Internet
Author: User

In the previous article, a simple media player was created in WPF 4 using the mediaelement control. This article will directly embed Windows Media Player into WPF to achieve the same effect. At first, the instance was created based on. NET Framework 4.0 and the compilation was successful, but there was a problem in F5. The system prompts that InterOP. wmplib cannot be loaded. After debugging for a long time, it has not been implemented. Finally, I switched to. Net 3.5. Is it not supported by 4.0? Interested friends can downloadCodeFurther research.

Create a project

1. Create a. NET Framework 3.5-based WPF ApplicationProgramProject: wpfwmp.

2. Create a Windows Forms control library project in the project: wmpcontrollibrary.

Create a WMP Control

Create the Windows Media Player control in wmpcontrollibrary and add Windows Media Player com to the project.

 

If you do not have the Windows Media Player control in the left-side toolbar, right-click general and choose items. Select the Windows Media Player option in the COM component list.

Drag the Windows Media Player control into the design window and set the dock to the fill filling control.

Controls:

 

After the F6 project is compiled, the following three DLL files are generated. This is the WMP control library we will use in WPF later.

Embedded WMP Control

Go back to the WPF project in the previous articleArticleThe "Open File" button and button style are retained. Add the above three DLL files, system. Windows. forms, and windowsformsintegration to the project.

Add the axwmplib namespace to XAML and replace the previous mediaelement with the axwindowsmediaplayer control. Note that the winform control is embedded in the WPF program. Therefore, place the axwindowsmediaplayer control in the <windowsformshost> label.

 <  Window  X  :  Class  = "Wpfwmp. mainwindow"  Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"  Xmlns  :  X  = "Http://schemas.microsoft.com/winfx/2006/xaml"  Xmlns  :  Mediacontrol  = "CLR-namespace: axwmplib; Assembly = axinterop. wmplib"  Title  = "WPF Media Player"  Height  = "450"  Width  = "520" Background  = "# Ff554d4d"> <  Window. Resources  > <  Style  X  :  Key  = "Btnstyle"  Targettype  = "Button"> ... ... 
</ Style > </ Window. Resources > < Stackpanel Horizontalalignment = "Center" Margin = "10"> < Border Borderthickness = "3" Background = "Black">... ...
< Windowsformshost Height = "340" Width = "450"> < Mediacontrol : Axwindowsmediaplayer X : Name = "Wpfmediaplayer"/> </ Windowsformshost > </ Border > < Button Content = "Open File" Click = "Openfile_click" Margin = "10" Width = "80" Style = "{ Staticresource Btnstyle } "/> </ Stackpanel > </ Window >

Click the "open file" button in the Windows API code pack to add a click event. The "Sample Video" folder is opened by default, and the video file is selected for automatic playback.

 Private void Openfile_click ( Object Sender,Routedeventargs E ){ Shellcontainer Selectedfolder = Null ; Selectedfolder = Knownfolders . Samplevideos As  Shellcontainer ; Commonopenfiledialog CFD = New  Commonopenfiledialog (); CFD. initialdirectoryshellcontainer = selectedfolder; CFD. ensurereadonly = True ; CFD. Filters. Add ( New Commonfiledialogfilter ( "WMV files" , "*. Wmv" ); CFD. Filters. Add ( New  Commonfiledialogfilter ( "AVI Files" , "*. Avi" ); CFD. Filters. Add ( New  Commonfiledialogfilter ( "MP3 files" , "*. MP3" )); If (CFD. showdialog () =Commonfiledialogresult . OK) {wpfmediaplayer. url = CFD. filename ;}}

Source code Download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.