在后台运行时,AVAssetWriter startWriting失败

时间:2014-12-15 18:06:20

标签: ios avassetwriter

我正在尝试将图像保存到视频文件中,而应用程序位于后台中

我尝试创建AVAssetWriter并开始写入,但此操作失败(返回NO) 以下是我执行的一些检查:

  • 该文件不存在
  • 我在运行之前调用了[[UIApplication sharedApplication] beginBackgroundTaskWithName: expirationHandler:] 代码
  • 在调用startWriting之前,assetWriter的状态为AVAssetWriterStatusUnknown

我收到的错误(在videoWriter.error下面的代码中)是:
错误域= AVFoundationErrorDomain代码= -11800“操作无法完成”UserInfo = 0x17e1e710 {NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x17ec85d0“操作无法完成。(OSStatus error -16980。)”, NSLocalizedFailureReason =发生未知错误(-16980)}

并具体针对潜在的错误: 错误域= NSOSStatusErrorDomain代码= -16980“操作无法完成。(OSStatus错误-16980。)

我找不到对此行为或错误代码的任何解释

请指教!

- (BOOL)_createAssetWriterWithFrameSize:(CGSize)frameSize filePath:(NSString *)filePath{
       NSURL *filePathURL = [NSURL fileURLWithPath:filePath];

        NSError *error = nil;

        AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:filePathURL
                                                               fileType:AVFileTypeQuickTimeMovie
                                                                  error:&error];
        if(error!=nil){
            LogDebug(@"Error creating %@", error);
        }

        if (videoWriter) {

            NSDictionary *videoSettings = @{AVVideoCodecKey: AVVideoCodecH264,
                                            AVVideoWidthKey: @(frameSize.width),
                                            AVVideoHeightKey: @(frameSize.height),//};

                                            };

            AVAssetWriterInput *writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                                                                 outputSettings:videoSettings];

            NSDictionary *attributes = @{(NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32ARGB),
                                         (NSString *)kCVPixelBufferWidthKey: @(frameSize.width),
                                         (NSString *)kCVPixelBufferHeightKey: @(frameSize.height)};


            AVAssetWriterInputPixelBufferAdaptor *adaptor = nil;
            if(attributes!=nil){
                adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                                                                                    sourcePixelBufferAttributes:attributes];
            }
            if ([videoWriter canAddInput:writerInput]) {
                [videoWriter addInput:writerInput];

                writerInput.expectsMediaDataInRealTime = YES;


                BOOL start = [videoWriter startWriting];

                if (start) {
                    [videoWriter startSessionAtSourceTime:kCMTimeZero];

                }
                else{

                    LogInfo(@"Could not start writing to video file due to %@. file is %@", videoWriter.error, filePath);
                    id o = [videoWriter.error.userInfo objectForKey:NSUnderlyingErrorKey];
                    if(o!=nil){
                        LogDebug(@"Failed writing, underlying error is %@", o);
                    }

                }
            }
        }
}

0 个答案:

没有答案