实验简单音频播放器程序实现Word格式文档下载.docx
《实验简单音频播放器程序实现Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验简单音频播放器程序实现Word格式文档下载.docx(17页珍藏版)》请在冰点文库上搜索。
本次作业参考了优酷上的指导视频,然后仿照编写的。
在完成此次大作业的过程中遇到了很多问题,首先音量控制就是一个难点,最初完成的版本可以运行但音量无法控制,后来添加了一个函数解决问题:
voidCMUSIC_PLAYER_100511217Dlg:
:
OnCustomdrawSlider(NMHDR*pNMHDR,LRESULT*pResult)
{
UpdateData(true);
m_vol=m_slider.GetPos()/10;
SetVolumn(m_slider.GetPos());
UpdateData(false);
*pResult=0;
}
最终成果:
六、总结
过完成这个简易的音乐播放器,掌握到了MFC编程的基本方法,使自己的编程能力得到进一步的提高,学会使用DirectSound实现音频播放程序的方法。
7、附录
player.cpp
//player.cpp:
Definestheclassbehaviorsfortheapplication.
//
#include"
stdafx.h"
player.h"
playerDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CMyApp
BEGIN_MESSAGE_MAP(CMyApp,CWinApp)
//{{AFX_MSG_MAP(CMyApp)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
ON_COMMAND(ID_HELP,CWinApp:
OnHelp)
END_MESSAGE_MAP()
//CMyAppconstruction
CMyApp:
CMyApp()
//TODO:
addconstructioncodehere,
//PlaceallsignificantinitializationinInitInstance
}
//TheoneandonlyCMyAppobject
CMyApptheApp;
//CMyAppinitialization
BOOLCMyApp:
InitInstance()
AfxEnableControlContainer();
//Standardinitialization
//Ifyouarenotusingthesefeaturesandwishtoreducethesize
//ofyourfinalexecutable,youshouldremovefromthefollowing
//thespecificinitializationroutinesyoudonotneed.
#ifdef_AFXDLL
Enable3dControls();
//CallthiswhenusingMFCinasharedDLL
#else
Enable3dControlsStatic();
//CallthiswhenlinkingtoMFCstatically
CMyDlgdlg;
m_pMainWnd=&
dlg;
intnResponse=dlg.DoModal();
if(nResponse==IDOK)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithOK
}
elseif(nResponse==IDCANCEL)
//dismissedwithCancel
//Sincethedialoghasbeenclosed,returnFALSEsothatweexitthe
//application,ratherthanstarttheapplication'
smessagepump.
returnFALSE;
playerDlg.cpp
//playerDlg.cpp:
implementationfile
#include"
Mmsystem.h"
Digitalv.h"
#pragmacomment(lib,"
Winmm.lib"
)
HWNDm_hWnd;
MCI_OPEN_PARMSmciopenparms;
DWORDItemLength,ItemTo;
DWORDitem;
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);
//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
CAboutDlg():
CDialog(CAboutDlg:
IDD)
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
voidCAboutDlg:
DoDataExchange(CDataExchange*pDX)
CDialog:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
//CMyDlgdialog
CMyDlg:
CMyDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CMyDlg:
IDD,pParent)
//{{AFX_DATA_INIT(CMyDlg)
m_int=0;
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->
LoadIcon(IDI_ICON1);
voidCMyDlg:
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX,IDC_volslider,m_controlvol);
DDX_Control(pDX,IDC_playslider,m_process);
DDX_Text(pDX,IDC_EDIT1,m_int);
BEGIN_MESSAGE_MAP(CMyDlg,CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_about,Onabout)
ON_BN_CLICKED(IDC_exit,Onexit)
ON_BN_CLICKED(IDC_play,Onplay)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_filechoice,Onfilechoice)
ON_NOTIFY(NM_CUSTOMDRAW,IDC_playslider,OnCustomdrawplayslider)
ON_BN_CLICKED(IDC_pause,Onpause)
ON_BN_CLICKED(IDC_stop,Onstop)
ON_NOTIFY(NM_CUSTOMDRAW,IDC_volslider,OnCustomdrawvolslider)
//CMyDlgmessagehandlers
BOOLCMyDlg:
OnInitDialog()
OnInitDialog();
//Add"
About..."
menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&
0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<
0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->
AppendMenu(MF_SEPARATOR);
AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'
smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);
//Setbigicon
SetIcon(m_hIcon,FALSE);
m_controlvol.SetRange(0,1500);
m_controlvol.SetPos(750);
GetDlgItem(IDC_play)->
EnableWindow(false);
GetDlgItem(IDC_pause)->
GetDlgItem(IDC_stop)->
//Setsmallicon
Addextrainitializationhere
returnTRUE;
//returnTRUEunlessyousetthefocustoacontrol
OnSysCommand(UINTnID,LPARAMlParam)
if((nID&
0xFFF0)==IDM_ABOUTBOX)
CAboutDlgdlgAbout;
dlgAbout.DoModal();
else
CDialog:
OnSysCommand(nID,lParam);
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
OnPaint()
if(IsIconic())
CPaintDCdc(this);
//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&
rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
OnPaint();
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCMyDlg:
OnQueryDragIcon()
return(HCURSOR)m_hIcon;
DWORDGetLength(DWORDdwitem)//根据参数获取媒体文件信息
{/*//接受传入的命令参数,命令参数如下:
MCI_STATUS_LENGTH获取总播放时间,MCI_STATUS_POSITION获取当前已播放时间:
*/
MCI_STATUS_PARMSmcistatusparms;
mcistatusparms.dwCallback=(DWORD)m_hWnd;
mcistatusparms.dwItem=dwitem;
mcistatusparms.dwReturn=0;
//返回值初始化
mciSendCommand(mciopenparms.wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)&
mcistatusparms);
returnmcistatusparms.dwReturn;
voidopen(CStringstrnamepath)
mciSendCommand(mciopenparms.wDeviceID,MCI_CLOSE,0,0);
mciopenparms.lpstrElementName=strnamepath;
DWORDdwReturn;
if(dwReturn=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT,(DWORD)(LPVOID)&
mciopenparms))
//打开失败,显示警告,并存储错误信息
charbuffer[256];
mciGetErrorString(dwReturn,buffer,256);
MessageBox(m_hWnd,buffer,"
出错警告!
"
MB_ICONHAND|MB_ICONERROR|MB_ICONSTOP);
item=MCI_MAKE_HMS(0,0,0);
voidplay()
MCI_PLAY_PARMSmciplayparms;
ItemLength=GetLength(MCI_STATUS_LENGTH);
//得到播放长度
ItemTo=MCI_MAKE_HMS(MCI_HMS_HOUR(ItemLength),
MCI_HMS_MINUTE(ItemLength),
MCI_HMS_SECOND(ItemLength));
//根据长度计算出时、分、秒
mciplayparms.dwCallback=NULL;
mciplayparms.dwFrom=item;
//播放起点
mciplayparms.dwTo=ItemTo;
//播放终点
mciSendCommand(mciopenparms.wDeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&
mciplayparms);
voidpause()
DWORDdwsf=GetLength(MCI_STATUS_POSITION);
item=MCI_MAKE_MSF(MCI_MSF_MINUTE(dwsf),
MCI_MSF_SECOND(dwsf),
MCI_MSF_FRAME(dwsf));
mciSendCommand(mciopenparms.wDeviceID,MCI_PAUSE,0,0);
voidresume()
mciSendCommand(mciopenparms.wDeviceID,MCI_RESUME,0,0);
voidstop()
mciSendCommand(mciopenparms.wDeviceID,MCI_STOP,0,0);
mciSendCommand(mciopenparms.wDeviceID,MCI_SEEK,MCI_SEEK_TO_START,0);
item=MCI_MAKE_HMS(0,0,0);
DWORDsetvolume(DWORDvol)
MCI_DGV_SETAUDIO_PARMSmcisetvolume;
mcisetvolume.dwCallback=NULL;
mcisetvolume.dwItem=MCI_DGV_SETAUDIO_VOLUME;
mcisetvolume.dwValue=vol;
mciSendCommand(mciopenparms.wDeviceID,MCI_SETAUDIO,MCI_DGV_SETAUDIO_VALUE|MCI_DGV_SETAUDIO_ITEM,(DWORD)(LPVOID)&
mcisetvolume);
return0;
Onabout()
CAboutDlgdlg;
dlg.DoModal();
//TODO:
Addyourcontrolnotificationhandlercodehere
Onexit()
OnCancel()