2017-03-27 101 views
0

我是新來的目標c,並嘗試編碼一個可以播放聲音的程序,如混響或延遲。不幸的是,我只能播放聲音,但沒有效果。我現在陷入了3天,無法找到解決方案。有沒有人可以告訴我做錯了什麼? 我嘗試這樣的代碼,但它沒有音質可言:AVAudiounit:我無法播放延遲效果的聲音

- (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]; 
} 

回答

0

你忘了來實例對象:

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

... 

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

此外,你必須初始化playerA只有一次,在使用它之前。

對於深入挖掘,您可能需要結算Apples code samples

+0

它仍然沒有工作:(但是,感謝您的幫助! – slic

+0

您可能仍在初始化'self.playerA' _after_您(試圖)將其附加到引擎。你的問題與你的新發現和適當的說明發生了什麼「不起作用」。 – shallowThought

+0

我更新了我的代碼。不幸的是,我找不到任何有用的代碼示例。你有任何提示,我可以找到樣本解決方案? – slic