|

楼主 |
发表于 2007-3-7 19:09:00
|
显示全部楼层
Re:游戏中的dshow媒体播放-即学即会-详细注释
3。进一步实现游戏中的一些需要(播放暂停,循环,Esc跳过动画播放等)
使用另一种方式循环播放:
用一个定时器,每1000ms检查一下当前播放位置,如果播放到结束,就重放一遍。
#include <dshow.h>
#include <windows.h>
#pragma comment (lib,"Ole32.lib")
#pragma comment (lib,"Strmiids.lib")
#define ture (bool)-1
//you should REPLACE the media file path
#define FILENAME L"C:\\Documents and Settings\\vs6\\桌面\\temp\\FL5007822001-500-2.avi"
#define WM_GRAPH_NOTIFY (WM_APP + 1) //the msg of g_pMediaEventEx
bool g_bExit=false; //this APP will be exited when g_bExit=TURE
HWND g_hwnd=0; //our player's main window
IMediaEventEx *g_pMediaEventEx; //recieve event msgs from directshow
//it notifies us when the media's playing is COMPLETE
IMediaControl *g_pMediaControl; //help us to link the filters(media file,codec...),
//in short,it helps us open and play a file EASILY.
IMediaPosition *g_pMediaPosition; //we use this to get/set the POSITION of a media's playing
double g_Len;
bool g_bIsPlaying=false;
LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
void WINAPI OnTimer(HWND, UINT, UINT, DWORD);
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR CmdLine,int nShowCmd)
{
IGraphBuilder *pGraphBuilder; //this is the kernel of directshow
IVideoWindow *pVideoWindow; //use this to control the video window of directshow
//init COM
CoInitialize(NULL);
//create FilterGraph:
CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,IID_IGraphBuilder,
(LPVOID*)&pGraphBuilder);
//ask directshow for the MediaControl:
pGraphBuilder->QueryInterface(IID_IMediaControl,(LPVOID*)&g_pMediaControl);
//ask directshow for the MediaPosition:
pGraphBuilder->QueryInterface(IID_IMediaPosition,(LPVOID*)&g_pMediaPosition);
//ask directshow for the MediaEventEx:
pGraphBuilder->QueryInterface(IID_IMediaEventEx,(LPVOID*)&g_pMediaEventEx);
//start to create a window and show it
WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,WinProc,0L,0L,
GetModuleHandle(0),0,0,0,0,"instemast",0};
RegisterClassEx(&wc);
//our window's CLIENT positions:
RECT rect={100,100,640+100-1,480+100-1};//width=right-left+1 , right=left+width-1
//calculate our window's REAL positions from client positions:
::AdjustWindowRect(&rect,WS_OVERLAPPEDWINDOW,false);
//NOW, rect's values are updated
g_hwnd=CreateWindow("instemast","Esc=stop Space=pause/play F1=speed+ F2=speed- F3=normal",WS_OVERLAPPEDWINDOW,rect.left,rect.top,
rect.right-rect.left,rect.bottom-rect.top,0,0,wc.hInstance,0);
::ShowWindow(g_hwnd,nShowCmd);
//end of creating window
//we will recieve directshow's msg by g_hwnd:
g_pMediaEventEx->SetNotifyWindow((OAHWND)g_hwnd,WM_GRAPH_NOTIFY, NULL);
//create Graph:
g_pMediaControl->RenderFile(FILENAME);
//get our media's length:
g_pMediaPosition->get_Duration(&g_Len);
//ask directshow for the VideoWindow interface:
pGraphBuilder->QueryInterface(IID_IVideoWindow,(LPVOID*)&pVideoWindow);
pVideoWindow->put_Owner((OAHWND)g_hwnd); //VIDEOwindow is the child of OUR g_hwnd
pVideoWindow->put_WindowStyle(WS_CHILD|WS_CLIPSIBLINGS);
pVideoWindow->SetWindowPosition(0,0,rect.right-rect.left+1,rect.bottom-rect.top+1);
pVideoWindow->put_Visible(OATRUE);
//start playing media:
g_pMediaControl->Run();
g_bIsPlaying=ture;
::SetTimer(g_hwnd,0,1000,&OnTimer);
MSG msg;
do
{
GetMessage(&msg,0,0,0);
::TranslateMessage(&msg);
: ispatchMessage(&msg);
}while(!g_bExit); //do msg loop,until g_bExit=TURE
//release the resource:
g_pMediaEventEx->Release();
pVideoWindow->Release();
g_pMediaControl->Release();
pGraphBuilder->Release();
//COM destroy:
CoUninitialize();
return 0;
}
LRESULT WINAPI WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
double rate;
switch(msg)
{
case WM_KEYDOWN:
switch(wparam)
{
case 27: //[Esc] key--Stop media
g_pMediaControl->Stop(); //stop media
g_bIsPlaying=false;
//show FIRST frame:
g_pMediaPosition->put_CurrentPosition(0);
g_pMediaControl->Run();
::Sleep(2);
g_pMediaControl-> ause();
//stop media:
g_pMediaPosition->put_CurrentPosition(0);
g_pMediaControl->Stop();
break;
case ' ': //pause or resume or play
if(g_bIsPlaying)
{
g_pMediaControl->Pause();
g_bIsPlaying=false;
}
else
{
g_pMediaControl->Run();
g_bIsPlaying=ture;
};
break;
case VK_F1: //rate(speed): 1.0=normal
g_pMediaPosition->get_Rate(&rate);
g_pMediaPosition->put_Rate(rate*1.25);
break;
case VK_F2:
g_pMediaPosition->get_Rate(&rate);
g_pMediaPosition->put_Rate(rate*0.8);
break;
case VK_F3:
g_pMediaPosition->put_Rate(1.0);
};
break;
case WM_DESTROY:
g_pMediaControl->Stop(); //stop our media
g_bIsPlaying=false;
g_bExit=ture; //our g_hwnd has been closed,so we want to exit the app
break;
};
return DefWindowProc(hwnd,msg,wparam,lparam);
}
void WINAPI OnTimer(HWND hwnd, UINT, UINT, DWORD)
{
double pos;
g_pMediaPosition->get_CurrentPosition(&pos);
if(pos>=g_Len)
{
g_pMediaPosition->put_CurrentPosition(0);
g_pMediaControl->Run();
};
} |
|