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.")
    }
}
相关推荐
顾道长生'2 小时前
(Arxiv-2025)通过动态 token 剔除实现无需训练的高效视频生成
计算机视觉·音视频·视频生成
每次的天空10 小时前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
爱分享的飘哥12 小时前
【V6.0 - 听觉篇】当AI学会“听”:用声音特征捕捉视频的“情绪爽点”
人工智能·音视频
菜包eo13 小时前
二维码驱动的独立站视频集成方案
网络·python·音视频
阿蒙Amon15 小时前
【Python小工具】使用 OpenCV 获取视频时长的详细指南
python·opencv·音视频
aqi0017 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
旷世奇才李先生18 小时前
Swift 安装使用教程
开发语言·ios·swift
正在走向自律19 小时前
第二章-AIGC入门-开启AIGC音频探索之旅:从入门到实践(6/36)
人工智能·aigc·音视频·语音识别·ai音乐·ai 音频·智能语音助手
美狐美颜sdk1 天前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
melonbo1 天前
使用FFmpeg将H.264码流封装为MP4
ffmpeg·音视频·h.264