使用AVAudioEngine解决AVAudioUnitEffect问题

时间:2014-06-27 04:18:02

标签: objective-c ios8

我一直在玩AVAudioEngine而且我在整合AVAudioUnitEffect类时遇到了麻烦。例如,使用AVAudioUnitDelay ...

@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”会议中的一些示例之后对此进行了建模。我知道可能有一些显而易见的东西我不知道但是无法理解......

2 个答案:

答案 0 :(得分:6)

在链接之前,您忘记将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]; 
}

答案 1 :(得分: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中的一个错误,因为它会导致内存泄漏:它开始播放第一个样本,然后由于内存使用量过大而崩溃(在我的情况下为16 kB时超过300 MB) m4a文件)。


2014年12月12日更新:Apple使用iOS 8 Seed 3(Build 12A4318c)修复了此问题!