【发布时间】:2015-05-28 22:39:58
【问题描述】:
我正在 Visual Studio 2013 中编写一个简单的多媒体应用程序,我需要枚举连接到我的计算机的相机设备并创建一个媒体源对象以链接到其中一个。我使用 Media Foundation SDK 并尝试在此处运行指南:https://msdn.microsoft.com/en-us/library/windows/desktop/dd940326(v=vs.85).aspx:
#include <Mfapi.h>
#include <mfidl.h>
#include <mfobjects.h>
#include <iostream>
#pragma comment(lib, "Mfplat")
#pragma comment(lib, "Mf")
template <class T> void SafeRelease(T **ppT) {
if (*ppT) {
(*ppT)->Release();
*ppT = NULL;
}
}
HRESULT CreateVideoDeviceSource(IMFMediaSource **ppSource) {
*ppSource = NULL;
IMFMediaSource *pSource = NULL;
IMFAttributes *pAttributes = NULL;
IMFActivate **ppDevices = NULL;
// Create an attribute store to specify the enumeration parameters.
HRESULT hr = MFCreateAttributes(&pAttributes, 1);
if (FAILED(hr))
{
goto done;
}
// Source type: video capture devices
hr = pAttributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID
);
if (FAILED(hr))
{
goto done;
}
// Enumerate devices.
UINT32 count;
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
if (FAILED(hr))
{
goto done;
}
if (count == 0)
{
hr = E_FAIL;
goto done;
}
// Create the media source object.
hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
if (FAILED(hr))
{
std::cout << "Failed to create device object" << hr <<std::endl;
goto done;
}
*ppSource = pSource;
(*ppSource)->AddRef();
DWORD chs;
(*ppSource)->GetCharacteristics(&chs);
std::cout << chs << std::endl;
done:
SafeRelease(&pAttributes);
for (DWORD i = 0; i < count; i++)
{
SafeRelease(&ppDevices[i]);
}
CoTaskMemFree(ppDevices);
SafeRelease(&pSource);
return hr;
}
int main(int argc, char* argv[]) {
IMFMediaSource* ppSource;
CreateVideoDeviceSource(&ppSource);
std::cout << "END" << std::endl;
return 0;
}
问题在于这部分代码:
// Create the media source object.
hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
if (FAILED(hr))
{
goto done;
}
未能创建媒体源对象(返回的 HRESULT 为 0x800401F0 (CO_E_NOTINITIALIZED)--"尚未调用 CoInitialize。 ")。错误代码是什么意思,可能是什么问题导致失败?我使用的是WIN8.1。
【问题讨论】:
-
如果您告诉读者 hr 中的错误代码值是什么,可能会增加您获得有用答案的机会。
-
@Calvin,欢迎来到 StackOverflow。如果答案解决了您的问题,请不要忘记将此标记为已回答(检查答案旁边的复选标记)。如果您对答案表示赞赏,请随时给个人投票。