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.")
    }
}
相关推荐
沐飞工具箱4 小时前
长视频自动切片的技术逻辑:如何结合语音、画面与时间轴识别真正的内容高光?
ide·macos·音视频·xcode·用户运营·内容运营
揽秀亭长6 小时前
音频如何转成可编辑乐谱?扒谱工具完整使用流程与格式链路解析(2026 实操版)
音视频
I Am a robert girl7 小时前
Semantic Forcing:当视频生成学会“想清楚再动笔”
音视频·视频生成·ai视频·时序一致性·语义建模·事件语义
河南博为智能科技有限公司7 小时前
配电站房动环系统视频接入与AI巡检!
人工智能·音视频
开开心心就好8 小时前
游戏安装报错缺环境?运行库整合包补上
java·mysql·游戏·pdf·ocr·音视频·lua
ARM+FPGA+AI工业主板定制专家9 小时前
基于 Jetson 整机搭建 ROS2 开发环境:机器人软件环境配置指南
人工智能·机器人·音视频·机器人运控
开开心心就好11 小时前
重复图片查找工具,拖入文件夹就开搜
java·mysql·pdf·ocr·vim·音视频·emacs
FungLeo12 小时前
Swift macOS 游戏开发实战|空当接龙 17|Swift 游戏测试,验证规则、交互与存档边界
软件测试·macos·swift·游戏开发·回归测试
林墨聊AIGC13 小时前
AI首尾帧一键生成连贯视频
大数据·人工智能·科技·ai作画·aigc·音视频
king135987455013 小时前
Clipstral 视频语义检索:FFmpeg PTS 验证、向量索引与端侧部署
人工智能·ffmpeg·音视频·大语言模型·llama·本地部署·llama.cpp