Using RenderFile (Play MPEG)

This page shows you how to play a MPEG file using directshow. Please note that error handling codes are omitted to keep the sample code simple.

Sample code

I would like to show a sample code first, since I think it would be easier to understand rather than explaining first. Please read the comments included in the sample code.

The following sample code shows how to use play a MPEG file. This sample plays a MPEG file "C:\DXSDK\Samples\Media\butterfly.mpg". Please change the "#define" to play other MPEG files.

#include <stdio.h>

#include <dshow.h>

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

 IGraphBuilder *pGraphBuilder;
 IMediaControl *pMediaControl;

 // Initialize COM

 // Create a FilterGraph
	(LPVOID *)&pGraphBuilder);

 // Get MediaControl Interface
	(LPVOID *)&pMediaControl);

 // Create a Graph

 // Start playing

 // This is to block the execution.
 // If you remove this MessageBox, this sample will
 // end in a blink of an eye.
 // This is because playback thread is created
 // automatically in directshow, and Run() is returned
 // immediately.
 // Blocking while playing a MPEG file is explained later.
	"Block Execution",

 // Release

 // Finish using COM

 return 0;

Please note that MessageBox in this sample code is very important. The MPEG playback will continue until you press "OK". This is because a playback thread is created automatically within directshow, and Run() is returned immediately. If the MessageBox block is not present, the Release() code will be done right after Run(), and everything will stop.

You can try changing MessageBox into Sleep(). If you use Sleep(), the MPEG playback will only continue during the time set to Sleep function. After the time set to Sleep() pass, the main() function will end.

This sample shows you how to playback a MPEG file. You can also use this sample to play media files other than MPEG format. For example, you can just change the "#define FILENAME" value into an AVI file, or a WMV file. You can also play audio files using this sample too. If you change the "#defile FILENAME" value into a MP3 file, directshow will play a MP3 file.

good luck.


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