Pergunta

My application uses the AVFoundation framework to record audio. The first time the user records some audio it works like a charm. However, when the user records additional audio, I would like to append this audio to the end of the existing file. I can't figure out how to do this. I've tried simply appending the NSData itself to the existing NSData, but this doesn't work:

NSError *error;
//This is the newly recorded second bit of audio saved to a temp file.
NSData *data = [NSData dataWithContentsOfFile:tempInputFilePath options:0  error:&error];

//Here I get out the old audio data that I have saved.
NSMutableData *mData = [fileBrowserVC.currentlySelectedFile.audioContent mutableCopy];
 NSLog(@"%lu",(unsigned long)mData.length);
if (mData==nil) {
    mData = [[NSMutableData alloc] init];
}


[mData appendData:data];
 NSLog(@"%lu",(unsigned long)mData.length);

fileBrowserVC.currentlySelectedFile.audioContent = mData;
[[AppDelegate sharedDelegate] saveAction:nil]; 

When I try to play back the newly created audio content, only the first piece of audio is played. Is this something that's not going to work, or is there something wrong with this code?

Is there another way to append audio to an existing file?

Foi útil?

Solução

I was able to achieve this using AVMutableComposition as shown in this Apple article.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top