#ifndef MUSIC_H_WFPB
#define MUSIC_H_WFPB
#include "Sound.h"
class CMusic
{
//public:
// static CString s_strSong;
private:
UINT m_devID; //设备描述
MCI_SEEK_PARMS m_mciSeekParms; //标记播放位置
CSound m_sound; //声音类,用来控制声音
protected:
//辅助函数
static CString GetMusicExt(CString file); //获取歌曲后缀名
static bool IsEqual(CString fileExt,CString devExt); //判断后缀格式相同(不区分大小写).
public:
// static CString s_strSong; //歌曲路径检查
// static void SetSongFile(); //设置歌曲路径
static bool IsMusic(CString file);
CMusic();
~CMusic();
public:
DWORD GetCurPos();
HRESULT Open(CString song);//打开歌曲文件
void Close(); //关闭歌曲文件
void Play(); //播放歌曲
void Repeat(); //重复播放一次
void Stop(); //停止播放
void Seek(DWORD pos);
bool IsEof(); //检查歌曲是否结束
DWORD GetLength();
void AddVoice(UINT n); //增加声音
void SubVoice(UINT n); //减少声音
void SetVoice(UINT n); //设置声音
UINT GetVoice(); //获取声音大小
void SetSilent(bool silent=true);//静音开关
bool IsSilent(); //是否静音
};
//CString CMusic::s_strSong="";
#endif //MUSIC_H_WFPB
#include "StdAfx.h"
#include "music.h"
//辅助函数
CString CMusic::GetMusicExt(CString file) //获取歌曲后缀名
{
return file.Right(file.GetLength()-
file.ReverseFind('.')-1);
}
bool CMusic::IsEqual(CString fileExt,CString devExt) //判断后缀格式相同(不区分大小写).
{
int devlen=fileExt.GetLength();
if(devlen!=devExt.GetLength())
return false;
for (int i=0;i<devlen;i++)
if(toupper(fileExt)!=toupper(devExt))
return false;
return true;
}
//构造函数
CMusic::CMusic()
{
m_devID=0;
memset(&m_mciSeekParms,0,sizeof(MCI_SEEK_PARMS));
}
CMusic::~CMusic()
{
Close();
}
//操作函数
HRESULT CMusic::Open(CString song) //打开歌曲文件
{
if(song=="")
return 1; //空文件
Close();
MCI_OPEN_PARMS openParms;
memset(&openParms,0,sizeof(MCI_OPEN_PARMS));
openParms.lpstrElementName=song;
CString postFix=GetMusicExt(song);
if( IsEqual(postFix,"mp3")||IsEqual(postFix,"aif")||IsEqual(postFix,"asf")||
IsEqual(postFix,"asx")||IsEqual(postFix,"wma")||IsEqual(postFix,"mpeg")||
IsEqual(postFix,"mpg")||IsEqual(postFix,"snd")||IsEqual(postFix,"wmv"))
openParms.lpstrDeviceType="mpegvideo";
else if(IsEqual(postFix,"wav"))
openParms.lpstrDeviceType="waveaudio";
else if(IsEqual(postFix,"mid")||IsEqual(postFix,"rmi"))
openParms.lpstrDeviceType="sequencer";
else if(IsEqual(postFix,"cad"))
openParms.lpstrDeviceType="CDAudio";
else
return FALSE;
if(mciSendCommand(0,MCI_OPEN,MCI_NOTIFY|MCI_OPEN_ELEMENT|MCI_OPEN_TYPE,(DWORD)(LPVOID)&openParms))
return 2; //MCI_OPEN失败
m_devID=openParms.wDeviceID;
return 0; //成功
}
void CMusic::Close() //关闭歌曲文件
{
if(m_devID!=0) mciSendCommand(m_devID,MCI_CLOSE,NULL,NULL);
memset(&m_mciSeekParms,0,sizeof(m_mciSeekParms));
m_devID=0;
}
void CMusic::Play() //播放
{
mciSendCommand(m_devID,MCI_SEEK,MCI_TO,(DWORD)(LPVOID)&m_mciSeekParms);
MCI_PLAY_PARMS playParms;
mciSendCommand(m_devID,MCI_PLAY,NULL,(DWORD)(LPVOID)&playParms);
}
void CMusic::Repeat()