用VC写一个简易的MIDI/WAV播放器

 
Windows MCI(Media Control Interface) Windows 提供的控制多媒体设备的高层、通用 的命令接口。它提供一组与设备无关的函数和 命令,可有效地控制多媒体设备。 使用MCI我们可以十分容易的对多媒体进行操作,不需要了解多媒体设备的工作原理和调用底层函数库的方法,这样就极大的减轻了开发的难度和负担。这里举一个WAV/MIDI播放器的小例子,希望以后能用到。
1、首先建立一个基于对话框的工程MIDIPlay,在对话框添加7个按钮,按钮ID分别为ID_OPEN、ID_PLAY、ID_PAUSE、ID_PREVIOUS、ID_NEXT、ID_STOP、ID_EXIT,另外加一个Slider控件。



 
2、为CMIDIPlayDlg类加入成员变量:
CString strFileExt;
 DWORD dCurrentPosition;
 unsigned long m_dLength;
 bool isPause;
 WORD m_wDeviceID;
 CString strFileName;


3、各个按钮的消息相应函数代码如下
None.gif void  CMIDIPlayDlg::OnOpen() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your control notification handler code here
InBlock.gif
    CFileDialog file(TRUE,"","",OFN_FILEMUSTEXIST,"(*.mid及*.wav文件)||*.wav||*.mid");
InBlock.gif    
if (file.DoModal()==IDOK)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        strFileName
=file.GetFileName();
InBlock.gif        strFileExt
=file.GetFileExt();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
this -> SetWindowText("请单击Play按钮,欣赏"+strFileName);
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CMIDIPlayDlg::OnPlay() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your control notification handler code here
InBlock.gif
    isPause=true;
InBlock.gif    SetTimer(
1,33,NULL);
InBlock.gif    mciSendCommand(m_wDeviceID,MCI_CLOSE,
0,NULL);
InBlock.gif    MCI_OPEN_PARMS mciOpen;
InBlock.gif    MCI_PLAY_PARMS mciPlay;
InBlock.gif    mciOpen.lpstrElementName
=strFileName.GetBuffer(strFileName.GetLength());
InBlock.gif    mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT,(DWORD)(LPVOID)
&mciOpen);
InBlock.gif    m_wDeviceID
=mciOpen.wDeviceID;
InBlock.gif    MCI_STATUS_PARMS mciStatusParms;
InBlock.gif    mciStatusParms.dwItem
=MCI_STATUS_LENGTH;
InBlock.gif    mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_WAIT
|MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);
InBlock.gif    m_dLength
=mciStatusParms.dwReturn;
InBlock.gif    mciSendCommand(m_wDeviceID,MCI_PLAY,
0,(DWORD)(LPVOID)&mciPlay);
InBlock.gif    m_position.SetRange(
0,m_dLength);
InBlock.gif    m_position.SetPos(
0);
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CMIDIPlayDlg::OnStop() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your control notification handler code here
InBlock.gif
    mciSendCommand(m_wDeviceID,MCI_STOP,0,NULL);
InBlock.gif    
//m_position.SetPos(0);
ExpandedBlockEnd.gif
}

None.gif
None.gif
void  CMIDIPlayDlg::OnPause() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your control notification handler code here
InBlock.gif
    if(isPause)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        isPause
=FALSE;
InBlock.gif        MCI_GENERIC_PARMS mciPause;
InBlock.gif        mciSendCommand(m_wDeviceID,MCI_PAUSE,
0,(DWORD)(LPVOID)&mciPause);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        isPause
=TRUE;
InBlock.gif        
if(strFileExt=="mid"||strFileExt=="MID")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MCI_STATUS_PARMS mciStatusParms;
InBlock.gif            MCI_PLAY_PARMS mciPlayParms;
InBlock.gif            mciStatusParms.dwItem
=MCI_STATUS_POSITION;
InBlock.gif            mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)
&mciStatusParms);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MCI_GENERIC_PARMS mciResume;
InBlock.gif            mciSendCommand(m_wDeviceID,MCI_RESUME,
0,(DWORD)(LPVOID)&mciResume);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CMIDIPlayDlg::OnPrevious() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your control notification handler code here
InBlock.gif
    isPause=true;
InBlock.gif    MCI_STATUS_PARMS mciStatusParms;
InBlock.gif    MCI_PLAY_PARMS mciPlayParms;
InBlock.gif    mciStatusParms.dwItem
=MCI_STATUS_POSITION;
InBlock.gif    mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)
&mciStatusParms);
InBlock.gif    dCurrentPosition
=mciStatusParms.dwReturn;
InBlock.gif    
if(dCurrentPosition<=(m_dLength/16))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        mciSendCommand(m_wDeviceID,MCI_SEEK,MCI_SEEK_TO_START,NULL);
InBlock.gif        mciSendCommand(m_wDeviceID,MCI_PLAY,
0,(DWORD)(LPVOID)&mciPlayParms);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        mciPlayParms.dwFrom
=dCurrentPosition-(DWORD)(m_dLength/16);
InBlock.gif        mciSendCommand(m_wDeviceID,MCI_PLAY,MCI_FROM,(DWORD)(LPVOID)
&mciPlayParms);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CMIDIPlayDlg::OnNext() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your control notification handler code here
InBlock.gif
    isPause=true;
InBlock.gif    MCI_STATUS_PARMS mciStatusParms;
InBlock.gif    MCI_PLAY_PARMS mciPlayParms;
InBlock.gif    mciStatusParms.dwItem
=MCI_STATUS_POSITION;
InBlock.gif    mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)
&mciStatusParms);
InBlock.gif    dCurrentPosition
=mciStatusParms.dwReturn;
InBlock.gif    
if((m_dLength-dCurrentPosition)<=(m_dLength/16))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        mciSendCommand(m_wDeviceID,MCI_SEEK,MCI_SEEK_TO_END,NULL);
InBlock.gif        mciSendCommand(m_wDeviceID,MCI_PLAY,
0,(DWORD)(LPVOID)&mciPlayParms);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        mciPlayParms.dwFrom
=dCurrentPosition+(DWORD)(m_dLength/16);
InBlock.gif        mciSendCommand(m_wDeviceID,MCI_PLAY,MCI_FROM,(DWORD)(LPVOID)
&mciPlayParms);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

4、加入Timer定时函数
None.gif void  CMIDIPlayDlg::OnTimer(UINT nIDEvent) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your message handler code here and/or call default
InBlock.gif
    MCI_STATUS_PARMS mciStatusParms;
InBlock.gif    mciStatusParms.dwItem
=MCI_STATUS_POSITION;
InBlock.gif    mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,
InBlock.gif        (DWORD)(LPVOID)
&mciStatusParms);
InBlock.gif    dCurrentPosition
=mciStatusParms.dwReturn;
InBlock.gif    m_position.SetPos(dCurrentPosition);
InBlock.gif
InBlock.gif    CDialog::OnTimer(nIDEvent);
}


5、在StdAfx.h文件中添加
#include <mmsystem.h>
#pragma comment (lib,"winmm.lib")

这样一个简单的MIDI/WAV播放器就做好了

转载于:https://www.cnblogs.com/karlchen/archive/2006/10/07/522886.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值