|   版权声明:本文为博主原创文章,未经博主允许不得转载。

  基于MFC的Media Player播放器的制作(4---功能实现代码)-LMLPHP

基于MFC的Media Player播放器的制作(4---功能实现代码)-LMLPHP

PandaPlayerDlg.h

 // PandaPlayerDlg.h : header file
//
//{{AFX_INCLUDES()
#include "wmpplayer4.h"
#include "wmpcontrols.h"
#include "wmpsettings.h"
//}}AFX_INCLUDES #if !defined(AFX_PANDAPLAYERDLG_H__BD44EB72_E7B8_4CCE_831C_6781A9FDFD2B__INCLUDED_)
#define AFX_PANDAPLAYERDLG_H__BD44EB72_E7B8_4CCE_831C_6781A9FDFD2B__INCLUDED_ #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 /////////////////////////////////////////////////////////////////////////////
// CPandaPlayerDlg dialog class CPandaPlayerDlg : public CDialog
{
// Construction
public:
CPandaPlayerDlg(CWnd* pParent = NULL); // standard constructor //文件关联对象的变量声明
CWMPControls setControl;
CWMPSettings setVolume;
CSliderCtrl *pSlidCtrl;
CSliderCtrl sliderVolume; // Dialog Data
//{{AFX_DATA(CPandaPlayerDlg)
enum { IDD = IDD_PANDAPLAYER_DIALOG };
CWMPPlayer4 m_ActiveMovie;
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPandaPlayerDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL // Implementation
protected:
HICON m_hIcon; void listDisplay(CString a[]); // Generated message map functions
//{{AFX_MSG(CPandaPlayerDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnExit();
afx_msg void OnFullscreen();
afx_msg void OnOpen();
afx_msg void OnPlay();
afx_msg void OnPause();
afx_msg void OnStop();
afx_msg void OnGo();
afx_msg void OnBack();
afx_msg void OnAbout();
afx_msg void OnOpenfiles();
afx_msg void OnMute();
afx_msg void OnLower();
afx_msg void OnUpper();
afx_msg void OnOutofmemoryVolumeslider();
afx_msg void OnNext();
afx_msg void OnPrevious();
afx_msg void OnLoop();
afx_msg void OnOrder();
afx_msg void OnRandom();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnFullscr();
afx_msg void OnOnlyplay();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
}; //{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_PANDAPLAYERDLG_H__BD44EB72_E7B8_4CCE_831C_6781A9FDFD2B__INCLUDED_)

PandaPlayerDlg.cpp

 // PandaPlayerDlg.cpp : implementation file
// #include "stdafx.h"
#include "PandaPlayer.h"
#include "PandaPlayerDlg.h" #ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif //文件数组大小预定义
#define N 100 //全局变量声明
static int count = ;
static int times = ;
static int modelFlag = ;
static int flag = ; //0表示
static int playStatus = ;
static int pauseStatus = ;
static int stopStatus = ; static int volumeSlider = ;
static long maxVloume = ;
static bool listFlag = true;
CString filesPath[N] = {"\0"};
time_t t; /////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About 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() /////////////////////////////////////////////////////////////////////////////
// CPandaPlayerDlg dialog CPandaPlayerDlg::CPandaPlayerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPandaPlayerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPandaPlayerDlg)
// 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 CPandaPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPandaPlayerDlg)
DDX_Control(pDX, IDC_OCX1, m_ActiveMovie);
//}}AFX_DATA_MAP
} BEGIN_MESSAGE_MAP(CPandaPlayerDlg, CDialog)
//{{AFX_MSG_MAP(CPandaPlayerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_FULLSCREEN, OnFullscreen)
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_BN_CLICKED(IDC_PLAY, OnPlay)
ON_BN_CLICKED(IDC_PAUSE, OnPause)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_BN_CLICKED(IDC_GO, OnGo)
ON_BN_CLICKED(IDC_BACK, OnBack)
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
ON_BN_CLICKED(IDC_OPENFILES, OnOpenfiles)
ON_BN_CLICKED(IDC_MUTE, OnMute)
ON_BN_CLICKED(IDC_LOWER, OnLower)
ON_BN_CLICKED(IDC_UPPER, OnUpper)
ON_NOTIFY(NM_OUTOFMEMORY, IDC_VOLUMESLIDER, OnOutofmemoryVolumeslider)
ON_BN_CLICKED(IDC_NEXT, OnNext)
ON_BN_CLICKED(IDC_PREVIOUS, OnPrevious)
ON_BN_CLICKED(IDC_LOOP, OnLoop)
ON_BN_CLICKED(IDC_ORDER, OnOrder)
ON_BN_CLICKED(IDC_RANDOM, OnRandom)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_FULLSCR, OnFullscr)
ON_BN_CLICKED(IDC_ONLYPLAY, OnOnlyplay)
//}}AFX_MSG_MAP
END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////
// CPandaPlayerDlg message handlers BOOL CPandaPlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog(); //添加文件关联
setControl = static_cast<CWMPControls>(m_ActiveMovie.GetControls());
setVolume = m_ActiveMovie.GetSettings(); //文件列表的初始化的状态为列表处于关闭的状态
SetWindowPos(NULL,,,,,SWP_NOMOVE); //初始化音量的滑块条的初始位置
//sliderVolume.SetRange(0, 100, true);
pSlidCtrl = (CSliderCtrl*)GetDlgItem(IDC_VOLUMESLIDER);
pSlidCtrl->SetPos(setVolume.GetVolume()); //得到初始化时的声音滑块条的位置坐标
volumeSlider = pSlidCtrl->GetPos(); //得到最大的音量值
maxVloume = setVolume.GetVolume();// * 2 //初始化文件数组
filesPath[] = "\0"; // 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 // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control
} void CPandaPlayerDlg::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 CPandaPlayerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), ); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + ) / ;
int y = (rect.Height() - cyIcon + ) / ; // 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 CPandaPlayerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
} //打开文件
void CPandaPlayerDlg::OnOpen()
{
// TODO: Add your control notification handler code here
char szFileFilter[]="Mp3 File&(*.mp3)|*.mp3|"
"Wma File((*.wma)|*.wma|"
"Video File(*.dat)|*.dat|"
"Wave File(*.wav)|*.wav|"
"AVI File(*.avi)|*.avi|"
"Movie File(*.mov)|*.mov|"
"Media File(*.mmm)|*.mmm|"
"Mid File(*.mid *.rmi)|*.mid *.rmi|"
"MPEG File(*.mpeg)|*.mpeg|"
"All File(*.*)|*.*|| ";//文件类型过滤
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFileFilter);
if(dlg.DoModal()==IDOK)
{
CString strFilePath = dlg.GetPathName();
//strFilePath.MakeUpper();
filesPath[count++] = strFilePath;
m_ActiveMovie.SetUrl(strFilePath);
times = count;
}
} //列表播放文件项目的显示
void CPandaPlayerDlg::listDisplay(CString fileUrl[])
{
int index = times;
for(int i = ; i < index; i++)
{ }
} //播放文件
void CPandaPlayerDlg::OnPlay()
{
// TODO: Add your control notification handler code here
setControl.play();
//如果是播放状态的时候,播放则为1,暂停和停止均为0
playStatus = ;
pauseStatus = ;
stopStatus = ;
} //暂停播放
void CPandaPlayerDlg::OnPause()
{
// TODO: Add your control notification handler code here
setControl.pause();
//如果暂停为真,则暂停为1,播放和停止为0
playStatus = ;
pauseStatus = ;
stopStatus = ;
} //停止播放
void CPandaPlayerDlg::OnStop()
{
// TODO: Add your control notification handler code here
setControl.stop();
//如果停止为真,则停止为1,播放和暂停为0
playStatus = ;
pauseStatus = ;
stopStatus = ; } //快进
void CPandaPlayerDlg::OnGo()
{
// TODO: Add your control notification handler code here
double CurrPosition = setControl.GetCurrentPosition();
setControl.SetCurrentPosition(CurrPosition+);
} //快退
void CPandaPlayerDlg::OnBack()
{
// TODO: Add your control notification handler code here
double CurrPosition = setControl.GetCurrentPosition();
setControl.SetCurrentPosition(CurrPosition-);
} //关于框的调用函数
void CPandaPlayerDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg aboutDlg;
aboutDlg.DoModal();
} //文件列表框的弹出和关闭函数
void CPandaPlayerDlg::OnOpenfiles()
{
// TODO: Add your control notification handler code here
listFlag=!listFlag;
if(listFlag)
{
//两个数字分别表示宽和高
SetWindowPos(NULL,,,,,SWP_NOMOVE);
}
else
{
//文件列表的三个函数在wmpcontrols.h,wmpplaylist.h和wmpmedia.h中
//分别是CWMPMedia GetCurrentItem();void SetCurrentItem(LPDISPATCH newValue);void playItem(LPDISPATCH pIWMPMedia);
SetWindowPos(NULL,,,,,SWP_NOMOVE);
CString str;
}
} //声音控制的静音函数
void CPandaPlayerDlg::OnMute()
{
// TODO: Add your control notification handler code here
if(setVolume.GetMute())
{
setVolume.SetMute(false);
}
else
{
setVolume.SetMute(true);
}
} //降低音量函数
void CPandaPlayerDlg::OnLower()
{
// TODO: Add your control notification handler code here
long currVolume = setVolume.GetVolume();
if(setVolume.GetMute())
{
MessageBox(_T("已处于静音状态,不能降低音量"), _T("警告"), MB_OK);
}
else
{
setVolume.SetVolume(currVolume-);
}
//减少音量之后重新设置音量的滑动条的位置坐标
volumeSlider = pSlidCtrl->GetPos();
flag = ;
OnOutofmemoryVolumeslider();
} //音量增加函数
void CPandaPlayerDlg::OnUpper()
{
// TODO: Add your control notification handler code here
long currVolume = setVolume.GetVolume();
if(setVolume.GetMute())
{
MessageBox(_T("已处于静音状态,不能增加音量"), _T("警告"), MB_OK);
}
else
{
setVolume.SetVolume(currVolume+);
}
//增加音量之后重新设置音量的滑动条的位置坐标
volumeSlider = pSlidCtrl->GetPos();
flag = ;
OnOutofmemoryVolumeslider();
} //音量滑块条的控制
void CPandaPlayerDlg::OnOutofmemoryVolumeslider()
{
// TODO: Add your control notification handler code here
int indexSlider;
if(flag == || flag == )
{
pSlidCtrl->SetPos(setVolume.GetVolume());
}
else
{
indexSlider = pSlidCtrl->GetPos();
//如果indexSlider-volumeSlider>0,说明滑动条往声音调大的范围滑动
if(indexSlider-volumeSlider>=)
{
setVolume.SetVolume(setVolume.GetVolume()+((indexSlider-volumeSlider)/maxVloume)*);
}
else
{
setVolume.SetVolume(setVolume.GetVolume()-((indexSlider-volumeSlider)/maxVloume)*);
}
}
} //退出函数
void CPandaPlayerDlg::OnExit()
{
// TODO: Add your control notification handler code here
setControl.stop();
exit();
} //窗口关闭函数
void CPandaPlayerDlg::OnFullscreen()
{
// TODO: Add your control notification handler code here
static int flag1 = ;
if(flag1%==)
{
//将状态标志设为暂停的标志(暂停为1, 播放和停止为0)
pauseStatus = ;
playStatus = ;
stopStatus = ;
m_ActiveMovie.ShowWindow(false);
}
else
{
//将状态标志设为播放的标志(播放为1, 暂停和停止为0)
pauseStatus = ;
playStatus = ;
stopStatus = ;
m_ActiveMovie.ShowWindow(true);
}
flag1++; } //全屏模式
void CPandaPlayerDlg::OnFullscr()
{
// TODO: Add your control notification handler code here
if(m_ActiveMovie.GetFullScreen())
{
m_ActiveMovie.SetFullScreen(false);
}
else
{
m_ActiveMovie.SetFullScreen(true);
}
} //下一曲函数
void CPandaPlayerDlg::OnNext()
{
// TODO: Add your control notification handler code here
if(modelFlag==)
{
if(filesPath[times+] == "\0")
{
times = ;
CString playFiles = filesPath[times++];
m_ActiveMovie.SetUrl(playFiles);
}
else
{
CString playFiles = filesPath[times++];
m_ActiveMovie.SetUrl(playFiles);
}
}
else
{
if(filesPath[times+] == "\0")
{
MessageBox(_T("已到达文件列表尾"),_T("警告"),MB_OK);
}
else
{
CString playFiles = filesPath[times++];
m_ActiveMovie.SetUrl(playFiles);
}
}
} //上一曲函数
void CPandaPlayerDlg::OnPrevious()
{
// TODO: Add your control notification handler code here
if(modelFlag==)
{
if(times- < )
{
times = count;
CString playFiles = filesPath[times--];
m_ActiveMovie.SetUrl(playFiles);
}
else
{
CString playFiles = filesPath[times--];
m_ActiveMovie.SetUrl(playFiles);
}
}
else
{
if(times- < )
{
MessageBox(_T("已到达文件列表头"),_T("Warning"),MB_OK);
}
else
{
CString playFiles = filesPath[times--];
m_ActiveMovie.SetUrl(playFiles);
}
}
} //循环播放
void CPandaPlayerDlg::OnLoop()
{
// TODO: Add your control notification handler code here
modelFlag = ;
if(filesPath[] == "\0")
{
MessageBox(_T("列表无文件!"),_T("警告"),MB_OK);
CPandaPlayerDlg::OnOpen();
}
else
{
if(setControl.GetCurrentPosition() == )
{
//此时说明播放已经停止,可以进入下一曲播放了
if(filesPath[times+] != "\0")
{
CString playFiles = filesPath[times++];
m_ActiveMovie.SetUrl(playFiles);
setControl.SetCurrentPosition(0.01);
}
else
{
times = ;
CString playFiles = filesPath[times++];
m_ActiveMovie.SetUrl(playFiles);
setControl.SetCurrentPosition(0.01);
}
}
else
{
//通过标志来判断是否进行那种类型的播放状态
if(playStatus == && pauseStatus == && stopStatus == )
setControl.play();
else if(pauseStatus == && playStatus == && stopStatus == )
setControl.pause();
else if(stopStatus == && playStatus == && pauseStatus == )
setControl.stop();
else
{
MessageBox(_T("出现错误"),_T("ERROR"),MB_OK);
}
}
}
SetTimer(,,NULL);
} //顺序播放
void CPandaPlayerDlg::OnOrder()
{
// TODO: Add your control notification handler code here
modelFlag = ;
if(filesPath[] == "\0")
{
MessageBox(_T("列表无文件!"),_T("警告"),MB_OK);
CPandaPlayerDlg::OnOpen();
}
else
{
if(setControl.GetCurrentPosition() == )
{
//此时说明播放已经停止,可以进入下一曲播放了
//首先判断列表是否为空
if(filesPath[times+] != "\0")
{
CString playFiles = filesPath[times++];
m_ActiveMovie.SetUrl(playFiles);
setControl.SetCurrentPosition(0.01);
}
else
{
MessageBox(_T("已到列表末尾!"),_T("Warning"),MB_OK);
CString playFiles = filesPath[];
m_ActiveMovie.SetUrl(playFiles);
setControl.SetCurrentPosition(0.01);
}
}
else
{
if(playStatus == && pauseStatus == && stopStatus == )
setControl.play();
else if(pauseStatus == && playStatus == && stopStatus == )
setControl.pause();
else if(stopStatus == && playStatus == && pauseStatus == )
setControl.stop();
else
{
MessageBox(_T("出现错误"),_T("ERROR"),MB_OK);
}
}
}
SetTimer(,,NULL);
} //随机播放
void CPandaPlayerDlg::OnRandom()
{
// TODO: Add your control notification handler code here
modelFlag = ;
if(filesPath[] == "\0")
{
MessageBox(_T("列表无文件!"),_T("警告"),MB_OK);
CPandaPlayerDlg::OnOpen();
}
else
{
if(setControl.GetCurrentPosition() == )
{
//此时说明播放已经停止,可以进入下一曲播放了
srand((unsigned)time(&t));
int index = rand() % count + ;
CString playFiles = filesPath[index];
m_ActiveMovie.SetUrl(playFiles);
setControl.SetCurrentPosition(0.01);
}
else
{
if(playStatus == && pauseStatus == && stopStatus == )
setControl.play();
else if(pauseStatus == && playStatus == && stopStatus == )
setControl.pause();
else if(stopStatus == && playStatus == && pauseStatus == )
setControl.stop();
else
{
MessageBox(_T("出现错误"),_T("ERROR"),MB_OK);
}
}
}
SetTimer(,,NULL);
} //单曲循环播放
void CPandaPlayerDlg::OnOnlyplay()
{
// TODO: Add your control notification handler code here
modelFlag = ;
if(filesPath[] == "\0")
{
MessageBox(_T("列表无文件!"),_T("警告"),MB_OK);
CPandaPlayerDlg::OnOpen();
}
else
{
if(setControl.GetCurrentPosition() == )
{
int index = times;
CString playFiles = filesPath[index];
m_ActiveMovie.SetUrl(playFiles);
setControl.SetCurrentPosition(0.01);
}
else
{
if(playStatus == && pauseStatus == && stopStatus == )
setControl.play();
else if(pauseStatus == && playStatus == && stopStatus == )
setControl.pause();
else if(stopStatus == && playStatus == && pauseStatus == )
setControl.stop();
else
{
MessageBox(_T("出现错误"),_T("ERROR"),MB_OK);
}
}
}
SetTimer(,,NULL);
} //定时器
void CPandaPlayerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default switch(nIDEvent)
{
case :
{
KillTimer();
OnLoop();
break;
}
case :
{
KillTimer();
OnOrder();
break;
}
case :
{
KillTimer();
OnRandom();
break;
}
case :
{
KillTimer();
OnOnlyplay();
break;
}
} CDialog::OnTimer(nIDEvent);
}
05-12 07:38