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.")
    }
}
相关推荐
君为先-bey19 分钟前
CogVideoX——Transformer从文本到视频的扩散模型
深度学习·音视频·transformer·扩散模型
Raink老师23 分钟前
【AI面试临阵磨枪-77】音视频 + AI:实时字幕、翻译、降噪、虚拟人、多模态对话
人工智能·面试·音视频
FrameNotWork1 小时前
HarmonyOS 短视频滑动交互实现:打造流畅的上下切换体验
音视频·交互·harmonyos
人月神话-Lee2 小时前
【图像处理】图像导出与工业级压缩策略——从像素到文件的最后一公里
图像处理·人工智能·ios·ai编程·swift
做萤石二次开发的哈哈4 小时前
ERTC-产品介绍-应用场景
音视频·实时音视频
iOS日常4 小时前
iOS 横竖屏实践(UIKit)
swift
Hommy888 小时前
【剪映小助手】视频处理接口
开源·github·音视频·视频剪辑自动化·剪映api
ai产品老杨8 小时前
解耦异构算力与多协议接入:基于 Docker 与 GB28181 的企业级 AI 视频管理平台架构演进与源码交付实践
人工智能·docker·音视频
看谷秀9 小时前
wift Part 5 oc -> swift
swift
Gary Studio10 小时前
芯片界的“缝合怪”:拆解既管供电又管音频的 Rockchip RK809
音视频