将音频文件保存在后台

我有一个应用程序,当选择音高按钮时改变音频的音高,现在我使用installTapOnBus来保存文件,但是这个方法在按下音高按钮后被调用,因此只保存了部分音频,我想要无论何时选择音高按钮,都可以保存整个音频,有什么办法

此方法用于播放音频

 -(void)playAudio { NSError *err = nil; audioEngine = [[AVAudioEngine alloc] init]; AudioFileplayer = [[AVAudioPlayerNode alloc] init]; pitch = [[AVAudioUnitTimePitch alloc] init]; reverb = [[AVAudioUnitReverb alloc] init]; [audioEngine stop]; [AudioFileplayer stop]; [audioEngine reset]; file = [[AVAudioFile alloc] initForReading:[NSURL URLWithString:[self filePath]] error:&err]; [audioEngine attachNode:AudioFileplayer]; [audioEngine attachNode:pitch]; [audioEngine attachNode:reverb]; [audioEngine connect:AudioFileplayer to:reverb format:nil]; [audioEngine connect:reverb to:pitch format:nil]; [audioEngine connect:pitch to:audioEngine.outputNode format:nil]; [reverb loadFactoryPreset:AVAudioUnitReverbPresetLargeRoom2]; [AudioFileplayer scheduleFile:file atTime:nil completionHandler:^{ AudioFileplayer = nil; }]; [audioEngine prepare]; [audioEngine startAndReturnError:&err]; if (err != nil) { NSLog(@"An error occured"); } [AudioFileplayer play]; } 

并且这种方法可以节省音调影响音频

 -(void)saveEffectedAudioToFolder { NSError *error; if (audioEngine) { pitchEffect.pitch = 1000; AVAudioFormat * commonFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:44100 channels:2 interleaved:NO]; // AVAudioFile *outputFile = [[AVAudioFile alloc] initForWriting:[NSURL URLWithString:[self filePath1]] settings:commonFormat.settings error:&error]; // if (error) { NSLog(@"Error is 1 %@",[error localizedDescription]); } [pitchEffect installTapOnBus:0 bufferSize:4096 format:commonFormat block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) { if (outputFile.length < file.length) {//Let us know when to stop saving the file, otherwise saving infinitely NSError *error; NSAssert([outputFile writeFromBuffer:buffer error:&error], @"error writing buffer data to file, %@", [error localizedDescription]); }else{ audioEngine = nil; [pitchEffect removeTapOnBus:0];//if we dont remove it, will keep on tapping infinitely } } ]; } } 

任何解决方法都会很有帮助

实际上我们在输出音频文件的设置中犯了错误。 输出音频文件处理格式应与输入文件(放置效果或音高)相同。

输出文件格式应为wav或caf格式。 此格式仅保存到输出音频文件。

  - (IBAction)save_it_after_changes:(id)sender { engine = [[AVAudioEngine alloc] init]; audio_player_node= [[AVAudioPlayerNode alloc] init]; [engine attachNode:audio_player_node]; [self setupEQ]; AVAudioMixerNode *mixerNode = [engine mainMixerNode]; [engine connect:audio_player_node to:unitEq format:audioFile.processingFormat]; [engine connect:unitEq to:mixerNode format:audioFile.processingFormat]; NSError *error12; [engine startAndReturnError:&error12]; if (!error12) { NSLog(@"Engine = %@",engine); [audio_player_node scheduleFile:audioFile atTime:nil completionHandler:nil]; NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]; [recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; NSError *error; outputFile = [[AVAudioFile alloc] initForWriting:[self testFilePathURL] settings:recordSetting error:&error]; NSLog(@"outputfile = %@",outputFile); if (error) { NSLog(@"outputFile error = %@",error); } else { //(AVAudioFrameCount)audioFile.length [audio_player_node installTapOnBus:0 bufferSize:8192 format:audioFile.processingFormat block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) { NSLog(@"Buffer Size = %@",buffer); NSLog(@"when = %lld",when.sampleTime); NSLog(@"outputfile length = %lli",outputFile.length); NSLog(@"input file length = %lld",audioFile.length); if (outputFile.length 0) ? [paths objectAtIndex:0] : nil; return basePath; } //------------------------------------------------------------------------------ - (NSURL *)testFilePathURL { return [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/test.caf", [self applicationDocumentsDirectory]]]; } 

请在成功保存后播放该文件。 这个对我有用。 一探究竟。

请参考下面的链接,我从这里得到更多,我可以使用AVAudioEngine从文件读取,处理音频单元并写入文件,比实时更快?

参考这个示例项目。 这是我们正在寻找的https://github.com/VladimirKravchenko/AVAudioEngineOfflineRender