Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behavior of background music in cocos2d iOS
    primarykey
    data
    text
    <p>I am using cocos2d v2 and experiencing a very strange behaviour.</p> <p>I have a couple of audio tracks which are supposed to be played as background music one after another. But I noticed when these tracks are playing in background, any updates on screen (rendering) isn't working.</p> <p>For instance I added a new sprite marker after every new track but nothing shown on screen until all the tracks are done playing. I also tried displaying track # using CCLABELBMFont but that also didn't show anything on screen until all tracks are finished playing.</p> <p>Here's the code:</p> <pre><code>NSString *keyString; CCARRAY_FOREACH([[GameManager sharedGameManager] _musicItems], keyString){ if ([[[GameManager sharedGameManager] _soundEngine] isBackgroundMusicPlaying]) { int waitCycles = 0; while (waitCycles &lt; AUDIO_MAX_WAITTIME) { [NSThread sleepForTimeInterval:0.1f]; if (![[[GameManager sharedGameManager] _soundEngine] isBackgroundMusicPlaying]) { break; } waitCycles += 1; } } //play sound file CCLOG(@"Playing Sound file: %@", keyString); [[GameManager sharedGameManager] playBackgroundTrack:keyString]; **EDIT:** /******** changed to include dispatch: start *********/ dispatch_async(dispatch_get_main_queue(), ^{ CCLOG(@"on main thread"); CCSprite *marker = [CCSprite spriteWithSpriteFrameName:@"marker.png"]; [marker setPosition:ccp(100 * count, 200)]; [self addChild:marker z:100]; }); /***************** end **********************/ } </code></pre> <p><strong>EDIT: Here's implementation for audio setup</strong> </p> <pre><code>-(void)setupAudioEngine{ if(_hasAudioBeenInitialized){ return; //sound engine already initialized } else{ _hasAudioBeenInitialized = YES; NSOperationQueue *queue = [[NSOperationQueue new] autorelease]; NSInvocationOperation *asyncSetupOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(initAudioAsync) object:nil]; [queue addOperation:asyncSetupOperation]; [asyncSetupOperation autorelease]; } } -(void)initAudioAsync{ //Initialize audio engine asynchronously CCLOG(@"Audio Manager Initializing"); _managerSoundState = kAudioManagerInitializing; //start audio engine [CDSoundEngine setMixerSampleRate:CD_SAMPLE_RATE_HIGH]; //Init audio manager asynchronously as it can take a few seconds //The kAMM_FxPlusMusic mode ensure only this game plays audio [CDAudioManager initAsynchronously:kAMM_FxPlusMusic]; //wait for audio manager to initialize while ([CDAudioManager sharedManagerState] != kAMStateInitialised) { [NSThread sleepForTimeInterval:0.1]; } CDAudioManager *audioManager = [CDAudioManager sharedManager]; if (audioManager.soundEngine == nil || audioManager.soundEngine.functioning == NO) { CCLOG(@"COCOS Dension failed to init. No audio will play"); _managerSoundState = kAudioManagerFailed; } else{ [audioManager setResignBehavior:kAMRBStopPlay autoHandle:YES]; _soundEngine = [SimpleAudioEngine sharedEngine]; _managerSoundState = kAudioManagerReady; CCLOG(@"COCOS Dension is ready now"); } } </code></pre> <p>Anyone has ideas why it's happening?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload