当前位置: 源码素材网 » VC++教程 » 详情页

VC++写的简单CD 播放器

  •   时间:2019-05-11
  • 概述:播放器

VC++写的简单Windows CD 播放器代码,本例介绍如何制作自己的CD 播放器,通过一组按钮可实现CD 的播放、暂停、停止、向后跳一曲、向后跳一小段、向前跳一小段和向前跳一曲等控制功能。

MCI 可以通过mciSendCommand 函数控制媒体设备,不同的消息和参数就对应不同的指令。需要按照先打开设备,然后播放,最后关闭设备的顺序调用该函数。mciSendCommand 函数的定义为:

MCIERROR mciSendCommand(MCIDEVICEID wDeviceID, //设备标识
UINT uMsg, //消息命令,指定将如何控制设备
DWORD dwFlags, //访问标识
DWORD dwParam ); //标识程序在访问MCI 时需要的一些信息

程序代码:
(1)AppWizard 创建一个基于对话框的程序MyCDPlayer,读者参照图3-4 可以方便地增加相应控件。
(2)封装一个类CCDAudio。下面包括了打开、关闭设备,播放、停止和暂停等主要操作的代码。

//打开设备
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 );
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 );
}
mciSendCommand( m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL );
return( TRUE );
}
//关闭设备
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;
}
//播放
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() );
if( mciSendCommand( m_wDeviceID, MCI_PLAY, MCI_FROM, (DWORD)(LPVOID) &PlayParms ) )
return( FALSE );
m_bPaused = FALSE;
m_bPlaying = TRUE;
return( TRUE );
}
//停止
void CCDAudio::Stop( void )
{
if( !m_bOpened || !m_bPlaying )
return;
mciSendCommand( m_wDeviceID, MCI_STOP, MCI_WAIT, NULL );
mciSendCommand( m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL );
m_bPlaying = FALSE;
m_bPaused = FALSE;
}
//暂停
void CCDAudio::Pause( void )
{
if( !m_bOpened || !m_bPlaying )
return;
MCI_PLAY_PARMS PlayParms;
if( mciSendCommand( m_wDeviceID, MCI_PAUSE, 0, (DWORD)(LPVOID) &PlayParms ) )
return;
m_bPaused = TRUE;
}

(3)在对话框的头文件中增加一个CCDAudio 类型的成员变量m_CDAudio,所有操作将通过该变量的函数来执行。
(4)在OnInitialUpdate 函数中打开设备,并将所有曲目加到CcomboBox 列表框中,然后增加一个定时器,其代码如下:

BOOL CMyCDPlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//⋯⋯⋯
//其他初始化代码
//打开设备
m_CDAudio.Open();
//创建一个定时器
SetTimer( 1, 1000, NULL );
//将曲目加到CComboBox 控件中
for(int i=0;i<m_CDAudio.GetTotalTracks();i++)
{
CString str;
str.Format("曲目%d",i+1);
m_lstMusics.AddString(str);
}
m_lstMusics.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}

(5)在退出窗口时杀掉定时器:

void CMyCDPlayerDlg::OnDestroy()
{
//杀掉定时器
KillTimer(1);
CDialog::OnDestroy();
}

(6)响应定时器,随时更新屏幕信息,如曲目播放进度、调整按钮状态等,其代码如下:

void CMyCDPlayerDlg::OnTimer(UINT nIDEvent)
{
BOOL bDriveReady = TRUE;
//获得当前曲目已经播放的时间,并显示
CString strStatus;
strStatus.Format( "[%d] %02d:%02d",
m_CDAudio.GetCurrentTrack(),
m_CDAudio.GetMinutes(),
m_CDAudio.GetSeconds() );
//没有CD
if( m_CDAudio.GetCurrentTrack() == -1 ){
strStatus = "没有CD";
bDriveReady = FALSE;
}
SetDlgItemText( IDC_TRACKINFO, strStatus );
//获得CD 的总共长度,并显示
CString strLength;
int nMinutes, nSeconds;
m_CDAudio.GetTotalLength( &nMinutes,
&nSeconds );
strLength.Format( "总共长度(m:s): %02d:%02d",
nMinutes, nSeconds );
if( nMinutes == -1 )
strLength = "总共长度(m:s): 00:00";
SetDlgItemText( IDC_TOTALTIME, strLength );
//获得当前们曲目长度,并显示
m_CDAudio.GetTrackLength(
m_CDAudio.GetCurrentTrack(),
&nMinutes, &nSeconds );
strLength.Format( "曲目长度(m:s): %02d:%02d",
nMinutes, nSeconds );
if( nMinutes == -1 )
strLength = "曲目长度(m:s): 00:00";
SetDlgItemText( IDC_TRACKTIME, strLength );
//根据播放状态改变按钮的可用与不可用状态
CWnd *pWnd;
pWnd = GetDlgItem( IDC_BACK );
pWnd->EnableWindow( bDriveReady );
pWnd = GetDlgItem( IDC_FORWARD );
pWnd->EnableWindow( bDriveReady );
pWnd = GetDlgItem( IDC_SKIPBACK );
pWnd->EnableWindow( bDriveReady );
pWnd = GetDlgItem( IDC_SKIPFORWARD );
pWnd->EnableWindow( bDriveReady );
BOOL bPaused;
if( m_CDAudio.IsPlaying( &bPaused ) ){
pWnd = GetDlgItem( IDC_PLAY );
pWnd->EnableWindow( bPaused );
pWnd = GetDlgItem( IDC_STOP );
pWnd->EnableWindow( bDriveReady );
pWnd = GetDlgItem( IDC_PAUSE );
pWnd->EnableWindow( bDriveReady && !bPaused );
}
else{
pWnd = GetDlgItem( IDC_PLAY );
pWnd->EnableWindow( bDriveReady );
pWnd = GetDlgItem( IDC_STOP );
pWnd->EnableWindow( FALSE );
pWnd = GetDlgItem( IDC_PAUSE );
pWnd->EnableWindow( FALSE );
}
CDialog::OnTimer(nIDEvent);
}

(7)播放、停止、跳跃等控制命令的代码如下:

void CMyCDPlayerDlg::OnPlay()
{
// 如果光驱中的CD 已经准备好就播放
if( m_CDAudio.IsDriveReady() )
m_CDAudio.Play();
}
void CMyCDPlayerDlg::OnStop()
{
// 停止播放
m_CDAudio.Stop();
}
void CMyCDPlayerDlg::OnPause()
{
// 暂停播放
m_CDAudio.Pause();
}
void CMyCDPlayerDlg::OnForward()
{
// 获得当前曲目的播放长度
int nMinutes = m_CDAudio.GetMinutes();
int nSeconds = m_CDAudio.GetSeconds();
int nTrack = m_CDAudio.GetCurrentTrack();
if( nMinutes == -1 )
return;
//向前跳跃5s,保证没有出界
nSeconds += 5;
if( nSeconds > 59 ){
nMinutes++;
nSeconds -= 60;
}
int nTrackMinutes, nTrackSeconds;
m_CDAudio.GetTrackLength( nTrack,
&nTrackMinutes, &nTrackSeconds );
if( nMinutes * 60 + nSeconds >
nTrackMinutes * 60 + nTrackSeconds ){
nMinutes = nTrackMinutes;
nSeconds = nTrackSeconds;
}
//定位到新的位置
m_CDAudio.SeekTo( nTrack,
nMinutes, nSeconds, 0 );
}
void CMyCDPlayerDlg::OnBack()
{
// 获得当前曲目的播放长度
int nMinutes = m_CDAudio.GetMinutes();
int nSeconds = m_CDAudio.GetSeconds();
int nTrack = m_CDAudio.GetCurrentTrack();
if( nMinutes == -1 )
return;
//向前跳跃5s,保证没有出界
nSeconds -= 5;
if( nSeconds < 0 ){
nMinutes--;
nSeconds += 60;
if( nMinutes < 0 )
nMinutes = 0;
}
//定位到新的位置
m_CDAudio.SeekTo( nTrack,
nMinutes, nSeconds, 0 );
}
void CMyCDPlayerDlg::OnSkipback()
{
//向后跳跃一个曲目
int nTrack = m_CDAudio.GetCurrentTrack() - 1;
if( nTrack < 1 )
nTrack = m_CDAudio.GetTotalTracks();
m_CDAudio.SeekTo( nTrack, 0, 0, 0 );
}
void CMyCDPlayerDlg::OnSkipforward()
{
//向前跳跃一个曲目
int nTrack = m_CDAudio.GetCurrentTrack() + 1;
if( nTrack > m_CDAudio.GetTotalTracks() )
nTrack = 1;
m_CDAudio.SeekTo( nTrack, 0, 0, 0 );
}
void CMyCDPlayerDlg::OnSelchangeMusicList()
{
//当CComboBox 的曲目选择改变时,播放被选择曲目
int nSelTrack = m_lstMusics.GetCurSel();
m_CDAudio.SeekTo(nSelTrack+1,0,0,0);
}
m_ActiveMovie.Run ();
}

    相关声明:

      若“VC++写的简单CD 播放器”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。