salve o arquivo de áudio em segundo plano
Eu tenho um aplicativo que altera a afinação do áudio quando o botão de afinação é selecionado, agora estou usandoinstallTapOnBus
para salvar o arquivo, mas esse método é invocado depois que eu pressiono o botão de afinação; portanto, apenas parte do áudio é salva. Quero salvar o áudio inteiro, independentemente do momento em que o botão de afinação é selecionado.
Este método é usado para reproduzir o áudio
-(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];
}
e este método para salvar o tom afetado pelo áudio
-(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
}
}
];
}
}
qualquer solução alternativa seria útil