⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playerdlg.cpp

📁 播放被H264_AAC所压缩的avi文件的播放器。
💻 CPP
字号:
// PlayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Player.h"
#include "PlayerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CPlayerDlg *theDlg = NULL;

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayerDlg dialog

CPlayerDlg::CPlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlayerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlayerDlg)
	DDX_Control(pDX, IDC_TIME, m_cTime);
	DDX_Control(pDX, IDC_TIME_SLIDER, m_TimeSlider);
	DDX_Control(pDX, IDC_VIDEO_VIEW, m_Screen);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CPlayerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_PLAY, OnPlay)
	ON_BN_CLICKED(IDC_BTN_STOP, OnStop)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_TIME_SLIDER, OnReleasedcaptureTimeSlider)
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBrowse)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayerDlg message handlers

BOOL CPlayerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	theDlg = this;
	CheckRadioButton(IDC_DIRECTDRAW, IDC_CALLBACK, IDC_DIRECTDRAW);

	m_strFileName = "";
	//m_pAVPlayer = new CAVPlayer(WriteFrameCallBack, m_Screen.m_hWnd);
	m_pAVPlayer = new CAVPlayer(NULL, m_Screen.m_hWnd);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPlayerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPlayerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPlayerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPlayerDlg::OnPlay() 
{
	if( !m_pAVPlayer )
		return;

	if( m_strFileName != "" && m_pAVPlayer->GetState() == READY )
	{
		m_hDib = DrawDibOpen();
		if( m_hDib )
		{
			m_pAVPlayer->GetAviHeader(&m_AviHdr);

			m_BitmapInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
			m_BitmapInfo.bmiHeader.biWidth = m_AviHdr.Width;
			m_BitmapInfo.bmiHeader.biHeight = m_AviHdr.Height;
			m_BitmapInfo.bmiHeader.biPlanes = 1;
			m_BitmapInfo.bmiHeader.biBitCount = 24;
			m_BitmapInfo.bmiHeader.biCompression = 0;
			m_BitmapInfo.bmiHeader.biSizeImage = 0;
			m_BitmapInfo.bmiHeader.biXPelsPerMeter = 0;
			m_BitmapInfo.bmiHeader.biYPelsPerMeter = 0;
			m_BitmapInfo.bmiHeader.biClrUsed = 0;
			m_BitmapInfo.bmiHeader.biClrImportant = 0;

			DrawDibBegin(m_hDib, 
						m_Screen.GetDC()->GetSafeHdc(), 
						-1,
						-1, 
						&m_BitmapInfo.bmiHeader, 
						m_AviHdr.Width, 
						m_AviHdr.Height,
						0);
		}
		m_TimeSlider.SetRange(0, m_pAVPlayer->GetTotalPlayTime(), TRUE);
		m_pAVPlayer->Play();
		GetDlgItem(IDC_BTN_PLAY)->SetWindowText("Pause");
	}
	else if( m_pAVPlayer->GetState() == PAUSE )
	{
		m_pAVPlayer->Resume();
		GetDlgItem(IDC_BTN_PLAY)->SetWindowText("Pause");
	}
	else if( m_pAVPlayer->GetState() == PLAY )
	{
		m_pAVPlayer->Pause();
		GetDlgItem(IDC_BTN_PLAY)->SetWindowText("Play");
	}
}

void CPlayerDlg::OnStop() 
{
	if( !m_pAVPlayer )
		return;

	if( m_pAVPlayer->GetState() != NONE )
	{
		m_pAVPlayer->Stop();
		GetDlgItem(IDC_BTN_PLAY)->SetWindowText("Play");
	}

	if( m_hDib )
	{
		m_TimeSlider.SetPos(0);
		DrawDibEnd(m_hDib);
		DrawDibClose(m_hDib);
		m_hDib = NULL;
		m_cTime.SetWindowText("0:00:00");
		Invalidate();
	}
	
}

void CPlayerDlg::OnReleasedcaptureTimeSlider(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int nTime = m_TimeSlider.GetPos();

	if( m_pAVPlayer->GetState() == PLAY )
	{
		m_pAVPlayer->Seek(nTime);
	}
	*pResult = 0;
}

void CPlayerDlg::OnBrowse() 
{
	char szFilter[] = "AVI Files (*.avi)|*.avi||";
	CFileDialog fd(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilter);

	if( fd.DoModal() == IDOK )
	{
		AVIHDR aviHdr;

		m_strFileName = fd.GetPathName();

		m_pAVPlayer->GetFileInfo(m_strFileName.GetBuffer(255), &aviHdr);

		m_pAVPlayer->Release();
		m_pAVPlayer->Load(m_strFileName.GetBuffer(255));
	}
	
}

LRESULT CPlayerDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if( message == WM_DISPLAY_TIME )
	{
		int nSec = (wParam/1000)%60;
		int nMin = ((wParam/1000)/60)%60;
		int nHour = ((wParam/1000)/60)/60;
		
		char szTime[250];
		sprintf(szTime, "%d:%.2d:%.2d", nHour, nMin, nSec);
		m_cTime.SetWindowText(szTime);

		int nPrePos = m_TimeSlider.GetPos();
		if( abs( nPrePos - (int)wParam/1000 ) < 3 )
		{
			m_TimeSlider.SetPos((int)wParam/1000);
		}
	}
	return CDialog::WindowProc(message, wParam, lParam);
}

void CPlayerDlg::DisplayFrame(BYTE *lpBits, DWORD &dwTime)
{
	CRect Rect;

	m_Screen.GetClientRect(&Rect);

	DrawDibDraw(m_hDib, 
				m_Screen.GetDC()->GetSafeHdc(), 
				0, 0, Rect.Width(),	Rect.Height(), 
				&m_BitmapInfo.bmiHeader,
				lpBits,
				0, 0, m_AviHdr.Width, m_AviHdr.Height,
				0);	
}

int CPlayerDlg::WriteFrameCallBack(BYTE *lpBits, DWORD &dwTime)
{
	if( lpBits )
	{
		theDlg->DisplayFrame(lpBits, dwTime);
	}
	return 1;
}


void CPlayerDlg::OnClose() 
{
	if( m_pAVPlayer )
	{
		delete m_pAVPlayer;
	}
	
	CDialog::OnCancel();
}

void CPlayerDlg::OnCancel() 
{
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -