AVAudiounit:我无法发挥延迟效果的声音

时间:2017-03-27 11:16:46

标签: ios objective-c avaudioplayer effects

我是目标c的新手,并尝试编写一个可以播放对其产生影响的声音的程序,如混响或延迟。不幸的是,我只能播放声音,但没有效果。我现在被困3天了,无法找到解决方案。有谁能说出我做错了什么? 我尝试了这段代码,但有了它根本没有声音:

- (void)viewDidLoad {
    [super viewDidLoad];
    AVAudioEngine *engine = [AVAudioEngine new];
    AVAudioPlayerNode *playerA = [AVAudioPlayerNode new];

    playerA.volume = 0.5;


    NSURL *Mia1url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MIA1" ofType:@"m4a"]];

    AVAudioFile *MIA1 = [[AVAudioFile alloc] initForReading:Mia1url error:nil];
    AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:MIA1.processingFormat frameCapacity:1024];
    [MIA1 readIntoBuffer:buffer error:nil];

    AVAudioUnitDelay *delay = [AVAudioUnitDelay new];
    delay.delayTime = 100;
    delay.wetDryMix = 90;

    [engine attachNode:playerA];
    [engine attachNode:delay];

    [engine connect: playerA to: delay format:MIA1.processingFormat];
    [engine connect: delay to: engine.mainMixerNode format: MIA1.processingFormat];

    [playerA scheduleBuffer:buffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];

    [engine prepare];
    [engine startAndReturnError:nil];

    [playerA play];
}

之后我尝试了这段代码,但声音只是没有效果:

- (void)viewDidLoad {
    [super viewDidLoad];
    AVAudioEngine *engine = [AVAudioEngine new];
    AVAudioPlayerNode *playerA = [AVAudioPlayerNode new];

    playerA.volume = 0.5;

    NSURL *Mia1url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MIA1" ofType:@"m4a"]];

    AVAudioFile *MIA1 = [[AVAudioFile alloc] initForReading:Mia1url error:nil];
    AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:MIA1.processingFormat frameCapacity:1024];
    [MIA1 readIntoBuffer:buffer error:nil];

    AVAudioUnitDelay *delay = [AVAudioUnitDelay new];
    delay.delayTime = 100;
    delay.wetDryMix = 90;

    [engine attachNode:playerA];
    [engine attachNode:delay];

    [engine connect: playerA to: delay format:MIA1.processingFormat];
    [engine connect: delay to: engine.mainMixerNode format: MIA1.processingFormat];

    [playerA scheduleBuffer:buffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];

    [engine prepare];
    [engine startAndReturnError:nil];

    //change
    self.playerA = [[AVAudioPlayer alloc] initWithContentsOfURL:Mia1url error:nil];

    //change from [playerA play] to:
    [self.playerA play];
}

1 个答案:

答案 0 :(得分:0)

您忘记实例化对象:

//AVAudioEngine *engine; // no instance exists. 
AVAudioEngine *engine = [AVAudioEngine new]; 
AVAudioPlayerNode *playerA = [AVAudioPlayerNode new];

...

AVAudioUnitDelay *delay = [AVAudioUnitDelay new];
...

此外,您必须在使用之前初始化playerA一次。

为了深入挖掘,您可能需要结帐Apples code samples