📄 readyplayvideo.h
字号:
#include <dshow.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <tchar.h>
#include <atlbase.h>
#pragma comment(lib,"strmiids.lib")
HRESULT InitPlayerWindow(void);
HRESULT InitVideoWindow(int nMultiplier, int nDivider);
HRESULT HandleGraphEvent(void);
void PaintAudioWindow(void);
void CheckVisibility(void);
void CloseInterfaces(void);
void OpenClip(void);
void StopClip(void);
void CloseClip(void);
void Msg(TCHAR *szFormat, ...);
HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister);
void RemoveGraphFromRot(DWORD pdwRegister);
#define VOLUME_FULL 0L
#define VOLUME_SILENCE -10000L
extern HWND main_window_handle;
extern HINSTANCE hinstance;
#define DEFAULT_MEDIA_PATH TEXT("\\\0")
#define WM_GRAPHNOTIFY WM_USER+13
enum PLAYSTATE {Stopped, Paused, Running, Init};
#define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }
#define JIF(x) if (FAILED(hr=(x))) \
{Msg(TEXT("FAILED(hr=0x%x) in ") TEXT(#x) TEXT("\n"), hr); return hr;}
#define LIF(x) if (FAILED(hr=(x))) \
{Msg(TEXT("FAILED(hr=0x%x) in ") TEXT(#x) TEXT("\n"), hr);}
HRESULT ToggleFullScreen();
#define REGISTER_FILTERGRAPH
BOOL g_bAudioOnly=FALSE, g_bFullscreen=FALSE;
LONG g_lVolume=VOLUME_FULL;
DWORD g_dwGraphRegister=0;
PLAYSTATE g_psCurrent=Stopped;
double g_PlaybackRate=1.0;
IGraphBuilder *pGB = NULL;
IMediaControl *pMC = NULL;
IMediaEventEx *pME = NULL;
IVideoWindow *pVW = NULL;
IBasicAudio *pBA = NULL;
IBasicVideo *pBV = NULL;
IMediaSeeking *pMS = NULL;
IMediaPosition *pMP = NULL;
IVideoFrameStep *pFS = NULL;
HRESULT PlayMovieInWindow(LPTSTR szFile)
{
USES_CONVERSION;
WCHAR wFile[MAX_PATH];
HRESULT hr;
UpdateWindow(main_window_handle);
wcscpy(wFile, T2W(szFile));
JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGB));
if(SUCCEEDED(hr))
{
JIF(pGB->RenderFile(wFile, NULL));
JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC));
JIF(pGB->QueryInterface(IID_IMediaEventEx, (void **)&pME));
JIF(pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS));
JIF(pGB->QueryInterface(IID_IMediaPosition, (void **)&pMP));
JIF(pGB->QueryInterface(IID_IVideoWindow, (void **)&pVW));
JIF(pGB->QueryInterface(IID_IBasicVideo, (void **)&pBV));
JIF(pGB->QueryInterface(IID_IBasicAudio, (void **)&pBA));
JIF(pME->SetNotifyWindow((OAHWND)main_window_handle, WM_GRAPHNOTIFY, 0));
if (!g_bAudioOnly)
{
JIF(pVW->put_Owner((OAHWND)main_window_handle));
JIF(pVW->put_WindowStyle(CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW));
JIF(InitVideoWindow(2,1));
}
else
{
JIF(InitPlayerWindow());
}
ShowWindow(main_window_handle, SW_SHOWNORMAL);
UpdateWindow(main_window_handle);
SetForegroundWindow(main_window_handle);
SetFocus(main_window_handle);
g_bFullscreen = FALSE;
g_PlaybackRate = 2.0;
#ifdef REGISTER_FILTERGRAPH
hr = AddGraphToRot(pGB, &g_dwGraphRegister);
if (FAILED(hr))
{
Msg(TEXT("Failed to register filter graph with ROT! hr=0x%x"), hr);
g_dwGraphRegister = 0;
}
#endif
JIF(pMC->Run());
g_psCurrent=Running;
SetFocus(main_window_handle);
}
return hr;
}
HRESULT InitVideoWindow(int nMultiplier, int nDivider)
{
HRESULT hr = S_OK;
RECT rect;
GetClientRect(main_window_handle, &rect);
JIF(pVW->SetWindowPosition(rect.left, rect.top, rect.right, rect.bottom));
return hr;
}
HRESULT InitPlayerWindow(void)
{
SetWindowPos(main_window_handle, NULL, 0, 0,1280,1024,SWP_NOMOVE | SWP_NOOWNERZORDER);
return S_OK;
}
void StopClip(void)
{
HRESULT hr;
if ((!pMC) || (!pMS))
return;
if((g_psCurrent == Paused) || (g_psCurrent == Running))
{
LONGLONG pos = 0;
hr = pMC->Stop();
g_psCurrent = Stopped;
hr = pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
NULL, AM_SEEKING_NoPositioning);
hr = pMC->Pause();
}
}
void OpenClip()
{
HRESULT hr;
InitPlayerWindow();
SetForegroundWindow(main_window_handle);
g_psCurrent = Stopped;
g_lVolume = VOLUME_FULL;
hr = PlayMovieInWindow("2515.avi");
if (FAILED(hr))
CloseClip();
}
void CloseClip()
{
HRESULT hr;
if(pMC)
hr = pMC->Stop();
g_psCurrent = Stopped;
g_bAudioOnly = TRUE;
g_bFullscreen = FALSE;
CloseInterfaces();
g_psCurrent = Init;
RECT rect;
GetClientRect(main_window_handle, &rect);
InvalidateRect(main_window_handle, &rect, TRUE);
InitPlayerWindow();
}
void CloseInterfaces(void)
{
HRESULT hr;
if(pVW)
{
hr = pVW->put_Visible(OAFALSE);
hr = pVW->put_Owner(NULL);
}
if (pME)
hr = pME->SetNotifyWindow((OAHWND)NULL, 0, 0);
#ifdef REGISTER_FILTERGRAPH
if (g_dwGraphRegister)
{
RemoveGraphFromRot(g_dwGraphRegister);
g_dwGraphRegister = 0;
}
#endif
SAFE_RELEASE(pME);
SAFE_RELEASE(pMS);
SAFE_RELEASE(pMP);
SAFE_RELEASE(pMC);
SAFE_RELEASE(pBA);
SAFE_RELEASE(pBV);
SAFE_RELEASE(pVW);
SAFE_RELEASE(pFS);
SAFE_RELEASE(pGB);
}
#ifdef REGISTER_FILTERGRAPH
HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)
{
IMoniker * pMoniker;
IRunningObjectTable *pROT;
if (FAILED(GetRunningObjectTable(0, &pROT)))
{
return E_FAIL;
}
WCHAR wsz[128];
wsprintfW(wsz, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph,
GetCurrentProcessId());
HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);
if (SUCCEEDED(hr))
{
hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister);
pMoniker->Release();
}
pROT->Release();
return hr;
}
void RemoveGraphFromRot(DWORD pdwRegister)
{
IRunningObjectTable *pROT;
if (SUCCEEDED(GetRunningObjectTable(0, &pROT)))
{
pROT->Revoke(pdwRegister);
pROT->Release();
}
}
#endif
void Msg(TCHAR *szFormat, ...)
{
TCHAR szBuffer[512];
va_list pArgs;
va_start(pArgs, szFormat);
_vstprintf(szBuffer, szFormat, pArgs);
va_end(pArgs);
MessageBox(NULL, szBuffer, TEXT("PlayWnd Sample"), MB_OK);
}
HRESULT ToggleFullScreen(void)
{
HRESULT hr=S_OK;
LONG lMode;
static HWND hDrain=0;
if ((g_bAudioOnly) || (!pVW))
return S_OK;
JIF(pVW->get_FullScreenMode(&lMode));
if (lMode == OAFALSE)
{
LIF(pVW->get_MessageDrain((OAHWND *) &hDrain));
LIF(pVW->put_MessageDrain((OAHWND) main_window_handle));
lMode = OATRUE;
JIF(pVW->put_FullScreenMode(lMode));
g_bFullscreen = TRUE;
}
else
{
lMode = OAFALSE;
JIF(pVW->put_FullScreenMode(lMode));
LIF(pVW->put_MessageDrain((OAHWND) hDrain));
LIF(pVW->SetWindowForeground(-1));
UpdateWindow(main_window_handle);
SetForegroundWindow(main_window_handle);
SetFocus(main_window_handle);
g_bFullscreen = FALSE;
}
return hr;
}
HRESULT HandleGraphEvent(void)
{
LONG evCode, evParam1, evParam2;
HRESULT hr=S_OK;
if (!pME)
return S_OK;
while(SUCCEEDED(pME->GetEvent(&evCode, (LONG_PTR *) &evParam1,
(LONG_PTR *) &evParam2, 0)))
{
hr = pME->FreeEventParams(evCode, evParam1, evParam2);
if(EC_COMPLETE == evCode)
{
LONGLONG pos=0;
hr = pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
NULL, AM_SEEKING_NoPositioning);
if (FAILED(hr))
{
if (FAILED(hr = pMC->Stop()))
{
Msg(TEXT("Failed(0x%08lx) to stop media clip!\r\n"), hr);
break;
}
if (FAILED(hr = pMC->Run()))
{
Msg(TEXT("Failed(0x%08lx) to reset media clip!\r\n"), hr);
break;
}
}
}
}
return hr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -