首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudiounit:我不能播放像延迟这样的声音效果

AVAudiounit:我不能播放像延迟这样的声音效果
EN

Stack Overflow用户
提问于 2017-03-27 19:16:46
回答 1查看 139关注 0票数 0

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

代码语言:javascript
复制
- (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];
}

在那之后,我尝试了这个代码,但是声音是没有效果的:

代码语言:javascript
复制
- (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];
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-27 19:27:56

你忘了实例化你的对象:

代码语言:javascript
复制
//AVAudioEngine *engine; // no instance exists. 
AVAudioEngine *engine = [AVAudioEngine new]; 
AVAudioPlayerNode *playerA = [AVAudioPlayerNode new];

...

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

此外,在使用playerA之前,您只需初始化它一次。

为了更深入地挖掘,您可能想要检查Apples code samples

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43044916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档