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.")
    }
}
相关推荐
苏黎caius9 分钟前
SoX 语句,音频界的瑞士军刀
音视频
v1326656236822 分钟前
博通集成:BK7259 wifi6音视频芯片 200w视频流IPC 超低功耗
物联网·音视频·低功耗·ipc
v132665623683 小时前
博通集成:BK7259 支持200w视频流IPC 带ISP 硬件H264编解码 本地算力0.1T
物联网·音视频·ipc·ai边缘
纳祥科技3 小时前
拆解一款AUX立体声音频切换器,4进1出,乐器/便携效果器均可用
音视频
weixin_443478513 小时前
Flutter学习之第三方组件:视频播放器控件
学习·flutter·音视频
EasyCVR3 小时前
GB28181/RTSP/ONVIF/RTMP/SDK视频汇聚平台EasyCVR构建通信基站智慧安防可视化管理体系
音视频
日光明媚4 小时前
FFmpeg 视频生成推理 Pipeline:Python 版常用函数封装(可直接集成)
python·深度学习·ai作画·aigc·音视频
v132665623684 小时前
BK7258 wifi6音视频soc芯片应用分析
嵌入式硬件·物联网·音视频·iot·wifi6
日光明媚5 小时前
SoulX-FlashTalk 技术报告解读:从“严格因果”到“双向流式蒸馏”,实时数字人为什么能做到 0.87s 延迟、32FPS 和长时稳定?
人工智能·python·深度学习·ai作画·aigc·音视频
线束线缆组件品替网6 小时前
Amphenol网线组件RJE1Y12305152401线束选型指南替代方案解析
服务器·数码相机·电脑·音视频·电视盒子·智能电视