cocos2d iOS中背景音乐的奇怪行为
我正在使用cocos2dv2,并体验到一种非常奇怪的行为 我有几首应该作为背景音乐一首接一首播放的音轨。但我注意到,当这些曲目在后台播放时,屏幕上的任何更新(渲染)都不起作用 例如,我在每个新曲目之后添加了一个新的精灵标记,但在所有曲目播放完毕之前,屏幕上不会显示任何内容。我还尝试过用CCLABELBMFont显示曲目,但在所有曲目播放完毕之前,屏幕上也不会显示任何内容 代码如下:cocos2d iOS中背景音乐的奇怪行为,ios,cocos2d-iphone,Ios,Cocos2d Iphone,我正在使用cocos2dv2,并体验到一种非常奇怪的行为 我有几首应该作为背景音乐一首接一首播放的音轨。但我注意到,当这些曲目在后台播放时,屏幕上的任何更新(渲染)都不起作用 例如,我在每个新曲目之后添加了一个新的精灵标记,但在所有曲目播放完毕之前,屏幕上不会显示任何内容。我还尝试过用CCLABELBMFont显示曲目,但在所有曲目播放完毕之前,屏幕上也不会显示任何内容 代码如下: NSString *keyString; CCARRAY_FOREACH([[GameManager shared
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 **********************/
}
有人知道为什么会这样吗?您的精灵永远不会被绘制,因为您正在阻止主线程。您应该异步调度到后台队列,并且当您想要更改UI(添加或操作精灵)时,应将其调度回主队列。这是在主线程上运行的吗?是的,音频在后台异步加载和播放,因此它有不同的线程。有没有办法用两个线程来处理它?我想你是对的。我的实现不正确。但当我尝试使用dispatch_async或NSOperationQueue时,它没有任何帮助。也许我做得不对。我在上面添加了更改,以便以良好的格式显示:)。请告诉我我做错了什么?谢谢你的时间和帮助:D
-(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");
}
}