一个简单的DirectShow播放音频的程序,却没有声音
Hi
刚开始学DirectShow,先自己写了一个播放音乐文件的程序,程序如下:
运行以后没有声音,但是从显示"So it begins"到出现"Press any key to continue"之间的时间和1.wav这个文件的长度是差不都的,但是就是没有声音。用get_Volume()获取音量的话也得到是0,证明音量也没有问题。。。
void PlayAudio()
{
HRESULT hr;
CoInitialize(NULL);
IMediaControl *pCtrl=NULL;
IMediaEvent* pEvent=NULL;
IBaseFilter* pSrc=NULL;//Source Filter
IGraphBuilder* pGraph=NULL;
hr=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,IID_IGraphBuilder,(void**)&pGraph);
if (FAILED(hr))
return;
hr=pGraph->AddSourceFilter(L"c:\\sample\\1.wav",L"Source",&pSrc);
if (FAILED(hr))
return;
hr = pGraph->QueryInterface(IID_IMediaControl,(void**)&pCtrl);
if (FAILED(hr))
return;
hr = pGraph->QueryInterface(IID_IMediaEvent,(void**)&pEvent);
if (FAILED(hr))
return;
IPin* pPin=GetOutPin(pSrc,0);
hr=pGraph->Render(pPin);
if (FAILED(hr))
return;
hr=pCtrl->Run();
if (FAILED(hr))
return;
cout<<"So it begins"<<endl;
long evCode;
pEvent->WaitForCompletion(INFINITE,&evCode);
}
GetOutPin函数是从DShow自带的Sample里拿过来的,具体实现如下:
IPin * GetOutPin( IBaseFilter * pFilter, int nPin )
{
CComPtr<IPin> pComPin=0;
GetPin(pFilter, PINDIR_OUTPUT, nPin, &pComPin);
return pComPin;
}
HRESULT GetPin( IBaseFilter * pFilter, PIN_DIRECTION dirrequired, int iNum, IPin **ppPin)
{
CComPtr< IEnumPins > pEnum;
*ppPin = NULL;
HRESULT hr = pFilter->EnumPins(&pEnum);//hrererer!!!!!!!!!!!!
if(FAILED(hr))
return hr;
ULONG ulFound;
IPin *pPin;
hr = E_FAIL;
while(S_OK == pEnum->Next(1, &pPin, &ulFound))
{
PIN_DIRECTION pindir = (PIN_DIRECTION)3;
pPin->QueryDirection(&pindir);
if(pindir == dirrequired)
{
if(iNum == 0)
{
*ppPin = pPin; // Return the pin's interface
hr = S_OK; // Found requested pin, so clear error
break;
}
iNum--;
}
pPin->Release();
}
return hr;
}
声卡什么应该没有问题,在GraphEdit里构建一个链路的话也能正常播放=。=不知道为什么啊,小弟第一次发帖,希望有大牛能够解答一下,感激不尽
谢谢!
[解决办法]
直接RenderFile试试。