2011-03-14 93 views
2

我試圖爲需要快速連續播放樣本的應用程序獲得流暢的音頻播放。我希望Cocos2d和CocosDenshion能夠做到這一點,因爲AVAudioPlayer由於滯後問題而無法工作,但我仍然遇到問題 - 這裏模擬的「第16個音符」最終聽起來會搖擺不定。iOS CocosDenshion音頻播放參差不齊(延遲)

我將不得不去與RemoteIO或類似的東西?在iOS中播放聲音的精確時機最簡單的方法是什麼?

或者,正在使用CDAudioEngine playSound:函數不是我可以用CocosDenshion做的最有效的方法嗎?

要加載引擎:

[CDAudioManager sharedManager]; 

while ([CDAudioManager sharedManagerState] != kAMStateInitialised) { 
    [NSThread sleepForTimeInterval:0.1]; 
    NSLog(@"Not init yet..."); 
} 



CDSoundEngine *sse = [CDAudioManager sharedManager].soundEngine; 

NSArray *defs = [NSArray arrayWithObjects: 
       [NSNumber numberWithInt:16],nil]; 
[sse defineSourceGroups:defs]; 
[[CDAudioManager sharedManager].soundEngine setSourceGroupNonInterruptible:0 isNonInterruptible:TRUE]; 

NSLog(@"Loading sound: %i",[sse loadBuffer:1 filePath:@"bass drum.wav"]); 

[[CDAudioManager sharedManager] setResignBehavior:kAMRBStopPlay autoHandle:YES]; 
[[CDAudioManager sharedManager] setMode:(kAMM_MediaPlayback)]; 

線程代碼以測試播放(基本上節拍器示例應用程序,editted使用CocosDenshion播放):

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

// Give the sound thread high priority to keep the timing steady. 
[NSThread setThreadPriority:1.0]; 
BOOL continuePlaying = YES; 

CDSoundEngine *sse = [CDAudioManager sharedManager].soundEngine; 

while (continuePlaying) { // Loop until cancelled. 

    // An autorelease pool to prevent the build-up of temporary objects. 
    NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init]; 


    [sse playSound:1 sourceGroupId:0 pitch:1.0f pan:0.0f gain:1.0f loop:NO]; 
    withObject:nil waitUntilDone:NO]; 
    NSDate *curtainTime = [[NSDate alloc] initWithTimeIntervalSinceNow:0.125f]; 
    NSDate *currentTime = [[NSDate alloc] init]; 

    // Wake up periodically to see if we've been cancelled. 
    while (continuePlaying && ([currentTime compare:curtainTime] != NSOrderedDescending)) { 
     if ([soundPlayerThread isCancelled] == YES) { 
      continuePlaying = NO; 
     } 
     [NSThread sleepForTimeInterval:0.005]; 
     [currentTime release]; 
     currentTime = [[NSDate alloc] init]; 
    } 
    [curtainTime release];  
    [currentTime release];  
    [loopPool drain]; 
} 
[pool drain]; 

回答

2
+1

呃 - 我希望在那篇文章中他不需要介入「Doom的偉大音頻接口」。 – 2011-03-14 20:13:45

+0

我已經使用了RemoteIO。這有點複雜,但並不像我認爲的那樣糟糕。 – madmik3 2011-03-14 20:22:20

+0

任何好的指針(鏈接?)只是開始 - 只是用它播放WAV文件? – 2011-03-14 20:30:04