一個簡單的directshow的例子

//DirectShow應用程序的三個步驟
//1:建立圖像濾鏡管理器的實例
//2:用圖像濾鏡管理器建立一個圖像濾鏡
//3:給其運行消息,讓數據流在各個濾鏡中移動

//!!!!!!!!!!!!!!!!!    添加相應的庫後  此程序沒問題
//工程->設置->鏈接,添加strmbasd.lib ole32.lib
#include <dshow.h>
#include <stdio.h>
void main(void)
{
 IGraphBuilder *pGraph = NULL;  //圖像濾鏡管理器
 IMediaControl *pControl = NULL;     //控制數據流
 IMediaEvent   *pEvent = NULL;  //獲取圖像濾鏡的事件
    // Initialize the COM library.
// printf("ff\n");                           //用於調試    查找何處有問題
 HRESULT hr = CoInitialize(NULL);            //初始化com庫
 if (FAILED(hr))
 {
  printf("ERROR - Could not initialize COM library");
  return;
 }
 
    // Create the filter graph manager and query for interfaces. 
 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;
 }
//  printf("ff1\n");
    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);     //查詢接口
 hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
//  printf("ff2\n");
    // Build the graph. IMPORTANT: Change this string to a file on your system.  改變視頻地址
 hr = pGraph->RenderFile(L"zhanguo.avi", NULL);      //將視頻拷貝到當前工程目錄下
//  printf("ff3\n");                       
 if (SUCCEEDED(hr))
 {
//   printf("ff4\n");
  // Run the graph.
  hr = pControl->Run();
  if (SUCCEEDED(hr))
  {
   // Wait for completion.
   long evCode;
   pEvent->WaitForCompletion(INFINITE, &evCode);    //等待視頻結束
   printf("ff");
            // Note: Do not use INFINITE in a real application, because it
   // can block indefinitely.
  }
 }
 pControl->Release();       //釋放藉口
 pEvent->Release();   
 pGraph->Release();
 CoUninitialize();    //關閉com口
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章