WPF Media Player (MediaElement) instance for Progress and volume control

Source: Internet
Author: User

WPF Video audio playback controls MediaElement implementation Progress control, volume control instances

Description

The 1.Volume controls the size of the volume, double type, and implements the property dependency, which can be used for two-way binding; in 0 and 1. The number of media is represented by the linear layer between. The default value is 0.5.

2.Position Gets or sets the current playback location, the timespan type, without implementing the property dependency and cannot be used for two-way binding .

3. The total length of time to get the media files needs to be set in the Mediaopended event

Note that there is a comment on Naturalduration in MSDN: naturalduration is incorrect before the MediaOpened event is raised.

Therefore mePlayer.NaturalDuration.TimeSpan.TotalSeconds , it cannot be called in a constructor or in any other method before the MediaOpened event.

XAML Code

<Grid>    <grid.rowdefinitions>        <RowDefinitionHeight= "180*"/>        <RowDefinitionHeight= "89*"/>    </grid.rowdefinitions>    <MediaElementx:name= "MediaElement"Loadedbehavior= "Manual"Volume="{Binding Elementname=slidervolume,path=value}"Source= "F:\MyDocument\ Video \coolui concept chapter mp4"MediaOpened= "mediaelement_mediaopened"HorizontalAlignment= "Left"Height= "175"Margin= "7,20,0,0"VerticalAlignment= "Top"Width= "275"Grid.rowspan= "2"/>            <Buttonx:name= "button"Content= "Play"HorizontalAlignment= "Left"Margin= "10,31,0,0"Grid.Row= "1"VerticalAlignment= "Top"Width= "$"Rendertransformorigin= "0.333,-0.526"Height= "+"Click= "Button_Click"/>    <Labelx:name= "Label"Content= "Volume:"HorizontalAlignment= "Left"Margin= "73,50,0,0"Grid.Row= "1"VerticalAlignment= "Top"Height= "+"Width= "$"/>    <Sliderx:name= "Slidervolume"Minimum= "0"Maximum= "1"Value= "0.5"HorizontalAlignment= "Left"Margin= "119,52,0,0"Grid.Row= "1"VerticalAlignment= "Top"Width= "164"Height= "+"/>    <Labelx:name= "Label1"Content= "Progress:"HorizontalAlignment= "Left"Margin= "73,21,0,0"Grid.Row= "1"VerticalAlignment= "Top"Height= "+"Width= "$"/>    <Sliderx:name= "Sliderposition"valuechanged= "Sliderposition_valuechanged"HorizontalAlignment= "Left"Margin= "119,23,0,0"Grid.Row= "1"VerticalAlignment= "Top"Width= "164"Height= "+"/>    <Labelx:name= "Label2"Content="{Binding Elementname=sliderposition,path=value}"HorizontalAlignment= "Left"Margin= "140,6,0,0"VerticalAlignment= "Top"Height= "All"Width= "The "Grid.Row= "1"/></Grid>

C # backend Code:

Use the timer to control the state of the media file if the interface modifies the position

//Play buttonPrivate voidButton_Click (Objectsender, RoutedEventArgs e) {    if(button.) content.tostring () = ="Play") {button. Content="Pause";    Mediaelement.play (); }    Else{button. Content="Play";    Mediaelement.pause (); }}dispatchertimer Timer=NULL;Private voidMediaelement_mediaopened (Objectsender, RoutedEventArgs e) {Sliderposition.maximum=MediaElement.NaturalDuration.TimeSpan.TotalSeconds; //media file opened successfullyTimer =NewDispatcherTimer (); Timer. Interval= Timespan.fromseconds (1); Timer. Tick+=NewEventHandler (Timer_tick); Timer. Start ();}Private voidTimer_tick (Objectsender, EventArgs e) {Sliderposition.value=mediaElement.Position.TotalSeconds;}//control the location of the videoPrivate voidSliderposition_valuechanged (Objectsender, routedpropertychangedeventargs<Double>e) {    //mediaelement.stop ();Mediaelement.position =timespan.fromseconds (Sliderposition.value); //Mediaelement.play ();}

WPF Media Player (MediaElement) instance for Progress and volume control

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.