我正在参考以下文档部分“用于捕获屏幕的 Windows Media API”来捕获屏幕并且原始代码可以正常工作。当我添加附加功能时(我只是添加了几行以使其从默认音频设备录制),它在下一行失败(我调试并且看起来它在 COM 层失败?),有什么提示吗?我发布了原始源代码和修改后的源代码。
“如果(失败(hr=pSrcGrp->put_Profile(variant_t(pProfile))))”
http://www.geocities.com/krishnapg/screencap.html
原始源代码:
http://www.geocities.com/krishnapg/WMEncScrnCap.zip
我修改的源代码(我只修改了函数InitEncoder)
HRESULT InitEncoder(LPCTSTR szOutputFileName)
{
HRESULT hr = E_FAIL;
CComVariant varValue;
IWMEncSourceGroupCollection* pSrcGrpCollection=NULL;
IWMEncSourceGroup* pSrcGrp=NULL;
IWMEncSource* pSrc=NULL;
IWMEncSource* paSrc=NULL;
IPropertyBag* pPropertyBag=NULL;
IWMEncVideoSource2* pSrcVid=NULL;
IWMEncAudioSource* pSrcAud=NULL;
IWMEncFile* pOutFile=NULL;
IWMEncProfile* pProfile=NULL;
if(FAILED(CoCreateInstance(CLSID_WMEncoder,NULL,CLSCTX_INPROC_SERVER,IID_IWMEncoder2,(void**)&g_pEncoder)))
{
ErrorMessage("Unable to Create Encoder Object");
return E_FAIL;
}
if(FAILED(g_pEncoder->get_SourceGroupCollection(&pSrcGrpCollection))) //Retrieve the Source Group Collection - One Application can Have many Source Groups - We need to add as many as we want
{
ErrorMessage("Unable to Get Source Group Collection");
return E_FAIL;
}
do
{
if(FAILED(hr=pSrcGrpCollection->Add(CComBSTR("SourceGroup1"),&pSrcGrp)))//Add a Source Group to the Collection - Each Source can have one video one audio source input
{
ErrorMessage("Unable to Add A Source Group to the Collection");
break;
}
if(FAILED(hr=pSrcGrp->AddSource(WMENC_VIDEO,&pSrc))) //Add a Video Source to the Group
{
ErrorMessage("Unable to Add A Source to the Source Group");
break;
}
if(FAILED(hr=pSrcGrp->AddSource(WMENC_AUDIO,&paSrc))) //Add an Audio Source to the Group
{
ErrorMessage("Unable to Add A Source to the Source Group");
break;
}
if(FAILED(hr=pSrc->QueryInterface(IID_IWMEncVideoSource2,(void**)&pSrcVid)))
{
ErrorMessage("Unable to Query interface for Video Source");
break;
}
if(FAILED(hr=paSrc->QueryInterface(IID_IWMEncAudioSource,(void**)&pSrcAud)))
{
ErrorMessage("Unable to Query interface for Audio Source");
break;
}
if(FAILED(hr=pSrcVid->SetInput(CComBSTR("ScreenCap://ScreenCapture1"))))//The Video Input Source Device - Should be "ScreenCap" Device
{
ErrorMessage("Unable to Set Video Input Source");
break;
}
if(FAILED(hr=pSrcAud->SetInput(CComBSTR("Device://Default_Audio_Device"))))//The Video Input Source Device - Should be "Default_Audio_Device" Device
{
ErrorMessage("Unable to Set Audio Input Source");
break;
}
if(FAILED(hr=pSrcVid->QueryInterface(IID_IPropertyBag,(void**)&pPropertyBag)))
{
ErrorMessage("Unable to Query Interface for Propery bag");
break;
}
varValue = CAPTURE_FULLSCREEN;
if(FAILED(hr=pPropertyBag->Write(WMSCRNCAP_ENTIRESCREEN,&varValue))) //Set Full Screen Property true/false
{
ErrorMessage("Unable to Set Capture Screen Property");
break;
}
//int nLeft, nRight, nTop, nBottom; //Set Capture Area - when not in full screen mode
// // Initialize the capture area. The size must be even.
// varValue = false;
// if ( SUCCEEDED( hr ) )
// {
// hr = pPropertyBag->Write( WMSCRNCAP_ENTIRESCREEN, &varValue );
// }
// varValue = nLeft;
// if ( SUCCEEDED( hr ) )
// {
// hr = pPropertyBag->Write( WMSCRNCAP_WINDOWLEFT, &varValue );
// }
// varValue = nRight;
// if ( SUCCEEDED( hr ) )
// {
// hr = pPropertyBag->Write( WMSCRNCAP_WINDOWRIGHT, &varValue );
// }
// varValue = nTop;
// if ( SUCCEEDED( hr ) )
// {
// hr = pPropertyBag->Write( WMSCRNCAP_WINDOWTOP, &varValue );
// }
// varValue = nBottom;
// if ( SUCCEEDED( hr ) )
// {
// hr = pPropertyBag->Write( WMSCRNCAP_WINDOWBOTTOM, &varValue );
// }
// varValue = true;
// if ( SUCCEEDED( hr ) )
// {
// hr = pPropertyBag->Write( WMSCRNCAP_FLASHRECT, &varValue );
// }
if(FAILED(hr=SetupScreenCaptureProfile())) //Setup the Custom Profile
{
break;
}
if(FAILED(hr=g_pProfile->QueryInterface(IID_IWMEncProfile,(void**)&pProfile)))
{
ErrorMessage("Unable to Query Interface For Profile");
break;
}
if(FAILED(hr=pSrcGrp->put_Profile(variant_t(pProfile)))) //Select the Custom Profile into the Encoder
{
ErrorMessage("Unable to Set Profile For Source Group");
break;
}
if(FAILED(hr=g_pEncoder->get_File(&pOutFile)))
{
ErrorMessage("Unable to Get Encoder Output File Object");
break;
}
if(FAILED(hr=pOutFile->put_LocalFileName(CComBSTR(szOutputFileName)))) //Set the Target Output Filename
{
ErrorMessage("Unable to Set Output File Name");
break;
}
if(FAILED(hr=g_pEncoder->PrepareToEncode(VARIANT_TRUE))) //Using Prepare optimizes startig latency
{
ErrorMessage("Unable to Prepare for Encoding");
break;
}
}while(false);
if(pProfile)
{
pProfile->Release();
pProfile=NULL;
}
if(pOutFile)
{
pOutFile->Release();
pOutFile = NULL;
}
if(pPropertyBag)
{
pPropertyBag->Release();
pPropertyBag = NULL;
}
if(pSrcVid)
{
pSrcVid->Release();
pSrcVid = NULL;
}
if(pSrc)
{
pSrc->Release();
pSrc = NULL;
}
if(pSrcGrp)
{
pSrcGrp->Release();
pSrcGrp = NULL;
}
if(pSrcGrpCollection)
{
pSrcGrpCollection->Release();
pSrcGrpCollection = NULL;
}
return hr;
}