0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DirectShowで動画再生

Last updated at Posted at 2022-09-07

実行環境

Visual Studio2019 C++
Windows10

DirectShow

DirectShowは、Microsoft Windowsのメディア ストリーミング アーキテクチャであり、高品質のビデオとオーディオの再生またはキャプチャを実行できる。

動画再生

追加依存ファイルに「Strmiids.lib」を設定。
Strmiids.png
コード

main.cpp
#include <dshow.h>

int main(void)
{
    IGraphBuilder* pGraph = NULL;
    IMediaControl* pControl = NULL;
    IMediaEvent* pEvent = NULL;

    //COMライブラリを初期化。
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return 0;
    }
    //FilterGraphManagerを作成。
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
        IID_IGraphBuilder, (void**)&pGraph);
    if (FAILED(hr))
    {
        printf("ERROR - Could not create the Filter Graph Manager.");
        return 0;
    }

    //FilterGraphManagerにクエリを実行して、フィルターグラフを作成。
    //IMediaControlはストリーミング再生を制御する。
    hr = pGraph->QueryInterface(IID_IMediaControl, (void**)&pControl);
    //IMediaEventはGraphManagerからのイベントを取得するためのメソッドがある。
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void**)&pEvent);
    //指定したファイルを再生するためのフィルターグラフを作成。
    //第一引数にファイル名、第二引数はNULLでよい。
    hr = pGraph->RenderFile(L"C:\\Example.avi", NULL);
    if (SUCCEEDED(hr))
    {
        //フィルターグラフを実行(動画再生)。
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            long evCode;
            //動画再生が完了するまで待機。
            pEvent->WaitForCompletion(INFINITE, &evCode);

        }
    }
    //各ポインタを解放。
    pControl->Release();
    pEvent->Release();
    pGraph->Release();
    //COMライブラリを解放。
    CoUninitialize();
    return 0;
}

mp4ファイル再生には対応していない。
ファイルによっては、ノイズが発生したりする。

参考

Microsoft公式ドキュメント

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?