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

标签 swift avfoundation video-recording avkit replaykit

ReplayKit 最近真的让我很沮丧。出于某种原因

RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in

当我调用它时它实际上不起作用,因为我在其中有一个 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")
                    }
            }
        }
    }

打印的是“HERE”,而不是“RECORDING”

[附注抱歉,代码格式不正确,我相信您会理解的:)]


我也试过另一种方法:

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")
 }

并停止录制...

    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)
    }

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


有人可以帮我吗,因为这真的让我很沮丧,你怎么能正确地使用 ReplayKit 在 iOS 11 中录制你的屏幕?我在互联网上搜索过,但没有一种方法适合我,我不知道为什么。附言我的 Info.plist 中有必要的权限 key 。

谢谢

最佳答案

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

关于swift - ReplayKit:RPScreenRecorder.shared().startCapture() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51796101/

相关文章:

ios - 如何使用 Swift 将键中的值分配给常量名称?

ios - 为什么我的 View Controller 没有显示在 ScrollView Controller 上?

ios - 使用避雷线从 OSX cocoa 应用程序录制 iOS 设备的屏幕

c++ - QT 的 QMediaRecorder 不工作 - 无法从网络摄像头录制

encoding - 如何在录制和输入实时视频流之间同步?

swift - 如何在 Master 和 Detail View 之间 segue *without* storyboard?

swift - AVFoundation:缩放视频改变颜色

ios - 声音在模拟器上播放但在 iPhone 上不播放

android - 如何在设备锁定时继续录制视频?

swift - 如何在 Swift 中压缩展开多个可选值?