使用AVAudioPlayer更改语音音高

时间:2017-04-05 10:54:00

标签: ios objective-c avfoundation

我有一个应用程序可以重复你所说的内容,但是,不是仅仅重复你自己的声音,我希望我可以让它更低或更高,这可能吗?

方法在这里;

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

3 个答案:

答案 0 :(得分:1)

我不知道如何使用AVAudioPlayer来做到这一点。您需要使用像音频单元这样的低级框架。您所描述的内容(在不加速音频的情况下改变音高)实际上在数学上非常复杂。

答案 1 :(得分: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];

及其完成

答案 2 :(得分:-2)

iOS中没有内置API可以播放录制的语音。 但您可以使用以下链接作为参考:http://www.surina.net/soundtouch/