本文介绍如何插入RealPlay控件实现媒体文件的播放,代码运行效果图如下:
下面简要介绍一下具体实现步骤:
一、建立基于对话框的程序
二、在对话框内添加RealPlayer G2 control的ActiveX控件
(工程->添加工程->compontent and controls->registed ActiveX controls )。
三、在对话框内添加源程序内所示的按钮和静态文本 分别用于控制打开播放等控制及显示歌曲信息 其ID号如源程序
四、用MFC映射各按钮消息
void CSunapplerealplayerDlg::OnOpen()
{
char szFileFilter[]=
"RM File(*.rm)|*.rm|"
"Mp3 File(*.mp3)|*.mp3|"
"MPEG File(*.mpeg)|*.mpeg|"
"Media File(*.asf)|*.asf|"
"Video File(*.dat)|*.dat|"
"MPGA File(*.mpga)|*.mpga|"
"Wave File(*.wav)|*.wav|"
"AVI File(*.avi)|*.avi|"
"Movie File(*.mov)|*.mov|"
"Mid File(*.mid;*,rmi)|*.mid;*.rmi|"
"Wma File(*.wma)|*.wma|"
"All File(*.*)|*.*||";
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFileFilter);
if(dlg.DoModal()==IDOK){
CString PathName=dlg.GetPathName();
PathName.MakeUpper();
m_player->SetSource(PathName);
m_player->DoPlay();
SetDlgItemText(IDC_STATIC1,m_player->GetAuthor());
SetDlgItemText(IDC_STATIC2,m_player->GetTitle());
SetDlgItemText(IDC_COPYRIGHT,m_player->GetCopyright());
SetDlgItemText(IDC_SOURCE,m_player->GetSource());
}
}
void CSunapplerealplayerDlg::OnPlay()
{
SetDlgItemText(IDC_STATIC1,m_player->GetAuthor());
SetDlgItemText(IDC_STATIC2,m_player->GetTitle());
SetDlgItemText(IDC_COPYRIGHT,m_player->GetCopyright());
SetDlgItemText(IDC_SOURCE,m_player->GetSource());
m_player->DoPlay();
UpdateData(false);
SetTimer(1,20,NULL);
}
void CSunapplerealplayerDlg::OnTimer(UINT nIDEvent)
{
if(0&&isRepeat)
m_player->DoPlay();
CDialog::OnTimer(nIDEvent);
}
void CSunapplerealplayerDlg::OnClose()
{
///////添加此代码时不要忘了在stdafx.h开头处添加前两行
AnimateWindow(GetSafeHwnd(),1000,AW_HIDE|AW_BLEND);
KillTimer(0);
////////////////////////////////////
//此处采用DestroyWindow关闭窗口
//多谢杜修杏 老师指点
///////////////////////////////////
this->DestroyWindow();
}
void CSunapplerealplayerDlg::OnFullscreen()
{
m_player->DoPause();
m_player->SetFullScreen();
m_player->DoPlay();
}
void CSunapplerealplayerDlg::OnMp3down()
{
ShellExecute(NULL,_T("open"),"http://sunapple.51.net",NULL,NULL,TRUE);
}
void CSunapplerealplayerDlg::OnPause()
{
m_player->DoPause();
}
void CSunapplerealplayerDlg::OnStop()
{
m_player->DoStop();
KillTimer(0);
}
void CSunapplerealplayerDlg::OnRepeat()
{
m_player->SetLoop(true);
if(isRepeat){
isRepeat=FALSE;
SetDlgItemText(IDC_REPEAT,"循环");
}
else
{
isRepeat=TRUE;
SetDlgItemText(IDC_REPEAT,"正常");
}
}
void CSunapplerealplayerDlg::OnLower()
{
// TODO: Add your control notification handler code here
short volume=m_player->GetVolume();
m_player->DoPause();
m_player->SetVolume(volume-100);
m_player->DoPlay();
}
void CSunapplerealplayerDlg::OnUpper()
{
// TODO: Add your control notification handler code here
short volume=m_player->GetVolume();
m_player->DoPause();
m_player->SetVolume(volume+100);
m_player->DoPlay();
}
void CSunapplerealplayerDlg::OnFloat()
{
// TODO: Add your command handler code here
ShellExecute(NULL,_T("open"),"http://sunapple.51.net",NULL,NULL,TRUE);
}
void CSunapplerealplayerDlg::OnPetroleum()
{
// TODO: Add your command handler code here
ShellExecute(NULL,_T("open"),"http://www.hdpu.edu.cn",NULL,NULL,TRUE);
}