红色MP3播放器.docx
《红色MP3播放器.docx》由会员分享,可在线阅读,更多相关《红色MP3播放器.docx(24页珍藏版)》请在冰豆网上搜索。
红色MP3播放器
西安理工大学高科学院
计算机科学与技术
题目简单音乐播放器
学部计算机科学与技术
班级09级计算机科学与技术
姓名刘洋张丹王韦康惠翔翔
2012年9月至2012年11月
1题目要求
要求用所学知识编写mp3播放器来实现mp3播放基本功能
2功能需求
Mp3播放器基本的添加歌曲,播放,暂停,暂停恢复,停止,下一首,前一首,播放列表,最小化到托盘等
3总体设计
3.1系统模块
3.2系统业务处理流程
4详细设计
4.详细设计
1.新建对话框MFC应用程序
2.添加基类classMCIMP3
在头文件中添加
#include
#pragmacomment(lib,"winmm.lib")
类成员
public:
MCIMP3();
virtual~MCIMP3();
DWORDMCIOpen(constCString&strPath);
DWORDMCIClose();
DWORDMCIPlay();
DWORDMCIPause();
DWORDMCIResume();
DWORDMCIStop();
DWORDMCISetMute(BOOLbMute);
DWORDMCIGetLength(DWORD*dwReturn);
DWORDMCIGetPos(DWORD*dwPos);
DWORDMCISeekTo(DWORDdwTo);
DWORDMCISeekToStart();
DWORDMCISeekToEnd();
BOOLMCIIsPlaying();
BOOLMCIIsOpened();
DWORDm_dwLength;
protected:
MCIDEVICEIDm_wID;
在类实现文件MCIMP3.cpp中添加如下代码
//构造函数
MCIMP3:
:
MCIMP3()
{
m_wID=NULL;
m_dwLength=0;
}
//析构函数
~MCIMP3()
MCIClose();
//打开MCI设备参数为文件地址
DWORDMCIMP3:
MCIOpen(constCString&strPath)
MCI_OPEN_PARMSmciOP;
DWORDdwReturn;
ASSERT(m_wID==NULL);
mciOP.lpstrDeviceType=NULL;
mciOP.lpstrElementName=strPath;
dwReturn=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT|MCI_OPEN_SHAREABLE,
(DWORD)(LPVOID)&mciOP);
if(dwReturn==0)
m_wID=mciOP.wDeviceID;
if(this->MCIGetLength(&m_dwLength)!
=0)
else
returndwReturn;
//关闭MCI设备
MCIClose()
MCI_GENERIC_PARMSmciGP;
dwReturn=mciSendCommand(m_wID,MCI_CLOSE,MCI_NOTIFY|MCI_WAIT,
(DWORD)(LPVOID)&mciGP);
//播放
MCIPlay()
MCI_PLAY_PARMSmciPP;
returnmciSendCommand(m_wID,MCI_PLAY,MCI_NOTIFY,
(DWORD)(LPVOID)&mciPP);
//暂停
MCIPause()
returnmciSendCommand(m_wID,MCI_PAUSE,MCI_NOTIFY|MCI_WAIT,
//继续(从暂停恢复)
MCIResume()
returnmciSendCommand(m_wID,MCI_RESUME,MCI_NOTIFY|MCI_WAIT,
//停止播放
MCIStop()
returnmciSendCommand(m_wID,MCI_STOP,MCI_NOTIFY|MCI_WAIT,
//到dwTo处
MCISeekTo(DWORDdwTo)
DWORDdwreturn;
if(dwTo>0&&dwTo{MCI_SEEK_PARMSmciSP;mciSP.dwTo=dwTo*1000;dwreturn=mciSendCommand(m_wID,MCI_SEEK,MCI_WAIT|MCI_NOTIFY|MCI_TO,(DWORD)(LPVOID)&mciSP);}elseif(dwTo>=m_dwLength)dwreturn=MCISeekToEnd();elsedwreturn=MCISeekToStart();returndwreturn;}//到开始处DWORDMCIMP3::MCISeekToStart(){MCI_SEEK_PARMSmciSP;returnmciSendCommand(m_wID,MCI_SEEK,MCI_WAIT|MCI_NOTIFY|MCI_SEEK_TO_START,(DWORD)(LPVOID)&mciSP);}//到结束处DWORDMCIMP3::MCISeekToEnd(){MCI_SEEK_PARMSmciSP;returnmciSendCommand(m_wID,MCI_SEEK,MCI_WAIT|MCI_NOTIFY|MCI_SEEK_TO_END,(DWORD)(LPVOID)&mciSP);}//得到播放文件长度DWORDMCIMP3::MCIGetLength(DWORD*dwLength){MCI_STATUS_PARMSmciSP;mciSP.dwItem=MCI_STATUS_LENGTH;DWORDdwReturn=mciSendCommand(m_wID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciSP);if(dwReturn==0)*dwLength=mciSP.dwReturn/1000;returndwReturn;}//得到当前播放文件播放到的地方DWORDMCIMP3::MCIGetPos(DWORD*dwPos){MCI_STATUS_PARMSmciSP;mciSP.dwItem=MCI_STATUS_POSITION;DWORDdwReturn=mciSendCommand(m_wID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciSP);if(dwReturn==0)*dwPos=mciSP.dwReturn/1000;returndwReturn;}//静音DWORDMCIMP3::MCISetMute(BOOLbMute){MCI_SET_PARMSmciSP;mciSP.dwAudio=MCI_SET_AUDIO_ALL;DWORDdwFlags;if(bMute)dwFlags=MCI_WAIT|MCI_SET_AUDIO|MCI_SET_OFF;elsedwFlags=MCI_WAIT|MCI_SET_AUDIO|MCI_SET_ON;returnmciSendCommand(m_wID,MCI_SET,dwFlags,(DWORD)(LPVOID)&mciSP);}//判断是否播放BOOLMCIMP3::MCIIsPlaying(){MCI_STATUS_PARMSmciSP;mciSP.dwItem=MCI_STATUS_MODE;DWORDdwReturn=mciSendCommand(m_wID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciSP);if(dwReturn==0){if(mciSP.dwReturn==MCI_MODE_PLAY)returntrue;}returnfalse;}//判断是否打开BOOLMCIMP3::MCIIsOpened(){MCI_STATUS_PARMSmciSP;mciSP.dwItem=MCI_STATUS_MODE;DWORDdwReturn=mciSendCommand(m_wID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciSP);if(dwReturn==0){if(mciSP.dwReturn==MCI_MODE_OPEN||mciSP.dwReturn==MCI_MODE_PLAY||mciSP.dwReturn==MCI_MODE_PAUSE||mciSP.dwReturn==MCI_MODE_STOP||mciSP.dwReturn==MCI_MODE_SEEK)returntrue;}returnfalse;}3.按如图方式添加控件对话框属性改为无边框,列表框属性也为无边框编辑框改为只读无边框各按钮以及控件ID以及变量关联如下表控件名控件类型ID变量类型变量关联正在播放静态文本示例编辑框编辑框ID_NAMECStringpath示例编辑框编辑框ID_TIMECStringm_time列表框IDC_LISTCListBoxm_list滑块IDC_SLIDER1CSliderCrtlm_ctrlSlider?按钮ID_ABOUT-按钮ID_MIN退出按钮ID_EXIT添加按钮ID_OPEN上一首按钮ID_PRE播放按钮ID_PLAY暂停按钮ID_PAUSE下一首按钮ID_NEXT继续按钮IDC_RESUME停止按钮ID_STOPList按钮ID_LISTBUTTDel按钮ID_DELLIST4在CPlayerDlg.h中在CPlayDlg类内添加pubilc成员变量intflag;intj;intpause;voidview();CStringstr1;CStringstr;intfile1;intplay;inttime;intnPos;inttemp;inti;CStringm_Path;MCIMP3m_mp3player;5.引入背景位图并改ID为IDB_BEIJING和IDB_BEIJINGM在OnPaint函数中else后屏蔽CDialog::OnPaint();在后面添加如下代码if(flag==1){CPaintDCdc(this);CDCmemdc;memdc.CreateCompatibleDC(&dc);CBitmapbkg;bkg.LoadBitmap(IDB_BEIJING);BITMAPbkginfo;bkg.GetBitmap(&bkginfo);memdc.SelectObject(&bkg);RECTrect;GetWindowRect(&rect);dc.StretchBlt(0,0,rect.right-rect.left,rect.bottom-rect.top,&memdc,0,0,bkginfo.bmWidth,bkginfo.bmHeight,SRCCOPY);}if(flag==0){CPaintDCdc(this);CDCmemdc;memdc.CreateCompatibleDC(&dc);CBitmapbkg;bkg.LoadBitmap(IDB_BEIJINGM);BITMAPbkginfo;bkg.GetBitmap(&bkginfo);memdc.SelectObject(&bkg);RECTrect;GetWindowRect(&rect);dc.StretchBlt(0,0,rect.right-rect.left,rect.bottom-rect.top,&memdc,0,0,bkginfo.bmWidth,bkginfo.bmHeight,SRCCOPY);}6.系统托盘实现在CPlayerDlg类中添加函数toTray(boolshow)voidCPlayerDlg::toTray(boolshow){//TODO:AddyourcontrolnotificationhandlercodehereNOTIFYICONDATApnid;pnid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);pnid.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);strcpy(pnid.szTip,"MP3播放");pnid.hWnd=m_hWnd;pnid.uCallbackMessage=UM_SHOWWINDOW;pnid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;pnid.uID=ID_TASKBARICON;pnid.uID=(UINT)m_hIcon;if(show){Shell_NotifyIcon(NIM_ADD,&pnid);ShowWindow(SW_HIDE);}else{Shell_NotifyIcon(NIM_DELETE,&pnid);ShowWindow(SW_SHOW);}}在构造函数中添加ON_MESSAGE(UM_SHOWWINDOW,OnNotifyIcon)到BEGIN_MESSAGE_MAP(CPlayerDlg,CDialog)和END_MESSAGE_MAP()之间添加函数voidCPlayerDlg::OnNotifyIcon(WPARAMwParam,LPARAMlParam){switch(lParam){caseWM_LBUTTONDBLCLK:toTray(false);break;}}最后对按钮-添加消息响应函数voidCPlayerDlg::OnMin(){//TODO:AddyourcontrolnotificationhandlercodeheretoTray(true);}7.实现点击对话框拖动打开classwizard添加WM_LBUTTONDOWNvoidCPlayerDlg::OnLButtonDown(UINTnFlags,CPointpoint){//TODO:Addyourmessagehandlercodehereand/orcalldefaultPostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));CDialog::OnLButtonDown(nFlags,point);}8.初始化变量在构造函数中添加m_time=_T("");path=_T("");i=0;temp=0;nPos=0;play=0;file1=0;pause=0;j=0;flag=0;9.对?按钮添加消息响应函数voidCPlayerDlg::OnAbout(){CAboutDlgdlg;dlg.DoModal();}10.对退出按钮添加消息响应函数voidCPlayerDlg::OnExit(){exit(0);}11.对添加按钮添加消息响应函数voidCPlayerDlg::OnOpen(){CFileDialogfile(TRUE,NULL,NULL,OFN_HIDEREADONLY,"MP3Files(*.mp3)|*.mp3|AVIFiles(*.avi)|*.avi|WAVFiles(*.wav)|*.wav|WMAFiles(*.wma)|*.wma");file.DoModal();m_Path=file.GetPathName();intnIndex=m_list.InsertString(-1,m_Path);m_list.SetCurSel(j);j++;file1=1;}12.对播放按钮添加消息响应函数voidCPlayerDlg::OnPlay(){if(play==0&&file1==1){m_ctrlSlider.SetRange(0,100);SetTimer(1,1000,NULL);m_mp3player.MCIOpen(m_Path);m_mp3player.MCIPlay();play=1;path=m_Path;UpdateData(FALSE);}}13.对暂停按钮添加消息响应函数voidCPlayerDlg::OnPause(){if(play==1){KillTimer(1);m_mp3player.MCIPause();pause=1;}}14.对继续按钮添加消息响应函数voidCPlayerDlg::OnResume(){if(play==1&&pause==1){SetTimer(1,1000,NULL);m_mp3player.MCIResume();}}15.对停止按钮添加消息响应函数voidCPlayerDlg::OnStop(){if(play==1){m_mp3player.MCIStop();KillTimer(1);m_ctrlSlider.SetPos(0);//m_mp3player.MCISeekTo(0);m_mp3player.MCIClose();play=0;file1=0;i=0;view();UpdateData(FALSE);}}16.对上一首按钮添加消息响应函数voidCPlayerDlg::OnPre(){intn=m_list.GetCurSel();if(play==1&&file1==1&&n-1>=0){CStringp;KillTimer(1);m_list.GetText(n-1,p);m_Path=p;i=0;m_list.SetCurSel(n-1);OnStop();file1=1;play=1;m_mp3player.MCIOpen(m_Path);m_mp3player.MCIPlay();path=m_Path;SetTimer(1,1000,NULL);}if(play==1&&file1==1&&n-1<0){KillTimer(1);m_list.SetCurSel(j-1);CStringp;m_list.GetText(j-1,p);m_Path=p;i=0;OnStop();file1=1;play=1;m_mp3player.MCIOpen(m_Path);m_mp3player.MCIPlay();path=m_Path;SetTimer(1,1000,NULL);}}17.对下一首添加消息响应函数voidCPlayerDlg::OnNext(){intn=m_list.GetCurSel();if(play==1&&file1==1&&n+1{CStringp;KillTimer(1);m_list.GetText(n+1,p);m_Path=p;i=0;m_list.SetCurSel(n+1);OnStop();file1=1;play=1;m_mp3player.MCIOpen(m_Path);m_mp3player.MCIPlay();path=m_Path;SetTimer(1,1000,NULL);}if(play==1&&file1==1&&n+1>=j){n=0;KillTimer(1);m_list.SetCurSel(n);CString
MCI_SEEK_PARMSmciSP;
mciSP.dwTo=dwTo*1000;
dwreturn=mciSendCommand(m_wID,MCI_SEEK,MCI_WAIT|MCI_NOTIFY|MCI_TO,
(DWORD)(LPVOID)&mciSP);
}elseif(dwTo>=m_dwLength)
dwreturn=MCISeekToEnd();
dwreturn=MCISeekToStart();
returndwreturn;
//到开始处
MCISeekToStart()
returnmciSendCommand(m_wID,MCI_SEEK,
MCI_WAIT|MCI_NOTIFY|MCI_SEEK_TO_START,(DWORD)(LPVOID)&mciSP);
//到结束处
MCISeekToEnd()
returnmciSendCommand(m_wID,MCI_SEEK,MCI_WAIT|MCI_NOTIFY|MCI_SEEK_TO_END,
//得到播放文件长度
MCIGetLength(DWORD*dwLength)
MCI_STATUS_PARMSmciSP;
mciSP.dwItem=MCI_STATUS_LENGTH;
DWORDdwReturn=mciSendCommand(m_wID,MCI_STATUS,MCI_STATUS_ITEM,
*dwLength=mciSP.dwReturn/1000;
//得到当前播放文件播放到的地方
MCIGetPos(DWORD*dwPos)
mciSP.dwItem=MCI_STATUS_POSITION;
*dwPos=mciSP.dwReturn/1000;
//静音
MCISetMute(BOOLbMute)
MCI_SET_PARMSmciSP;
mciSP.dwAudio=MCI_SET_AUDIO_ALL;
DWORDdwFlags;
if(bMute)
dwFlags=MCI_WAIT|MCI_SET_AUDIO|MCI_SET_OFF;
dwFlags=MCI_WAIT|MCI_SET_AUDIO|MCI_SET_ON;
returnmciSendCommand(m_wID,MCI_SET,dwFlags,
//判断是否播放
BOOLMCIMP3:
MCIIsPlaying()
mciSP.dwItem=MCI_STATUS_MODE;
if(mciSP.dwReturn==MCI_MODE_PLAY)
returntrue;
returnfalse;
//判断是否打开
MCIIsOpened()
if(mciSP.dwReturn==MCI_MODE_OPEN||
mciSP.dwReturn==MCI_MODE_PLAY||
mciSP.dwReturn==MCI_MODE_PAUSE||
mciSP.dwReturn==MCI_MODE_STOP||
mciSP.dwReturn==MCI_MODE_SEEK)
3.按如图方式添加控件
对话框属性改为无边框,列表框属性也为无边框编辑框改为只读无边框
各按钮以及控件ID以及变量关联如下表
控件名
控件类型
ID
变量类型
变量关联
正在播放
静态文本
示例编辑框
编辑框
ID_NAME
CString
path
ID_TIME
m_time
列表框
IDC_LIST
CListBox
m_list
滑块
IDC_SLIDER1
CSliderCrtl
m_ctrlSlider
?
按钮
ID_ABOUT
-
ID_MIN
退出
ID_EXIT
添加
ID_OPEN
上一首
ID_PRE
播放
ID_PLAY
暂停
ID_PAUSE
下一首
ID_NEXT
继续
IDC_RESUME
停止
ID_STOP
List
ID_LISTBUTT
Del
ID_DELLIST
4在CPlayerDlg.h中在CPlayDlg类内添加pubilc成员变量
intflag;
intj;
intpause;
voidview();
CStringstr1;
CStringstr;
intfile1;
intplay;
inttime;
intnPos;
inttemp;
inti;
CStringm_Path;
MCIMP3m_mp3player;
5.引入背景位图并改ID为IDB_BEIJING和IDB_BEIJINGM
在OnPaint函数中else后屏蔽CDialog:
OnPaint();
在后面添加如下代码
if(flag==1)
CPaintDCdc(this);
CDCmemdc;
memdc.CreateCompatibleDC(&dc);
CBitmapbkg;
bkg.LoadBitmap(IDB_BEIJING);
BITMAPbkginfo;
bkg.GetBitmap(&bkginfo);
memdc.SelectObject(&bkg);
RECTrect;
GetWindowRect(&rect);
dc.StretchBlt(0,0,rect.right-rect.left,rect.bottom-rect.top,
&memdc,0,0,bkginfo.bmWidth,bkginfo.bmHeight,SRCCOPY);
if(flag==0)
bkg.LoadBitmap(IDB_BEIJINGM);
6.系统托盘实现
在CPlayerDlg类中添加函数toTray(boolshow)
voidCPlayerDlg:
toTray(boolshow)
//TODO:
Addyourcontrolnotificationhandlercodehere
NOTIFYICONDATApnid;
pnid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
pnid.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
strcpy(pnid.szTip,"MP3播放");
pnid.hWnd=m_hWnd;
pnid.uCallbackMessage=UM_SHOWWINDOW;
pnid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
pnid.uID=ID_TASKBARICON;
pnid.uID=(UINT)m_hIcon;
if(show)
Shell_NotifyIcon(NIM_ADD,&pnid);
ShowWindow(SW_HIDE);
Shell_NotifyIcon(NIM_DELETE,&pnid);
ShowWindow(SW_SHOW);
在构造函数中添加ON_MESSAGE(UM_SHOWWINDOW,OnNotifyIcon)
到BEGIN_MESSAGE_MAP(CPlayerDlg,CDialog)和END_MESSAGE_MAP()之间
添加函数
OnNotifyIcon(WPARAMwParam,LPARAMlParam)
switch(lParam)
caseWM_LBUTTONDBLCLK:
toTray(false);
break;
最后对按钮-添加消息响应函数
OnMin()
toTray(true);
7.实现点击对话框拖动
打开classwizard添加WM_LBUTTONDOWN
OnLButtonDown(UINTnFlags,CPointpoint)
Addyourmessagehandlercodehereand/orcalldefault
PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
CDialog:
OnLButtonDown(nFlags,point);
8.初始化变量在构造函数中添加
m_time=_T("");
path=_T("");
i=0;
temp=0;
nPos=0;
play=0;
file1=0;
pause=0;
j=0;
flag=0;
9.对?
按钮添加消息响应函数
OnAbout()
CAboutDlgdlg;
dlg.DoModal();
10.对退出按钮添加消息响应函数
OnExit()
exit(0);
11.对添加按钮添加消息响应函数
OnOpen()
CFileDialogfile(TRUE,NULL,NULL,OFN_HIDEREADONLY,"MP3Files(*.mp3)|*.mp3|AVIFiles(*.avi)|*.avi|WAVFiles(*.wav)|*.wav|WMAFiles(*.wma)|*.wma");
file.DoModal();
m_Path=file.GetPathName();
intnIndex=m_list.InsertString(-1,m_Path);
m_list.SetCurSel(j);
j++;
file1=1;
12.对播放按钮添加消息响应函数
OnPlay()
if(play==0&&file1==1)
m_ctrlSlider.SetRange(0,100);
SetTimer(1,1000,NULL);
m_mp3player.MCIOpen(m_Path);
m_mp3player.MCIPlay();
play=1;
path=m_Path;
UpdateData(FALSE);
13.对暂停按钮添加消息响应函数
OnPause()
if(play==1)
KillTimer
(1);
m_mp3player.MCIPause();
pause=1;
14.对继续按钮添加消息响应函数
OnResume()
if(play==1&&pause==1)
m_mp3player.MCIResume();
15.对停止按钮添加消息响应函数
OnStop()
m_mp3player.MCIStop();
m_ctrlSlider.SetPos(0);
//m_mp3player.MCISeekTo(0);
m_mp3player.MCIClose();
view();
16.对上一首按钮添加消息响应函数
OnPre()
intn=m_list.GetCurSel();
if(play==1&&file1==1&&n-1>=0)
CStringp;
m_list.GetText(n-1,p);
m_Path=p;
m_list.SetCurSel(n-1);
OnStop();
if(play==1&&file1==1&&n-1<0)
m_list.SetCurSel(j-1);
m_list.GetText(j-1,p);
17.对下一首添加消息响应函数
OnNext()
if(play==1&&file1==1&&n+1{CStringp;KillTimer(1);m_list.GetText(n+1,p);m_Path=p;i=0;m_list.SetCurSel(n+1);OnStop();file1=1;play=1;m_mp3player.MCIOpen(m_Path);m_mp3player.MCIPlay();path=m_Path;SetTimer(1,1000,NULL);}if(play==1&&file1==1&&n+1>=j){n=0;KillTimer(1);m_list.SetCurSel(n);CString
m_list.GetText(n+1,p);
m_list.SetCurSel(n+1);
if(play==1&&file1==1&&n+1>=j)
n=0;
m_list.SetCurSel(n);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1