Appleのドキュメントによると、ビデオをファイルに録画している間、アプリは問題なくその場でURLを変更できるとのことです。しかし、私は問題を見ています。これを試してみると、レコーディングデリゲートがエラーで呼び出されます...
操作を完了できませんでした。(OSStatusエラー-12780。)情報ディクショナリは次のとおりです。{AVErrorRecordingSuccessfullyFinishedKey = 0; }
(「できませんでした」のファンキーな一重引用符は、ロギング[errorlocalizedDescription]から来ています)
これが基本的にWWDC10AVCamサンプルの微調整であるコードです。
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]は、file-0.mov、file-5.mov、file-10.movなどのファイルURLを生成します。
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が書き込まれ、次にエラーが発生し、次にOKというように続きます。
その場でURLを変更しても機能しないようですが、書き込みが停止し、次のURL変更が機能するようになります。