DirectShow Beginner

Started by
10 comments, last by xuwen 21 years, 2 months ago
Hi. I''ve been attempting to figure out how to use DirectShow for a while now, and then I realized that I really don''t need to know the real nitty gritty of it. I''m basically trying to learn how to use the sound and movie playing capabilities of the DirectX API and the graphics capabilities of the OpenGL API. I''ve gotten the sound and graphics part ok, but that''s with some major handholding from the book OpenGL Game Programming. However, that only really goes over how to play .WAV and .MID files. I''d like to use .MP3 files but it seems I need to know DirectShow to do this. Well, I''ve been reading around and I really don''t understand much of it. I vaguely understand making a graph and so forth, but many tutorials give me theory. So, I was wondering if someone could maybe give me some sample code? Say I had a file called "nomorewords.mp3" that I want to play in the background of a game. Say I have a file called "intro.avi" that I''d like to play at the opening of the game. Could someone maybe write some sample code to explicitly load these? If I see it in action and maybe some commenting, I might be able to make it more OO. Thanks in advance.
Advertisement
#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 - Could not initialize COM library";        return;    }    // Create the filter graph manager and query for interfaces.    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,                         IID_IGraphBuilder, (void **)&pGraph);    if (FAILED(hr))    {        printf("ERROR - Could not create the Filter Graph Manager.";        return;    }    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);    // Build the graph. IMPORTANT: Change this string to a file on your system.    hr = pGraph->RenderFile(L"file.mp3.avi", NULL);    if (SUCCEEDED(hr))    {        // Run the graph.        hr = pControl->Run();        if (SUCCEEDED(hr))        {            // Wait for completion.            long evCode;            pEvent->WaitForCompletion(INFINITE, &evCode);            // Note: Do not use INFINITE in a real application, because it            // can block indefinitely.        }    }    pControl->Release();    pEvent->Release();    pGraph->Release();    CoUninitialize();} 
Thanks. This code would work for any file format that directX supports?
yes I think it will
// -------------------------------------------------
// mp3.h

class CMp3{protected:	IGraphBuilder* m_pGraph;	IMediaControl* m_pControl;public:	void Create(LPCWSTR lpwstrFile);	void Destroy();	void Play();}; 


// -------------------------------------------------

// -------------------------------------------------
// mp3.cpp

#include <dshow.h>#include "mp3.h"CMp3::CMp3(){	m_pGraph   = NULL;	m_PControl = NULL;}void CMp3::Create(LPCWSTR lpwstrFile){	CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&m_pGraph);	m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pControl);	m_pGraph->RenderFile(lpwstrFile, NULL);}void CMp3::Destroy(){	m_pControl->Release();	m_pGraph->Release();}void CMp3:lay(){	m_pControl->Run();} 
Thanks for the code from both of you. Maybe a small explanation could be given for some of the syntax? The code for the second example is much simpler and I''m able to understand. But I''m not sure how the WaitForCompletion method works. Could that be elaborated a little? And what other enumerations are there other than INFINITE?
I posted the first code
then I quickly made that CMp3 class

The WaitForCompletion method waits for the filter graph to render all available data. The filter graph must be running or the method fails.

Time-out interval, in milliseconds. Pass zero to return immediately. Pass the value INFINITE to block indefinitely.

so in the above example it will wait for the .avi or .mp3 to complete playing before returning
Both examples boil down to pretty much the same thing. You would probably not usually use WaitForCompletion in a game; that function stalls everything until the file is finished playing.

Here is a well-explained DirectShow MP3 player for use in a game. Have fun.





[edited by - micepick on February 2, 2003 6:31:31 PM]
Hmmm, I''m thinking I''m missing a link library. I need dxguid.lib and do i need like..dshow8.lib as well?
strmiids.lib

This topic is closed to new replies.

Advertisement