再生終了時間の取得

今回はメディアの再生終了時間を取得します。
再生が終わる時間がわかればループ再生の処理なども可能になります。

 

#pragma comment (lib, "strmiids.lib")

#include <windows.h>
#include <string>
#include <dshow.h>

using namespace std;

#define FileName TEXT("test.mid")

IGraphBuilder *pGB;
IMediaControl *pMC;
IMediaSeeking *pMSeek;
long length,end;
HWND hwnd;
HDC hdc;
wchar_t buf[256];
wstring str;

//ミリ秒表示用
wchar_t* L2A(long a)
{
 _ltow_s(a/100000, buf,256, 10);
 return buf;
}

void Init()
{
 //COMを初期化
 CoInitialize(NULL);
 //FilterGraphを生成
 CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,
 IID_IGraphBuilder,(LPVOID *)&pGB);

 //MediaControlインターフェース取得
 pGB->QueryInterface(IID_IMediaControl,
 (LPVOID *)&pMC);

 //Graphを生成
 pMC->RenderFile(FileName);

 //再生開始
 pMC->Run();
}
//コールバック
LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp)
{
 switch (msg) {
  case WM_KEYDOWN:        // キーが押されたとき
   if( wp == VK_RETURN ){//再生
    pMC->Run();
    return 0;
   }
   if( wp == VK_SPACE ){//一時停止
    pMC->Pause();
    return 0;
   }
   break;
  //終了処理
  case WM_DESTROY:
   //資源を解放
   pMC->Release();
   pGB->Release();

   //COM終了
   CoUninitialize();
   PostQuitMessage(0);
   return 0;
 }

 return DefWindowProc(hwnd , msg , wp , lp);
}

DWORD WINAPI MainLoop(LPVOID vdParam)
{
 hdc = GetDC(hwnd);
 while(TRUE){
  pGB->QueryInterface(IID_IMediaSeeking,(LPVOID *)&pMSeek);
  pMSeek->GetCurrentPosition((LONGLONG *)&length);//現在の再生時間を取得
  str=TEXT("現在の再生位置:");
  str+=L2A((long)length);
  str+=TEXT("         ");
  TextOut( hdc, 50, 90,str.c_str(),str.length());
 
  pMSeek->GetDuration((LONGLONG *)&end);
  str=TEXT("再生メディアの終了時間:");
  str+=L2A((long)end);
  str+=TEXT("         ");
  TextOut( hdc, 50, 70,str.c_str(),str.length());
 
  Sleep(16);
 }
 return 0;
}
//メイン
int WINAPI WinMain(HINSTANCE hInst , HINSTANCE hPrevI , PSTR lpCmd , int nCmdShow)
{
 MSG msg;
 WNDCLASS winc;
 DWORD dwID;

 winc.style  = CS_HREDRAW | CS_VREDRAW;
 winc.lpfnWndProc = WndProc;
 winc.cbClsExtra = winc.cbWndExtra = 0;
 winc.hInstance  = hInst;
 winc.hIcon  = LoadIcon(NULL , IDI_APPLICATION);
 winc.hCursor  = LoadCursor(NULL , IDC_ARROW);
 winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 winc.lpszMenuName = NULL;
 winc.lpszClassName = TEXT("window");

 if (!RegisterClass(&winc)) return 0;

 hwnd = CreateWindow(TEXT("window") , TEXT("Hellow DirectShow World!!") ,
   WS_OVERLAPPEDWINDOW  | WS_VISIBLE ,
   100 , 100 , 320 , 240 , NULL , NULL ,hInst , NULL);

 if (hwnd == NULL) return 0;

 Init();
 CreateThread(NULL , 0 , MainLoop , (LPVOID)hwnd , 0 , &dwID);
 while (GetMessage(&msg , NULL , 0 , 0)) DispatchMessage(&msg);
 return msg.wParam;
}

 

最終更新:2011年02月08日 11:48
添付ファイル