сохранить аудио файл в фоновом режиме
У меня есть приложение, которое изменяет высоту звука, когда выбрана кнопка, теперь я использую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
}
}
];
}
}
любой обходной путь будет полезным