2012-07-26 15:30:09 +08:00
|
|
|
#include "SimpleAudioEngineOpenSL.h"
|
|
|
|
|
2012-08-02 14:28:40 +08:00
|
|
|
#define PLAYSTATE_UNKNOWN 0
|
2012-07-26 15:30:09 +08:00
|
|
|
#define PLAYSTATE_STOPPED 1
|
|
|
|
#define PLAYSTATE_PAUSED 2
|
|
|
|
#define PLAYSTATE_PLAYING 3
|
|
|
|
#define FILE_NOT_FOUND -1
|
|
|
|
|
2012-07-26 16:22:23 +08:00
|
|
|
static OpenSLEngine * s_pOpenSL = 0;
|
2012-07-26 15:30:09 +08:00
|
|
|
static SimpleAudioEngineOpenSL * s_pEngine = 0;
|
|
|
|
|
|
|
|
SimpleAudioEngineOpenSL::SimpleAudioEngineOpenSL()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleAudioEngineOpenSL::~SimpleAudioEngineOpenSL()
|
|
|
|
{
|
|
|
|
if (s_pOpenSL)
|
|
|
|
{
|
|
|
|
s_pOpenSL->closeEngine();
|
|
|
|
|
|
|
|
delete s_pOpenSL;
|
|
|
|
s_pOpenSL = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngineOpenSL::initEngine()
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
2012-08-02 14:28:40 +08:00
|
|
|
if (s_pOpenSL == NULL)
|
2012-07-26 15:30:09 +08:00
|
|
|
{
|
|
|
|
s_pOpenSL = new OpenSLEngine();
|
|
|
|
s_pOpenSL->createEngine();
|
|
|
|
|
|
|
|
bRet = true;
|
2012-08-02 14:28:40 +08:00
|
|
|
}
|
2012-07-26 15:30:09 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleAudioEngineOpenSL* SimpleAudioEngineOpenSL::sharedEngine()
|
|
|
|
{
|
2012-08-02 14:28:40 +08:00
|
|
|
if (s_pEngine == NULL)
|
2012-07-26 15:30:09 +08:00
|
|
|
{
|
|
|
|
s_pEngine = new SimpleAudioEngineOpenSL();
|
|
|
|
}
|
2012-08-02 14:28:40 +08:00
|
|
|
s_pEngine->initEngine();
|
2012-07-26 15:30:09 +08:00
|
|
|
return s_pEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::end()
|
|
|
|
{
|
|
|
|
if (s_pOpenSL)
|
|
|
|
{
|
|
|
|
s_pOpenSL->closeEngine();
|
2012-08-02 14:28:40 +08:00
|
|
|
delete s_pOpenSL;
|
|
|
|
s_pOpenSL = NULL;
|
2012-07-26 15:30:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float SimpleAudioEngineOpenSL::getEffectsVolume()
|
|
|
|
{
|
2012-08-02 14:28:40 +08:00
|
|
|
return s_pOpenSL->getEffectsVolume();
|
2012-07-26 15:30:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::setEffectsVolume(float volume)
|
|
|
|
{
|
2012-08-02 14:28:40 +08:00
|
|
|
if (volume < 0.0f) volume = 0.0f;
|
|
|
|
if (volume > 1.0f) volume = 1.0f;
|
|
|
|
s_pOpenSL->setEffectsVolume(volume);
|
2012-07-26 15:30:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int SimpleAudioEngineOpenSL::playEffect(const char* pszFilePath, bool bLoop)
|
|
|
|
{
|
2012-08-02 14:28:40 +08:00
|
|
|
unsigned int soundID;
|
|
|
|
do
|
2012-07-26 15:30:09 +08:00
|
|
|
{
|
2012-08-02 14:28:40 +08:00
|
|
|
soundID = s_pOpenSL->preloadEffect(pszFilePath);
|
|
|
|
if (soundID != FILE_NOT_FOUND)
|
|
|
|
{
|
|
|
|
if (s_pOpenSL->getEffectState(soundID) == PLAYSTATE_PLAYING)
|
|
|
|
{
|
|
|
|
// recreate an effect player
|
|
|
|
s_pOpenSL->recreatePlayer(pszFilePath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s_pOpenSL->setEffectState(soundID, PLAYSTATE_STOPPED);
|
|
|
|
s_pOpenSL->setEffectState(soundID, PLAYSTATE_PLAYING);
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
s_pOpenSL->setEffectLooping(soundID, bLoop);
|
2012-07-26 15:30:09 +08:00
|
|
|
return soundID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::pauseEffect(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
s_pOpenSL->setEffectState(nSoundId, PLAYSTATE_PAUSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::pauseAllEffects()
|
|
|
|
{
|
|
|
|
s_pOpenSL->setAllEffectState(PLAYSTATE_PAUSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::resumeEffect(unsigned int nSoundId)
|
|
|
|
{
|
2012-07-27 10:36:05 +08:00
|
|
|
s_pOpenSL->resumeEffect(nSoundId);
|
2012-07-26 15:30:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::resumeAllEffects()
|
|
|
|
{
|
2012-07-27 10:36:05 +08:00
|
|
|
s_pOpenSL->resumeAllEffects();
|
2012-07-26 15:30:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::stopEffect(unsigned int nSoundId)
|
|
|
|
{
|
2012-08-02 14:28:40 +08:00
|
|
|
s_pOpenSL->setEffectState(nSoundId, PLAYSTATE_STOPPED, true);
|
2012-07-26 15:30:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::stopAllEffects()
|
|
|
|
{
|
|
|
|
s_pOpenSL->setAllEffectState(PLAYSTATE_STOPPED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::preloadEffect(const char* pszFilePath)
|
|
|
|
{
|
|
|
|
s_pOpenSL->preloadEffect(pszFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngineOpenSL::unloadEffect(const char* pszFilePath)
|
|
|
|
{
|
|
|
|
s_pOpenSL->unloadEffect(pszFilePath);
|
|
|
|
}
|
|
|
|
|