I have a camera app where I am trying to limit the capture length to exactly 15 seconds.
I have tried two different approaches, and neither of them are working to my satisfaction.
The first approach is to fire a repeating timer every second:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countTime:) userInfo:[NSDate date] repeats:YES];
- (void)countTime:(NSTimer*)sender {
NSDate *start = sender.userInfo;
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:start];
NSInteger time = round(duration);
if (time > 15) {
[self capture:nil]; // this stops capture
}
}
this gives me a 15 second video 8/10 times, with a periodic 16 second one... and I have tried a mixture of the NSTimeInterval double and the rounded integer here, with no apparent difference...
The second approach is to fire a selector once after the desired duration, like so:
self.timer = [NSTimer scheduledTimerWithTimeInterval:15.0f target:self selector:@selector(capture:) userInfo:nil repeats:NO];
this just calls the capture method - which stops camera capture - directly, and gives me the same results...
Is there something that I am overlooking here?
Now, because I have tested with a number of tweaked floating point values as the cap (14.5, 15.0, 15.1, 15.5, 16.0 etc) and I almost always see a 16 second video after a few tries, I am starting to wonder whether it's just the AVFoundation taking a second to flush the buffer... ???
Thanks to Paul and Linuxious for their comments and answers... and Rory for thinking outside the box (intriguing option).
And yes, in the end it is clear that NSTimer isn't sufficient by itself for this.
In the end, I listen for the captureOutput delegate method to fire, test for the length of the asset, and trim the composition appropriately.