Just wondering if anyone has implemented the code shown above. Below is the code I've got working so far. I'm guessing I got the HEADER_LENGTH right (anything less creates a file Quicktime can't play). My problem with the code below is that the file at combinedPath seems to only have the data for the first file. Is there a footer in audio files that has to be removed from the first file (similar to how we ignore the header of the second)? When debugging it runs through the second while loop several times so I think it's writing to the combined path but for some reason QuickTime thinks the file ends after the duration of the first audio.
[EDIT: After digging around the interwebs some more I think the problem is that the header info for the first file includes a duration property which is telling QuickTime the length of File 1. If anyone has any advice on how to override the header with the length of both files combined I think that would solve my problem]
Code:
#define CHUNK_SIZE 1000
#define HEADER_LENGTH 251
-(void) concatFiles:(NSString*) path1 And: (NSString*) path2 SaveTo:(NSString*) combinedPath
{
NSFileManager *fm = [[NSFileManager alloc] init];
NSFileHandle *f1Handle = [NSFileHandle fileHandleForReadingAtPath: path1];
NSFileHandle *f2Handle = [NSFileHandle fileHandleForReadingAtPath: path2];
//might need to check if file exists first, if not create it using NSFileManager's createFileAtPath
if(![fm fileExistsAtPath:combinedPath]){
[fm createFileAtPath:combinedPath contents:nil attributes:nil];
}
NSFileHandle *combinedHandle = [NSFileHandle fileHandleForWritingAtPath: combinedPath];
NSMutableData *read = [[NSMutableData alloc] initWithLength:CHUNK_SIZE];
// read data in small chunks to avoid hogging memory.
[read setData:[f1Handle readDataOfLength: CHUNK_SIZE]];
while([read length] != 0)
{
[combinedHandle writeData: read];
[read setData:[f1Handle readDataOfLength: CHUNK_SIZE]];
}
//now do the same for f2Handle
//might need to ignore a few bytes in the front of file2 that isnt audio
[f2Handle readDataOfLength: HEADER_LENGTH];
[read setData:[f2Handle readDataOfLength: CHUNK_SIZE]];
while([read length] != 0)
{
[combinedHandle writeData: read];
[read setData:[f2Handle readDataOfLength: CHUNK_SIZE]];
}
[f1Handle closeFile];
[f2Handle closeFile];
[combinedHandle closeFile];
}