BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Media Controls - MediaElement - Media Information

The eighty-seventh part of the Windows Presentation Foundation Fundamentals tutorial continues to look at the MediaElement control. This article describes some properties that provide information about the opened media.

MediaElement

In the last two articles we looked at the MediaElement control. This control allows you show video or play audio files or streams. In the first article describing the control we looked at the basic methods and properties required to play media, by creating a basic media player. In the second we described the events that allow you to detect when media is opened or finishes playing, or when there is an error during playback.

In this article we'll look at several MediaElement properties that allow you to obtain information about the opened media. This includes details about the duration of the clip, the type of media, the dimensions of video and the playback position.

To demonstrate the properties we'll add features to the media player project. If you don't have the source code, you can download it using the link at the top of the previous article.

Media Duration

The first property that we'll consider is NaturalDuration. This returns the length of the media that has been opened in the MediaElement control. It is called NaturalDuration because it returns the raw length of the media, not taking into account the speed of playback set in SpeedRatio. We can use it to display the overall length of the file that the user loads into the example media player.

NaturalDuration holds a value in a Duration structure. It cannot be used until the media has been opened and the MediaOpened event has been raised. You can check if a duration is available by checking the Boolean HasTimeSpan property. If this is true, the duration can be obtained from the TimeSpan property. As the name suggests, this returns a TimeSpan value.

Let's add a TextBlock to show the duration of the clip. Add the following XAML directly after the closing Border tag. This positions the text at the bottom left of the first cell in the Grid. It will be superimposed over the MediaElement.

<TextBlock Name="MediaInformation"
                 VerticalAlignment="Bottom"
                 HorizontalAlignment="Left"
                 Margin="3"
                 Background="#404040"/>

As the duration is only available after the media is opened, we'll show it during the MediaOpened event. Modify the code for that event as shown below:

private void Media_MediaOpened(object sender, RoutedEventArgs e)
{
    Status.Fill = Brushes.Green;
    ShowMediaInformation();
}

We can now add the ShowMediaInformation method to show the duration.

private void ShowMediaInformation()
{
    var duration = Media.NaturalDuration.HasTimeSpan
        ? Media.NaturalDuration.TimeSpan.TotalSeconds.ToString("#s")
        : "No duration";
    MediaInformation.Text = duration;
}

Try running the program and playing a video or audio file to see its duration displayed in seconds.

Detecting the Media Type

There are two Boolean properties that allow you to work out the type of media that has been opened. HasVideo is true if the media includes a video element. HasAudio is true if the media is an audio file or a video file that includes sound.

Let's modify ShowMediaInformation to show the media type in addition to the duration:

private void ShowMediaInformation()
{
    var sb = new StringBuilder();

    var duration = Media.NaturalDuration.HasTimeSpan
        ? Media.NaturalDuration.TimeSpan.TotalSeconds.ToString("#s")
        : "No duration";
    sb.Append(duration);

    if (Media.HasVideo)
    {
        sb.Append(", video");
    }

    if (Media.HasAudio)
    {
        sb.Append(", audio");
    }

    MediaInformation.Text = sb.ToString();
}
11 August 2014