Getting duration

This page shows you how to get the duration of the media file. Please note that error handling codes are omitted to keep the sample code simple.

Sample code

The following sample code shows how to get the duration from a MPEG file. This sample shows the duration of the MPEG file and ends.


#include <stdio.h>

#include <dshow.h>

// change here
#define	FILENAME L"c:\\DXSDK\\Samples\\Media\\butterfly.mpg"

int
main()
{
 IGraphBuilder *pGraphBuilder;
 IMediaControl *pMediaControl;
 IMediaPosition *pMediaPosition;

 REFTIME length;

 CoInitialize(NULL);

 CoCreateInstance(CLSID_FilterGraph,
	NULL,
	CLSCTX_INPROC,
	IID_IGraphBuilder,
	(LPVOID *)&pGraphBuilder);

 pGraphBuilder->QueryInterface(IID_IMediaControl,
	(LPVOID *)&pMediaControl);

 pGraphBuilder->QueryInterface(IID_IMediaPosition,
	(LPVOID *)&pMediaPosition);

 pMediaControl->RenderFile(FILENAME);

 pMediaPosition->get_Duration(&length);
 printf("%lf\n", length);

 pMediaPosition->Release();
 pMediaControl->Release();
 pGraphBuilder->Release();

 CoUninitialize();

 return 0;
}


The IMediaPosition::get_Duration method returns REFTIME. REFTIME shows the duration of the media content in seconds. REFTIME is defined as following, and is a double value.


typedef double REFTIME;

  

Copyright (C) GeekPage.JP. All rights reserved.