2014-06-27 17 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]; 
}