Videos in WPF -- (2)

Source: Internet
Author: User

because mediaplayer designed for Program Code , to mediaplayer the loaded media can be displayed. required. Use videodrawing or drawingcontext .

VideodrawingClass is used to draw the playing media to the specified rectangle. PassMediaplayerTo open and play a media fileMediaplayerObject assignedVideodrawingOfPlayerYou can use this attribute to draw videos.

The front-end code is as follows:

<Grid>

<Grid. Background>

<Drawingbrush>

<Drawingbrush. Drawing>

<Videodrawing X: Name = "video" rect = "0 0 1920 1080"/>

</Drawingbrush. Drawing>

</Drawingbrush>

</Grid. Background>

</GRID>

The background code is as follows:

Void windowreceivloaded (Object sender, routedeventargs E)

{

Mediaplayer player = new mediaplayer ();

Player. Open (New uri (@ "C:" users "public" videos "sample Videos" bear. wmv "));

Video. Player = player;

Player. Play ();

}

Performance description:VideodrawingFeatures providedMediaelementLess:VideodrawingLayout, input, or focus is not supported. However, becauseVideodrawingThese features are not supported, so it has some performance advantages, and makes these advantages especially suitable for plotting background and clip art and usingVisualObject for low-level painting.

Videodrawing And Mediaplayer The first method for playing video files together is described above. Another method is to create Mediatimeline , And Mediaplayer And Videodrawing . The difference between the two is: the first method Mediaplayer Work in independent mode, you can use your own interactive method ( Play , Pause , Stop ); Method 2 Mediaplayer Working in clock mode, need to use Mediatimeline Created Mediaclock Of Controller Interactive Response Clockcontroller Controls media playback and allows you to perform more time control on the media, including specifying whether to play the video repeatedly, the playback Time, And the start time.

The second method is used to play the video. The front-end code is the same. The background code is as follows:

Void windowreceivloaded (Object sender, routedeventargs E)

{

Mediatimeline timeline = new mediatimeline (New uri (@ "C:" users "public" videos "sample Videos" bear. wmv "));

Timeline. repeatbehavior = repeatbehavior. forever;

Mediaclock clock = timeline. createclock ();

Mediaplayer player = new mediaplayer ();

Player. Clock = clock;

Video. Player = player;

Player. Clock. Controller. Begin ();

}

DrawingcontextUsed to retain the mode of the graphics system, useDrawingcontextTo draw the object. Draw a series of rendering commands for later use by the graphic system, rather than drawing on the screen in real time. Never instantiate directlyDrawingcontextObject, you need to useDrawingvisual. renderopenOrDrawinggroup. OpenTo obtain the drawing context.

UseDrawingvisual. renderopenHour:

Front-end code:

<Grid>

<Grid. Background>

<Visualbrush X: Name = "brush"/>

</Grid. Background>

</GRID>

Background code:

Void windowreceivloaded (Object sender, routedeventargs E)

{

Drawingvisual visual = new drawingvisual ();

 

Mediaplayer play = new mediaplayer ();

Play. Open (New uri (@ "C:" users "public" videos "sample Videos" bear. wmv "));

Drawingcontext Dc = visual. renderopen ();

DC. drawvideo (play, new rect (0, 0, 1920,108 0 ));

Play. Play ();

DC. Close ();

Brush. Visual = visual;

}

 

UseDrawinggroup. OpenHour:

Front-end code:

<Grid>

<Grid. Background>

<Drawingbrush X: Name = "brush"/>

</Grid. Background>

</GRID>

Background code:

Void windowreceivloaded (Object sender, routedeventargs E)

{

Drawinggroup group = new drawinggroup ();

Drawingcontext Dc = group. open ();

Mediaplayer play = new mediaplayer ();

Play. Open (New uri (@ "C:" users "public" videos "sample Videos" bear. wmv "));

DC. drawvideo (play, new rect (0, 0, 1920,108 0 ));

Play. Play ();

DC. Close ();

Brush. Drawing = group;

}

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.