2014-06-27 15 views
2

我一直在用AVAudioEngine玩,並且無法整合AVAudioUnitEffect類。例如,AVAudioUnitDelay ...用AVAudioEngine麻煩掛鉤AVAudioUnitEffect

@implementation ViewController { 
AVAudioEngine *engine; 
AVAudioPlayerNode *player; 
} 

...

- (IBAction)playButtonHit:(id)sender { 
if (!player){ 
    NSURL *bandsURL = [[NSBundle mainBundle] URLForResource:@"Bands With Managers" withExtension:@"mp3"]; 
    AVAudioFile *file = [[AVAudioFile alloc] initForReading:bandsURL error:nil]; 

    engine = [[AVAudioEngine alloc] init]; 
    player = [[AVAudioPlayerNode alloc] init]; 
    [engine attachNode:player]; 

    AVAudioUnitDelay *delay = [[AVAudioUnitDelay alloc] init]; 
    delay.wetDryMix = 50; 

    [engine connect:player to:delay format:file.processingFormat]; 
    [engine connect:delay to:[engine outputNode] format:file.processingFormat]; 

    [player scheduleFile:file atTime:nil completionHandler:nil]; 
    [engine prepare]; 
    [engine startAndReturnError:nil]; 
} 
[player play]; 

}

當方法被調用應用程序崩潰,我得到這個錯誤:「*終止應用程序由於未捕獲異常'com.apple.coreaudio.avfaudio',原因:'所需條件爲false:[_nodes containsObject:node1] & & [_nodes containsObject:node2]'「

我在WWDC的「AVAudioEngine in Practice」會話中的一些例子之後對此進行了建模。我知道這可能是一些顯而易見的事情,但我想不出來......

+0

蘋果修復了這個問題與iOS 8種子3(建設12A4318c) –

回答

0

這不是AVAudioUnitEffect的問題!我與該代碼

NSError *err = nil; 

self.engine = [[AVAudioEngine alloc] init]; 
AVAudioPlayerNode *player = [[AVAudioPlayerNode alloc] init]; 
[self.engine attachNode:player]; 

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"sound" withExtension:@"m4a"]; 
AVAudioFile *file = [[AVAudioFile alloc] initForReading:fileURL error:&err]; 

AVAudioMixerNode *mainMixer = [self.engine mainMixerNode]; 
[self.engine connect:player to:mainMixer format:file.processingFormat]; 

[player scheduleFile:file atTime:nil completionHandler:nil]; 

[self.engine startAndReturnError:&err]; 

if (err != nil) { 
    NSLog(@"An error occured"); 
} 

[player play]; 

self.engine

@property (nonatomic, strong) AVAudioEngine *engine; 

定義我認爲這是在AVAudioEngine一個bug試了一下,因爲它會導致內存泄漏:它開始播放第一個樣品,然後將其由於內存使用量過大而崩潰(在我的16 kB m4a文件中,超過300 MB)。


更新2014年12月7日:蘋果修復了這個問題與iOS 8種子3(構建12A4318c)!

5

你忘聯繫起來之前,您AvAudioUnitDelay對象附加到你的AvAudioEngine節點;)

這裏是工作代碼:

- (IBAction)playMusic:(id)sender { 
    if (!player){ 
     NSURL *bandsURL = [[NSBundle mainBundle] URLForResource:@"Bands With Managers" withExtension:@"mp3"]; 
     AVAudioFile *file = [[AVAudioFile alloc] initForReading:bandsURL error:nil]; 

     engine = [[AVAudioEngine alloc] init]; 
     player = [[AVAudioPlayerNode alloc] init]; 
     [engine attachNode:player]; 

     AVAudioUnitDelay *delay = [[AVAudioUnitDelay alloc] init]; 
     delay.wetDryMix = 50; 
     [engine attachNode:delay]; 

     [engine connect:player to:delay format:file.processingFormat]; 
     [engine connect:delay to:[engine outputNode] format:file.processingFormat]; 

     [player scheduleFile:file atTime:nil completionHandler:nil]; 
     [engine prepare]; 
     [engine startAndReturnError:nil]; 
    } 
    [player play]; 
}