Playing the File

Playing a file using DirectShow is a three-step process:

  1. Create an instance of the filter graph manager.
  2. Use the filter graph manager to create a filter graph.
  3. Use the filter graph manager to run the filter graph.

To accomplish this, you need to use two interfaces:

The filter graph manager implements both of these interfaces. Here is the basic code, minus some COM details.

Note   To make the following code example easier to read, error checking is not included. This code example should not be used in a release configuration unless it has been modified to include secure error handling.

IGraphBuilder *pGraph;
IMediaControl *pMediaControl;
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, 
                    IID_IGraphBuilder, (void **)&pGraph);
// Query for IMediaControl (not shown)
// Filenames on Windows CE start with a \\ instead of a drive letter.
pGraph->RenderFile(L"\\Hello_World.avi", NULL);
pMediaControl->Run();

The IGraphBuilder::RenderFile method constructs a filter graph that will play the specified file. The first parameter is the file name, represented as a wide character (2-byte) Unicode string. For simplicity, the example program specifies a literal string, rather than have the user select a file name. The "L" prefix converts an ASCII string to a wide character string. The second parameter is reserved and must equal NULL.

After the filter graph manager has constructed a filter graph, it is ready to begin playback. The IMediaControl::Run method switches the graph into running mode. When the application invokes this method, media data begins to move through the filter graph and is rendered as video, audio, or both.

If this were the last statement in the program, the program would abruptly stop the moment playback began. In a real application, you would wait for an end-of-stream event, a topic discussed in Responding to Events. To keep the example as short as possible, the sample program merely displays a message box, which blocks program execution. Playback continues on a separate thread until the user clicks the OK button. Also, in a real application, you should test the return value of RenderFile to confirm that it created the filter graph successfully. For example, if the specified file does not exist, RenderFile fails and returns the error code VFW_E_NOT_FOUND.

Last updated on Wednesday, April 13, 2005

© 2005 Microsoft Corporation. All rights reserved.