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"

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

 // Initialize COM
 CoInitialize(NULL);

 // Create a FilterGraph
 CoCreateInstance(CLSID_FilterGraph,
	NULL,
	CLSCTX_INPROC,
	IID_IGraphBuilder,
	(LPVOID *)&pGraphBuilder);

 // Get MediaControl Interface
 pGraphBuilder->QueryInterface(IID_IMediaControl,
	(LPVOID *)&pMediaControl);

 // Create a Graph
 pMediaControl->RenderFile(FILENAME);

 // Start playing
 pMediaControl->Run();

 // 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.
 MessageBox(NULL,
	"Block Execution",
	"Block",
	MB_OK);

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

 // Finish using COM
 CoUninitialize();

 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值