IWMPMedia::duration property

The duration property gets the duration in seconds of the current media item.

This property is read-only.


public System.Double duration {get;}

Property value

A System.Double that is the duration in seconds.


If this property is used with a media item other than the one specified in AxWindowsMediaPlayer.currentMedia, it may not contain a valid value.

To retrieve the duration for files that are not in the user's library, you must wait for Windows Media Player to open the file; that is, the current OpenState must equal MediaOpen. You can verify this by handling the AxWindowsMediaPlayer._WMPOCXEvents_OpenStateChange event or by periodically checking the value of AxWindowsMediaPlayer.openState.

For playlists, the duration of each media item can be retrieved when the individual media item is opened, rather than the when the playlist is opened.

Before using this property, you must have read access to the library. For more information, see Library Access.


The following example uses duration to display the time remaining in the current media item in a label. A timer updates the text in the label every second.

// Set the timer to fire an event every second and start the timer.
timer.Interval = 1000;

// Note:  Use the AxWindowsMediaPlayer.PlayStateChange event with a switch statement to
// determine when to start and stop the timer. 

private void TimerEventProcessor(object sender, System.EventArgs e)
    // Subtract the current position from the duration of the current media to get
    // the time remaining. Use the Math.floor method to round the result down to the
    // nearest whole number.
    double t = Math.Floor(player.currentMedia.duration - player.Ctlcontrols.currentPosition);

    // Display the time remaining in the current media.
    timeRemaining.Text = ("Seconds remaining: " + t.ToString());        



Windows Media Player 9 Series or later




Interop.WMPLib.dll (Interop.WMPLib.dll.dll)

See also

AxWindowsMediaPlayer.currentMedia (VB and C#)
AxWindowsMediaPlayer.openState (VB and C#)
AxWindowsMediaPlayer.OpenStateChange Event (VB and C#)
IWMPMedia Interface (VB and C#)
IWMPMedia.durationString (VB and C#)