媒体播放器
![查看更多精彩图片](http://photo9.hexun.com/p/2007/1005/134168/b_08F97AA672E9D6CC88EE8221795089D3.jpg)
各控件资源清单:
控件ID 控件类型 用途
IDC_CAPTION 静态文本控件 显示播放消息
IDC_TIME 静态文本控件 显示播放时间
IDC_SLIDER_VOLUME 滑动块控件 调节音量
IDC_PROCESS 滑动块控件 剪辑音频
IDC_PREF 按钮控件 播放前一首歌曲
IDC_PLAYCD 按钮控件 播放CD
IDC_RESUME 按钮控件 暂停播放
IDC_STOP 按钮控件 停止播放
IDC_NEXT 按钮控件 播放下一首歌曲
IDC_PLAYWAV 按钮控件 播放Wav文件
注意图片中的有两个静态控件,按钮名字:Music Name (共-1首第-1首) 和 Time(-1分-1秒)。正在播放那里什么也没有,这个图片是做好后运行时的效果。
实现音量控制类
在vc中,插入一个类CVolumeCtrl,其基类选Generic Class,为该类添加一个私有成员:
CWnd * m_hParent; //父窗口指针
再重新添加一个构造函数,并完成上面私有成员的初始化:
CVolumeCtrl::CVolumeCtrl(CWnd* pParent)
{
m_hParent = pParent;
}
在为了添加几个私有成员:
UINT m_nNumMixers; //混音设备个数
HMIXER m_hMixer; //混音设备句柄
MIXERCAPS m_mxcaps;
为类添加一个成员函数amdInitialize():
BOOL CVolumeCtrl::amdInitialize()
{
//得到混音设备的个数
m_nNumMixers = ::mixerGetNumDevs();
m_hMixer = NULL;
::ZeroMemory(&m_mxcaps,sizeof(MIXERCAPS));
if(m_nNumMixers != 0) { //如果系统混音设备个数不为0
//获得混音设备句柄
if(::mixerOpen(&m_hMixer,
0,
(DWORD)m_hParent->GetSafeHwnd(),
NULL,
MIXER_OBJECTF_MIXER|CALLBACK_WINDOW)
!= MMSYSERR_NOERROR) {
return FALSE;//出错,则返回FALSE
}
//获得混音设备性能
if(::mixerGetDevCaps((UINT)m_hMixer,&m_mxcaps,sizeof(MIXERCAPS))
!= MMSYSERR_NOERROR) {
return FALSE; //出错,则返回FALSE
}
}
else { //如果混音器的个数为0
return FALSE;
}
return TRUE;
}
为来添加成员函数amdUnInitalize():
BOOL CVolumeCtrl::amdUnInitialize()
{
BOOL bResult = TRUE;
if (m_hMixer != NULL)//如果混音设备句柄不为空,关闭混音设备
{
if (::mixerClose(m_hMixer) != MMSYSERR_NOERROR)
{
bResult = FALSE;
}
m_hMixer =NULL;
}
return bResult;
}
在类的析构函数~CVolumeCtrl中:
CVolumeCtrl::~CVolumeCtrl()
{
amdUnInitialize(); //释放混音设备
}
再为类添加几个私有成员:
DWORD m_dwMaximum, m_dwMinimum;
CString m_strVolumeControlName, m_strDstLineName; //最小音量和最大音量
DWORD m_dwVolumeControlID; //音量控制器ID
在类中添加一成员函数SetVolume():
BOOL CVolumeCtrl::SetVolume(DWORD dwVol)
{
if (m_hMixer == NULL || m_strDstLineName.IsEmpty() || m_strVolumeControlName.IsEmpty())
{
return FALSE;
}
MIXERCONTROLDETAILS_UNSIGNED mxcdVolume = {dwVol};
MIXERCONTROLDETAILS mxcd;
//准备参数
mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
mxcd.dwControlID = m_dwVolumeControlID;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
mxcd.paDetails = &mxcdVolume;
//设置混音器音量
if (::mixerSetControlDetails((HMIXEROBJ)m_hMixer,
&mxcd,
MIXER_OBJECTF_HMIXER | MIXER_SETCONTROLDETAILSF_VALUE)
!= MMSYSERR_NOERROR)
{
return FALSE;
}
return TRUE;
}
在类中添加成员函数GetVolume():
//获得混音设备的音量
BOOL CVolumeCtrl::GetVolume(DWORD &dwVol)
{
if (m_hMixer == NULL ||
m_strDstLineName.IsEmpty() ||
m_strVolumeControlName.IsEmpty())
{
return FALSE;
}
MIXERCONTROLDETAILS_UNSIGNED mxcdVolume = {dwVol};
MIXERCONTROLDETAILS mxcd;
//准备参数
mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
mxcd.dwControlID = m_dwVolumeControlID;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
mxcd.paDetails = &mxcdVolume;
//获取混音设备的音量
if (::mixerSetControlDetails((HMIXEROBJ)m_hMixer,
&mxcd,
MIXER_OBJECTF_HMIXER | MIXER_SETCONTROLDETAILSF_VALUE)
!= MMSYSERR_NOERROR)
{
return FALSE;
}
dwVol = mxcdVolume.dwValue; //保存混音设备的音量
return TRUE;
}
为类添加成员函数 amdGetVolumeControl():
//获取混音设备的主音量控件
BOOL CVolumeCtrl::amdGetVolumeControl()
{
m_strDstLineName.Empty(); //清空字符串
m_strVolumeControlName.Empty();//清空字符串
if (m_hMixer == NULL) //如果混音器句柄为空,则返回
{
return FALSE;
}
//准备参数
MIXERLINE mxl;
mxl.cbStruct = sizeof(MIXERLINE);
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
//得到混音设备线信息
if (::mixerGetLineInfo((HMIXEROBJ)m_hMixer,
&mxl,
MIXER_OBJECTF_HMIXER |
MIXER_GETLINEINFOF_COMPONENTTYPE)
!= MMSYSERR_NOERROR)
{
return FALSE;
}
//准备参数
MIXERCONTROL mxc;
MIXERLINECONTROLS mxlc;
mxlc.cbStruct = sizeof(MIXERCONTROLDETAILS);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType =MIXERCONTROL_CONTROLTYPE_VOLUME;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(MIXERCONTROL);
mxlc.pamxctrl = &mxc;
//获取混音设备线控制器
if (::mixerGetLineControls((HMIXEROBJ)m_hMixer,
&mxlc,
MIXER_OBJECTF_HMIXER | MIXER_GETLINECONTROLSF_ONEBYTYPE)
!= MMSYSERR_NOERROR)
{
return FALSE;
}
//保存混音设备的相关信息
m_strDstLineName = mxl.szName;
m_strVolumeControlName = mxc.szName;
m_dwMinimum = mxc.Bounds.dwMinimum;
m_dwMaximum = mxc.Bounds.dwMaximum;
m_dwVolumeControlID = mxc.dwControlID;
return TRUE;
}
注意:当编译时会出现:
Compiling...
VolumeCtrl.cpp
f:\vc++深入详解\mymediaplayer\volumectrl.h(25) : error C2146: syntax error : missing ';' before identifier 'm_hMixer'
……
这时还必须加上头文件:#include <mmsystem.h>
类似接下来的两个类都要加上这个头文件。
但当我们连接时,又出现:
Linking...
VolumeCtrl.obj : error LNK2001: unresolved external symbol __imp__mixerGetDevCapsA@12
……
这时我们进行这样的设置:在工程--->设置---->在对象/库模块中输入Winmm.lib
实现Wav文件播放类:
在vc中,插入一个类CWavePlayer,其基类选Generic Class,为该类添加三个私有成员:
private:
MCIDEVICEID m_nDeviceID ;
MCIDEVICEID m_nElementID;
BOOL m_IsPlay;
在其构造函数中进行初始化:
CWavePlayer::CWavePlayer()
{
m_nDeviceID = 0;
m_nElementID = 0;
m_IsPlay = FALSE;
}
在其析构函数中对设备进行关闭:
CWavePlayer::~CWavePlayer()
{
if(m_nElementID) {
Stop(); //停止播放
}
if(m_nDeviceID) {
CloseDevice(); //关闭设备
}
}
为类添加成员函数OpenDevice():
DWORD CWavePlayer::OpenDevice()
{
DWORD dwResult = 0;
if(!m_nDeviceID) {
//准备参数
MCI_OPEN_PARMS mciOpenParms;
mciOpenParms.lpstrDeviceType = (LPSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
//open device
dwResult = mciSendCommand(NULL,MCI_OPEN,
MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_WAIT,(DWORD)(LPVOID)&mciOpenParms);
//保存设备ID
m_nDeviceID = mciOpenParms.wDeviceID;
//if(dwResult) DisplayErrorMsg(dwResult);
}
return dwResult;
}
为其类添加成员函数CloseDevice():
DWORD CWavePlayer::CloseDevice()
{
DWORD dwResult = 0;
if(m_nDeviceID) {
//close device
dwResult = mciSendCommand(m_nDeviceID,MCI_CLOSE,NULL,NULL);
if(dwResult) { //如果关闭失败
//DisplayErrorMsg(dwResult);
m_nDeviceID = m_nDeviceID; //可以不写,相当于什么都不做
}
else {
m_nDeviceID = 0;
}
}
return dwResult;
}
为类添加成员函数Play():
//播放Wav文件
DWORD CWavePlayer::Play(CWnd *pWnd, LPCSTR pFileName)
{
MCI_OPEN_PARMS mciOpenParms;
memset(&mciOpenParms,0,sizeof(MCI_OPEN_PARMS));
//设置Wav文件
mciOpenParms.lpstrElementName = pFileName;
//打开Wav文件
DWORD dwResult = mciSendCommand(m_nDeviceID,MCI_OPEN,MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);
if(dwResult) {
//DisplayErrorMsg(dwResult);
}
else {
//保存音频元素标识
m_nElementID = mciOpenParms.wDeviceID;
MCI_PLAY_PARMS mciPlayParms;
//
mciPlayParms.dwCallback = (DWORD)pWnd->GetSafeHwnd();
//
dwResult = mciSendCommand(m_nElementID,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);
if(dwResult) {
//DisplayErrorMsg(dwResult);
Stop();
}
else {
m_IsPlay = TRUE;
}
}
return dwResult;
}
在类中添加成员函数Stop():
DWORD CWavePlayer::Stop()
{
DWORD dwResult = 0;
if(m_nElementID) {
//停止播放Wav文件
dwResult = mciSendCommand(m_nElementID,MCI_CLOSE,NULL,NULL);
if(dwResult) {
//DisplayErrorMsg(dwResult);
}
else {
m_nElementID = 0;
m_IsPlay = FALSE;
}
}
return dwResult;
}
为类添加成员函数IsPlay():
BOOL CWavePlayer::IsPlay()
{
return m_IsPlay; //返回当前状态
}
实现CD播放类
插入一个基于Generic Class的新类CCDAudio。然后向类中增加三个私人成员:
private:
BOOL m_bOpened, m_bPaused, m_bPlaying; //标识变量
WORD m_wDeviceID; //设备句柄
在其构造函数中对成员变量进行初始化:
CCDAudio::CCDAudio()
{
m_bOpened = m_bPaused = m_bPlaying = FALSE;
m_wDeviceID = 0;
}
在析构函数中,关闭音频设备,以释放系统资源:
CCDAudio::~CCDAudio()
{
Close(); //关闭音频设备
}
为类添加成员函数Open():
BOOL CCDAudio::Open( void )
{
if( m_bOpened ) //如果设备已经打开
return( TRUE );
m_bPaused = m_bPlaying = FALSE;
m_wDeviceID = 0;
//准备打开音频设备的参数
MCI_OPEN_PARMS OpenParms;
OpenParms.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
OpenParms.wDeviceID = 0;
//打开音频设备
if( mciSendCommand( NULL, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE, (DWORD)(LPVOID) &OpenParms ) )
return( FALSE );
//保存音频设备ID
m_wDeviceID = OpenParms.wDeviceID;
m_bOpened = TRUE; //修改标识变量
//准备设置音频设备的参数
MCI_SET_PARMS SetParms;
SetParms.dwTimeFormat = MCI_FORMAT_TMSF;
//设置音频设备
if( mciSendCommand( m_wDeviceID, MCI_SET, MCI_WAIT | MCI_SET_TIME_FORMAT, (DWORD)(LPVOID) &SetParms ) ){
Close ();
return( FALSE );
}
//移动播放位置到CD头部
mciSendCommand( m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL );
return( TRUE );
}
为类添加成员函数Close():
void CCDAudio::Close( void )
{
if( !m_bOpened )
return;
if( m_bPlaying || m_bPaused ) //如果处于播放或暂停状态,则停止播放
mciSendCommand( m_wDeviceID, MCI_STOP, NULL, NULL );
//关闭音频设备
mciSendCommand( m_wDeviceID, MCI_CLOSE, MCI_WAIT, NULL );
//恢复成员变量
m_bOpened = m_bPaused = m_bPlaying = FALSE;
m_wDeviceID = 0;
}
为类添加成员函数Play():
BOOL CCDAudio::Play (void)
{
if( !Open() )
return( FALSE );
if( !IsAudioTrack( GetCurrentTrack() ) )
return( FALSE );
//准备参数
MCI_PLAY_PARMS PlayParms;
PlayParms.dwCallback = NULL;
PlayParms.dwFrom = MCI_MAKE_TMSF( GetCurrentTrack(), //得到当前曲目
GetMinutes(), //得到当前分数
GetSeconds(), //得到当前秒数
GetFrames() ); //得到当前帧数
//播放CD
if( mciSendCommand( m_wDeviceID, MCI_PLAY, MCI_FROM, (DWORD)(LPVOID) &PlayParms )
)
return( FALSE );
//设置标识变量
m_bPaused = FALSE;
m_bPlaying = TRUE;
return( TRUE );
}
为类添加成员函数Stop():
void CCDAudio::Stop( void )
{
if( !m_bOpened || !m_bPlaying )
return;
//停止播放CD
mciSendCommand( m_wDeviceID, MCI_STOP, MCI_WAIT, NULL );
//将播放指针移至CD头部
mciSendCommand( m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL );
//设置标识变量
m_bPlaying = FALSE;
m_bPaused = FALSE;
}
为类添加成员函数Pause():
void CCDAudio::Pause( void )
{
if( !m_bOpened || !m_bPlaying ) //如果并非处于播放状态
return;
MCI_PLAY_PARMS PlayParms;
//停止播放CD
if( mciSendCommand( m_wDeviceID, MCI_PAUSE, 0, (DWORD)(LPVOID) &PlayParms ) )
return;
//设置标识
m_bPaused = TRUE;
}
为类增加成员函数GetTotalTracks(),用来完成CD曲目信息的获取:
int CCDAudio::GetTotalTracks( void )
{
if( !m_bOpened ) //如果设备处于关闭状态
return( -1 );
//准备参数MCI_STATUS_NUMBER_OF_TRACKS
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
//获取CD状态
if( mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD)(LPVOID)
&StatusParms ) )
return( -1 );
return( (int) StatusParms.dwReturn );
}
为类添加成员函数GetTotalLength(),在其中完成曲目长度的获取功能:
void CCDAudio::GetTotalLength( int *pnMinutes, int *pnSeconds )
{
*pnMinutes = -1;
*pnSeconds = -1;
if( !m_bOpened )
return;
//准备参数MIC_STATUS_LENGTH
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_LENGTH;
//获取CD状态
if( mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM,
(DWORD)(LPVOID) &StatusParms ) )
return;
//得到CD总时间
*pnMinutes = MCI_MSF_MINUTE( StatusParms.dwReturn );
*pnSeconds = MCI_MSF_SECOND( StatusParms.dwReturn );
}
为类添加成员函数GetTrackLength(),在其中完成获取指定CD曲目长度的功能:
void CCDAudio::GetTrackLength( int nTrack, int *pnMinutes, int *pnSeconds )
{
*pnMinutes = -1;
*pnSeconds = -1;
if( !m_bOpened )
return;
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_LENGTH;
StatusParms.dwTrack = (DWORD) nTrack;
if( mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM |
MCI_TRACK, (DWORD)(LPVOID) &StatusParms ) )
return;
//获取指定时间CD曲目的时间
*pnMinutes = MCI_MSF_MINUTE( StatusParms.dwReturn );
*pnSeconds = MCI_MSF_SECOND( StatusParms.dwReturn );
}
为类增加成员函数GetCurrentTrack(),在其中完成获取当前播放CD曲目的功能:
int CCDAudio::GetCurrentTrack( void )
{
if( !m_bOpened )
return( -1 );
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_POSITION;
//获取CD状态
if( mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM,
(DWORD)(LPVOID) &StatusParms ) )
return( -1 );
//返回当前曲目
return( MCI_TMSF_TRACK( StatusParms.dwReturn ) );
}
为类添加成员函数GetMimutes(),在其中完成获取当前播放CD曲目分数的功能:
int CCDAudio::GetMinutes( void )
{
if( !m_bOpened )
return( -1 );
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_POSITION;
if( mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM,
(DWORD)(LPVOID) &StatusParms ) )
return( -1 );
return( MCI_TMSF_MINUTE( StatusParms.dwReturn ) );
}
为类添加成员函数GetSeconds(),在其中完成获取当前播放CD曲目秒数的功能:
int CCDAudio::GetSeconds( void )
{
if( !m_bOpened )
return( -1 );
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_POSITION;
if( mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM,
(DWORD)(LPVOID) &StatusParms ) )
return( -1 );
return( MCI_TMSF_SECOND( StatusParms.dwReturn ) );
}
为类增加成员函数GetFrames(),,在其中完成获取当前播放CD帧数的功能:
int CCDAudio::GetFrames( void )
{
if( !m_bOpened )
return( -1 );
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_POSITION;
if( mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM,
(DWORD)(LPVOID) &StatusParms ) )
return( -1 );
return( MCI_TMSF_FRAME( StatusParms.dwReturn ) );
}
为类添加成员函数IsAudioTrack(),在其中实现判断当前播放指针是否处于指定曲目位置:
BOOL CCDAudio::IsAudioTrack( int nTrack )
{
if( !m_bOpened )
return( FALSE );
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
StatusParms.dwTrack = nTrack;
if( mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK,
(DWORD)(LPVOID) &StatusParms ) )
return( FALSE );
if( StatusParms.dwReturn == MCI_CDA_TRACK_AUDIO )
return( TRUE );
return( FALSE );
}
为类增加成员函数SeekTo(),,在其中完成跳转的功能:
BOOL CCDAudio::SeekTo( int nTrack, int nMinute, int nSecond, int nFrame )
{
if( !m_bOpened )
return( FALSE );
MCI_SEEK_PARMS SeekParms;
SeekParms.dwTo = MCI_MAKE_TMSF( nTrack, nMinute, nSecond, nFrame );
if( m_bPlaying && !m_bPaused )
mciSendCommand( m_wDeviceID, MCI_PAUSE, 0, NULL );
if( mciSendCommand( m_wDeviceID, MCI_SEEK, MCI_TO | MCI_WAIT, (DWORD)(LPVOID)
&SeekParms ) )
return( FALSE );
if( m_bPlaying && !m_bPaused )
Play();
return( TRUE );
}
为类增加成员函数SkipForward(),,在其中完成前进的功能:
void CCDAudio::SkipForward( int nSeconds )
{
if( !m_bOpened )
return;
MCI_SET_PARMS SetParms;
SetParms.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
mciSendCommand( m_wDeviceID, MCI_SET, MCI_WAIT | MCI_SET_TIME_FORMAT, (DWORD)
(LPVOID) &SetParms );
MCI_STATUS_PARMS StatusParms;
DWORD dwPos;
StatusParms.dwItem = MCI_STATUS_POSITION;
mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)
(LPVOID) &StatusParms );
dwPos = StatusParms.dwReturn;
dwPos += (DWORD) nSeconds * 1000;
MCI_SEEK_PARMS SeekParms;
SeekParms.dwTo = dwPos;
if( m_bPlaying && !m_bPaused )
mciSendCommand( m_wDeviceID, MCI_PAUSE, 0, NULL );
mciSendCommand( m_wDeviceID, MCI_SEEK, MCI_TO | MCI_WAIT, (DWORD)(LPVOID)
&SeekParms );
SetParms.dwTimeFormat = MCI_FORMAT_TMSF;
mciSendCommand( m_wDeviceID, MCI_SET, MCI_WAIT | MCI_SET_TIME_FORMAT, (DWORD)
(LPVOID) &SetParms );
if( m_bPlaying && !m_bPaused )
Play();
}
为类增加成员函数SkipBack(),,在其中完成后退的功能:
void CCDAudio::SkipBack( int nSeconds )
{
if( !m_bOpened )
return;
MCI_SET_PARMS SetParms;
SetParms.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
mciSendCommand( m_wDeviceID, MCI_SET, MCI_WAIT | MCI_SET_TIME_FORMAT, (DWORD)
(LPVOID) &SetParms );
MCI_STATUS_PARMS StatusParms;
DWORD dwPos;
StatusParms.dwItem = MCI_STATUS_POSITION;
mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)
(LPVOID) &StatusParms );
dwPos = StatusParms.dwReturn;
dwPos -= (DWORD) nSeconds * 1000;
MCI_SEEK_PARMS SeekParms;
SeekParms.dwTo = dwPos;
if( m_bPlaying && !m_bPaused )
mciSendCommand( m_wDeviceID, MCI_PAUSE, 0, NULL );
mciSendCommand( m_wDeviceID, MCI_SEEK, MCI_TO | MCI_WAIT, (DWORD)(LPVOID)
&SeekParms );
SetParms.dwTimeFormat = MCI_FORMAT_TMSF;
mciSendCommand( m_wDeviceID, MCI_SET, MCI_WAIT | MCI_SET_TIME_FORMAT, (DWORD)
(LPVOID) &SetParms );
if( m_bPlaying && !m_bPaused )
Play();
}
媒体播放器的实现
首先:我们必须在CMyMediaPlayDlg类的头文件中添加下面这点代码,实现控件与函数的映射:
// Dialog Data
//{{AFX_DATA(CMyMediaPlayerDlg)
enum { IDD = IDD_MYMEDIAPLAYER_DIALOG };
CSliderCtrl m_sliderVolume;
CSliderCtrl m_sliderProcess;
CButton m_btnPlayWav;
CStatic m_msgTime;
CStatic m_msgCaption;
CButton m_btnNext;
CButton m_btnStop;
CButton m_btnPref;
CButton m_btnResume;
CButton m_btnPlayCD;
//}}AFX_DATA
和下面这些私有成员:
private:
CWavePlayer m_WavePlayer;
CCDAudio m_CDPlayer;
CVolumeCtrl * m_VolumeCtrl;
DWORD m_nVolume;
然后开始此类的OnInitDialog()函数中:
BOOL CMyMediaPlayerDlg::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
// TODO: Add extra initialization here
if(m_VolumeCtrl->amdInitialize()) {
if(m_VolumeCtrl->amdGetVolumeControl()) {
m_VolumeCtrl->GetVolume(m_nVolume);
}
else {
MessageBox("amdGetVolumeControl failed");
}
}
else {
MessageBox("amdInitialize failed");
}
//初始化界面提示信息
m_msgCaption.SetWindowText("");
m_msgTime .SetWindowText("");
//禁止按钮
m_btnResume.EnableWindow(FALSE);
m_btnStop.EnableWindow(FALSE);
m_btnNext.EnableWindow(FALSE);
m_btnPref.EnableWindow(FALSE);
//允许按钮
m_btnPlayCD .EnableWindow(TRUE);
m_btnPlayWav.EnableWindow(TRUE);
//初始化滑动块控件
m_sliderVolume.SetRangeMin(0);
m_sliderVolume.SetRangeMax(65000); //65535
m_sliderVolume.SetLineSize(100);
m_sliderVolume.SetPageSize(1000);
//设置滑动块控件的值
DWORD nVolume ;
m_VolumeCtrl->GetVolume(nVolume);
m_sliderVolume.SetPos(nVolume);
return TRUE; // return TRUE unless you set the focus to a control
}
利用MFC ClassWizard为类添加IDC_PLAYCD的单击响应函数OnPlaycd():
void CMyMediaPlayerDlg::OnPlaycd()
{
m_CDPlayer.Play();
m_btnResume.EnableWindow(TRUE);
m_btnStop.EnableWindow(TRUE);
m_btnNext.EnableWindow(TRUE);
m_btnPref.EnableWindow(TRUE);
m_btnPlayCD .EnableWindow(FALSE);
m_btnPlayWav.EnableWindow(FALSE);
SetTimer(1,1000,NULL); //设置计时器
}
利用MFC ClassWizard为类添加WM_TIMER的单击响应函数OnTimer():
void CMyMediaPlayerDlg::OnTimer(UINT nIDEvent)
{
int nTotalTracks = m_CDPlayer.GetTotalTracks ();
int nCurrentTracks = m_CDPlayer.GetCurrentTrack();
int nMinute = m_CDPlayer.GetMinutes();
int nSecond = m_CDPlayer.GetSeconds();
CString strCaption ;
strCaption.Format("共%d首 第%d首\t\t正在播放",nTotalTracks,nCurrentTracks);
m_msgCaption.SetWindowText(strCaption);
CString strTime;
strTime.Format("%d分%d秒",nMinute,nSecond);
m_msgTime.SetWindowText(strTime);
CDialog::OnTimer(nIDEvent);
}
利用MFC ClassWizard为类添加IDC_STOP的单击响应函数OnStop():
void CMyMediaPlayerDlg::OnStop()
{
m_CDPlayer.Stop();
KillTimer(1);
m_btnResume.EnableWindow(FALSE);
m_btnStop.EnableWindow(FALSE);
m_btnNext.EnableWindow(FALSE);
m_btnPref.EnableWindow(FALSE);
m_btnPlayCD .EnableWindow(TRUE);
m_btnPlayWav.EnableWindow(TRUE);
CString strCaption ;
strCaption.Format("共%d首 第%d首\t\t停止播放",0,0);
m_msgCaption.SetWindowText(strCaption);
CString strTime;
strTime.Format("%d分%d秒",0,0);
m_msgTime.SetWindowText(strTime);
}
利用MFC ClassWizard为类添加IDC_RESUME的单击响应函数OnResume():
void CMyMediaPlayerDlg::OnResume()
{
m_CDPlayer.Pause();
m_btnResume.EnableWindow(FALSE);
m_btnStop.EnableWindow(FALSE);
m_btnNext.EnableWindow(FALSE);
m_btnPref.EnableWindow(FALSE);
m_btnPlayCD .EnableWindow(TRUE);
m_btnPlayWav.EnableWindow(FALSE);
}
利用MFC ClassWizard为类添加IDC_PREF的单击响应函数OnPref():
void CMyMediaPlayerDlg::OnPref()
{
//得到CD中总曲目
int nTotalTracks = m_CDPlayer.GetTotalTracks ();
//得到当前曲目
int nCurrentTracks = m_CDPlayer.GetCurrentTrack();
//得到上一曲目
nCurrentTracks = nCurrentTracks - 1;
if(nCurrentTracks >= 1) {
m_CDPlayer.SeekTo(nCurrentTracks,0,0,0); //播放上一曲目
}
}
利用MFC ClassWizard为类添加IDC_NEXT的单击响应函数OnNext():
void CMyMediaPlayerDlg::OnNext()
{
//得到CD中总曲目数
int nTotalTracks = m_CDPlayer.GetTotalTracks ();
//得到当前曲目
int nCurrentTracks = m_CDPlayer.GetCurrentTrack();
//得到下一曲目
nCurrentTracks = nCurrentTracks + 1;
if(nCurrentTracks <= nTotalTracks) {
m_CDPlayer.SeekTo(nCurrentTracks,0,0,0); //播放下一曲目
}
}
利用MFC ClassWizard为类添加IDC_PLAYWAV的单击响应函数OnPlaywav():
void CMyMediaPlayerDlg::OnPlaywav()
{
//判断当前是否正在进行Wav文件的播放
if(!m_WavePlayer.IsPlay()) { //如果没有
//设置过滤字符串
CString szFilter = "Wav File(*.wav)|*.wav||";
//构造文件对话框
CFileDialog fileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter);
//打开文件对话框
if(fileDialog.DoModal() == IDCANCEL) {
return ;
}
CString fileName = fileDialog.GetPathName(); //得到文件全路径
m_WavePlayer.Play(this,fileName);//播放Wav文件
m_btnPlayWav.SetWindowText("停止"); //改变按钮文本信息
m_btnPlayCD .EnableWindow(FALSE); //禁止CD播放按钮
}
else {
m_WavePlayer.Stop();
m_btnPlayWav.SetWindowText("播放");
m_btnPlayCD .EnableWindow(TRUE);
}
}
利用MFC ClassWizard为CMyMediaPlayerDlg类,添加滑动块控件IDC_SLIDER_VOLUME的
NM_RELEASEDCAPTUREDE消息响应函数 OnReleassedcaptureSliderColume():
void CMyMediaPlayerDlg::OnReleasedcaptureSliderVolume(NMHDR* pNMHDR, LRESULT* pResult)
{
CString msg;
DWORD nVolume = m_sliderVolume.GetPos(); //获取滑动块的位置
m_VolumeCtrl->SetVolume(nVolume); //设置系统音量
*pResult = 0;
}
整个过程大概是这样:不过,不知怎么的,最后至于音量控制滑动块有效,而播放进度的滑动块无效。应该
是 在最后没有给出个控件的响应函数……