使用FMOD播放MP3、WMA

FMOD是一个音频库,个人可以免费使用。

 

FMOD 官方网站:http://www.fmod.org/

 

下面是一个对它简单的封装。

 

1. 头文件

class CPlayer { public: CPlayer(); ~CPlayer(); public: public: bool Play(CString strFileName); bool Pause(); bool Continue(); bool Stop(); const unsigned int GetCurPos() const; const unsigned int GetMilliSeconds() const; // 0.0 = silent, 1.0 = full volume void SetVolume(float f_volume); private: void ReadTags(); private: FMOD::System *m_pSystem; FMOD::Sound *m_pSound; FMOD::Channel *m_pChannel; };

 

2. 源文件

CPlayer::CPlayer() { FMOD::System_Create(&m_pSystem); m_pSystem->init(120, FMOD_INIT_NORMAL, 0); m_pSound = NULL; m_pChannel = NULL; } CPlayer::~CPlayer() { if (m_pChannel != NULL) m_pChannel->stop(); if (m_pSound != NULL) m_pSound->release(); if (m_pSystem != NULL) { m_pSystem->close(); m_pSystem->release(); } } bool CPlayer::Play(CString strFileName) { if (m_pChannel != NULL) m_pChannel->stop(); if (m_pSound != NULL) m_pSound->release(); std::string szFileName = CUtility::unicode_2_ansi(strFileName); m_pSystem->createStream(szFileName.c_str(), FMOD_DEFAULT, 0, &m_pSound); m_pSystem->playSound(FMOD_CHANNEL_FREE, m_pSound, false, &m_pChannel); ReadTags(); return true; } bool CPlayer::Pause() { if (m_pChannel != NULL) m_pChannel->setPaused(true); return false; } bool CPlayer::Continue() { if (m_pChannel != NULL) m_pChannel->setPaused(false); return false; } bool CPlayer::Stop() { if (m_pChannel != NULL) { m_pChannel->stop(); return true; } return false; } const unsigned int CPlayer::GetCurPos() const { unsigned int u_cur_pos; m_pChannel->getPosition(&u_cur_pos, FMOD_TIMEUNIT_MS); return u_cur_pos; } const unsigned int CPlayer::GetMilliSeconds() const { unsigned int u_max_length; m_pSound->getLength(&u_max_length, FMOD_TIMEUNIT_MS); return u_max_length; } void CPlayer::SetVolume(float f_volume) { m_pChannel->setVolume(f_volume); } void CPlayer::ReadTags() { /*int nNumTags; m_pSound->getNumTags(&nNumTags, 0); std::string s_title; std::string s_artist; FMOD_TAG tag; ZeroMemory(&tag, sizeof(FMOD_TAG)); m_pSound->getTag("TITLE", -1, &tag); char *p_title = new char[tag.datalen]; memset(p_title, 0, tag.datalen); strcpy(p_title, (char *) tag.data); s_title = CUtility::utf8_2_ansi(p_title); m_pSound->getTag("ARTIST", -1, &tag);*/ }

你可能感兴趣的:(String,null,System,Class,float)