Apple 文档似乎表明,在将视频录制到文件时,应用程序可以动态更改 URL,没有问题。但我看到了一个问题。当我尝试这个时,录制委托被调用并出现错误......
手术无法完成。(OSStatus 错误 -12780。)信息字典为:{ AVErrorRecordingSuccessfullyFinishedKey = 0; }
(“无法”中的时髦单引号来自日志记录 [错误本地化描述])
这是代码,基本上是对 WWDC10 AVCam 示例的调整:
1) 开始录制。启动计时器以每隔几秒更改一次输出 URL
- (void) startRecording
{
// start the chunk timer
self.chunkTimer = [NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:@selector(chunkTimerFired:)
userInfo:nil
repeats:YES];
AVCaptureConnection *videoConnection = [AVCamCaptureManager connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]];
if ([videoConnection isVideoOrientationSupported]) {
[videoConnection setVideoOrientation:[self orientation]];
}
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}]];
}
NSURL *fileUrl = [[ChunkManager sharedInstance] nextURL];
NSLog(@"now recording to %@", [fileUrl absoluteString]);
[[self movieFileOutput] startRecordingToOutputFileURL:fileUrl recordingDelegate:self];
}
2)当定时器触发时,在不停止录制的情况下更改输出文件名
- (void)chunkTimerFired:(NSTimer *)aTimer {
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}]];
}
NSURL *nextUrl = [self nextURL];
NSLog(@"changing capture output to %@", [[nextUrl absoluteString] lastPathComponent]);
[[self movieFileOutput] startRecordingToOutputFileURL:nextUrl recordingDelegate:self];
}
注意:[self nextURL] 生成文件 url,如 file-0.mov、file-5.mov、file-10.mov 等。
3) 每次文件更改时都会调用它,并且每次调用都是错误...
- (void) captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections
error:(NSError *)error
{
id delegate = [self delegate];
if (error && [delegate respondsToSelector:@selector(someOtherError:)]) {
NSLog(@"got an error, tell delegate");
[delegate someOtherError:error];
}
if ([self backgroundRecordingID]) {
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[[UIApplication sharedApplication] endBackgroundTask:[self backgroundRecordingID]];
}
[self setBackgroundRecordingID:0];
}
if ([delegate respondsToSelector:@selector(recordingFinished)]) {
[delegate recordingFinished];
}
}
当它运行时,file-0 被写入,然后我们在将 url 更改为 file-5 后立即看到错误 -12780,file-10 被写入,然后是一个错误,然后是好的,等等。
动态更改 URL 似乎不起作用,但它会停止写入,从而允许下一个 URL 更改起作用。