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.")
    }
}
相关推荐
Antonio9155 小时前
【音视频】WebRTC-Web 音视频采集与播放
前端·音视频·webrtc
不会写代码的丝丽10 小时前
iOS 队列与线程
swift
那年一路北10 小时前
Deforum Stable Diffusion,轻松实现AI视频生成自由!
人工智能·stable diffusion·音视频
移远通信11 小时前
不止 “听懂”,更能 “感知”!移远通信全新AI 音频模组 重新定义智能家居“听觉”逻辑
人工智能·音视频·智能家居
DogDaoDao11 小时前
WebRTC 2025全解析:从技术原理到商业落地
音视频·webrtc·实时音视频·p2p·流媒体·编解码
天天进步20151 天前
Python全栈项目--基于深度学习的视频内容分析系统
python·深度学习·音视频
bubiyoushang8881 天前
CentOS安装ffmpeg并转码视频为mp4
ffmpeg·centos·音视频
亿坊电商1 天前
AI 数字人在处理音频时,如何确保声音的自然度?
人工智能·算法·音视频
Swift社区1 天前
从字符串中“薅出”最长子串:LeetCode 340 Swift 解法全解析
算法·leetcode·swift
qq_429879671 天前
qt webengine播放视频
开发语言·qt·音视频