DirectShow Play Mp3's!!! (Hello World Version)-VBForums
Results 1 to 2 of 2

Thread: DirectShow Play Mp3's!!! (Hello World Version)

  1. #1

    Thread Starter
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,073

    DirectShow Play Mp3's!!! (Hello World Version)

    This will be the simplest way to play mp3's in C++ using DirectShow. Before you can even use DirectShow though, if you haven't already, download the latest Windows SDK. Next you will need to compile the BaseClasses hidden in the SDK over in this file path:

    C:\Program Files\Microsoft SDKs\Windows\v7.1\Samples\multimedia\directshow\baseclasses.

    Open up the solution file using Visual C++ 2010, and compile it in both Debug and Release. If it doesn't compile right, and sometimes it doesn't, refer to Google for a solution like I did. Unfortunately I forgot the issue I had since it was a long time ago. Once you successfully compiled it in Debug and Release, you won't ever have to do that again and can now work with DirectShow anytime.

    Create a new empty VC++ project, and right click your project and goto Properties. Then goto Configuration Properties > Linker > Input and where it says Additional Dependencies, click on it until you see a [v] symbol, and open it. Then click <Edit...>. Next in the large box, just put strmiids.lib, hit enter, and put quartz.lib. Click Ok. Then in the Properties window, click Ok there too. Copy and paste this code, change the name of the mp3 you desire and put it where your exe is being compiled to, whether its Debug or Release, and run it. Right now I got World of Warcraft - Seasons of War.mp3. It should play majority of the mp3's. For some reason it doesn't play all of em out there. And I have to conduct some experiments to find out which ones run and which ones don't but it should play. And heres the code. If you want shorter code then this, then you would have to eliminate the error handling and hr's, but its not wise to run without error handling:

    C++ Code:
    1. #include <DShow.h>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. void ShowError(HRESULT hr);
    7.  
    8. int CALLBACK WinMain(
    9.   _In_  HINSTANCE hInstance,
    10.   _In_  HINSTANCE hPrevInstance,
    11.   _In_  LPSTR lpCmdLine,
    12.   _In_  int nCmdShow
    13. )
    14. {
    15.     HRESULT hr;
    16.     IGraphBuilder *pGraph = NULL;
    17.     IMediaControl *pControl = NULL;
    18.     IMediaEvent   *pEvent = NULL;
    19.  
    20.     try
    21.     {
    22.         hr = CoInitialize(NULL);
    23.         ShowError(hr);
    24.        
    25.         hr = CoCreateInstance(CLSID_FilterGraph, NULL,
    26.             CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
    27.         ShowError(hr);
    28.        
    29.         char File_Name[255] = "World of Warcraft - Seasons Of War.mp3";
    30.         char buffer[MAX_PATH];
    31.         string Result;
    32.         GetModuleFileName( NULL, buffer, MAX_PATH );
    33.         string startuppath = string(buffer);
    34.         string filename = string(File_Name);
    35.         startuppath = startuppath.substr(0, startuppath.find_last_of( "\\/" ));
    36.         Result = startuppath + string("\\") + filename;
    37.         const char *File_Path = Result.c_str();
    38.         WCHAR *MediaFile = new WCHAR[strlen(File_Path)+1];
    39.         MultiByteToWideChar(CP_ACP, 0, File_Path, -1, MediaFile, strlen(File_Path)+1);
    40.  
    41.         hr = pGraph->RenderFile(MediaFile, NULL);
    42.         ShowError(hr);
    43.  
    44.         hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    45.         ShowError(hr);
    46.  
    47.         hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
    48.         ShowError(hr);
    49.  
    50.         hr = pControl->Run();
    51.         ShowError(hr);
    52.  
    53.         long evCode = 0;
    54.         hr = pEvent->WaitForCompletion(INFINITE, &evCode);
    55.         ShowError(hr);
    56.     }
    57.     catch(TCHAR szErr[])
    58.     {
    59.         MessageBox(NULL, (LPCSTR)szErr, TEXT("DirectShow"), 0);
    60.     }
    61.  
    62.     if(pGraph)
    63.     {
    64.         pGraph->Release();
    65.         pGraph = NULL;
    66.     }
    67.  
    68.     if (pControl)
    69.     {
    70.         pControl->Release();
    71.         pControl = NULL;
    72.     }
    73.  
    74.     if (pEvent)
    75.     {
    76.         pEvent->Release();
    77.         pEvent = NULL;
    78.     }
    79.     CoUninitialize();
    80.     return hr;
    81. }
    82.  
    83. void ShowError(HRESULT hr)
    84. {
    85.     if (FAILED(hr))
    86.     {
    87.         TCHAR szErr[MAX_ERROR_TEXT_LEN] = {0};
    88.         DWORD res = AMGetErrorText(hr, szErr, MAX_ERROR_TEXT_LEN);
    89.         if (res == 0)
    90.         {
    91.             StringCchPrintf(szErr, MAX_ERROR_TEXT_LEN, "Unknown Error: 0x%2x", hr);
    92.         }
    93.         throw szErr;
    94.     }
    95. }

    The short short hello world version (no error handling)

    C++ Code:
    1. #include <DShow.h>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int CALLBACK WinMain(
    7.   _In_  HINSTANCE hInstance,
    8.   _In_  HINSTANCE hPrevInstance,
    9.   _In_  LPSTR lpCmdLine,
    10.   _In_  int nCmdShow
    11. )
    12. {
    13.     IGraphBuilder *pGraph = NULL;
    14.     IMediaControl *pControl = NULL;
    15.     IMediaEvent   *pEvent = NULL;
    16.  
    17.     CoInitialize(NULL);
    18.     CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
    19.    
    20.     char File_Name[255] = "World of Warcraft - Seasons Of War.mp3";
    21.     char buffer[MAX_PATH];
    22.     string Result;
    23.     GetModuleFileName( NULL, buffer, MAX_PATH );
    24.     string startuppath = string(buffer);
    25.     string filename = string(File_Name);
    26.     startuppath = startuppath.substr(0, startuppath.find_last_of( "\\/" ));
    27.     Result = startuppath + string("\\") + filename;
    28.     const char *File_Path = Result.c_str();
    29.     WCHAR *MediaFile = new WCHAR[strlen(File_Path)+1];
    30.     MultiByteToWideChar(CP_ACP, 0, File_Path, -1, MediaFile, strlen(File_Path)+1);
    31.  
    32.     pGraph->RenderFile(MediaFile, NULL);
    33.     pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    34.     pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
    35.     pControl->Run();
    36.  
    37.     long evCode = 0;
    38.     pEvent->WaitForCompletion(INFINITE, &evCode);
    39.  
    40.     pGraph->Release();
    41.     pGraph = NULL;
    42.     pControl->Release();
    43.     pControl = NULL;
    44.     pEvent->Release();
    45.     pEvent = NULL;
    46.     CoUninitialize();
    47. }

    Do I have to make it shorter.....yeeeesh. Fine but the mp3 will be in the C drive instead

    C++ Code:
    1. #include <DShow.h>
    2.  
    3. int CALLBACK WinMain(
    4.   _In_  HINSTANCE hInstance,
    5.   _In_  HINSTANCE hPrevInstance,
    6.   _In_  LPSTR lpCmdLine,
    7.   _In_  int nCmdShow
    8. )
    9. {
    10.     IGraphBuilder *pGraph = NULL;
    11.     IMediaControl *pControl = NULL;
    12.     IMediaEvent   *pEvent = NULL;
    13.  
    14.     CoInitialize(NULL);
    15.     CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
    16.    
    17.     pGraph->RenderFile(L"C:\\World of Warcraft - Seasons of War.mp3", NULL);
    18.     pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    19.     pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
    20.     pControl->Run();
    21.  
    22.     long evCode = 0;
    23.     pEvent->WaitForCompletion(INFINITE, &evCode);
    24.  
    25.     pGraph->Release();
    26.     pGraph = NULL;
    27.     pControl->Release();
    28.     pControl = NULL;
    29.     pEvent->Release();
    30.     pEvent = NULL;
    31.     CoUninitialize();
    32. }

  2. #2

    Thread Starter
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,073

    Re: Play Mp3's!!! (Hello World Version)

    This is pretty much the same concept, without the error handling for readability, only this one actually opens a window, so you can close out of it to close the program, or you can press escape. Now in the previous example, if you were to run the exe itself and not through VC++, you won't be able to close out of it unless you Ctrl+Alt+Delete. At least through VC++ theres a stop button. So heres a windowed version with the ability to close out of it (without error handling. If you want, add the error handling in the previous example):

    C++ Code:
    1. #include <Windows.h>
    2. #include <DShow.h>
    3. #include <string>
    4. using namespace std;
    5.  
    6. IGraphBuilder *DirectShow_GraphBuilder = NULL;
    7. IMediaEvent *DirectShow_Event = NULL;
    8. IMediaControl *DirectShow_Control = NULL;
    9.  
    10. HWND hWnd;
    11. MSG msg;
    12.  
    13. int DirectShow_Load_Media();
    14. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    15.  
    16. int DirectShow_Load_Media(const char *File_Name)
    17. {
    18.     char buffer[MAX_PATH];
    19.     string Result;
    20.     GetModuleFileName( NULL, buffer, MAX_PATH );
    21.     string startuppath = string(buffer);
    22.     string filename = string(File_Name);
    23.     startuppath = startuppath.substr(0, startuppath.find_last_of( "\\/" ));
    24.     Result = startuppath + string("\\") + filename;
    25.     const char *File_Path = Result.c_str();
    26.    
    27.     WCHAR *MediaFile = new WCHAR[strlen(File_Path)+1];
    28.     MultiByteToWideChar(CP_ACP, 0, File_Path, -1, MediaFile, strlen(File_Path)+1);
    29.    
    30.     CoInitialize(NULL);
    31.     CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&DirectShow_GraphBuilder);
    32.     DirectShow_GraphBuilder->QueryInterface(IID_IMediaControl,(void **)&DirectShow_Control);
    33.     DirectShow_GraphBuilder->QueryInterface(IID_IMediaEvent, (void **)&DirectShow_Event);
    34.     DirectShow_GraphBuilder->RenderFile(MediaFile, NULL);
    35.     //MessageBoxW(NULL, MediaFile, L"test", MB_OK);
    36.     return 0;
    37. }
    38.  
    39. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
    40. {
    41.     WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "TUTORIAL", NULL};
    42.     RegisterClassEx(&wc);
    43.     hWnd = CreateWindowEx (0, "TUTORIAL", "MP3 Player", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);
    44.     ShowWindow (hWnd, nCmdShow);
    45.     DirectShow_Load_Media("World of Warcraft - Seasons of War.mp3");
    46.     while (1)
    47.     {
    48.         if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
    49.         {
    50.             if (WM_QUIT == msg.message) break;
    51.             TranslateMessage (&msg);
    52.             DispatchMessage (&msg);
    53.         }
    54.         else
    55.             DirectShow_Control->Run();
    56.     }
    57.     return msg.wParam;
    58. }
    59.  
    60. LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    61. {
    62.     switch (msg)
    63.     {
    64.         case WM_DESTROY:
    65.             DirectShow_Control->Release();
    66.             DirectShow_Event->Release();
    67.             DirectShow_GraphBuilder->Release();
    68.             DirectShow_Control = NULL;
    69.             DirectShow_Event = NULL;
    70.             DirectShow_GraphBuilder = NULL;
    71.             CoUninitialize();
    72.             PostQuitMessage (0);
    73.             break;
    74.         case WM_KEYDOWN:
    75.             if(wParam == VK_ESCAPE)
    76.             {
    77.                 DestroyWindow(hWnd);
    78.                 return(0);
    79.             }
    80.         default:
    81.             return DefWindowProc (hWnd, msg, wParam, lParam);
    82.     }
    83.     return 0;
    84. }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.