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,16 +546,17 @@ static BOOL configured = FALSE;
{ {
[self.backgroundMusic load:filePath]; [self.backgroundMusic load:filePath];
if (!willPlayBackgroundMusic || _mute) {
CDLOGINFO(@"Denshion::CDAudioManager - play bgm aborted because audio is not exclusive or sound is muted");
return;
}
if (loop) { if (loop) {
[self.backgroundMusic setNumberOfLoops:-1]; [self.backgroundMusic setNumberOfLoops:-1];
} else { } else {
[self.backgroundMusic setNumberOfLoops:0]; [self.backgroundMusic setNumberOfLoops:0];
} }
if (!willPlayBackgroundMusic || _mute) {
CDLOGINFO(@"Denshion::CDAudioManager - play bgm aborted because audio is not exclusive or sound is muted");
return;
}
[self.backgroundMusic play]; [self.backgroundMusic play];
} }
@ -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);
@ -1381,8 +1381,7 @@ static BOOL _mixerRateSet = NO;
-(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;
} }

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();
} }