mp3_CSound和CMusic类_转
#ifndef __MP3H__
#define __MP3H__
#define _AFXDLL
#include <afx.h>
#include <mmsystem.h>
#include <mixerocx.h>
class CSound
{
long m_device;
MIXERCONTROL m_volCtrl;
private:
bool CSound::GetDevice(int dev,HMIXER &hmixer);
public:
CSound();
virtual ~CSound();
bool GetVolumeControl(HMIXER hmixer,long componentType,long ctrlType,MIXERCONTROL* mxc);
unsigned GetVolumeValue(HMIXER hmixer,MIXERCONTROL *mxc);
bool SetVolumeValue(HMIXER hmixer,MIXERCONTROL *mxc,long volume);
long GetMuteValue(HMIXER hmixer,MIXERCONTROL* mxc);
bool SetMuteValue(HMIXER hmixer,MIXERCONTROL *mxc,bool mute);
unsigned GetVolume(UINT dev);
bool SetVolume(long dev,long vol);
bool GetMute(long dev);
bool SetMute(long dev,bool sound=TRUE);
};
CSound::CSound()
{
m_device=0;
}
CSound::~CSound()
{
}
bool CSound::GetDevice(int dev,HMIXER &hmixer)
{
switch(dev)
{
case 1: //WAVE
m_device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
break;
case 2: //MIDI
m_device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
break;
case 3: //LINE IN / cd音量
case 4:
m_device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;
break;
case 5: //麦克风音量
m_device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
break;
case 6: //PC 扬声器音量
m_device=MIXERLINE_COMPONENTTYPE_SRC_LINE;
break;
default: //主音量
m_device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}
if(mixerOpen(&hmixer,0,0,0,0))
return FALSE;
return TRUE;
}
//设定音量
bool CSound::GetVolumeControl(HMIXER hmixer,long componentType,long ctrlType,MIXERCONTROL* mxc)
{
MIXERLINECONTROLS mxlc;
MIXERLINE mxl;
mxl.cbStruct = sizeof(mxl);
mxl.dwComponentType = componentType;
if(!mixerGetLineInfo((HMIXEROBJ)hmixer,&mxl,MIXER_GETLINEINFOF_COMPONENTTYPE))
{
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType = ctrlType;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(MIXERCONTROL);
mxlc.pamxctrl = mxc;
if(mixerGetLineControls((HMIXEROBJ)hmixer,&mxlc,MIXER_GETLINECONTROLSF_ONEBYTYPE))
return FALSE;
else
return TRUE;
}
return FALSE;
}
long CSound::GetMuteValue(HMIXER hmixer,MIXERCONTROL* mxc)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_BOOLEAN mxcdMute;
mxcd.hwndOwner = 0;
mxcd.cbStruct = sizeof(mxcd);
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbDetails = sizeof(mxcdMute);
mxcd.paDetails = &mxcdMute;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
if(mixerGetControlDetails((HMIXEROBJ)hmixer,&mxcd,
MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))
return -1;
return mxcdMute.fValue;
}
bool CSound::SetMuteValue(HMIXER hmixer,MIXERCONTROL* mxc,bool mute)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_BOOLEAN mxcdMute;
mxcdMute.fValue=mute;
mxcd.hwndOwner = 0;
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(mxcdMute);
mxcd.paDetails = &mxcdMute;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
if(mixerSetControlDetails((HMIXEROBJ)hmixer,&mxcd,
MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))
return FALSE;
return TRUE;
}
unsigned CSound::GetVolumeValue(HMIXER hmixer,MIXERCONTROL *mxc)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_UNSIGNED vol; vol.dwValue=0;
mxcd.hwndOwner = 0;
mxcd.cbStruct = sizeof(mxcd);
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbDetails = sizeof(vol);
mxcd.paDetails = &vol;
mxcd.cChannels = 1;
if(mixerGetControlDetails((HMIXEROBJ)hmixer,&mxcd,MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))
return -1;
return vol.dwValue;
}
bool CSound::SetVolumeValue(HMIXER hmixer,MIXERCONTROL *mxc,long volume)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_UNSIGNED vol;
vol.dwValue = volume;
mxcd.hwndOwner = 0;
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(vol);
mxcd.paDetails = &vol;
mxcd.cChannels = 1;
if(mixerSetControlDetails((HMIXEROBJ)hmixer,&mxcd,
MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))
return FALSE;
return TRUE;
}
//得到设备的音量
unsigned CSound::GetVolume(UINT dev)
{
unsigned rt=0;
HMIXER hmixer;
if(!GetDevice(dev,hmixer))
return 0;
if(!GetVolumeControl(hmixer,m_device,MIXERCONTROL_CONTROLTYPE_VOLUME,&m_volCtrl))
return 0;
rt=GetVolumeValue(hmixer,&m_volCtrl)*100/m_volCtrl.Bounds.lMaximum;
mixerClose(hmixer);
return rt;
}
//设置设备的音量
bool CSound::SetVolume(long dev,long vol)
{
bool rc=FALSE;
HMIXER hmixer;
if(!GetDevice(dev,hmixer))
return FALSE;
if(GetVolumeControl(hmixer,m_device,MIXERCONTROL_CONTROLTYPE_VOLUME,&m_volCtrl))
{
vol=vol*m_volCtrl.Bounds.lMaximum/100;
if(SetVolumeValue(hmixer,&m_volCtrl,vol))
rc=TRUE;
}
mixerClose(hmixer);
return rc;
}
//设置设备静音
bool CSound::SetMute(long dev,bool sound)
{
bool rc=FALSE;
HMIXER hmixer;
if(!GetDevice(dev,hmixer))
return FALSE;
if(GetVolumeControl(hmixer,m_device,MIXERCONTROL_CONTROLTYPE_MUTE,&m_volCtrl))
if(SetMuteValue(hmixer,&m_volCtrl,sound))
rc=TRUE;
mixerClose(hmixer);
return rc;
}
//检查设备是否静音(静音返回TRUE)
bool CSound::GetMute(long dev)
{
long rc=0;
HMIXER hmixer;
if(!GetDevice(dev,hmixer))
return false;
if(GetVolumeControl(hmixer,m_device,MIXERCONTROL_CONTROLTYPE_MUTE,&m_volCtrl))
rc=GetMuteValue(hmixer,&m_volCtrl);
mixerClose(hmixer);
if(rc)
return true;
return false;
}
class CMusic
{
//public:
// static CString s_strSong;
private:
UINT m_devID; //设备描述
MCI_SEEK_PARMS m_mciSeekParms; //标记播放位置
CSound m_sound; //声音类,用来控制声音
BOOL m_bOpen;
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 CyclePlay(HWND hWnd);
void Repeat(); //重复播放一次
void Stop(); //停止播放
void Seek(DWORD pos);
bool IsEof(); //检查歌曲是否结束
bool IsOpen();
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::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;
CString str1=fileExt;
CString str2=devExt;
str1.MakeLower();
str2.MakeLower();
if(str1!=str2)
return false;
return true;
}
//构造函数
CMusic::CMusic()
{
m_devID=0;
memset(&m_mciSeekParms,0,sizeof(MCI_SEEK_PARMS));
m_bOpen=false;
}
CMusic::~CMusic()
{
Close();
}
//操作函数
HRESULT CMusic::Open(CString song) //打开歌曲文件
{
if(song==_T(""))
{
m_bOpen=false;
return 1; //空文件
}
Close();
MCI_OPEN_PARMS openParms;
memset(&openParms,0,sizeof(MCI_OPEN_PARMS));
openParms.lpstrElementName=song;
CString postFix=GetMusicExt(song);
if( IsEqual(postFix,_T("mp3"))||IsEqual(postFix,_T("aif"))||IsEqual(postFix,_T("asf"))||
IsEqual(postFix,_T("asx"))||IsEqual(postFix,_T("wma"))||IsEqual(postFix,_T("mpeg"))||
IsEqual(postFix,_T("mpg"))||IsEqual(postFix,_T("snd"))||IsEqual(postFix,_T("wmv")))
openParms.lpstrDeviceType=L"mpegvideo";
else if(IsEqual(postFix,+_T("wav")))
openParms.lpstrDeviceType=L"waveaudio";
else if(IsEqual(postFix,_T("mid"))||IsEqual(postFix,_T("rmi")))
openParms.lpstrDeviceType=L"sequencer";
else if(IsEqual(postFix,_T("cad")))
openParms.lpstrDeviceType=L"CDAudio";
else
{
m_bOpen=false;
return FALSE;
}
if(mciSendCommand(0,MCI_OPEN,MCI_NOTIFY|MCI_OPEN_ELEMENT|MCI_OPEN_TYPE,(DWORD)(LPVOID)&openParms))
{
m_bOpen=false;
return 2; //MCI_OPEN失败
}
m_devID=openParms.wDeviceID;
m_bOpen=true;
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;
m_bOpen=false;
}
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::CyclePlay(HWND hWnd)
{
//mciSendCommand(m_devID,MCI_SEEK,MCI_TO,(DWORD)(LPVOID)&m_mciSeekParms);
MCI_PLAY_PARMS mciPlay;
mciPlay.dwCallback = (DWORD)hWnd;
mciSendCommand(m_devID,MCI_PLAY,MCI_NOTIFY,(DWORD)&mciPlay);
}
void CMusic::Repeat() //重复播放一次
{
MCI_PLAY_PARMS playParms;
mciSendCommand(m_devID,MCI_SEEK,MCI_SEEK_TO_START,NULL);
mciSendCommand(m_devID,MCI_PLAY,MCI_NOTIFY ,(DWORD)(LPVOID)&playParms);
}
void CMusic::Stop() //停止播放
{
MCI_STATUS_PARMS statusParms;
statusParms.dwItem=MCI_STATUS_POSITION;
mciSendCommand(m_devID,MCI_STATUS,MCI_WAIT|MCI_STATUS_ITEM,(DWORD)(LPVOID)&statusParms);
m_mciSeekParms.dwTo=statusParms.dwReturn;
mciSendCommand(m_devID,MCI_STOP,NULL,NULL);
}
void CMusic::Seek(DWORD pos)
{
m_mciSeekParms.dwTo=pos;
mciSendCommand(m_devID,MCI_SEEK,MCI_TO,(DWORD)(LPVOID)&m_mciSeekParms);
}
DWORD CMusic::GetLength()
{
MCI_STATUS_PARMS statusParms;
statusParms.dwItem=MCI_STATUS_LENGTH;
mciSendCommand(m_devID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&statusParms);
return statusParms.dwReturn;
}
DWORD CMusic::GetCurPos()
{
MCI_STATUS_PARMS statusParms;
statusParms.dwItem=MCI_STATUS_POSITION;
mciSendCommand(m_devID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&statusParms);
return statusParms.dwReturn;
}
bool CMusic::IsEof()
{
return GetCurPos()>=GetLength()?true:false;
}
bool CMusic::IsOpen()
{
return m_bOpen;
}
void CMusic::SetVoice(UINT n)
{
m_sound.SetVolume(1,n);
}
void CMusic::AddVoice(UINT n) //增加声音
{
m_sound.SetVolume(1,m_sound.GetVolume(1)+n);
}
void CMusic::SubVoice(UINT n) //减少声音
{
m_sound.SetVolume(1,m_sound.GetVolume(1)-n);
}
UINT CMusic::GetVoice() //获取声音大小
{
return m_sound.GetVolume(1);
}
void CMusic::SetSilent(bool silent)
{
m_sound.SetMute(1,silent);
}
bool CMusic::IsSilent()
{
return m_sound.GetMute(1);
}
bool CMusic::IsMusic(CString file)
{
CString ext=GetMusicExt(file);
if(IsEqual(ext,_T("mp3"))||IsEqual(ext,_T("aif"))||IsEqual(ext,_T("asf"))||
IsEqual(ext,_T("asx"))||IsEqual(ext,_T("wma"))||IsEqual(ext,_T("mpeg"))||
IsEqual(ext,_T("mpg"))||IsEqual(ext,_T("snd"))||IsEqual(ext,_T("wmv"))||
IsEqual(ext,_T("wav"))||IsEqual(ext,_T("mid"))||IsEqual(ext,_T("cad")))
return true;
return false;
}
#endif