I'm using NSFileHandle
to get the data of a video as its being recorded.
It works fine notification-wise and I'm getting notified using NSFileHandleDataAvailableNotification
. The problem is the video file eventually doesn't work.
Everytime when comparing the original file with the file created using NSFileHandle data, there are always just several bytes being wrong , meaning NSFileHandle reads them incorrectly.
This is how I append the data
-(void) gotData: (NSNotification *) not{
NSFileHandle *handle = not.object;
NSData *data = [handle availableData];
if(data.length){
NSLog(@"got %d", data.length);
[test appendData: data];
[handle waitForDataInBackgroundAndNotify];
}else{
NSLog(@"Ended");
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleDataAvailableNotification object: handle];
}
}
This is how I'm writing the data eventually :
[test writeToFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"x.mp4"] atomically:YES];
And when doing a diff between the original file and the one from NSFileHandle, here are the bytes wrong (even though both are the exact same size):
I'm really clueless about this stranger behaviour, and if you ever got stuck with a similar issue I'd love your help on this.