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

📄 mp3playerdlg.cpp

📁 随着计算机信息技术的飞速发展
💻 CPP
字号:
// MP3PlayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MP3Player.h"
#include "MP3PlayerDlg.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);    // 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()

/////////////////////////////////////////////////////////////////////////////
// CMP3PlayerDlg dialog

CMP3PlayerDlg::CMP3PlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMP3PlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMP3PlayerDlg)
	m_filename = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
pGBuilder=NULL;
pMControl=NULL;
pMPos=NULL;
}

void CMP3PlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMP3PlayerDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_pos);
	DDX_Text(pDX, IDC_EDIT1, m_filename);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMP3PlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CMP3PlayerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_WM_TIMER()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_RESUME, OnResume)
	ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMP3PlayerDlg message handlers

BOOL CMP3PlayerDlg::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
	this->SysInitia(); 
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMP3PlayerDlg::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 CMP3PlayerDlg::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 CMP3PlayerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMP3PlayerDlg::OnPlay() 
{
UpdateData();

//pGBuilder->Abort();   
CHAR strSoundPath[MAX_PATH]; //存储音乐所在路径
WCHAR wstrSoundPath[MAX_PATH]; //存储UNICODE形式的路径
GetCurrentDirectory(MAX_PATH, strSoundPath);
//、、strcat(strSoundPath, "\\Sounds\\");
strcpy(strSoundPath, this->m_filename); //要播放文件名
MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1,wstrSoundPath, MAX_PATH);
pGBuilder->RenderFile(wstrSoundPath, NULL); //调入文件
pMPos->put_CurrentPosition(0); //移动到文件头
pMControl->Run(); //播放
SetTimer(1,1000,NULL);//显示进度
double total;

pMPos->get_Duration(&total);//播放文件的总长度
m_pos.SetRange(0,total); 
this->GetDlgItem(IDC_OPENFILE)->EnableWindow(0);   
this->GetDlgItem(IDC_EDIT1)->EnableWindow(0); 	
}

void CMP3PlayerDlg::OnStop() 
{
//pMControl->Stop(); //停止播放
pMControl->Pause();//暂停

	
}

void CMP3PlayerDlg::OnTimer(UINT nIDEvent) 
{
if(pMPos==NULL)return;
double rate;
 
 pMPos->get_CurrentPosition(&rate);
//pMPos->get_Rate(&rate);  
m_pos.SetPos(rate); 
 
	
	CDialog::OnTimer(nIDEvent);
}

void CMP3PlayerDlg::SysInitia()
{
	CoInitialize(NULL); //初始化COM
//创建各个对象
CoCreateInstance(CLSID_FilterGraph, NULL,
CLSCTX_INPROC, IID_IGraphBuilder, (void**)&pGBuilder);
pGBuilder->QueryInterface(IID_IMediaControl, (void**)&pMControl);
pGBuilder->QueryInterface(IID_IMediaPosition, (void**)&pMPos);
m_pos.SetRange(0,100); 
}

void CMP3PlayerDlg::OnClose() 
{
	//释放对象
//pMControl->GetState( 
	if(pMControl!=NULL){
pMControl->Release(); ;
pMPos->Release() ;
pGBuilder->Release() ;
CoUninitialize(); //释放COM
	}
	
	CDialog::OnClose();
}

void CMP3PlayerDlg::OnResume() //继续
{
pMControl->Run(); 	
}

void CMP3PlayerDlg::OnOpenfile() //打开播放文件
{
CFileDialog dlg(1,"*.mp3","",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"MP3  Files (*.mp3)|*.mp3");
dlg.DoModal();
m_filename=dlg.GetPathName();
UpdateData(0);  	
}

⌨️ 快捷键说明

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