2017-04-05 48 views
1

但是,我有一個應用程序可以重複您所說的內容,而不是僅僅重複您自己的聲音,我希望能夠將其調低或調高,這可能嗎?使用AVAudioPlayer更改語音音高

方法就在這裏;

-(void)playOnMainThread:(id)param 
{ 
    if (!outUrl) { 
     return; 
    } 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:&error]; 
    if (error) 
     NSLog(@"AVAudioPlayer error %@, %@", error, [error userInfo]); 

    player.delegate = self; 
    [player setVolume:10.0]; 
    [player play]; 
} 
+0

你可以用'AVAudioPlayerNode'爲的幫助下'AVAudioUnitTimePitch'您可以輕鬆地改變音高 –

+0

這聽起來很有趣,我將如何實現這個(OBJ-C) ? – Hypergater

回答

1

我不知道用AVAudioPlayer做這件事的方法。您需要使用像音頻單元這樣的較低級別的框架。你所描述的(在不加速音頻的情況下改變音高)實際上在數學上非常複雜。

1

所以這是如何完成的。您將需要幾件事情,開始

首次進口<AVFoundation/AVFoundation.h>

然後在.h文件中

AVAudioEngine *audioEngine; 
AVAudioPlayerNode *playerNode; 
AVAudioFile *avAudioFile; 
AVAudioUnitTimePitch *pitch; 
AVAudioMixerNode *mixer; 

您將需要這些東西。

然後在.m文件

-(void)allocInitRecorders 
{ 
    audioEngine = [[AVAudioEngine alloc] init]; 
    playerNode = [[AVAudioPlayerNode alloc] init]; 
    pitch = [[AVAudioUnitTimePitch alloc] init]; 
    mixer = [[AVAudioMixerNode alloc] init]; 

    [audioEngine stop]; 
    [playerNode stop]; 
    [audioEngine reset]; 

} 


-(void)emptytheRecorders 
{ 
    audioEngine = nil; 
    playerNode = nil; 

} 


-(void)attachingNodes 
{ 

[audioEngine attachNode:playerNode]; 

[audioEngine attachNode:pitch]; 
mixer = [audioEngine mainMixerNode]; 
[audioEngine connect:playerNode to:pitch format:[avAudioFile processingFormat]]; 
[audioEngine connect:pitch to:mixer format:[avAudioFile processingFormat]]; 

} 


-(NSString *)filePathOfCollaboratedAudio 
{ 


NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Recording" 
                ofType:@"m4a"]; 

return filePath; 
} 

-(void)playAudio 
{ 

NSError *err = nil; 



[self emptytheRecorders]; 
[self allocInitRecorders]; 
[self attachingNodes]; 




BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[self filePathOfCollaboratedAudio]]; 



if (fileExists) 
{ 
    avAudioFile = [[AVAudioFile alloc] initForReading:[NSURL URLWithString:[self filePathOfCollaboratedAudio]] error:&err]; 
} 


pitch.pitch = 1000; 

[audioEngine prepare]; 
[audioEngine startAndReturnError:&err]; 

NSError *error; 

AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:avAudioFile.processingFormat frameCapacity:(AVAudioFrameCount)avAudioFile.length]; 

[avAudioFile readIntoBuffer:buffer error:&error]; 



[playerNode scheduleBuffer:buffer atTime:nil options:AVAudioPlayerNodeBufferInterruptsAtLoop completionHandler:^{ 


}]; 


if (err != nil) { 
    NSLog(@"An error occured"); 
} 
else 
{ 

    [playerNode play]; 


} 
} 

然後,只需調用方法

[self playAudio]; 

和對其做

+0

嗨拉胡爾,當我嘗試這樣的時候,我聽不到任何聲音,並且語音交談的動畫繼續進行(嘴巴一直沒有聲音地移動)。我的文件路徑不是m4a,是'NSString'會有所作爲嗎? – Hypergater

+0

你在哪裏存儲你的音頻?在NSBundle和m4a沒有太大區別。我可以看到你的代碼嗎? –

+0

我們能否去聊天 – Hypergater

-2

沒有內置在iOS中的API,它可以做的間距與記錄語音。 但是你可以用下面的鏈接以供參考:http://www.surina.net/soundtouch/

+0

有幾種方法可以做到這一點。 – MDB983