axmol/CocosDenshion/uphone/SoundPlayer.cpp

200 lines
4.3 KiB
C++
Raw Normal View History

#include "SoundPlayer.h"
#include "TCOM_MediaPlayer_IIDs.h"
#include "TCOM_Generic_DataType_IIDs.h"
2010-10-13 14:18:03 +08:00
#define BREAK_IF(cond) if (cond) break;
namespace CocosDenshion {
SoundPlayer::SoundPlayer()
: m_pPlayer(NULL)
, m_pMediaFile(NULL)
, m_MethodEmun(NULL)
{
TCoInitialize(NULL);
if(m_MethodEmun.EnumMethod(TIID_DataType_SysFile, 0, TIID_MediaPlayer_Method_Play )>0)
{
if(m_MethodEmun.GetDataTypeInterface(0,TCOM_CLSCTX_INPROC_SERVER,(LPVOID *)&m_pMediaFile)>= 0)
{
HRESULT hr = m_pMediaFile->QueryInterface( TIID_MediaPlayer_Method_Play ,(LPVOID*)&m_pPlayer);
if(TCOM_S_FAIL(hr))
{
}
else
{
m_pPlayer->SetVolume(100);
}
}
}
2010-11-12 11:48:51 +08:00
MemSet(m_fileName, 0, sizeof(TUChar) * MAX_PATH);
}
SoundPlayer::~SoundPlayer()
{
Release();
}
Boolean SoundPlayer::OpenAudioFile(const char* pszFilePath)
{
Boolean bRet = FALSE;
2010-10-13 14:18:03 +08:00
do
{
2010-10-13 14:18:03 +08:00
BREAK_IF(!m_pMediaFile);
TUString::StrGBToUnicode(m_fileName, (const Char*)pszFilePath);
2010-10-13 14:18:03 +08:00
BREAK_IF(!EOS_IsFileExist(m_fileName));
m_pMediaFile->SetName(m_fileName);
2010-11-12 10:58:19 +08:00
BREAK_IF(! m_pPlayer || ! m_pPlayer->Open());
2010-10-13 14:18:03 +08:00
bRet = TRUE;
} while (0);
return bRet;
}
void SoundPlayer::PlaySoundFile(const char* pFileName, Int32 nTimes)
{
if (OpenAudioFile(pFileName))
{
m_pPlayer->Start();
m_pPlayer->SetLoopTimes(nTimes);
}
}
void SoundPlayer::PlaySoundFromMem(UInt8* pData, Int32 nSize, const char* FileName, Int32 nTimes)
{
if (m_pMediaFile)
{
Int32 nRet = m_pMediaFile->SetContent(pData, nSize);
2010-10-13 14:18:03 +08:00
if (! strlen(FileName))
2010-10-13 14:18:03 +08:00
{
const TUChar ExtendName[] = {'.', 'w', 'a', 'v', 0};
const TUChar format[] = { '%', 'd', 0};
TUString::StrPrintF(m_fileName, format, nSize);
TUString::StrCat(m_fileName, ExtendName);
}
else
{
std::string strTemp = FileName;
std::string strFileName = strTemp.substr(strTemp.rfind('/') + 1);
TUString::StrGBToUnicode(m_fileName, (const Char*)(strFileName.c_str()));
2010-10-13 14:18:03 +08:00
}
m_pMediaFile->SetName(m_fileName);
m_pMediaFile->SetMode(SYS_FILE_MEMORY_FILE_TYPE);
if (m_pPlayer->Open())
{
m_pPlayer->Start();
m_pPlayer->SetLoopTimes(nTimes);
}
}
}
void SoundPlayer::SetVolumeValue(Int32 nValue)
{
if (m_pPlayer)
{
m_pPlayer->SetVolume(nValue);
}
}
void SoundPlayer::Release()
{
if (m_pPlayer)
{
m_pPlayer->Release();
m_pPlayer = NULL;
}
if (m_pMediaFile)
{
m_pMediaFile->Release();
m_pMediaFile = NULL;
}
TCoUninitialize();
}
void SoundPlayer::Pause()
{
if (m_pPlayer && m_pPlayer->GetCurrentStatus() == PLAYER_STATUS_PLAYING)
{
m_pPlayer->Pause();
}
}
void SoundPlayer::Resume()
{
if (m_pPlayer && m_pPlayer->GetCurrentStatus() == PLAYER_STATUS_PAUSED)
{
2010-11-26 11:31:03 +08:00
m_pPlayer->Resume();
}
}
void SoundPlayer::Stop()
{
if (m_pPlayer)
{
m_pPlayer->Stop();
}
}
void SoundPlayer::Rewind()
{
if (m_pPlayer && m_pPlayer->HasAudio())
{
m_pPlayer->Stop();
m_pPlayer->Start();
}
}
void SoundPlayer::Mute(bool bMute)
{
if (m_pPlayer)
{
m_pPlayer->Mute(bMute);
}
}
bool SoundPlayer::IsPlaying()
{
2010-11-12 10:58:19 +08:00
TMediaPlayerStatus status = m_pPlayer->GetCurrentStatus();
return (status == PLAYER_STATUS_PLAYING);
}
Int32 SoundPlayer::GetFileBufferSize(const char* pszFilePath)
{
Int32 nRet = -1;
if (OpenAudioFile(pszFilePath))
{
2010-11-11 11:18:58 +08:00
nRet = m_pPlayer->GetDecodedAudioSize();
m_pPlayer->Close();
}
return nRet;
}
Int32 SoundPlayer::DecodeFile(void* buffer, Int32 bufferLen, const char* pszFilePath)
{
Int32 nRet = -1;
UInt8* pBuffer = (UInt8*) buffer;
if (OpenAudioFile(pszFilePath))
{
nRet = m_pPlayer->DecodeAudioToBuffer(pBuffer, bufferLen, TMM_AUDIO_FILE_WAV);
m_pPlayer->Close();
}
return nRet;
}
} // end of namespace CocosDenshion