實驗8 數(shù)字視頻播放器120515修改_第1頁
實驗8 數(shù)字視頻播放器120515修改_第2頁
實驗8 數(shù)字視頻播放器120515修改_第3頁
實驗8 數(shù)字視頻播放器120515修改_第4頁
實驗8 數(shù)字視頻播放器120515修改_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗8 數(shù)字視頻播放器實驗目的:1. 進一步掌握MCI函數(shù)的應用。2. 制作數(shù)字視頻播放器,并具有以下功能:打開視頻播放設備,播放視頻文件,可靜態(tài)觀看每一幀視頻,并且可設置播放速度,還能在全屏播放與正常播放之間轉換。實驗步驟:一 如實驗1建立應用程序框架二 制作應用程序界面1. 制作對話框對話框控件屬性表控件ID標題控件ID標題pictrueIDC_DISPLAY StaticIDC_PLAY_SOUNDGroup BoxIDC_STATIC視頻信息StaticIDC_FRAMESStaticIDC_STATIC視頻文件名:StaticIDC_DEMENSIONStaticIDC_STATIC

2、播放速度:ButtonIDC_OPEN打開StaticIDC_STATIC是否靜音:ButtonIDC_PLAY播放StaticIDC_STATIC視頻幀數(shù):ButtonIDC_PAUSE暫停StaticIDC_STATIC視頻尺寸:ButtonIDC_PRE上一幀StaticIDC_VIDEO_NAMEButtonIDC_NEXT下一幀StaticIDC_PLAY_SPEEDButtonIDC_STOP停止現(xiàn)在在應用程序中插入一個對話框資源(插入資源對話框),用來設置播放速度,其ID設為IDD_SPEED:IDD_SPEED對話框控件屬性表控件ID標題Edit BoxIDC_SPEED1Bu

3、ttonIDOK確定ButtonIDCANCEL取消還需象應用程序中插入一個對話框資源,用做視頻輸出窗口,ID為IDD_DISPLAY。2. 制作菜單插入一個菜單資源,將ID設為IDR_MAINFRAME。IDR_MAINFRAME 中的菜單項菜單標題菜單項標題ID文件打開IDC_OPEN關閉IDC_OK選項靜音IDC_SOUND全屏IDC_FULLSCREEN循環(huán)播放IDC_LOOP視頻速度IDC_SPEED在你的主對話框的屬性中的菜單項中,選中IDR_MAINFRAME現(xiàn)在編譯一下,就得到了我們所要的界面。3. 添加消息處理函數(shù)(查看建立類向導)在C*Dlg中需要添加的消息處理函數(shù)ID消息

4、IDC_PLAYBN_CLICKEDIDC_STOPBN_CLICKEDIDC_PAUSEBN_CLICKEDIDC_PREBN_CLICKEDIDC_NEXTBN_CLICKEDIDC_OPENBN_CLICKEDIDC_SPEEDCOMMANDIDC_FULLSCREENCOMMANDIDC_LOOPCOMMANDIDC_SOUNDCOMMANDC*DlgWM_TIMER然后為IDD_SPEED對話框資源添加一個int m_speed成員變量(選擇IDD_SPEED對話框資源,右鍵選中建立類向導,新建類CSpeed,在該類中選中成員變量一項,選中ID_SPEED1雙擊添加成員變量int m

5、_speed)三 完成應用程序功能設計1. 將已設計好的類Cvideo加到你的工程中來(先把文件video.cpp和video.h拷貝到工程文件目錄,在fileview中分別選擇源文件和頭文件目錄,右鍵選擇將文件添加到工程中,把這兩個文件添加進工程目錄中),并使C*Dlg的頭文件包含該類的頭文件(#include “video.h”; #include “speed.h” ),然后在C*Dlg類的聲明中加入以下代碼:protected:CVideo m_Video;bool m_bIsOpen;bool m_bSound;bool m_bLoop;bool m_bFullScreen;CDia

6、log *display;2. 初始化你的主對話框在OnInitDlialog()中/ TODO: Add extra initialization here后加入以下代碼:display=new CDialog;display->Create(IDD_DISPLAY);SetTimer(1,100,NULL);3. 完成消息函數(shù)OnTimer, OnOpen, OnPlay, OnStop, OnPause,OnPre, OnNext, OnSound, OnLoop, OnFullScreen, OnSpeedvoid CVideoPlayerDlg:OnTimer(UINT nID

7、Event) / TODO: Add your message handler code here and/or call defaultCWnd *m_pWnd=GetDlgItem(IDC_PLAY);m_pWnd->EnableWindow(m_bIsOpen);m_pWnd=GetDlgItem(IDC_PAUSE);m_pWnd->EnableWindow(m_bIsOpen);m_pWnd=GetDlgItem(IDC_STOP);m_pWnd->EnableWindow(m_bIsOpen);m_pWnd=GetDlgItem(IDC_NEXT);m_pWnd-

8、>EnableWindow(m_bIsOpen);m_pWnd=GetDlgItem(IDC_PRE);m_pWnd->EnableWindow(m_bIsOpen);if(m_bIsOpen)CString Info;Info.Format("當前播放幀數(shù):%d",m_Video.GetFrame();m_pWnd=GetDlgItem(IDC_DISPLAY);/獲得靜態(tài)圖像控件位置CClientDC dc(m_pWnd);CRect rect;CBrush brush(COLORREF(0),*OldBrush;OldBrush=(CBrush*)dc.S

9、electObject(brush);m_pWnd->GetClientRect(rect);/獲得靜態(tài)圖像控件大小dc.SetTextColor(COLORREF RGB(0,0,255);dc.SetBkColor(COLORREF RGB(0,0,0);dc.FillRect(rect,&brush);CSize size=dc.GetTextExtent(Info);/獲得文本尺寸dc.TextOut(rect.Width()-size.cx)/2,(rect.Height()-size.cy)/2,Info);dc.SelectObject(&OldBrush)

10、;CString str;str.Format("%d",m_Video.GetSpeed();SetDlgItemText(IDC_PLAY_SPEED,str);str.Format("%d",m_Video.GetFrames();SetDlgItemText(IDC_FRAMES,str);if(m_Video.GetSound()str=_T("否");elsestr=_T("是");SetDlgItemText(IDC_PLAY_SOUND,str);CDialog:OnTimer(nIDEvent);

11、void CVideoPlayerDlg:OnOpen() / TODO: Add your control notification handler code hereCString Filter,str;Filter=" 視頻文件(*.AVI)|*.AVI|"/ 得到窗口尺寸CRect rect;this->GetWindowRect(rect);CFileDialog FileDlg(true,NULL,NULL,OFN_HIDEREADONLY,Filter);if(FileDlg.DoModal()=IDOK)/單擊文件選擇對話框的“確定”鍵if(!m_Vi

12、deo.Open(FileDlg.GetPathName(),display,0,0)/打開視頻文件MessageBox("不能打開文件!","Error",MB_OK|MB_ICONSTOP);return;/將顯示屏幕定位在主對話框的下方,并根據(jù)視頻大小確定屏幕大小int Border,Caption;Border=GetSystemMetrics(SM_CXSIZEFRAME);Caption=GetSystemMetrics(SM_CYCAPTION);display->SetWindowPos(this,rect.left+30,rect

13、.bottom+30,m_Video.GetWidth()+Border,m_Video.GetHeight()+Caption+Border,SWP_SHOWWINDOW);display->ShowWindow(true);display->SetWindowText(FileDlg.GetFileName();SetDlgItemText(IDC_VIDEO_NAME,FileDlg.GetFileName();str.Format("%d×%d",m_Video.GetWidth(),m_Video.GetHeight();SetDlgItem

14、Text(IDC_DEMENSION,str);m_bIsOpen=true;void CVideoPlayerDlg:OnPlay() / TODO: Add your control notification handler code herem_Video.Play(m_bLoop,m_bFullScreen);void CVideoPlayerDlg:OnStop() / TODO: Add your control notification handler code herem_Video.Stop();void CVideoPlayerDlg:OnPause() / TODO: A

15、dd your control notification handler code herem_Video.Pause();void CVideoPlayerDlg:OnPre() / TODO: Add your control notification handler code herem_Video.Backward(1);void CVideoPlayerDlg:OnNext() / TODO: Add your control notification handler code herem_Video.Forward(1);void CVideoPlayerDlg:OnSound()

16、 / TODO: Add your command handler code herem_bSound=!m_bSound;m_Video.SetSound(m_bSound);void CVideoPlayerDlg:OnSpeed() / TODO: Add your command handler code hereCSpeed dlg;dlg.m_speed=m_Video.GetSpeed();if(dlg.DoModal()=IDOK)m_Video.SetSpeed(dlg.m_speed);void CVideoPlayerDlg:OnFullscreen() / TODO:

17、Add your command handler code herem_bFullScreen=!m_bFullScreen;void CVideoPlayerDlg:OnLoop() / TODO: Add your command handler code herem_bLoop=!m_bLoop;現(xiàn)在編譯執(zhí)行,視頻播放器變完成了。實驗注意事項:1. 調試程序時,Ctrl+F7 為Compile,Build為F7,運行時按F5。2. 注意可重用類CVideo的設計。3. 熟練掌握MCI多媒體函數(shù)的應用。4. 編譯時,菜單選中“項目”à“設置”,要在庫的鏈接中加入winmm.lib

18、。/ Video.cpp: implementation of the CVideo class./#include "stdafx.h"#include "Video.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE=_FILE_;#define new DEBUG_NEW#endif/ Construction/Destruction/CVideo:CVideo()/ 初始化成員變量m_bOpened = m_bPaused = m_bPlaying = FALSE;m_wDeviceID =

19、0;CVideo:CVideo()Close ();BOOL CVideo:Open (CString Filename, CWnd *pWnd, int left, int top)if (!m_bOpened)/ 初始化成員變量m_bOpened = m_bPaused = m_bPlaying = FALSE;m_wDeviceID = 0;/ 文件是否存在CFileStatus Status;if (!CFile:GetStatus (Filename, Status)return FALSE;/ 打開設備MCI_DGV_OPEN_PARMS OpenParms;OpenParms.l

20、pstrDeviceType = "avivideo"/設置播放設備為視頻播放設備OpenParms.lpstrElementName = (LPSTR) Filename.GetBuffer (0);OpenParms.wDeviceID = 0;OpenParms.hWndParent = pWnd->m_hWnd;/將要播放視頻文件的主窗口OpenParms.dwStyle = WS_CHILD | WS_VISIBLE;if (mciSendCommand (NULL, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE | MCI_OPEN

21、_ELEMENT | MCI_DGV_OPEN_PARENT | MCI_DGV_OPEN_WS, (DWORD)(LPVOID) &OpenParms)return FALSE;m_wDeviceID = OpenParms.wDeviceID;m_bOpened = TRUE;/ 將文件長度設置為幀MCI_DGV_SET_PARMS SetParms;SetParms.dwTimeFormat = MCI_FORMAT_FRAMES;if (mciSendCommand (m_wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD)(LPVO

22、ID) &SetParms)Close ();return FALSE;/ 找到視頻文件的開始位置mciSendCommand (m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL);/將播放畫面置于主窗口之中MCI_DGV_PUT_PARMS PutParms;PutParms.rc.left = left;PutParms.rc.top = top;PutParms.rc.right = GetWidth ();PutParms.rc.bottom = GetHeight ();if (mciSendCommand (m_wDeviceID

23、, MCI_PUT, MCI_WAIT | MCI_DGV_PUT_CLIENT | MCI_DGV_PUT_WINDOW | MCI_DGV_RECT, (DWORD)(LPVOID) &PutParms)Close ();return FALSE;return TRUE;void CVideo:Close (void)if (m_bOpened)/ 停止播放并關閉設備if (m_bPlaying | m_bPaused)mciSendCommand (m_wDeviceID, MCI_STOP, NULL, NULL);mciSendCommand (m_wDeviceID, MC

24、I_CLOSE, NULL, NULL);/ 清空成員變量m_bOpened = m_bPaused = m_bPlaying = FALSE;m_wDeviceID = 0;/獲得視頻文件窗口的寬度int CVideo:GetWidth (void)if (m_bOpened)MCI_DGV_WHERE_PARMS WhereParms;if (mciSendCommand (m_wDeviceID, MCI_WHERE, MCI_DGV_WHERE_SOURCE, (DWORD)(LPVOID) &WhereParms)return 0;return (int) WhereParm

25、s.rc.right;return 0;/獲得視頻文件窗口的高度int CVideo:GetHeight (void)if (m_bOpened)MCI_DGV_WHERE_PARMS WhereParms;if (mciSendCommand (m_wDeviceID, MCI_WHERE, MCI_DGV_WHERE_SOURCE, (DWORD)(LPVOID) &WhereParms)return 0;return (int) WhereParms.rc.bottom;return 0;BOOL CVideo:Play (BOOL bLoop, BOOL bFullScreen

26、)if (m_bOpened)/ 如果是視頻的尾部則重新開始播放if (GetFrame () >= GetFrames ()mciSendCommand (m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL);/ 播放MCI_DGV_PLAY_PARMS PlayParms;PlayParms.dwCallback = NULL;PlayParms.dwFrom = GetFrame ();DWORD dwFlags = MCI_FROM;if (bLoop)dwFlags |= MCI_DGV_PLAY_REPEAT;if (bFullScr

27、een)dwFlags |= MCI_MCIAVI_PLAY_FULLSCREEN;if (mciSendCommand (m_wDeviceID, MCI_PLAY, dwFlags, (DWORD)(LPVOID) &PlayParms)return FALSE;m_bPaused = FALSE;m_bPlaying = TRUE;return TRUE;return FALSE;void CVideo:Stop (void)if (m_bOpened && m_bPlaying)mciSendCommand (m_wDeviceID, MCI_STOP, NUL

28、L, NULL);mciSendCommand (m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL);m_bPlaying = FALSE;m_bPaused = FALSE;void CVideo:Pause( void )if (m_bOpened && m_bPlaying)MCI_DGV_PAUSE_PARMS PauseParms;if (mciSendCommand (m_wDeviceID, MCI_PAUSE, 0, (DWORD)(LPVOID) &PauseParms)return;m_bPaused =

29、TRUE;int CVideo:GetFrames (void)if (m_bOpened)MCI_DGV_STATUS_PARMS StatusParms;StatusParms.dwItem = MCI_STATUS_LENGTH;if (mciSendCommand (m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms)return 0;return (int) StatusParms.dwReturn;return 0;int CVideo:GetFrame (voi

30、d)if (m_bOpened)MCI_DGV_STATUS_PARMS StatusParms;StatusParms.dwItem = MCI_STATUS_POSITION;if (mciSendCommand (m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms)return -1;return (int) StatusParms.dwReturn;return -1;BOOL CVideo:IsPlaying (BOOL *pbPaused)*pbPaused =

31、m_bPaused;if (m_bOpened)MCI_DGV_STATUS_PARMS StatusParms;StatusParms.dwItem = MCI_STATUS_MODE;if (mciSendCommand (m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms)return FALSE;if (StatusParms.dwReturn = MCI_MODE_PLAY)m_bPlaying = TRUE;m_bPaused = FALSE;return TRU

32、E;else if (StatusParms.dwReturn = MCI_MODE_PAUSE)m_bPlaying = TRUE;m_bPaused = FALSE;return TRUE;elsem_bPlaying = FALSE;m_bPaused = FALSE;return FALSE;return FALSE;BOOL CVideo:SeekTo (int nFrame)if (m_bOpened)MCI_SEEK_PARMS SeekParms;SeekParms.dwTo = nFrame;if (m_bPlaying && !m_bPaused)mciSe

33、ndCommand (m_wDeviceID, MCI_PAUSE, 0, NULL);if (mciSendCommand (m_wDeviceID, MCI_SEEK, MCI_TO | MCI_WAIT, (DWORD)(LPVOID) &SeekParms)return FALSE;if (m_bPlaying && !m_bPaused)Play ();return TRUE;return FALSE;void CVideo:Forward (int nFrames)if (m_bOpened)if (m_bPlaying && !m_bPau

34、sed)Pause ();MCI_DGV_STEP_PARMS StepParms;StepParms.dwFrames = nFrames;mciSendCommand (m_wDeviceID, MCI_STEP, MCI_DGV_STEP_FRAMES, (DWORD)(LPVOID) &StepParms);void CVideo:Backward (int nFrames)if (m_bOpened)if (m_bPlaying && !m_bPaused)Pause ();MCI_DGV_STEP_PARMS StepParms;StepParms.dwFrames = nFrames;mciSendCommand (m_wDeviceID, MCI_STEP, MC

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論