How to Get the Playback Duration

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

This topic describes how to get the playback duration of a media file using MFPlay.

To Get the Playback Duration

  1. Call IMFPMediaPlayer::CreateMediaItemFromURL or IMFPMediaPlayer::CreateMediaItemFromObject to create a media item for the file.
  2. Call IMFPMediaItem::GetDuration. Specify MFP_POSITIONTYPE_100NS for the first parameter. The duration is returned as a PROPVARIANT that contains a LARGE_INTEGER value. The duration is given in 100-nanosecond units.

The following example shows step 2:

#include <propvarutil.h>

HRESULT GetPlaybackDuration(IMFPMediaItem *pItem, ULONGLONG *phnsDuration)
{
    PROPVARIANT var;

    HRESULT hr = pItem->GetDuration(MFP_POSITIONTYPE_100NS, &var);

    if (SUCCEEDED(hr))
    {
        hr = PropVariantToUInt64(var, phnsDuration);
        PropVariantClear(&var);
    }

    return hr;
}

Requirements

MFPlay requires Windows 7.

Using MFPlay for Audio/Video Playback