fixed #1338:stop animation when entering background and start animation when comming to foreground

This commit is contained in:
minggo 2012-06-21 11:42:49 +08:00
parent f8ebbccb98
commit 5cc1f7d230
8 changed files with 42 additions and 36 deletions

View File

@ -113,6 +113,10 @@ NSString * const kCDN_AudioManagerInitialised = @"kCDN_AudioManagerInitialised";
} }
-(void) resume { -(void) resume {
if (!self->systemPaused) {
return;
}
[audioSourcePlayer play]; [audioSourcePlayer play];
} }
@ -542,17 +546,18 @@ static BOOL configured = FALSE;
{ {
[self.backgroundMusic load:filePath]; [self.backgroundMusic load:filePath];
if (!willPlayBackgroundMusic || _mute) { if (loop) {
CDLOGINFO(@"Denshion::CDAudioManager - play bgm aborted because audio is not exclusive or sound is muted"); [self.backgroundMusic setNumberOfLoops:-1];
return; } else {
} [self.backgroundMusic setNumberOfLoops:0];
}
if (loop) {
[self.backgroundMusic setNumberOfLoops:-1]; if (!willPlayBackgroundMusic || _mute) {
} else { CDLOGINFO(@"Denshion::CDAudioManager - play bgm aborted because audio is not exclusive or sound is muted");
[self.backgroundMusic setNumberOfLoops:0]; return;
} }
[self.backgroundMusic play];
[self.backgroundMusic play];
} }
-(void) stopBackgroundMusic -(void) stopBackgroundMusic
@ -713,7 +718,7 @@ static BOOL configured = FALSE;
[self audioSessionResumed]; [self audioSessionResumed];
} }
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 #if __CC_PLATFORM_IOS >= 40000
-(void) endInterruptionWithFlags:(NSUInteger)flags { -(void) endInterruptionWithFlags:(NSUInteger)flags {
CDLOGINFO(@"Denshion::CDAudioManager - interruption ended with flags %i",flags); CDLOGINFO(@"Denshion::CDAudioManager - interruption ended with flags %i",flags);
if (flags == AVAudioSessionInterruptionFlags_ShouldResume) { if (flags == AVAudioSessionInterruptionFlags_ShouldResume) {

View File

@ -88,6 +88,7 @@ void* CDloadWaveAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outD
theData = malloc(dataSize); theData = malloc(dataSize);
if (theData) if (theData)
{ {
memset(theData, 0, dataSize);
AudioFileReadBytes(afid, false, 0, &dataSize, theData); AudioFileReadBytes(afid, false, 0, &dataSize, theData);
if(err == noErr) if(err == noErr)
{ {
@ -195,6 +196,7 @@ void* CDloadCafAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDa
theData = malloc(dataSize); theData = malloc(dataSize);
if (theData) if (theData)
{ {
memset(theData, 0, dataSize);
AudioBufferList theDataBuffer; AudioBufferList theDataBuffer;
theDataBuffer.mNumberBuffers = 1; theDataBuffer.mNumberBuffers = 1;
theDataBuffer.mBuffers[0].mDataByteSize = dataSize; theDataBuffer.mBuffers[0].mDataByteSize = dataSize;

View File

@ -521,7 +521,7 @@ static BOOL _mixerRateSet = NO;
if (soundId >= bufferTotal) { if (soundId >= bufferTotal) {
//Need to resize the buffers //Need to resize the buffers
int requiredIncrement = CD_BUFFERS_INCREMENT; int requiredIncrement = CD_BUFFERS_INCREMENT;
while (bufferTotal + requiredIncrement <= soundId) { while (bufferTotal + requiredIncrement < soundId) {
requiredIncrement += CD_BUFFERS_INCREMENT; requiredIncrement += CD_BUFFERS_INCREMENT;
} }
CDLOGINFO(@"Denshion::CDSoundEngine - attempting to resize buffers by %i for sound %i",requiredIncrement,soundId); CDLOGINFO(@"Denshion::CDSoundEngine - attempting to resize buffers by %i for sound %i",requiredIncrement,soundId);
@ -1379,12 +1379,11 @@ static BOOL _mixerRateSet = NO;
@synthesize filePath, soundId; @synthesize filePath, soundId;
-(id) init:(int) theSoundId filePath:(const NSString *) theFilePath { -(id) init:(int) theSoundId filePath:(const NSString *) theFilePath {
if ((self = [super init])) { if ((self = [super init])) {
soundId = theSoundId; soundId = theSoundId;
filePath = [theFilePath copy];//TODO: is retain necessary or does copy set retain count filePath = [theFilePath copy];
[filePath retain]; }
} return self;
return self;
} }
-(void) dealloc { -(void) dealloc {

View File

@ -169,7 +169,7 @@ void SimpleAudioEngine::end()
s_pEngine = NULL; s_pEngine = NULL;
} }
static_end(); static_end();
} }
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
@ -184,42 +184,42 @@ void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData) void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
{ {
static_stopBackgroundMusic(); static_stopBackgroundMusic();
} }
void SimpleAudioEngine::pauseBackgroundMusic() void SimpleAudioEngine::pauseBackgroundMusic()
{ {
static_pauseBackgroundMusic(); static_pauseBackgroundMusic();
} }
void SimpleAudioEngine::resumeBackgroundMusic() void SimpleAudioEngine::resumeBackgroundMusic()
{ {
static_resumeBackgroundMusic(); static_resumeBackgroundMusic();
} }
void SimpleAudioEngine::rewindBackgroundMusic() void SimpleAudioEngine::rewindBackgroundMusic()
{ {
static_rewindBackgroundMusic(); static_rewindBackgroundMusic();
} }
bool SimpleAudioEngine::willPlayBackgroundMusic() bool SimpleAudioEngine::willPlayBackgroundMusic()
{ {
return static_willPlayBackgroundMusic(); return static_willPlayBackgroundMusic();
} }
bool SimpleAudioEngine::isBackgroundMusicPlaying() bool SimpleAudioEngine::isBackgroundMusicPlaying()
{ {
return static_isBackgroundMusicPlaying(); return static_isBackgroundMusicPlaying();
} }
float SimpleAudioEngine::getBackgroundMusicVolume() float SimpleAudioEngine::getBackgroundMusicVolume()
{ {
return static_getBackgroundMusicVolume(); return static_getBackgroundMusicVolume();
} }
void SimpleAudioEngine::setBackgroundMusicVolume(float volume) void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
{ {
static_setBackgroundMusicVolume(volume); static_setBackgroundMusicVolume(volume);
} }
float SimpleAudioEngine::getEffectsVolume() float SimpleAudioEngine::getEffectsVolume()

View File

@ -57,13 +57,13 @@ bool AppDelegate::applicationDidFinishLaunching()
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() void AppDelegate::applicationDidEnterBackground()
{ {
CCDirector::sharedDirector()->pause(); CCDirector::sharedDirector()->stopAnimation();
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
} }
// this function will be called when the app is active again // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() void AppDelegate::applicationWillEnterForeground()
{ {
CCDirector::sharedDirector()->resume(); CCDirector::sharedDirector()->startAnimation();
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
} }

View File

@ -37,7 +37,7 @@ bool AppDelegate::applicationDidFinishLaunching() {
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() { void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->pause(); CCDirector::sharedDirector()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause // if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
@ -45,7 +45,7 @@ void AppDelegate::applicationDidEnterBackground() {
// this function will be called when the app is active again // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() { void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->resume(); CCDirector::sharedDirector()->startAnimation();
// if you use SimpleAudioEngine, it must resume here // if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

View File

@ -66,7 +66,7 @@ bool AppDelegate::applicationDidFinishLaunching()
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() void AppDelegate::applicationDidEnterBackground()
{ {
CCDirector::sharedDirector()->pause(); CCDirector::sharedDirector()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause // if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
@ -75,7 +75,7 @@ void AppDelegate::applicationDidEnterBackground()
// this function will be called when the app is active again // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() void AppDelegate::applicationWillEnterForeground()
{ {
CCDirector::sharedDirector()->resume(); CCDirector::sharedDirector()->startAnimation();
// if you use SimpleAudioEngine, it must resume here // if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

View File

@ -44,7 +44,7 @@ bool AppDelegate::applicationDidFinishLaunching()
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() void AppDelegate::applicationDidEnterBackground()
{ {
CCDirector::sharedDirector()->pause(); CCDirector::sharedDirector()->stopAnimation();
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
SimpleAudioEngine::sharedEngine()->pauseAllEffects(); SimpleAudioEngine::sharedEngine()->pauseAllEffects();
} }
@ -52,7 +52,7 @@ void AppDelegate::applicationDidEnterBackground()
// this function will be called when the app is active again // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() void AppDelegate::applicationWillEnterForeground()
{ {
CCDirector::sharedDirector()->resume(); CCDirector::sharedDirector()->startAnimation();
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
SimpleAudioEngine::sharedEngine()->resumeAllEffects(); SimpleAudioEngine::sharedEngine()->resumeAllEffects();
} }