OK, this is the C++ code I believe when compiled should play back an mp3 file (since it's not working the most probable reason for that might be that I made a cardinal mistake):
//Don't forget to change project settings:
//1. C++: add include path to DirectShow include folder (such as c:\dxsdk\include)
//2. Link: add link path to DirectShow lib folder (such as c:\dxsdk\lib).
//3. Link: add strmiids.lib and quartz.lib
#include "stdafx.h"
#include <DShow.h>
#include <atlbase.h>
#include <initguid.h>
#include <dvdmedia.h>
BOOL hrcheck(HRESULT hr, char* errtext)
{
if (hr >= S_OK)
return FALSE;
TCHAR szErr[MAX_ERROR_TEXT_LEN];
DWORD res = AMGetErrorText(hr, szErr, MAX_ERROR_TEXT_LEN);
if (res)
printf("Error %x: %s\n%s\n",hr, errtext,szErr);
else
printf("Error %x: %s\n", hr, errtext);
return TRUE;
}
//change this macro to fit your style of error handling
#define CHECK_HR(hr, msg) if (hrcheck(hr, msg)) return hr;
CComPtr<IPin> GetPin(IBaseFilter *pFilter, LPCOLESTR pinname)
{
CComPtr<IEnumPins> pEnum;
CComPtr<IPin> pPin;
HRESULT hr = pFilter->EnumPins(&pEnum);
if (hrcheck(hr, "Can't enumerate pins."))
return NULL;
while(pEnum->Next(1, &pPin, 0) == S_OK)
{
PIN_INFO pinfo;
pPin->QueryPinInfo(&pinfo);
BOOL found = !wcsicmp(pinname, pinfo.achName);
if (pinfo.pFilter) pinfo.pFilter->Release();
if (found)
return pPin;
pPin.Release();
}
printf("Pin not found!\n");
return NULL;
}
// {0F40E1E5-4F79-4988-B1A9-CC98794E6B55}
DEFINE_GUID(CLSID_ffdshowAudioDecoder,
0x0F40E1E5, 0x4F79, 0x4988, 0xB1, 0xA9, 0xCC, 0x98, 0x79, 0x4E, 0x6B, 0x55); //ffdshow.ax
HRESULT BuildGraph(IGraphBuilder *pGraph, LPCOLESTR srcFile1)
{
HRESULT hr = S_OK;
//graph builder
CComPtr<ICaptureGraphBuilder2> pBuilder;
hr = pBuilder.CoCreateInstance(CLSID_CaptureGraphBuilder2);
CHECK_HR(hr, "Can't create Capture Graph Builder");
hr = pBuilder->SetFiltergraph(pGraph);
CHECK_HR(hr, "Can't SetFiltergraph");
//add File Source (Async.)
CComPtr<IBaseFilter> pFileSourceAsync;
hr = pFileSourceAsync.CoCreateInstance(CLSID_AsyncReader);
CHECK_HR(hr, "Can't create File Source (Async.)");
hr = pGraph->AddFilter(pFileSourceAsync, L"File Source (Async.)");
CHECK_HR(hr, "Can't add File Source (Async.) to graph");
//set source filename
CComQIPtr<IFileSourceFilter, &IID_IFileSourceFilter> pFileSourceAsync_src(pFileSourceAsync);
if (!pFileSourceAsync_src)
CHECK_HR(E_NOINTERFACE, "Can't get IFileSourceFilter");
hr = pFileSourceAsync_src->Load(srcFile1, NULL);
CHECK_HR(hr, "Can't load file");
//add MPEG-I Stream Splitter
CComPtr<IBaseFilter> pMPEGIStreamSplitter;
hr = pMPEGIStreamSplitter.CoCreateInstance(CLSID_MPEG1Splitter);
CHECK_HR(hr, "Can't create MPEG-I Stream Splitter");
hr = pGraph->AddFilter(pMPEGIStreamSplitter, L"MPEG-I Stream Splitter");
CHECK_HR(hr, "Can't add MPEG-I Stream Splitter to graph");
//connect File Source (Async.) and MPEG-I Stream Splitter
hr = pGraph->ConnectDirect(GetPin(pFileSourceAsync, L"Output"), GetPin(pMPEGIStreamSplitter, L"Input"), NULL);
CHECK_HR(hr, "Can't connect File Source (Async.) and MPEG-I Stream Splitter");
//add ffdshow Audio Decoder
CComPtr<IBaseFilter> pffdshowAudioDecoder;
hr = pffdshowAudioDecoder.CoCreateInstance(CLSID_ffdshowAudioDecoder);
CHECK_HR(hr, "Can't create ffdshow Audio Decoder");
hr = pGraph->AddFilter(pffdshowAudioDecoder, L"ffdshow Audio Decoder");
CHECK_HR(hr, "Can't add ffdshow Audio Decoder to graph");
//connect MPEG-I Stream Splitter and ffdshow Audio Decoder
hr = pGraph->ConnectDirect(GetPin(pMPEGIStreamSplitter, L"Audio"), GetPin(pffdshowAudioDecoder, L"In"), NULL);
CHECK_HR(hr, "Can't connect MPEG-I Stream Splitter and ffdshow Audio Decoder");
//add Default DirectSound Device
CComPtr<IBaseFilter> pDefaultDirectSoundDevice;
hr = pDefaultDirectSoundDevice.CoCreateInstance(CLSID_DSoundRender);
CHECK_HR(hr, "Can't create Default DirectSound Device");
hr = pGraph->AddFilter(pDefaultDirectSoundDevice, L"Default DirectSound Device");
CHECK_HR(hr, "Can't add Default DirectSound Device to graph");
//connect ffdshow Audio Decoder and Default DirectSound Device
hr = pGraph->ConnectDirect(GetPin(pffdshowAudioDecoder, L"Out"), GetPin(pDefaultDirectSoundDevice, L"Audio Input pin (rendered)"), NULL);
CHECK_HR(hr, "Can't connect ffdshow Audio Decoder and Default DirectSound Device");
return S_OK;
}
//int _tmain(int argc, _TCHAR* argv[]) //use this line in VS2008
int main(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
CComPtr<IGraphBuilder> graph;
graph.CoCreateInstance(CLSID_FilterGraph);
printf("Building graph...\n");
HRESULT hr = BuildGraph(graph, argv[1]);
if (hr==S_OK) {
printf("Running");
CComQIPtr<IMediaControl, &IID_IMediaControl> mediaControl(graph);
hr = mediaControl->Run();
CHECK_HR(hr, "Can't run the graph");
CComQIPtr<IMediaEvent, &IID_IMediaEvent> mediaEvent(graph);
BOOL stop = FALSE;
MSG msg;
while(!stop)
{
long ev=0, p1=0, p2=0;
Sleep(500);
printf(".");
while(PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
DispatchMessage(&msg);
while (mediaEvent->GetEvent(&ev, &p1, &p2, 0)==S_OK)
{
if (ev == EC_COMPLETE || ev == EC_USERABORT)
{
printf("Done!\n");
stop = TRUE;
}
else
if (ev == EC_ERRORABORT)
{
printf("An error occured: HRESULT=%x\n", p1);
mediaControl->Stop();
stop = TRUE;
}
mediaEvent->FreeEventParams(ev, p1, p2);
}
}
}
CoUninitialize();
return 0;
}
The code was built with Microsoft VS2010 and this is the log report from the build:
1>------ Rebuild All started: Project: nov poskus, Configuration: Debug Win32 ------
1> stdafx.cpp
1> nov poskus.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\atlconv.h(757): warning C4995: 'wcscpy': name was marked as #pragma deprecated
1>c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\atlconv.h(768): warning C4995: 'wcscat': name was marked as #pragma deprecated
1>c:\vc\nov poskus\nov poskus\nov poskus.cpp(41): warning C4996: 'wcsicmp': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _wcsicmp. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\wchar.h(1095) : see declaration of 'wcsicmp'
1> nov poskus.vcxproj -> C:\vc\nov poskus\Debug\nov poskus.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
I'm not sure about these warnings, I googled them a bit and I assume they are not important for the proper functioning of the application.
I also debugged the app and this is the log:
'nov poskus.exe': Loaded 'C:\vc\nov poskus\Debug\nov poskus.exe', Symbols loaded.
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\quartz.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\winmm.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\qcap.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\msvfw32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7600.16661_none_ebfb56996c72aefc\comctl32.dll', Symbols loaded (source information stripped).
'nov poskus.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Symbols loaded (source information stripped).
The thread 'Win32 Thread' (0x1114) has exited with code 0 (0x0).
The program '[816] nov poskus.exe: Native' has exited with code 0 (0x0).
When I try to run the application (open console, drag'n drop the app in there, space, drag'n drop some mp3 file) this is the error message I get:
C:\Users\MicoMaco>"C:\vc\nov poskus\Debug\nov poskus.exe" "C:\Users\MicoMaco\Music\AC DC\Back In Black\AC DC_BACK IN BLACK_Hells Bells.mp3"
Building graph...
Error 80070002: Can't load file
N
Can you please tell me what's wrong with the code? I'd be very grateful.
Regards, MicoMaco