Windows Media Player 11 SDK IWMPMedia.duration (VB and C#)
Previous | Next |
IWMPMedia.duration (VB and C#)
The duration property gets the duration in seconds of the current media item.
[Visual Basic] ReadOnly Property duration As Double [C#] double duration {get;}
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 the when the playlist is opened.
Before using this property, you must have read access to the library. For more information, see Library Access.
Example Code
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.
[Visual Basic]
' 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
FakePre-0e7c6c51813c4b0bad57a4cd40dc68fc-29e8483b2fdf4beab012db05bbe687f7
// 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()); }
Requirements
Version: Windows Media Player 9 Series or later
Namespace: WMPLib
Assembly: Interop.WMPLib.dll (automatically generated by Visual Studio)
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#)
Previous | Next |