Http://kb.cnblogs.com/page/69121/author: suyan010203 Source: blog Park Release Date: Reading: 329 Original article links full screen reading [favorites]
In WPF, you can play a video in two ways: mediaelement + visualbrush, and mediaplayer + videodrawing. While mediaelement scales the content of the video window to adapt to the package container when processing the video, mediaplayer does not need to manage the layout, focus, and all other element details. Therefore, the latter is more efficient than the former. Of course, modern processors do not see the obvious difference between the two. But I tried it. for the two windows of the same video in Net Framework 3.5, the frame rate of the video in one of the windows is different, that is, the video playing in one window is smooth, the other is in the form of animation, which may be caused by frame Cutting Technology in 3.5 to achieve synchronization. However, if I try to upgrade the same project to. NET Framework 4.0, this will not happen. It can be seen that Microsoft has done a lot of optimization technology under 4.0. As Microsoft said in 4.0, its efficiency has been greatly improved.
Of course, I personally recommend that you use the 4.0 framework in the video playback design, so that you can run the video playback developed by you very efficiently.Program. In this example, we use two different methods to control video playback and implement some popular video programming frameworks.
Let's take a look at several important types.:
MediatimelineClass
A timeline object that controls media timing in the same way as an animation timeline object controls animation. This type has an important method createclock (); its signature is as follows:
Public mediaclockCreateclock(); Create a New mediaclock associated with mediatimeline.
MediaclockClass
Class that maintains the timing status of the media through mediatimeline. With this feature, we can synchronize mediaelement and mediaplayer objects. To control video playback.
Public clockcontrollerController{Get;} This attribute controls playback.
VisualbrushClass
Use Visual to draw a region.
Public visualVisual{Get; set;} sets the source object.
Public transform relativetransform {Get; set;} gets or sets to apply relative coordinates to the paint brush transformation.
DrawingbrushClass
Draw a region with drawing, which can include a shape, text, video, image, or other drawing.
Method 1: mediaelement + visualbrush
Step 1: deploy visual elements: For convenience, we have placed a two-line grid control in the main form. The first line is better than the key control installation, the second line is used for video playback and reflection windows.CodeAs follows:
Code
Step 2: declare three objects at the beginning of the code file to reference the elements declared by XAML:
// Save referenced Elements
Private Frameworkelement reflectorelement;
Private Frameworkelement originelement;
Private Mediaclock clock;
Step 3: process the mediaelement object in the post code. As we can see in the XAML declarative code, we declare the mediaelement object as a resource here, in this way, elements can be used in a shared manner. Here we first reference the mediaelement object and then declare a mediaclock object. The Code is as follows:
Code
Step 4: declare a visualbrush object and use its relativetransform attribute to put the video upside down to form a reflected image.
Visualbrush brush = New Visualbrush ();
Brush. Visual = Mediaelement; // Use mediaelement object
Brush. relativetransform = New Scaletransform {scaley = - 1 , Centery = 0.5 }; // Use a visualbrush object for layout Control
Step 5: Set the transparent mask of the opacitymask.
Code
So far, we have implemented the combination of visualelement and visualbrush to achieve video playback.
Method 2: mediaplayer + drawingbrush
Step 1: Use the layout format of method 1, and the corresponding code remains unchanged.
Step 2: declare the mediaplayer object and clock control.
Code
Step 3: Define a videodrawing object and associate it with the mediaplayer object.
Code
Step 4: associate with the layout element.
Code
Step 5: now we can use the Controller object of the mediaclock object to control the start and stop of playing.
Code
Summary:Both of the above methods achieve video playback control. By associating the mediatimeline clock, we can control the implementation of any of them. As for the efficiency of the two methods, I think it is more convenient to use mediaelement, because it can be simply declared in the XAML code, while mediaplayer requires the combination of the corresponding post code, which is not as convenient as the former, because of its slightly higher efficiency, we can weigh the two methods in terms of efficiency and convenience.
The effect is as follows:
Click to download the source code: Part1, Part2, without the source code of the video file.