ReplayKit:RPScreenRecorder.shared().startCapture() 不工作

J.T*_*ein 2 avfoundation video-recording swift avkit replaykit

ReplayKit最近真的让我很沮丧。因为某些原因

RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in
Run Code Online (Sandbox Code Playgroud)

当我调用它时实际上不起作用,因为我在其中有一个print()语句并且它从未被调用过。

我的代码ViewController是:

import UIKit
import AVFoundation
import SpriteKit
import ReplayKit
import AVKit

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, RPPreviewViewControllerDelegate {

    var assetWriter:AVAssetWriter!
    var videoInput:AVAssetWriterInput!

func startRecording(withFileName fileName: String) {
        if #available(iOS 11.0, *)
        {

            assetWriter = try! AVAssetWriter(outputURL: fileURL, fileType:
                AVFileType.mp4)
            let videoOutputSettings: Dictionary<String, Any> = [
                AVVideoCodecKey : AVVideoCodecType.h264,
                AVVideoWidthKey : UIScreen.main.bounds.size.width,
                AVVideoHeightKey : UIScreen.main.bounds.size.height
            ];

            videoInput  = AVAssetWriterInput (mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
            videoInput.expectsMediaDataInRealTime = true
            assetWriter.add(videoInput)
            print("HERE")
            RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in
                print("RECORDING")
            }
      }
}

func stopRecording(handler: @escaping (Error?) -> Void)
    {
        if #available(iOS 11.0, *)
        {
            RPScreenRecorder.shared().stopCapture
                {    (error) in
                    handler(error)
                    self.assetWriter.finishWriting
                        {
                            print("STOPPED")
                    }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

打印“HERE”,但不打印“RECORDING”

[ps 很抱歉代码格式不正确,我相信你会理解:)]


我也尝试了不同的方法:

let recorder = RPScreenRecorder.shared()
recorder.startRecording{ [unowned self] (error) in
    guard error == nil else {
        print("There was an error starting the recording.")
        return
    }
    print("Started Recording Successfully")
 }
Run Code Online (Sandbox Code Playgroud)

并停止录音...

    recorder.stopRecording { [unowned self] (preview, error) in
        print("Stopped recording")
        guard preview != nil else {
            print("Preview controller is not available.")
            return
        }
        onGoingScene = true
        preview?.previewControllerDelegate = self
        self.present(preview!, animated: true, completion: nil)
    }
Run Code Online (Sandbox Code Playgroud)

当我调用 recorder.stopRecording() 函数时,此方法不会停止,永远不会调用“停止录制”。


有人可以帮助我,因为这真的让我很沮丧,您如何正确使用ReplayKit在 iOS 11 中录制屏幕?我已经在互联网上搜索了所有方法,但没有一种方法适合我,我不知道为什么。PS 我的 Info.plist 中有必要的权限密钥。

谢谢

Meh*_*dız 5

一个巨大的提醒,ReplayKit 在模拟器中不起作用。我在完全相同的问题上浪费了几个小时,直到意识到 ReplayKit 永远不会触发 startCapture 处理程序,因为它永远不会在模拟器中记录。