DShow用于播放视频的示例代码无法播放视频 [英] DShow Sample Code for playing video does not play the video

查看:62
本文介绍了DShow用于播放视频的示例代码无法播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例程序在此处运行视频文件。我正在使用 avi格式的文件在Visual Studio 2015中玩DShow API。

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.

请参阅完整的代码:

#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;
}

构建成功,但是当我运行它时,会弹出一个带有标题的窗口 ActiveMovie窗口。
但是窗口中没有视频。

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.

我做错了什么事?

我的问题来自另一个类似的问题。我无法在这个问题中发表评论,因为我是Stack Overflow的新手。

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

推荐答案

代码是正确的(对我来说只是玩原样)。代码的典型问题包括:

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


  1. 存在编解码器问题,您正在尝试播放未选择特定编码的文件up或处理被另一个第三方软件覆盖(它本身可能还可以,但与下面的#2一起会造成问题)。

  2. 您忽略了 WaitForCompletion 调用阻止了线程的执行,并且您同时负责在其中调度窗口消息,因为您将COM初始化为STA。

  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.

一种简单的方法来找出问题2是否是在运行 MessageBox 调用c $ c>和 WaitForCompletion MessageBox 会为您调度消息,只要您保持打开状态,视频也会播放(或者,即使关闭此框也可以开始播放并继续播放)。正确的解决方案是同时等待和发送消息( WaitDispatchingMessages 此SO问题或类似的内容。

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).

这篇关于DShow用于播放视频的示例代码无法播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆