iphone - 连续调用 startRecordingToOutputFileURL :

标签 iphone ios avfoundation avcam

The Apple docs似乎表明在将视频录制到文件时,该应用程序可以毫无问题地即时更改 URL。但我看到了一个问题。当我尝试这样做时,记录委托(delegate)被调用并出现错误...

The operation couldn’t be completed. (OSStatus error -12780.) Info dictionary is: { AVErrorRecordingSuccessfullyFinishedKey = 0; }

(“无法”中的时髦单引号来自日志记录 [error localizedDescription])

这是代码,基本上是对 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 更改起作用。

最佳答案

谢谢大家的评论和好的想法。这是来自 Apple DTS 的词...

I spoke with our AV Foundation engineers, and it is definitely a bug in that this method is not doing what the documentation says it should ("You do not need to call stopRecording before calling this method while another recording is in progress."). Please file a bug report using the Apple Bug Reporter (http://developer.apple.com/bugreporter/) so the team can investigate. Make sure and include your minimal project in the report.

我已将此作为错误 11632087 提交给 Apple

关于iphone - 连续调用 startRecordingToOutputFileURL :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10858793/

相关文章:

ios - 安装socket mobile scan api sdk后报"The application bundle does not contain a valid identifier"

iphone - CoreData迁移——删除关系规则

iphone - 如何为缩放设置 anchor (GMGridView)

ios - 想要将一些代码应用到同一个 Storyboard 上的第二个场景(Xcode、Swift 2)

iphone - UIScrollView 只随文本滚动

ios - 在iOS 7中阻止传入的短信

iOS 在模拟器上播放音频成功,但在设备上播放失败

swift - 在 Swift 中使用按钮切换相机

ios - 为什么 AVPlayerItem 的 canPlayFastForward 方法返回 False?

iphone - iOS 7 UINavigationBar 与 iOS 6 风格