怎样在VC++中实现播放MP3背景音乐
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样在VC++中实现播放MP3背景音乐相关的知识,希望对你有一定的参考价值。
最近急需用到背景音乐,找到了播放 wav 的方法,不过这种格式太难找了,而且太大了。
在不涉及MFC 和 D3D 的情况下,
如何让其播放MP3音乐。
什么都不要,麻烦直接告诉我代码就可以了。 多谢。
分数有限。只能给10分了。。。多多海涵。
麻烦给我个全面的。。。。整个代码。能运行的。。
共有两个头文件和两个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类对象,然后用他的成员函数就可以播放支持的音乐文件了 参考技术A sndPlaySound这个函数好像不能播放mp3的。
可以使用mciSendCommand函数来进行处理,还可以进行播放控制。具体可参照MSDN。 参考技术B sndPlaySound
用Opencv1.0+VC++6.0怎样打开.mp4格式的视频?
参考技术A 电脑上下一个转换格式工具吧追问转换成.avi之后,我用播放器都能打开,但用Opencv就显示不是正常格式的.avi文件
追答我手机都不能放avi格式 换成mp4就ok了 所以换成你播放支持的格式就好了 上面格式都很齐全的
以上是关于怎样在VC++中实现播放MP3背景音乐的主要内容,如果未能解决你的问题,请参考以下文章