swift 屏幕录制

步骤 1:导入 ReplayKit

swift 复制代码
import ReplayKit

步骤 2:开始录屏

swift 复制代码
let screenRecorder = RPScreenRecorder.shared()
// 麦克风或系统音频
screenRecorder.isMicrophoneEnabled = true

func startRecording() {
    guard screenRecorder.isAvailable else {
        print("Screen recording is not available on this device.")
        return
    }

    screenRecorder.startRecording { error in
        if let error = error {
            print("Error starting recording: \(error.localizedDescription)")
        } else {
            print("Recording started successfully.")
        }
    }
}

步骤 3:停止录屏

swift 复制代码
func stopRecording() {
    screenRecorder.stopRecording { previewController, error in
        if let error = error {
            print("Error stopping recording: \(error.localizedDescription)")
        } else {
            print("Recording stopped successfully.")
        }

        // 可选择展示预览界面
        if let previewController = previewController {
            previewController.previewControllerDelegate = self
            self.present(previewController, animated: true)
        }
    }
}
extension YourViewController: RPPreviewViewControllerDelegate {
    func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
        previewController.dismiss(animated: true, completion: nil)
    }
}

4. 不预览直接保存录制内容

swift 复制代码
RPScreenRecorder.shared().startCapture { sampleBuffer, bufferType, error in
    if let error = error {
        print("Error capturing: \(error.localizedDescription)")
        return
    }

    // 处理视频和音频数据
    if bufferType == .video {
        print("Captured video frame.")
    } else if bufferType == .audioApp || bufferType == .audioMic {
        print("Captured audio frame.")
    }
} completionHandler: { error in
    if let error = error {
        print("Error starting capture: \(error.localizedDescription)")
    } else {
        print("Capture started successfully.")
    }
}
相关推荐
FrameNotWork1 小时前
HarmonyOS 6.1 云应用客户端适配实战(二):Native Window 视频渲染
华为·音视频·harmonyos
2023自学中2 小时前
imx6ull 开发板 推流ov5640数据,虚拟机用 ffplay 拉流播放
linux·音视频·嵌入式·开发板
天天进步20152 小时前
Python全栈项目--基于深度学习的视频目标跟踪系统
python·深度学习·音视频
EasyDSS4 小时前
私有化音视频系统/视频高清直播点播EasyDSS重塑企业视频门户新生态
音视频
byte轻骑兵5 小时前
【LE Audio】CAP精讲[13]: Central侧LE连接建立全流程解析
人工智能·音视频·cap·le audio·低功耗音频
EasyDSS5 小时前
视频直播点播/音视频点播/云点播/云直播EasyDSS一体化音视频平台赋能企业数字化转型
音视频
俊哥工具8 小时前
电脑怎么玩手机APP?超简单新手教程
pdf·电脑·word·excel·音视频
FrameNotWork8 小时前
HarmonyOS 6.1 云应用客户端适配实战(五):日志调试与问题排查
华为·音视频·harmonyos
人月神话-Lee8 小时前
【图像处理】图像直方图——从“频率分布“到“智能决策“
图像处理·人工智能·ios·ai编程·swift
划水的code搬运工小李11 小时前
下载CSDN到PDF
开发语言·pdf·swift