2013-08-19 60 views
0

我正在使用cocos2d v2並遇到非常奇怪的行爲。cocos2d背景音樂的奇怪行爲iOS

我有一些應該作爲背景音樂接連播放的音軌。但是我注意到當這些軌道在後臺播放時,屏幕上的任何更新(渲染)都不起作用。

例如,我在每個新軌道後添加了一個新的精靈標記,但在屏幕上沒有顯示,直到所有軌道完成播放。我也嘗試使用CCLABELBMFont顯示軌道#,但在所有軌道完成播放之前,屏幕上也沒有顯示任何內容。

下面的代碼:

NSString *keyString; 
CCARRAY_FOREACH([[GameManager sharedGameManager] _musicItems], keyString){ 
    if ([[[GameManager sharedGameManager] _soundEngine] isBackgroundMusicPlaying]) { 
     int waitCycles = 0; 
     while (waitCycles < 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 **********************/ 


} 

編輯: 這裏實施的音頻設置

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

任何人有想法,爲什麼它的發生?

+1

這是在主線程上運行嗎? – Sebastian

+0

是的音頻是異步加載和播放的背景,所以有一個不同的線程。有兩種線程處理它的方法嗎? – ganesh

回答

0

你的精靈永遠不會被繪製,因爲你阻塞了主線程。您應該異步調度到後臺隊列,並且當您想要對UI進行更改(添加或操作精靈)時,將調度回主隊列。

+0

我認爲你是對的。我的實現不正確。但是當我嘗試使用dispatch_async或NSOperationQueue時,它沒有幫助。可能是我沒有做正確的方式。我添加了上面的變化,以很好的格式顯示:)。請告訴我我做錯了什麼?感謝您的時間和幫助:D – ganesh