怎样在VC++中实现播放MP3背景音乐

最近急需用到背景音乐,找到了播放wav的方法,不过这种格式太难找了,而且太大了。在不涉及MFC和D3D的情况下,如何让其播放MP3音乐。什么都不要,麻烦直接告诉我代码就可... 最近急需用到背景音乐,找到了播放 wav 的方法,不过这种格式太难找了,而且太大了。
在不涉及MFC 和 D3D 的情况下,
如何让其播放MP3音乐。

什么都不要,麻烦直接告诉我代码就可以了。 多谢。

分数有限。只能给10分了。。。多多海涵。
麻烦给我个全面的。。。。整个代码。能运行的。。
展开
 我来答
保晏然2Q
推荐于2016-12-01 · TA获得超过1053个赞
知道小有建树答主
回答量:422
采纳率:0%
帮助的人:519万
展开全部
两个类:CSound、CMusic
共有两个头文件和两个cpp文件:

// Sound.h: interface for the CSound class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SOUND_H__43D8A7ED_654E_4677_AE58_8D50A0B7BAA0__INCLUDED_)
#define AFX_SOUND_H__43D8A7ED_654E_4677_AE58_8D50A0B7BAA0__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "mmsystem.h"
#pragma comment (lib,"winmm.lib")
class CSound
{
long m_device;
MIXERCONTROL m_volCtrl;
private:
bool CSound::GetDevice(int dev,HMIXER &hmixer);
public:
CSound();
virtual ~CSound();

//operate...
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);

};

#endif // !defined(AFX_SOUND_H__43D8A7ED_654E_4677_AE58_8D50A0B7BAA0__INCLUDED_)

// Sound.cpp: implementation of the CSound class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Sound.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

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;
}

//////////Music.h头文件////////////
/*音乐播放管理类*/
#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; //声音类,用来控制声音
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::s_strSong="";

#endif //MUSIC_H_WFPB

///////Music.cpp源文件//////////////////
#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;

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;
}

使用时声明一个CMusic类对象,然后用他的成员函数就可以播放支持的音乐文件了
上海风华峻极智能科技
2022-05-05 广告
上海风华峻极智能科技有限公司【远程会议视频系统】一体化解决方案,如同面对面开会一般,让远程会议更简单,多种会议场景布局,舞台设备厂家,高保真语音的传输,极速快捷远程会议体验,只为高效沟通,支持万人及多房间同时在线.... 点击进入详情页
本回答由上海风华峻极智能科技提供
arbies
2010-07-27 · TA获得超过442个赞
知道小有建树答主
回答量:398
采纳率:0%
帮助的人:550万
展开全部
sndPlaySound这个函数好像不能播放mp3的。
可以使用mciSendCommand函数来进行处理,还可以进行播放控制。具体可参照MSDN。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
freelaxy
2010-07-27 · TA获得超过253个赞
知道小有建树答主
回答量:373
采纳率:0%
帮助的人:335万
展开全部
sndPlaySound
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式