Windows API - Win32
一组适用于桌面和服务器应用程序的核心 Windows 应用程序编程接口 (API)。 以前称为 Win32 API。
116 个问题
When I use Microsoft Media Foundation to play a local video, a Microsoft C++ exception bad_hresult is displayed in the output window. I want to know if it will affect the video playback. For demonstration, I wrote a simple example:
#include <Windows.h>
#include <mfapi.h>
#include <mfidl.h>
#include <mferror.h>
#include <shlwapi.h>
#pragma comment(lib, "mf")
#pragma comment(lib, "mfplat")
#pragma comment(lib, "mfuuid")
#pragma comment(lib, "shlwapi")
HRESULT CreateStreamSink(IMFStreamDescriptor* streamInfo, HWND window, IMFActivate** ppActivate)
{
IMFMediaTypeHandler* handler = nullptr;
IMFActivate* activate = nullptr;
HRESULT hr = streamInfo->GetMediaTypeHandler(&handler);
GUID typeGuid;
hr = handler->GetMajorType(&typeGuid);
if (typeGuid == MFMediaType_Audio)
hr = MFCreateAudioRendererActivate(&activate);
else if (typeGuid == MFMediaType_Video)
hr = MFCreateVideoRendererActivate(window, &activate);
*ppActivate = activate;
handler->Release();
return hr;
}
IMFMediaSession* _mediaSession = nullptr;
IMFMediaSource* _mediaSource = nullptr;
int main()
{
HRESULT hr = MFStartup(MF_VERSION);
hr = MFCreateMediaSession(nullptr, &_mediaSession);
MF_OBJECT_TYPE objectType = MF_OBJECT_INVALID;
IMFSourceResolver* resolver = nullptr;
IUnknown* source = nullptr;
hr = MFCreateSourceResolver(&resolver);
hr = resolver->CreateObjectFromURL(L"D:/Video.mp4", MF_RESOLUTION_MEDIASOURCE, nullptr, &objectType, &source);
hr = source->QueryInterface(IID_PPV_ARGS(&_mediaSource));
IMFPresentationDescriptor* sourceInfo = nullptr;
hr = _mediaSource->CreatePresentationDescriptor(&sourceInfo);
IMFTopology* pTopology = nullptr;
DWORD streamCount = 0;
hr = MFCreateTopology(&pTopology);
hr = sourceInfo->GetStreamDescriptorCount(&streamCount);
IMFStreamDescriptor* streamInfo = nullptr;
IMFActivate* pSinkActivate = nullptr;
IMFTopologyNode* sourceNode = nullptr;
IMFTopologyNode* outputNode = nullptr;
BOOL selected = FALSE;
for (DWORD index = 0; index < streamCount; index++)
{
hr = sourceInfo->GetStreamDescriptorByIndex(index, &selected, &streamInfo);
if (selected)
{
hr = CreateStreamSink(streamInfo, nullptr, &pSinkActivate);
IMFTopologyNode* sourceNode = nullptr;
hr = MFCreateTopologyNode(MF_TOPOLOGY_SOURCESTREAM_NODE, &sourceNode);
hr = sourceNode->SetUnknown(MF_TOPONODE_SOURCE, source);
hr = sourceNode->SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, sourceInfo);
hr = sourceNode->SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, streamInfo);
hr = pTopology->AddNode(sourceNode);
IMFTopologyNode* outputNode = nullptr;
hr = MFCreateTopologyNode(MF_TOPOLOGY_OUTPUT_NODE, &outputNode);
hr = outputNode->SetObject(pSinkActivate);
hr = outputNode->SetUINT32(MF_TOPONODE_STREAMID, 0);
hr = outputNode->SetUINT32(MF_TOPONODE_NOSHUTDOWN_ON_REMOVE, FALSE);
hr = pTopology->AddNode(outputNode);
hr = sourceNode->ConnectOutput(0, outputNode, 0);
}
}
hr = _mediaSession->SetTopology(0, pTopology);
PROPVARIANT varStart;
PropVariantInit(&varStart);
hr = _mediaSession->Start(&GUID_NULL, &varStart);
PropVariantClear(&varStart);
system("pause");
return 0;
}