WPF MediaElement control in VB.NET

In this article we will learn how to use MediaElement control in WPF 4 using VB.NET.
  • 5069

In this article we will learn how to use MediaElement control in WPF using VB.NET.

MediaElement control

MediaElement control is used to Represent an object that contains audio, video, or both.

Properties - This control has the following properties.

me1.gif
 

Figure 1.

Source - The Source property specifies name of the media file to be played immediately after the MediaElement object has loaded.

WPF also supports WMA media.

WMV (Windows Media video)

Short for Windows Media video, a Microsoft file format for encoding digital video files similar to video file though can compress files at a higher rate than video file. WMA files, which use the .wmv file extension, can be of any size compressed to match many different connection speeds, or bandwidths. You can download it with the given below link.

Download WMV Converter

Creating MediaElement in XAML

<MediaElement Height="120" HorizontalAlignment="Left" Margin="10,10,0,0"Name="MediaElement1" VerticalAlignment="Top" Width="160" />

 

The Width and Height property represents the width and the height of the control. Name property represents name of the control.

 

How to set Video file

 

Source property
me6.gif
 

Figure 2.

The Source attribute of the tag takes the full path of the video.

The following code snippet sets the Source property to a .wmv file.

<MediaElement Source="E:\BOLLY WOOD VIDEO\song.wmv" />

For Example

Drag and drop a MediaElement and three Button controls on the page and name them mediaElement1, Play Button, Pause Button. The form looks like this.

me2.gif

Figure 3.

XAML code

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">

    <Grid x:Name="LayoutRoot" Background="White" Width="460" Height="360">

        <MediaElement Height="238" HorizontalAlignment="Left" Margin="12,12,0,0"Name="mediaElement1"

                      VerticalAlignment="Top" Width="388" Volume="1" LoadedBehavior="Manual"UnloadedBehavior="Manual" Source="E:\BOLLY WOOD VIDEO\song.wmv" />

        <Button Content="Play" Height="28" HorizontalAlignment="Left" Margin="12,260,0,0"

                Name="PlayButton" VerticalAlignment="Top" Width="88" Click="PlayButton_Click" />

        <Button Content="Pause" Height="30" HorizontalAlignment="Right" Margin="0,258,255,0"

                Name="PauseButto" VerticalAlignment="Top" Width="85" Click="PauseButto_Click" />

    </Grid>

</Window>

 

Now double click on the play button and add the following code.

Private Sub PlayButton_Click(ByVal sender As System.Object,ByVal eAsSystem.Windows.RoutedEventArgs)

        Me.mediaElement1.Play()

    End Sub

Now double click on the pause button and add the following code.

Private Sub PauseButto_Click(ByVal sender As System.Object,ByVal eAsSystem.Windows.RoutedEventArgs)

        Me.mediaElement1.Stop()

    End Sub

Now run the application press CTRL+F5.

me3.gif
 

Figure 4.

 

Now to play the video click on the play Button.  


 

me4.gif 

Figure 5.

 

To Pause the Video click on the Pause Button.

 

me5.gif
  

Figure 6.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.