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.")
    }
}
相关推荐
老董杂货铺21 分钟前
安防视频互联网化利器:EasyNVR全面解析
网络·信息可视化·音视频
二等饼干~za8986681 小时前
Geo优化源码开发:关键技术解析与实践
数据库·sql·重构·mybatis·音视频
中议视控2 小时前
网络中央控制系统主机与音频综合管理平台的对接
网络·音视频
来鸟 鸣间2 小时前
音视频之ffmpeg基本信息查询命令
ffmpeg·音视频
这儿有一堆花2 小时前
AI视频生成的底层逻辑与技术架构
人工智能·音视频
Alonse_沃虎电子3 小时前
沃虎音频变压器:专业音频系统中的关键组件
网络·物联网·音视频·产品·方案·变压器·电子元器件
Engineer-Jsp3 小时前
音频降噪 - SNR/ASNR/STOI
音视频
郭涤生3 小时前
视场角(FOV)
音视频
ai产品老杨3 小时前
企业级AI视频管理平台,内置算法商城,集群管理、标注平台开源了
人工智能·开源·音视频
云边散步4 小时前
godot2D游戏教程系列一(8)
笔记·学习·音视频