Using Renderfile (Play MPEG via HTTP)

The previous samples showed how to play a local media file. Directshow also supports download media playing. This page shows you how to download and play a MPEG file using HTTP. Please note that error handling codes are omitted to keep the sample code simple.

Sample code

The following sample code shows how to use play a remote media file using HTTP.

The following sample downloads a MPEG file from "http://localhost/butterfly.mpg". "localhost" means your local machine. You can change this URL if you do not have a HTTP server in your local machine. I used apache (http://www.apache.org) as a local HTTP server.


#include <stdio.h>

#include <dshow.h>

#define	URL L"http://localhost/butterfly.mpg"

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

 // Initialize COM
 CoInitialize(NULL);

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

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

 // Create Graph
 pMediaControl->RenderFile(URL);

 // Start playing
 pMediaControl->Run();

 // block while playing
 MessageBox(NULL,
	"Block Execution",
	"Block",
	MB_OK);

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

 // finish COM
 CoUninitialize();

 return 0;
}

Please note that the only difference between file playing and HTTP playing is the argument given to RenderFile. The difference between local HDD and network download is absorbed by directshow, and the application programmer does not have to know the difference.

However, please note that there are some differences an application programmer should know. For example, network download is likely to be slower than local HDD playing. It might take much time to start, and it might stop during playing because of network congestion.

  

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