axmol/CocosDenshion/uphone/SoundPlayer.cpp

218 lines
4.6 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;
SoundPlayer::SoundPlayer()
: m_pPlayer(NULL)
, m_pMediaFile(NULL)
, m_MethodEmun(NULL)
, m_nCurrentSoundID(0)
, m_bPaused(FALSE)
{
// TCOM<4F><4D>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>TCOM<4F><4D><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ȳ<EFBFBD>ʼ<EFBFBD><CABC>
TCoInitialize(NULL);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
if(m_MethodEmun.EnumMethod(TIID_DataType_SysFile, 0, TIID_MediaPlayer_Method_Play )>0) //<=0<><30>ʾû<CABE>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽӿ<CDBD>
if(m_MethodEmun.GetDataTypeInterface(0,TCOM_CLSCTX_INPROC_SERVER,(LPVOID *)&m_pMediaFile)>= 0)
{
//<2F><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
HRESULT hr = m_pMediaFile->QueryInterface( TIID_MediaPlayer_Method_Play ,(LPVOID*)&m_pPlayer);
if(TCOM_S_FAIL(hr))
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
else
{
// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD>
m_pPlayer->SetVolume(100);
}
}
}
MemSet(m_fileName, 0, sizeof(m_fileName));
}
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-10-13 14:18:03 +08:00
BREAK_IF(! m_pPlayer->Open());
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
{
// û<><C3BB>ָ<EFBFBD><D6B8><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> .wav <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
const TUChar ExtendName[] = {'.', 'w', 'a', 'v', 0};
const TUChar format[] = { '%', 'd', 0};
TUString::StrPrintF(m_fileName, format, nSize);
TUString::StrCat(m_fileName, ExtendName);
}
else
{
// ʹ<><CAB9>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
TUString::StrGBToUnicode(m_fileName, (const Char*)(FileName));
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()
{
// <20>ͷŲ<CDB7><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
if (m_pPlayer)
{
m_pPlayer->Release();
m_pPlayer = NULL;
}
if (m_pMediaFile)
{
m_pMediaFile->Release();
m_pMediaFile = NULL;
}
TCoUninitialize(); // TCOM<4F><4D><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>TCOM<4F><4D>Դ
}
void SoundPlayer::Pause()
{
if (! m_bPaused)
{
m_pPlayer->Pause();
m_bPaused = TRUE;
}
}
void SoundPlayer::Resume()
{
if (m_bPaused)
{
m_pPlayer->Pause();
m_bPaused = FALSE;
}
}
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()
{
// TMediaPlayerStatus status = m_pPlayer->GetCurrentStatus();
//
// return (status == PLAYER_STATUS_PLAYING || status == PLAYER_STATUS_PAUSED);
return false;
}
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;
}
void SoundPlayer::SetCurrentSoundID(Int32 nSoundID)
{
m_nCurrentSoundID = nSoundID;
}
Int32 SoundPlayer::GetCurrentSoundID()
{
return m_nCurrentSoundID;
}