Expand Minimize

MediaElement.Position property

Applies to Windows and Windows Phone

Gets or sets the current position of progress through the media's playback time.

Syntax

Public Property Position As TimeSpan

Property value

Type: System.TimeSpan [.NET] | Windows::Foundation::TimeSpan [C++]

The amount of time since the beginning of the media. The default is a TimeSpan with value "0:0:0".

Remarks

Set this property to set a specific start-play position within the media.

A TimeSpan is represented as different types depending on the programming language you are using.

  • For C# and Visual Basic, you use a System.TimeSpan structure. System.TimeSpan has utility members such as Parse(String) that make it easy to create, change or compare time values.
  • For Visual C++ component extensions (C++/CX), you use a Windows::Foundation::TimeSpan structure. This structure has only one field member (Duration), which measures by 100-nanosecond units. If you intend to display a Position value in an hours:minutes:seconds format, you'll have to write your own logic for converting to that format from the Duration units.

If you are not sure of the current media source loaded as MediaElement.Source, always check CanSeek first. A streaming media source results in the MediaElement reporting CanSeek as false, and in this case you cannot use Position to set the playback point.

If Position is settable, then these value constraints apply:

  • If Position is set to a negative value, the value will revert to a TimeSpan with value "0:0:0".
  • If Position is set to a value larger than the media's NaturalDuration, then Position becomes the value of NaturalDuration.

The Position value when media is being played is often presented to the user as a Slider control. This is shown briefly in the example in this topic, and you can see more code for associating the Position value with a Slider in Quickstart: Creating a media player app.

Notes on XAML syntax

Setting a Position value in XAML is technically possible but not common. If you do set a Position in XAML, simulate the "FromString" type conversion behavior for a TimeSpan, in other words specify the value in the form "Hh:Mm:Ss".

Examples

This code shows the simple pattern of setting Position based on detecting the changed NewValue of a Slider control. The Slider control is part of the MediaElement transport UI, and enables the user to set the Position. In this code, "sliderpressed" is a sentinel Boolean value that is set to true while a pointer is pressed, so that the code does not change the position during intermediate slider repositioning. A prerequisite (not shown) is that the Maximum of the Slider should be reset to be the current media source's NaturalDuration whenever a new non-streaming media source is finished loading. For the context of the larger code sample where this snippet came from, see Quickstart: Creating a media player app.


void timelineSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    if (!_sliderpressed)
    {
        videoMediaElement.Position = TimeSpan.FromSeconds(e.NewValue);
    }
}

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Controls
Windows::UI::Xaml::Controls [C++]

Metadata

Windows.winmd

See also

MediaElement
NaturalDuration
Quickstart: Creating a media player app
Quickstart: Video and audio

 

 

Show:
© 2014 Microsoft