Документы Apple, кажется, указывают, что во время записи видео в файл приложение может без проблем изменить URL-адрес на лету. Но я вижу проблему. Когда я пытаюсь это сделать, делегат записи вызывается с ошибкой...
Операция не может быть завершена. (Ошибка OSStatus -12780.) Информация словарь: { AVErrorRecordingSuccessfulFinishedKey = 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, файл-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];
}
}
Когда это выполняется, файл-0 записывается, затем мы видим ошибку -12780 сразу после изменения URL-адреса файла. -5, пишется файл-10, потом ошибка, потом ладно и так далее.
Похоже, что изменение URL-адреса на лету не работает, но оно останавливает запись, что позволяет работать следующему изменению URL-адреса.