【问题标题】:AVAudiounit: I can't play a sound with an effect like delayAVAudiounit:我无法播放具有延迟效果的声音
【发布时间】:2017-03-27 11:16:45
【问题描述】:

我是 Objective 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];
}

【问题讨论】:

    标签: ios objective-c avaudioplayer effects


    【解决方案1】:

    你忘了实例化你的对象:

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

    另外,playerA 只需在使用前初始化一次。

    要深入挖掘,您可能需要查看Apples code samples

    【讨论】:

    • 还是不行 :( 不过谢谢你的帮助!
    • 您可能仍在初始化self.playerA 您(尝试)将其附加到引擎之后。首先查看链接的示例代码。第二次用你的新发现更新你的问题,并正确描述正在发生的事情“不起作用”。
    • 我更新了我的代码。不幸的是,我在代码示例中找不到任何有用的东西。你有什么提示我可以在哪个示例中找到解决方案吗?
    • 我不明白您的意思是“找不到有用的代码示例”。单击给定的链接,单击“下载示例代码”,您将获得一个完全正常工作的 Xcode 项目,该项目完全符合您的要求(播放带有效果的音频)。慢慢来。
    猜你喜欢
    • 2015-10-25
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    相关资源
    最近更新 更多