ios - 使用 AVAudioPlayer 更改音高

标签 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];
}

最佳答案

所以这就是它的完成方式。您需要做一些事情才能开始

首次导入<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];

大功告成

关于ios - 使用 AVAudioPlayer 更改音高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43229110/

相关文章:

iOS - 使用 AVFoundation Framework 从相机模糊预览

ios - 像 QuickTime Player 一样从 OS X 应用程序录制 iPhone/iPad 屏幕

ios - UrbanAirship 在 IOS 上通过 URL 推送

ios - 带有 childKey 的 Firebase queryEqualToValue

ios - Xcode 如何加载主 Storyboard ?

objective-c - %qx 格式说明符是什么意思

ios - 将 Realm 添加到 watchkit 项目时出现 Mach-O header 错误

ios - AVCaptureMovieFileOutput NSInvalidArgumentException 没有事件/启用的连接

iOS:在展开可选值时意外发现 nil

ios - 自引用快速关闭