DShow Sample Code for playing video does not play the video

那年仲夏 提交于 2020-01-06 20:07:33

问题


The example program provided here to run a video file. I am using "avi" format file for playing with DShow APIs in Visual Studio 2015.

Refer to the complete code:

#include<dshow.h>
#include<iostream>

using namespace std;

int CALLBACK WinMain(
    _In_ HINSTANCE hInstance,
    _In_ HINSTANCE hPrevInstance,
    _In_ LPSTR     lpCmdLine,
    _In_ int       nCmdShow
    )
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent *pEvent = NULL;


    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        cout << "ERROR - Could not initialize COM library"<<endl;
        return -1;
    }

    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        cout << "EROR - Could not create the Filter Graph Manager";
        return -1 ;
    }

    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void**)&pEvent);

    hr = pGraph->RenderFile(L"C:\\Users\\sunil\\Documents\\Ramp\\output.avi", NULL);

    if (SUCCEEDED(hr))
    {
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);
        }

    }

    pControl->Release();
    pEvent->Release();
    pGraph->Release();
    CoUninitialize();
    return 0;
}

The build is successful but when I run it a windows pops up with the title "ActiveMovie window". But there is no video in the window.

I referred to the comments on the same page and many others faced the same issue. However a few were able to successfully run the program.

What is it that I am doing wrong?

My question is taken from another similar question. I could not comment in that question coz I am new to Stack Overflow.


回答1:


The code is about right (plays for me just as is). Typical problems with the code include:

  1. there is a codec issue, you are trying to play a file with specific encoding, which is not picked up or handling is overridden by another third party software (which might be okay on its own but creates a problem together with #2 below).
  2. you are ignoring the fact that WaitForCompletion call is blocking execution on the thread and you are in the same time responsible for dispatching window messages there because you initialized COM as STA.

An easy way to find out if #2 is the problem is placing MessageBox call between Run and WaitForCompletion. MessageBox dispatches messages for you and as long as you keep the box open, video plays as well (or start playing well and keeps playing even after you close the box). Proper solution is to wait and displach messages in the same time (WaitDispatchingMessages, this SO question or similar).



来源:https://stackoverflow.com/questions/35624172/dshow-sample-code-for-playing-video-does-not-play-the-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!