In WPF, you can use MediaElement to add media playback controls to your application to complete the playback of audio and video functions. Because MediaElement belongs to UIElement, it also supports mouse and keyboard operation. This article uses the MediaElement class and the Windows API Code Pack to create a simple video player that implements some basic functionality.
First, the background logic code description
1. Select a video file
In the video File Browsing section introduced, and add WMV, AVI and other file filters.
Click for detailed reference
//Select a video filePrivate voidOpenbtn_click (Objectsender, RoutedEventArgs e) {Commonopenfiledialog Open=NewCommonopenfiledialog (); Open. Ensurereadonly=true; Open. Filters.add (NewCommonfiledialogfilter ("Mp4 File","*.mp4")); Open. Filters.add (NewCommonfiledialogfilter ("wmv File","*.wmv")); Open. Filters.add (NewCommonfiledialogfilter ("avi file","*.avi")); Open. Filters.add (NewCommonfiledialogfilter ("Mp3 File","*.mp3")); if(Open. ShowDialog () = =Commonfiledialogresult.ok) {//Specify the media file addressMediaelement.source =NewUri (open. FileName, urikind.relative); Playbtn.isenabled=true; }}
2. You can use the play ()/pause () method to control video playback or pause
//Start PlaybackPrivate voidPlaybtn_click (Objectsender, RoutedEventArgs e) {Mediaelement.play (); Mediaelement.tooltip="Start Playback";}//Stop playingPrivate voidStopbtn_click (Objectsender, RoutedEventArgs e) {Mediaelement.pause (); Mediaelement.tooltip="Stop playing";}
3. The video Progress adjustment operation is achieved by modifying the position value of the MediaElement. The time interval is set by a timespan (the following code is 10 seconds apart).
// back void Backbtn_click (object sender, RoutedEventArgs e) {mediaelement.position = Mediaelement.position-timespan.fromseconds (10 // forward private void Forwardbtn_click (object sender, RoutedEventArgs e) {mediaelement.position = mediaelement.position +timespan.fromseconds (10 );}
4. The volume adjustment section simply binds the slider value change value and the MediaElement Volume value to a simple binding.
<Height= " Width= " "max" = "mediaElement" Volume= "{Binding elementname=volumeslider,path=value}" Loadedbehavior = "Manual" />
Second, the foreground XAML code definition
<StackPanelHorizontalAlignment= "Center"Margin= " the"> <Borderborderthickness= "3"Background= "Black"> <Border.borderbrush> <LinearGradientBrushStartPoint= "0.5,0"EndPoint= "0.5,1"> <GradientStopOffset= "0"Color= "White"/> <GradientStopOffset= "0.5"Color= "Gold"/> </LinearGradientBrush> </Border.borderbrush><MediaElementHeight= "+"Width= "The "Name= "MediaElement"Volume="{Binding Elementname=volumeslider,path=value}"Loadedbehavior= "Manual" /> </Border> <StackPanelOrientation= "Horizontal"Height= "Max"HorizontalAlignment= "Center"> <Buttonx:name= "Openbtn"Content= "Open File"Style="{StaticResource Btnstyle}"Click= "Openbtn_click"Margin= "0"Width= " +"/> <Buttonx:name= "Playbtn"Content= "Play"Style="{StaticResource Btnstyle}"Click= "Playbtn_click"/> <Buttonx:name= "Stopbtn"Content= "Stop"Style="{StaticResource Btnstyle}"Click= "Stopbtn_click"/> <Buttonx:name= "Backbtn"Content= "Back"Style="{StaticResource Btnstyle}"Click= "Backbtn_click"/> <Buttonx:name= "Forwardbtn"Content= "Forward"Style="{StaticResource Btnstyle}"Click= "Forwardbtn_click"Height= "+"Margin= "5,5,5,0"VerticalAlignment= "Top"/> </StackPanel> <StackPanelOrientation= "Horizontal"HorizontalAlignment= "Center"Margin= "5"> <TextBlockText= "Volume"Foreground= "Gold"/> <Sliderx:name= "Volumeslider"Minimum= "0"Maximum= "1"Value= "0.5"Width= "$"/> </StackPanel></StackPanel>
Style resource definition
<window.resources> <Stylex:key= "Btnstyle"TargetType= "button"> <Setter Property= "Background"> <Setter.value> <LinearGradientBrushStartPoint= "0.5,0"EndPoint= "0.5,1"> <GradientStopOffset= "0"Color= "White"/> <GradientStopOffset= "0.5"Color= "#FF554D4A"/> </LinearGradientBrush> </Setter.value> </Setter> <Setter Property= "FontStyle"Value= "Italic"/> <Setter Property= "Margin"Value= "5"/> <Setter Property= "Width"Value= "$"/> <Setter Property= "Foreground"Value= "Gold"/> <style.triggers> <Trigger Property= "Button.ismouseover"Value= "True"> <Setter Property= "Foreground"Value= "Black"/> </Trigger> </style.triggers> </Style></window.resources>
Operation Result:
Reference article from: http://www.cnblogs.com/gnielee/archive/2010/05/06/wpf4-media-player-mediaelement.html
WPF Media Player (MediaElement) Usage instance (RPM)