ReplayKit保存视频首先尝试使用麦克风

时间:2017-11-01 21:26:11

标签: ios iphone avassetwriter replaykit rpscreenrecorder

情景1:

  1. 使用音频/视频编写器输入启动AVAssetWriter。
  2. 使用RPScreenRecorder开始无麦克风录制并处理样本缓冲区。
  3. 首次尝试将文件写入照片。
  4. 情景2:

    1. 使用音频/视频编写器输入启动AVAssetWriter。
    2. 使用RPScreenRecorder在启用麦克风的情况下开始录制并处理示例缓冲区。
    3. 文件写入无法在第一次尝试时写出。

      UserInfo = {NSLocalizedRecoverySuggestion =再次尝试保存。,NSLocalizedDescription =无法保存,NSUnderlyingError = 0x1c464f3c0 {错误域= NSOSStatusErrorDomain代码= -12412“(null)”}} 2017-10-26 23:25:16.896673-0400 [2135:771655]状态失败!:3错误域= AVFoundationErrorDomain代码= -11823“无法保存”

    4. 第二次尝试正常。

      我做错了什么?

2 个答案:

答案 0 :(得分:0)

添加了重试逻辑来规避问题。不是最好的解决方案,但它有效。

[self.screenRecorder startCaptureWithHandler:^(CMSampleBufferRef  _Nonnull sampleBuffer, RPSampleBufferType bufferType, NSError * _Nullable error) {
        if(CMSampleBufferDataIsReady(sampleBuffer) == false || self.assetWriter == nil)
        {
            return;
        }

        if (self.assetWriter.status == AVAssetWriterStatusFailed) {
            NSLog(@"AVWriter Failed!");
            return;
        }

        if (CMSampleBufferDataIsReady(sampleBuffer)) {
            if(self.assetWriter.status == AVAssetWriterStatusWriting) {
                if (bufferType == RPSampleBufferTypeVideo) {
                    if (!self.startedSession) {

                        dispatch_async(dispatch_get_main_queue(), ^{
                            _startDate = [NSDate date];
                            _recordingTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateRecordingTime) userInfo:nil repeats:YES];

                            // Disable the idle timer while recording
                            [UIApplication sharedApplication].idleTimerDisabled = YES;
                        });

                        CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
                        [self.assetWriter startSessionAtSourceTime:pts];
                        self.startedSession = YES;
                        NSLog(@"MP4Writer: started session in appendVideoSample");
                    }

                    if (CMTimeCompare(kCMTimeInvalid, self.firstVideoFrameTime) == 0) {
                        self.firstVideoFrameTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
                    }

                    if (self.assetWriterVideoInput.readyForMoreMediaData) {
                        @try {
                            [self.assetWriterVideoInput appendSampleBuffer:sampleBuffer];
                        }
                        @catch(NSException *expection) {
                            NSLog(@"Missed Video Buffer: %@", self.assetWriter.error);
                        }
                    }
                }                

                if (bufferType == RPSampleBufferTypeAudioMic) {
                    if (CMTimeCompare(kCMTimeInvalid, self.firstVideoFrameTime) == 0 ||
                        CMTimeCompare(self.firstVideoFrameTime, CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) == 1) {
                        return;
                    }

                    if (!self.startedSession) {
                        CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
                        [self.assetWriter startSessionAtSourceTime:pts];
                        self.startedSession = YES;
                        NSLog(@"MP4Writer: started session in appendAudioSample");
                    }

                    if (self.assetWriterAudioInput.isReadyForMoreMediaData) {
                        @try {
                            [self.assetWriterAudioInput appendSampleBuffer:sampleBuffer];
                        }
                        @catch(NSException *expection) {
                            NSLog(@"Missed Audio Buffer: %@", self.assetWriter.error);
                        }
                    }
                }
            }

        }
    } completionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Recording started successfully.");
        }
    }];

答案 1 :(得分:0)

output
15