我是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];
}发布于 2017-03-27 19:27:56
你忘了实例化你的对象:
//AVAudioEngine *engine; // no instance exists.
AVAudioEngine *engine = [AVAudioEngine new];
AVAudioPlayerNode *playerA = [AVAudioPlayerNode new];
...
AVAudioUnitDelay *delay = [AVAudioUnitDelay new];
...此外,在使用playerA之前,您只需初始化它一次。
为了更深入地挖掘,您可能想要检查Apples code samples。
https://stackoverflow.com/questions/43044916
复制相似问题