logo资料库

C++编写音乐播放器.pdf

第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
资料共14页,剩余部分请下载后查看
目录 多媒体播放器系统实现 一.设计目标 二.系统设计 三.系统实现过程 1.1.1.1.程序构成 2.2.2.2.创建项目 3.3.3.3.界面设计 4.4.4.4.程序设计 5.5.5.5.本系统的源代码 四.实验小结 一.设计目标 制作一个具有自己设计风格的简单的媒体播放器。 要求能随机选取要播放的文件,实现前进,回退,暂停,循环播放,音量大小调节 等基本功能。 能播放多种格式的音频视频格式文件,它的功能是能够选择一个有效的以.wav 为后 缀的音乐文件,有效的以.mp3 为后缀的音乐文件,有效的以.mid 为后缀的音乐文件, 有效的以.avi 为后缀的音乐文件,以及 CD 文件,在音乐播放的同时,有一个滑块来显 示播放的进度。 实现其它附加功能如关闭文件,播放下一首曲目等。 界面良好,功能完善 二.系统设计 媒体播放器是一个对话框窗口,主要由四部分构成,概括如下: 1. 打开文件:打开要播放的音频文件并进行选择。 2. 播放、循环播放:对音频文件进行播放或循环播放。 3. 音量调节。 4. 关闭文件。 三.系统实现过程 1.1.1.1.程序构成 媒体播放器主要由以下几个类构成: CMyplayerApp:多媒体播放器主程序。 CMyplayerDlg:用于展现系统运行时的状态。 CAboutDlg 2.2.2.2.创建项目 打开 Visual C++6.0 的集成开发环境,执行“文件”�“新建”菜单项,从弹出的 新建对话框。单击项目选项卡,在工程类型的列表中选择 MFC AppWizard exe 项, 在 Project name 编辑框中输入工程名称 myplyer,在 Location 编辑框中输入保存该 工程文件的路径。单击 OK 按钮弹出 MFC AppWizard-Step 1 对话框,选择“基本
对话框”,如图一所示。下面的步骤选取默认值。最后在步骤六单击“完成”按钮, 之后单击“确定”,完成,打开工作空间。 图一: 3.3.3.3.界面设计 在出现的工作界面单击鼠标右键,在弹出的菜单选择其中的 Insert ActiveX Control 选项,在弹出的对话框选择 Microsoft Multimedia Control 选项,添加一个 MMControl 控件,再依次在对话框窗口上放置六个 Button 控件,两个 Slider 控件, 一个编辑框控件,五个 Radio Button 控件,一个静态控件,添加控件后的窗口体 如图二所示。 图二:
窗口的各个控件的属性如下: IDC_open 打开音乐 IDC_play 播放 IDC_pause 暂停 IDC_next 选择下一首播放 IDC_circle 循环播放 IDC_close 关闭 IDC_STATIC 静态控件 您所选择播放的文件类型 IDC_RADIO1 wav 音频 IDC_RADIO2 动画音频 IDC_RADIO3 mid 音频 IDC_RADIO4 CD 音乐 IDC_RADIO5 mp3 音乐 IDC_EDIT1 4.4.4.4.程序设计 给相关控件添加关联变量,单击菜单栏的“查看”菜单,选择“建立类向导” 在弹出的对话框选择 Member Variable 标签,给相关控件添加关联变量,添加完变 量的对话框如下 给各个按钮添加相应的函数,各个函数的功能如下: CMyplayerDlg::Onopen():打开音乐文件,并选择要播放的音乐 CMyplayerDlg::Onplay() 播放选中的音乐 CMyplayerDlg::Onpause() 暂停音乐 CMyplayerDlg::Onclose() 关闭音乐 CMyplayerDlg::Onnext() 选择下一首音乐 CMyplayerDlg::Oncircle() 实现循环播放音乐
CMyplayerDlg::OnRadio1() 播放 wav 音频 CMyplayerDlg::OnRadio2() 播放动画音频 CMyplayerDlg::OnRadio3() 播放 mid 音频 CMyplayerDlg::OnRadio4() 播放 CD 音乐 CMyplayerDlg::OnRadio5() 播放 mp3 音乐 首先对界面进行初始化,在程序运行初期,若没打开有效的音频文件,出打开 音乐按钮外,其他按钮均处于禁用状态,代码设计如下 BOOL CMyplayerDlg::OnInitDialog() { CDialog::OnInitDialog(); · · · // TODO: Add extra initialization here this->m_pause.EnableWindow(FALSE); this->m_play.EnableWindow(FALSE); this->m_next.EnableWindow(FALSE); this->m_circle.EnableWindow(FALSE); this->m_close.EnableWindow(FALSE); return TRUE; } 运行程序,界面如下, // return TRUE unless you set the focus to a control 打开多媒体文件,给 CMyplayerDlg::Onopen()函数添加相应代码,如下所示: void CMyplayerDlg::Onopen() { // TODO: Add your control notification handler code here CFileDialog OpenDlg(TRUE);
switch(this->m_Mediatype) { case 1:OpenDlg.m_ofn.lpstrFilter="Wave Files(*.wav)\0*.wav\0\0"; this->m_multimedia.SetDeviceType("WaveAudio"); break; case 2:OpenDlg.m_ofn.lpstrFilter="AVI Files(*.avi)\0*.avi\0\0"; this->m_multimedia.SetDeviceType("AviAudio"); break; case 3:OpenDlg.m_ofn.lpstrFilter="Midi Files(*.mid)\0*.mid\0\0"; this->m_multimedia.SetDeviceType("Sequencer"); break; case 4:OpenDlg.m_ofn.lpstrFilter=" "; this->m_multimedia.SetDeviceType("CDAudio"); this->m_multimedia.SetCommand("Open"); this->m_pause.EnableWindow(TRUE); this->m_play.EnableWindow(TRUE); this->m_next.EnableWindow(TRUE); this->m_circle.EnableWindow(TRUE); this->m_close.EnableWindow(TRUE); } if(OpenDlg.DoModal()==IDOK) { m_edit.SetWindowText(this->m_multimedia.GetFileName()); this->m_multimedia.SetFileName(OpenDlg.GetFileName()); this->m_multimedia.SetCommand("Open"); this->m_pause.EnableWindow(TRUE); this->m_play.EnableWindow(TRUE); this->m_next.EnableWindow(TRUE); this->m_circle.EnableWindow(TRUE); this->m_close.EnableWindow(TRUE); } } 这样,程序便可以打开一个选择音频的对话框,选择音乐,如下图所示
编写 CMyplayerDlg::Onplay()的代码, 播放选中的音乐, void CMyplayerDlg::Onplay() { // TODO: Add your control notification handler code here this->m_multimedia.SetCommand("Play"); } 运行程序,便可对选中的音乐进行播放。 编写 CMyplayerDlg::Onpause()的代码, 暂停播放音乐 void CMyplayerDlg::Onpause() { // TODO: Add your control notification handler code here this->m_multimedia.SetCommand("Pause"); } 编写 CMyplayerDlg::Onclose()的代码, 关闭音乐 void CMyplayerDlg::Onclose() { // TODO: Add your control notification handler code here this->m_multimedia.SetCommand("Close"); this->m_pause.EnableWindow(FALSE); this->m_play.EnableWindow(FALSE); this->m_next.EnableWindow(FALSE); this->m_circle.EnableWindow(FALSE); this->m_close.EnableWindow(FALSE); } 编写 CMyplayerDlg::Onnext()的代码,实现选择下一首音乐 void CMyplayerDlg::Onnext() { // TODO: Add your control notification handler code here this->m_multimedia.SetCommand("Next"); } 最后,程序运行结果的界面如下: Onopen(); this->m_multimedia.SetCommand("Play"); } 编写 CMyplayerDlg::Oncircle() 的代码,实现循环播放音乐 void CMyplayerDlg::Oncircle() { // TODO: Add your control notification handler code here int i; for(i=0;i<50;i++) { this->m_multimedia.SetCommand("Prev"); this->m_multimedia.SetCommand("Play");}
5.5.5.5.本系统的源代码如下 // myplayerDlg.cpp : implementation file // #include "stdafx.h" #include "myplayer.h" #include "myplayerDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // 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); //}}AFX_VIRTUAL // DDX/DDV support // 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() ///////////////////////////////////////////////////////////////////////////// // CMyplayerDlg dialog CMyplayerDlg::CMyplayerDlg(CWnd* pParent /*=NULL*/) : CDialog(CMyplayerDlg::IDD, pParent) //{{AFX_DATA_INIT(CMyplayerDlg) m_voice = 0; //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
分享到:
收藏