IWMPMedia::duration property

[The feature associated with this page, Windows Media Player SDK, is a legacy feature. It has been superseded by MediaPlayer. MediaPlayer has been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use MediaPlayer instead of Windows Media Player SDK, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]

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

This property is read-only.

Syntax

public System.Double duration {get;}

Public ReadOnly Property duration As System.Double

Property value

A System.Double that is the duration in seconds.

Remarks

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 when the playlist is opened.

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

Examples

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;
timer.Start();

// 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());        
}

' Set the timer to fire an event every second and start the timer.
Timer.Interval = 1000
Timer.Start()

' Note:  Use the AxWindowsMediaPlayer.PlayStateChange event with a Select Case statement to
' determine when to start and stop the timer. 

Public Sub TimerEventProcessor(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer.Tick

    ' 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.
    Dim t As Double = Math.Floor(player.currentMedia.duration - player.Ctlcontrols.currentPosition)

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

End Sub

Requirements

Requirement Value
Version
Windows Media Player 9 Series or later
Namespace
WMPLib
Assembly
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#)