2010-09-29 17:29:43 +08:00
|
|
|
#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;
|
|
|
|
|
2010-12-15 18:28:18 +08:00
|
|
|
namespace CocosDenshion {
|
|
|
|
|
2010-09-29 17:29:43 +08:00
|
|
|
SoundPlayer::SoundPlayer()
|
|
|
|
: m_pPlayer(NULL)
|
|
|
|
, m_pMediaFile(NULL)
|
|
|
|
, m_MethodEmun(NULL)
|
|
|
|
{
|
|
|
|
TCoInitialize(NULL);
|
2010-11-12 16:33:50 +08:00
|
|
|
if(m_MethodEmun.EnumMethod(TIID_DataType_SysFile, 0, TIID_MediaPlayer_Method_Play )>0)
|
|
|
|
{
|
2010-09-29 17:29:43 +08:00
|
|
|
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);
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SoundPlayer::~SoundPlayer()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
Boolean SoundPlayer::OpenAudioFile(const char* pszFilePath)
|
|
|
|
{
|
|
|
|
Boolean bRet = FALSE;
|
|
|
|
|
2010-10-13 14:18:03 +08:00
|
|
|
do
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2010-10-13 14:18:03 +08:00
|
|
|
BREAK_IF(!m_pMediaFile);
|
|
|
|
|
2010-09-29 17:29:43 +08:00
|
|
|
TUString::StrGBToUnicode(m_fileName, (const Char*)pszFilePath);
|
2010-10-13 14:18:03 +08:00
|
|
|
BREAK_IF(!EOS_IsFileExist(m_fileName));
|
2010-09-29 17:29:43 +08:00
|
|
|
|
|
|
|
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);
|
2010-09-29 17:29:43 +08:00
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundPlayer::PlaySoundFile(const char* pFileName, Int32 nTimes)
|
|
|
|
{
|
|
|
|
if (OpenAudioFile(pFileName))
|
|
|
|
{
|
|
|
|
m_pPlayer->Start();
|
|
|
|
m_pPlayer->SetLoopTimes(nTimes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 10:21:24 +08:00
|
|
|
void SoundPlayer::PlaySoundFromMem(UInt8* pData, Int32 nSize, const char* FileName, Int32 nTimes)
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
|
|
|
if (m_pMediaFile)
|
|
|
|
{
|
|
|
|
Int32 nRet = m_pMediaFile->SetContent(pData, nSize);
|
2010-10-13 14:18:03 +08:00
|
|
|
|
2010-10-21 10:21:24 +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
|
|
|
|
{
|
2010-12-14 18:03:31 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-09-29 17:29:43 +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;
|
|
|
|
}
|
|
|
|
|
2010-11-12 16:33:50 +08:00
|
|
|
TCoUninitialize();
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundPlayer::Pause()
|
|
|
|
{
|
2011-05-19 14:57:16 +08:00
|
|
|
if (m_pPlayer && PLAYER_STATUS_PLAYING == m_pPlayer->GetCurrentStatus())
|
2010-10-13 16:46:33 +08:00
|
|
|
{
|
|
|
|
m_pPlayer->Pause();
|
|
|
|
}
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundPlayer::Resume()
|
|
|
|
{
|
2011-05-19 14:57:16 +08:00
|
|
|
if (m_pPlayer)
|
2010-10-13 16:46:33 +08:00
|
|
|
{
|
2010-11-26 11:31:03 +08:00
|
|
|
m_pPlayer->Resume();
|
2010-10-13 16:46:33 +08:00
|
|
|
}
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2010-11-12 16:33:50 +08:00
|
|
|
return (status == PLAYER_STATUS_PLAYING);
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Int32 SoundPlayer::GetFileBufferSize(const char* pszFilePath)
|
|
|
|
{
|
|
|
|
Int32 nRet = -1;
|
|
|
|
|
|
|
|
if (OpenAudioFile(pszFilePath))
|
|
|
|
{
|
2010-11-11 11:18:58 +08:00
|
|
|
nRet = m_pPlayer->GetDecodedAudioSize();
|
2010-09-29 17:29:43 +08:00
|
|
|
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;
|
|
|
|
}
|
2010-12-15 18:28:18 +08:00
|
|
|
|
|
|
|
} // end of namespace CocosDenshion
|