Playing with the MediaElement
Hi everyone! This is the first post on this blog. I would like to begin with something very cool in WPF. The MediaElement is a control that really adds some fun to WPF applications. The MediaElement can be used to play either videos or music files. In this article I just will show you how to make it play videos but it really works the same way with music files.
It's about making application display videos like Window Media Player does. Actually behind that control this is Media Player. That is why - this is the first info in that article - you cannot display videos you don't get the codec of.
Now let's see how to add a MediaElement in our WPF application. The cool thing with the MediaElement is that you can use it alone or you can also set it in the background of another control. Here is the first solution:
<MediaElement
Name="me1"
ScrubbingEnabled="True"
Source="Bear.wmv"
LoadedBehavior="Manual"
SpeedRatio="2"
Volume="10" />
And there is the second solution:
<Rectangle>
<Rectangle.Fill>
<VisualBrush>
<VisualBrush.Visual>
<MediaElement
Name="me1"
ScrubbingEnabled="True"
Source="Bear.wmv"
LoadedBehavior="Manual"
SpeedRatio="2"
Volume="10" />
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
Here I used the Rectangle Fill property to use my MediaElement but you could do it with anything that has a property of type Brush; buttons, grid, menu, etc. This make the MediaElement a very convenient control. Let's talk about some properties of the MediaElement control:
- ScrubbingEnabled : Enables to update the image of the videos even when the MediaElement is in pause.
- Source : The video file to show.
- LoadedBehaviour : When in Manual mode, the MediaElement will be able to be controlled by its methods Play(), Pause(), Stop(), ... Otherwise you can put it to play in order to make the MediaElement play as soon as the video is loaded.
- SpeedRation : you can control the speed ratio of the video with this property.
- Volume : you also can control the volume of the video and so on with properties of the MediaElement.
To conclude this first article I wish you some good fun programming with the MediaElement and see how WPF makes more interactive applications.