RPPreviewViewController

RPPreviewViewController

本文介绍了ReplayKit 无需转到 RPPreviewViewController 即可获取视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ReplayKit 但我的问题是我需要获取视频(或临时视频),而不必通过 RPPreviewViewController 我正在使用以下内容,但必须使用 RPPreviewViewController,还有其他方法吗?

i am using ReplayKit but my problem is that i need to acquire the video (or the temp video), without having to go through RPPreviewViewController i am using the following, but RPPreviewViewController has to be used, is there any other way?

- (void)stopScreenRecording {
    RPScreenRecorder *sharedRecorder = RPScreenRecorder.sharedRecorder;
    [sharedRecorder stopRecordingWithHandler:^(RPPreviewViewController *previewViewController, NSError *error) {
        if (error) {
            NSLog(@"stopScreenRecording: %@", error.localizedDescription);
        }

        if (previewViewController) {
            previewViewController.previewControllerDelegate = self;
            self.previewViewController = previewViewController;

            // RPPreviewViewController only supports full screen modal presentation.
            self.previewViewController.modalPresentationStyle = UIModalPresentationFullScreen;

            [self presentViewController:previewViewController animated:YES completion:nil];
        }
    }];

推荐答案

我起诉了以下内容,但我不得不裁剪视频,因为它记录了整个屏幕我希望这有帮助:

i sued the following but i had to crop the video because it records the whole screen i hope this is helpful:

- (void)startScreenRecording {
    self.screenRecorder = [RPScreenRecorder sharedRecorder];
    if (self.screenRecorder.isRecording) {
        return;
    }
    NSError *error = nil;
    NSArray *pathDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *outputURL = pathDocuments[0];

    NSString *videoOutPath = [[outputURL stringByAppendingPathComponent:[NSString stringWithFormat:@"%u", arc4random() % 1000]] stringByAppendingPathExtension:@"mp4"];
    self.assetWriter = [AVAssetWriter assetWriterWithURL:[NSURL fileURLWithPath:videoOutPath] fileType:AVFileTypeMPEG4 error:&error];

    NSDictionary *compressionProperties = @{AVVideoProfileLevelKey         : AVVideoProfileLevelH264HighAutoLevel,
                                            AVVideoH264EntropyModeKey      : AVVideoH264EntropyModeCABAC,
                                            AVVideoAverageBitRateKey       : @(1920 * 1080 * 11.4),
                                            AVVideoMaxKeyFrameIntervalKey  : @60,
                                            AVVideoAllowFrameReorderingKey : @NO};
    NSNumber* width= [NSNumber numberWithFloat:self.view.frame.size.width];
    NSNumber* height = [NSNumber numberWithFloat:self.view.frame.size.height];

    if (@available(iOS 11.0, *)) {
        NSDictionary *videoSettings = @{AVVideoCompressionPropertiesKey : compressionProperties,
                                        AVVideoCodecKey                 : AVVideoCodecTypeH264,
                                        AVVideoWidthKey                 : width,
                                        AVVideoHeightKey                : height};

        self.assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
    } else {
        // Fallback on earlier versions
    }

    [self.assetWriter addInput:self.assetWriterInput];
    [self.assetWriterInput setMediaTimeScale:60];
    [self.assetWriter setMovieTimeScale:60];
    [self.assetWriterInput setExpectsMediaDataInRealTime:YES];

    if (@available(iOS 11.0, *)) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (granted)
                {
                    [self.screenRecorder setMicrophoneEnabled:YES];

                    [self.screenRecorder startCaptureWithHandler:^(CMSampleBufferRef  _Nonnull sampleBuffer, RPSampleBufferType bufferType, NSError * _Nullable error) {
                        if (CMSampleBufferDataIsReady(sampleBuffer)) {
                            if (self.assetWriter.status == AVAssetWriterStatusUnknown && bufferType == RPSampleBufferTypeVideo) {
                                [self.assetWriter startWriting];
                                [self.assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
                            }

                            if (self.assetWriter.status == AVAssetWriterStatusFailed) {
                                NSLog(@"An error occured.");
                                //show alert
                                [[RPScreenRecorder sharedRecorder] stopCaptureWithHandler:^(NSError * _Nullable error) {}];
                                return;
                            }
                            if (bufferType == RPSampleBufferTypeVideo) {
                                if (self.assetWriterInput.isReadyForMoreMediaData) {
                                    [self.assetWriterInput appendSampleBuffer:sampleBuffer];
                                }else{
                                    NSLog(@"Not ready for video");
                                }
                            }
                        }
                    } completionHandler:^(NSError * _Nullable error) {
                        if (!error) {
                            AVAudioSession *session = [AVAudioSession sharedInstance];
                            [session setActive:YES error:nil];
                            // Start recording
                            NSLog(@"Recording started successfully.");
                        }else{
                            //show alert
                        }
                    }];
                }
            });
        }];


    } else {
        // Fallback on earlier versions
    }
}

这篇关于ReplayKit 无需转到 RPPreviewViewController 即可获取视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 03:30