Getting full path in before playing sound.

This commit is contained in:
James Chen 2013-01-27 19:20:49 +08:00
parent 09cef5511a
commit eec0ba3c19
10 changed files with 91 additions and 84 deletions

View File

@ -34,6 +34,8 @@ THE SOFTWARE.
#include <vorbis/vorbisfile.h>
#include "SimpleAudioEngine.h"
#include "cocos2d.h"
USING_NS_CC;
using namespace std;
@ -257,9 +259,12 @@ namespace CocosDenshion
//
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
if (!s_isBackgroundInitialized || s_currentBackgroundStr != pszFilePath)
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
if (!s_isBackgroundInitialized || s_currentBackgroundStr != fullPath)
{
string path = pszFilePath;
string path = fullPath;
if (isOGGFile(path.data()))
{
@ -285,17 +290,20 @@ namespace CocosDenshion
alSourcei(s_backgroundSource, AL_BUFFER, s_backgroundBuffer);
checkALError("preloadBackgroundMusic");
s_currentBackgroundStr = pszFilePath;
s_currentBackgroundStr = fullPath;
}
s_currentBackgroundStr = pszFilePath;
s_currentBackgroundStr = fullPath;
s_isBackgroundInitialized = true;
}
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
if (!s_isBackgroundInitialized)
preloadBackgroundMusic(pszFilePath);
preloadBackgroundMusic(fullPath.c_str());
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE);
alSourcePlay(s_backgroundSource);
@ -383,17 +391,20 @@ namespace CocosDenshion
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
{
EffectsMap::iterator iter = s_effects.find(pszFilePath);
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
EffectsMap::iterator iter = s_effects.find(fullPath);
if (iter == s_effects.end())
{
preloadEffect(pszFilePath);
preloadEffect(fullPath.c_str());
// let's try again
iter = s_effects.find(pszFilePath);
iter = s_effects.find(fullPath);
if (iter == s_effects.end())
{
fprintf(stderr, "could not find play sound %s\n", pszFilePath);
fprintf(stderr, "could not find play sound %s\n", fullPath.c_str());
return -1;
}
}
@ -415,7 +426,10 @@ namespace CocosDenshion
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
{
EffectsMap::iterator iter = s_effects.find(pszFilePath);
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
EffectsMap::iterator iter = s_effects.find(fullPath);
// check if we have this already
if (iter == s_effects.end())
@ -423,7 +437,7 @@ namespace CocosDenshion
ALuint buffer;
ALuint source;
soundData *data = new soundData;
string path = pszFilePath;
string path = fullPath;
checkALError("preloadEffect");
@ -459,13 +473,16 @@ namespace CocosDenshion
data->buffer = buffer;
data->source = source;
s_effects.insert(EffectsMap::value_type(pszFilePath, data));
s_effects.insert(EffectsMap::value_type(fullPath, data));
}
}
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
{
EffectsMap::iterator iter = s_effects.find(pszFilePath);
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
EffectsMap::iterator iter = s_effects.find(fullPath);
if (iter != s_effects.end())
{

View File

@ -1,5 +1,7 @@
#include "SimpleAudioEngine.h"
#include "FmodAudioPlayer.h"
#include "cocos2d.h"
USING_NS_CC;
namespace CocosDenshion {
@ -19,30 +21,17 @@ SimpleAudioEngine* SimpleAudioEngine::sharedEngine() {
void SimpleAudioEngine::end() {
oAudioPlayer->close();
// sharedMusic().Close();
//
// EffectList::iterator p = sharedList().begin();
// while (p != sharedList().end())
// {
// delete p->second;
// p->second = NULL;
// p++;
// }
// sharedList().clear();
// return;
}
//void SimpleAudioEngine::setResource(const char* pszZipFileName) {
//}
//////////////////////////////////////////////////////////////////////////
// BackgroundMusic
//////////////////////////////////////////////////////////////////////////
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath,
bool bLoop) {
oAudioPlayer->playBackgroundMusic(pszFilePath, bLoop);
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
oAudioPlayer->playBackgroundMusic(fullPath.c_str(), bLoop);
}
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData) {
@ -70,7 +59,9 @@ bool SimpleAudioEngine::isBackgroundMusicPlaying() {
}
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) {
return oAudioPlayer->preloadBackgroundMusic(pszFilePath);
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
return oAudioPlayer->preloadBackgroundMusic(fullPath.c_str());
}
//////////////////////////////////////////////////////////////////////////
@ -79,7 +70,9 @@ void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) {
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath,
bool bLoop) {
return oAudioPlayer->playEffect(pszFilePath, bLoop);
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
return oAudioPlayer->playEffect(fullPath.c_str(), bLoop);
}
void SimpleAudioEngine::stopEffect(unsigned int nSoundId) {
@ -87,11 +80,15 @@ void SimpleAudioEngine::stopEffect(unsigned int nSoundId) {
}
void SimpleAudioEngine::preloadEffect(const char* pszFilePath) {
return oAudioPlayer->preloadEffect(pszFilePath);
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
return oAudioPlayer->preloadEffect(fullPath.c_str());
}
void SimpleAudioEngine::unloadEffect(const char* pszFilePath) {
return oAudioPlayer->unloadEffect(pszFilePath);
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
return oAudioPlayer->unloadEffect(fullPath.c_str());
}
void SimpleAudioEngine::pauseEffect(unsigned int uSoundId) {
@ -136,24 +133,5 @@ void SimpleAudioEngine::setEffectsVolume(float volume) {
return oAudioPlayer->setEffectsVolume(volume);
}
//////////////////////////////////////////////////////////////////////////
// static function
//////////////////////////////////////////////////////////////////////////
const char * _FullPath(const char * szPath) {
}
unsigned int _Hash(const char *key) {
// unsigned int len = strlen(key);
// const char *end=key+len;
// unsigned int hash;
//
// for (hash = 0; key < end; key++)
// {
// hash *= 16777619;
// hash ^= (unsigned int) (unsigned char) toupper(*key);
// }
// return (hash);
}
} // end of namespace CocosDenshion

View File

@ -26,9 +26,10 @@ THE SOFTWARE.
#include "SimpleAudioEngine.h"
#include "s3e.h"
#include "IwUtil.h"
#include <string>
#include <map>
#include "cocos2d.h"
USING_NS_CC;
using namespace std ;
@ -103,9 +104,11 @@ namespace CocosDenshion
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
s3eFile *fileHandle = s3eFileOpen(pszFilePath, "rb");
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
s3eFile *fileHandle = s3eFileOpen(fullPath.c_str(), "rb");
IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", pszFilePath, s3eFileGetError()));
IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", fullPath.c_str(), s3eFileGetError()));
g_AudioFileSize = s3eFileGetSize(fileHandle);
g_AudioBuffer = (int16*)malloc(g_AudioFileSize);
@ -116,18 +119,20 @@ namespace CocosDenshion
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
s3eResult result;
result = s3eAudioPlayFromBuffer(g_AudioBuffer, g_AudioFileSize, bLoop ? 0 : 1);
if ( result == S3E_RESULT_ERROR)
{
result = s3eAudioPlay(pszFilePath, bLoop ? 0 : 1);
result = s3eAudioPlay(fullPath.c_str(), bLoop ? 0 : 1);
}
if ( result == S3E_RESULT_ERROR)
{
IwAssert(GAME, ("Play music %s Failed. Error Code : %s", pszFilePath, s3eAudioGetErrorString()));
IwAssert(GAME, ("Play music %s Failed. Error Code : %s", fullPath.c_str(), s3eAudioGetErrorString()));
}
}
@ -191,20 +196,23 @@ namespace CocosDenshion
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
{
SoundFxMap::iterator it = g_pSoundFxMap->find(pszFilePath) ;
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
SoundFxMap::iterator it = g_pSoundFxMap->find(fullPath) ;
int16* buff = 0 ;
if( it==g_pSoundFxMap->end() ) {
preloadEffect(pszFilePath) ;
preloadEffect(fullPath.c_str()) ;
}
buff = (*g_pSoundFxMap)[pszFilePath].data ;
buff = (*g_pSoundFxMap)[fullPath].data ;
int channel = s3eSoundGetFreeChannel();
s3eSoundChannelPlay(channel, buff, (*g_pSoundFxMap)[pszFilePath].size/2, (bLoop ? 0 : 1), 0);
s3eSoundChannelPlay(channel, buff, (*g_pSoundFxMap)[fullPath].size/2, (bLoop ? 0 : 1), 0);
if (s3eSoundGetError()!= S3E_SOUND_ERR_NONE) {
IwAssertMsg(GAME, false, ("Play sound %s Failed. Error Code : %s", pszFilePath, s3eSoundGetErrorString()));
IwAssertMsg(GAME, false, ("Play sound %s Failed. Error Code : %s", fullPath.c_str(), s3eSoundGetErrorString()));
}
return channel;
@ -218,16 +226,18 @@ namespace CocosDenshion
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
{
SoundFxMap::iterator it = g_pSoundFxMap->find(pszFilePath) ;
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
SoundFxMap::iterator it = g_pSoundFxMap->find(fullPath) ;
if( it==g_pSoundFxMap->end() ) {
s3eFile *fileHandle = s3eFileOpen(pszFilePath, "rb");
s3eFile *fileHandle = s3eFileOpen(fullPath.c_str(), "rb");
IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", pszFilePath, s3eFileGetError()));
IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", fullPath.c_str(), s3eFileGetError()));
int32 fileSize = s3eFileGetSize(fileHandle);
int16* buff = (int16*)malloc(fileSize);
(*g_pSoundFxMap)[pszFilePath] = SoundFx(buff,fileSize) ;
(*g_pSoundFxMap)[fullPath] = SoundFx(buff,fileSize) ;
memset(buff, 0, fileSize);
s3eFileRead(buff, fileSize, 1, fileHandle);
s3eFileClose(fileHandle);
@ -236,9 +246,11 @@ namespace CocosDenshion
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
// effect must not be playing!
SoundFxMap::iterator it = g_pSoundFxMap->find(pszFilePath) ;
SoundFxMap::iterator it = g_pSoundFxMap->find(fullPath) ;
if( it != g_pSoundFxMap->end() ) {
free(it->second.data) ;
g_pSoundFxMap->erase(it) ;

View File

@ -110,7 +110,7 @@ bool HelloWorld::init()
// see http://www.cocos2d-x.org/boards/6/topics/1478
this->schedule( schedule_selector(HelloWorld::updateGame) );
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("background-music-aac.wav"), true);
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true);
bRet = true;
} while (0);
@ -237,7 +237,7 @@ void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
projectile->setTag(2);
_projectiles->addObject(projectile);
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("pew-pew-lei.wav"));
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
}
void HelloWorld::updateGame(float dt)

View File

@ -76,8 +76,8 @@ m_nSoundId(0)
setTouchEnabled(true);
// preload background music and effect
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE) );
SimpleAudioEngine::sharedEngine()->preloadEffect( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE) );
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );
SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
// set default volume
SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
@ -106,7 +106,7 @@ void CocosDenshionTest::menuCallback(CCObject * pSender)
// play background music
case 0:
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str(), true);
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE, true);
break;
// stop background music
case 1:
@ -137,11 +137,11 @@ void CocosDenshionTest::menuCallback(CCObject * pSender)
break;
// play effect
case 6:
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str());
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
break;
// play effect
case 7:
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str(), true);
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true);
break;
// stop effect
case 8:
@ -149,7 +149,7 @@ void CocosDenshionTest::menuCallback(CCObject * pSender)
break;
// unload effect
case 9:
SimpleAudioEngine::sharedEngine()->unloadEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str());
SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE);
break;
// add bakcground music volume
case 10:

View File

@ -524,7 +524,7 @@ bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect)
void SpriteBlur::initProgram()
{
GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString();
CCFileUtils::sharedFileUtils()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
CCGLProgram* pProgram = new CCGLProgram();
pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
setShaderProgram(pProgram);
@ -674,7 +674,7 @@ bool ShaderRetroEffect::init()
{
if( ShaderTestDemo::init() ) {
GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_HorizontalColor.fsh"))->getCString();
GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathForFilename("Shaders/example_HorizontalColor.fsh").c_str())->getCString();
CCGLProgram *p = new CCGLProgram();
p->initWithVertexShaderByteArray(ccPositionTexture_vert, fragSource);

View File

@ -1254,7 +1254,7 @@ TMXOrthoFromXMLTest::TMXOrthoFromXMLTest()
string resources = "TileMaps"; // partial paths are OK as resource paths.
string file = resources + "/orthogonal-test1.tmx";
CCString* str = CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(file.c_str()));
CCString* str = CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathForFilename(file.c_str()).c_str());
CCAssert(str != NULL, "Unable to open file");
CCTMXTiledMap *map = CCTMXTiledMap::createWithXML(str->getCString() ,resources.c_str());

View File

@ -45,7 +45,7 @@ bool AppDelegate::applicationDidFinishLaunching()
pEngine->executeString(pstrFileContent->getCString());
}
#else
std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("hello.lua");
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("hello.lua");
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
pEngine->executeScriptFile(path.c_str());
#endif

View File

@ -157,7 +157,7 @@ local function main()
local function menuCallbackOpenPopup()
-- loop test sound effect
local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("effect1.wav")
local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")
effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath)
menuPopup:setVisible(true)
end
@ -187,10 +187,10 @@ local function main()
-- play background music, preload effect
-- uncomment below for the BlackBerry version
-- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.ogg")
local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.mp3")
-- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.ogg")
local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.mp3")
SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true)
local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("effect1.wav")
local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")
SimpleAudioEngine:sharedEngine():preloadEffect(effectPath)
-- run

View File

@ -41,7 +41,7 @@ bool AppDelegate::applicationDidFinishLaunching()
pEngine->executeString(pstrFileContent->getCString());
}
#else
std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath((dirPath + "/controller.lua").c_str());
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename((dirPath + "/controller.lua").c_str());
pEngine->addSearchPath(path.substr(0, path.find_last_of("/") - dirPath.length()).c_str());
pEngine->executeScriptFile(path.c_str());
#endif