How to play a file

Source: Internet
Author: User
The purpose of this article is to show the DirectShow programming style. This is a simple console application used to play an audio or video file. The program has only a few lines, but it demonstrates some of the capabilities of DirectShow programming.

As written in <DirectShow Application Programming introduction>, the basic steps for running a DirectShow application are as follows: 1. Create an instance of the filter table manager.
2. Use the filter table manager to generate a filter table
3. Run the table to make the data flow in the filter. Call cointitialize to initialize the com library hresult hR = coinitialize (null );
If (failed (HR ))
{
// Add the error handling code (omitted for clarity .)
}
For simplicity, this example ignores the return value, but you 'd better check the value of hresult in a method call. Next, call cocreateinstance to create the filter table manager: igraphbuilder * pgraph;
Hresult hR = cocreateinstance (clsid_filtergraph, null,
Clsctx_inproc_server, iid_igraphbuilder, (void **) & pgraph); As shown above, the class ID (CLSID) is clsid_filtergraph. The filter table manager is provided by a DLL in a process, so the execution context is clsctx_inproc_server. DirectShow supports free thread processing mode. Therefore, you can use the coinit_multithreaded flag to call coinitializeex. Call cocreateinstance to return the igraphbuilder interface, which mainly contains the method for generating the filter table. The other two interfaces used in this example are:
  • Imediacontrol: control flow. It contains methods for stopping and starting tables.
  • Imediaevent, which contains a method to get an event from the filter table manager. In this example, this interface is used to wait for the end of the playback.

Both interfaces appear in the filter table manager. You can use the returned igraphbuilder pointer to query.

Imediacontrol * pcontrol;
Imediaevent * pevent;
HR = pgraph-> QueryInterface (iid_imediacontrol, (void **) & pcontrol );
HR = pgraph-> QueryInterface (iid_imediaevent, (void **) & pevent );

Now you can generate a filter table. To play a file, you can call the following methods: Hr = pgraph-> renderfile (L "C: // example. Avi", null );
The igraphbuilder: renderfile method generates a filter table to play the specified file. The first parameter is the file name, represented by a wide character string (2 bytes. The second parameter is a reserved parameter and must be null. If the specified file does not exist or the file format is not available, this method will fail. If the method is called successfully, the filter restoration tool prepares for playback. To run the table, call the imediacontrol: Run method. HR = pcontrol-> Run (); when the filter is running, the data is removed from the filter and restored as video and audio. Playing starts another thread. You can call the imediaevent: waitforcompletion method. Long evcode = 0;
Pevent-> waitforcompletion (infinite, & evcode );

When the application ends, the interface pointer is released and the com library is closed. Pcontrol-> release ();
Pevent-> release ();
Pgraph-> release ();
Couninitialize (); Sample Code

The complete code of the example described in this article is listed here: # include <dshow. h>
Void main (void)
{
Igraphbuilder * pgraph = NULL;
Imediacontrol * pcontrol = NULL;
Imediaevent * pevent = NULL; // initialize the com Library
Hresult hR = coinitialize (null );
If (failed (HR ))
{
Printf ("error-cocould not initialize com library ");
Return;
} // Create a filter table manager and query the interface
HR = cocreateinstance (clsid_filtergraph, null, clsctx_inproc_server,
Iid_igraphbuilder, (void **) & pgraph );
If (failed (HR ))
{
Printf ("error-cocould not create the filter graph manager .");
Return;
} HR = pgraph-> QueryInterface (iid_imediacontrol, (void **) & pcontrol );
HR = pgraph-> QueryInterface (iid_imediaevent, (void **) & pevent); // create a table. Key: Change the string to the file name on your system.
HR = pgraph-> renderfile (L "C: // example. Avi", null );
If (succeeded (HR ))
{
// Run table
HR = pcontrol-> Run ();
If (succeeded (HR ))
{
// Wait for the end
Long evcode;
Pevent-> waitforcompletion (infinite, & evcode); // Note: Do not use infinite in actual applications
// Because it blocks applications, it is uncertain.
}
}
Pcontrol-> release ();
Pevent-> release ();
Pgraph-> release ();
Couninitialize ();
}

This method will be blocked until the file is played completely or exceeds the specified time. The infinite value indicates that the application will block the file from playing. A more practical example is event processing. For more information, see <RESPONSE event>.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.