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.")
    }
}
相关推荐
liliangcsdn17 小时前
文本视频音频分块工具 - Semantic Chunkers
人工智能·音视频
风栖柳白杨19 小时前
【语音识别】一些音频的使用方法
人工智能·音视频·语音识别
fendoudexiaoniao_ios1 天前
iOS 列表拖拽cell排序
ios·swift
Black蜡笔小新1 天前
国密GB35114平台EasyGBS筑牢安防安全防线,GB28181/GB35114无缝接入
网络·安全·音视频·gb35114
川西胖墩墩1 天前
文生视频AI工具深度评测:2024年主流视频生成模型的技术对比与创作指南
人工智能·数据挖掘·音视频
TSINGSEE1 天前
国标GB28181视频质量诊断:EasyGBS服务插件EasyVQD快速识别花屏、蓝屏、画面冻结抖动
人工智能·音视频·实时音视频·视频编解码·视频质量诊断·花屏检测·画面抖动
XHW___0011 天前
webrtc中音频3A处理开关配置
音视频·webrtc
愚公搬代码1 天前
【愚公系列】《AI短视频创作一本通》012-AI 短视频分镜头设计(AI绘画提示词入门)
人工智能·ai作画·音视频
咸鱼永不翻身1 天前
Unity视频资源压缩详解
unity·游戏引擎·音视频