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.")
    }
}
相关推荐
飞Link6 小时前
【网络与 AI 工程的交叉】多模态模型的数据传输特点:视频、音频、文本混合通道
网络·人工智能·音视频
音视频牛哥7 小时前
从“能播”到“能控”:深入解读 SmartMediakit 与 OTT 播放器的架构裂变
音视频·ott·低延迟rtsp播放器·smartmediakit·低延迟rtmp播放器·低延迟音视频技术方案·具身智能低延迟rtsp方案
1024小神8 小时前
Swift中跨view视图组件实现全局状态共享的方式汇总
ios·swiftui·swift
Wcowin9 小时前
【自荐】OneClip—— 一款简单专业的 macOS 剪贴板管理工具
mac·swift·粘贴板
Likeadust14 小时前
视频直播点播平台EasyDSS如何重塑媒体行业的内容分发与交互体验
音视频·媒体
赫尔·普莱蒂科萨·帕塔18 小时前
【翻译】从生成的人体视频到物理可行的机器人轨迹
机器人·音视频
EasyCVR18 小时前
智慧油田视频融合平台EasyCVR重塑油田油井智能监管新体系
音视频
小尧嵌入式18 小时前
音视频入门基础知识
开发语言·c++·qt·算法·音视频
别动哪条鱼19 小时前
FFmpeg AVFormatContext 分配函数详解
数据结构·ffmpeg·音视频
Sheffi6621 小时前
Swift 与 OC 混编底层交互原理
ios·objective-c·swift