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.")
    }
}
相关推荐
Flamingˢ4 小时前
ZYNQ + OV5640 + HDMI 视频系统调试记录:一次 RGB888 与 RGB565 引发的黑屏问题
arm开发·嵌入式硬件·fpga开发·vim·音视频
ˇasushiro6 小时前
终端工具配置
开发语言·ios·swift
Flamingˢ6 小时前
YNQ + OV5640 视频系统开发(二):OV5640_Data IP 核源码解析
arm开发·嵌入式硬件·网络协议·tcp/ip·fpga开发·vim·音视频
Flamingˢ6 小时前
ZYNQ + OV5640 视频系统开发(三):AXI VDMA 帧缓存原理
arm开发·嵌入式硬件·fpga开发·vim·音视频
Hello World . .7 小时前
Linux:Linux命令行音视频播放器
linux·音视频
飞翔的SA8 小时前
MLX‑VLM :Mac本地跑通多模态大模型的开源项目!让图片、音频、视频理解一键上手
人工智能·python·macos·音视频
愚公搬代码20 小时前
【愚公系列】《剪映+DeepSeek+即梦:短视频制作》046-转场:短视频一气呵成的秘密(什么是转场)
人工智能·音视频
PiaoShiSun1 天前
小米手机浏览器缓存视频如何导出
缓存·智能手机·音视频
艾为电子1 天前
【新品发布】攻克音频切换痛点|艾为 AW35331FDR 高保真音频开关全新登场
音视频
Swift社区1 天前
LeetCode 401 二进制手表 - Swift 题解
算法·leetcode·swift