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.")
    }
}
相关推荐
集成显卡3 小时前
前端视频播放方案选型:主流 Web 播放器对比 + Vue3 实战
前端·vue·音视频
爱吃番茄鼠骗3 小时前
回顾ESP32S3系列---音频开发
音视频
ViiTor_AI3 小时前
AI 在线字幕去除工具:一键无损删除视频硬字幕与软字幕
人工智能·音视频
愚公搬代码3 小时前
【愚公系列】《AI短视频创作一本通》027-AI 短视频创作的注意事项及未来展望(AI短视频的技术展望)
人工智能·音视频
binderIPC4 小时前
macos环境下FFmpeg打包成.so文件
macos·ffmpeg·音视频
binderIPC4 小时前
macos的FFmpeg环境搭建
macos·ffmpeg·音视频
ViiTor_AI4 小时前
视频字幕怎么去除?5 种方法删除硬编码字幕与软字幕(CapCut 实操)
人工智能·计算机视觉·音视频
西***63476 小时前
深耕政务数字化,矩阵会议系统多点落地,解锁政务会商新范式
矩阵·音视频·会议系统
奔跑吧 android7 小时前
【车载audio】【audio hal 01】【Android 音频子系统:Audio HAL Server 启动全流程深度解析】
android·音视频·audio·audioflinger·aosp15·车载音频·audiohal
Sheffi6620 小时前
Swift 所有权宏 `~Copyable` 深度解析:如何在 Swift 中实现类似 Rust 的内存安全模型?
rust·ssh·swift