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.")
    }
}
相关推荐
chjqxxxx3 小时前
php使用ffmpeg实现视频随机截图并转成图片
ffmpeg·php·音视频
ZEGO即构开发者8 小时前
WebRTC 实战:用即构 SDK 搭建 Web 端 1v1 视频通话(含完整流程与 Demo)
前端·音视频·webrtc
趣浪吧11 小时前
AI在手机上真没用吗?
人工智能·智能手机·aigc·音视频·媒体
qq_2821953111 小时前
嵌入式音频USB Audio调试
linux·音视频
代码狂想家11 小时前
CANN视频增强实战:基于Ascend平台的历史影像修复
音视频
1***815314 小时前
Docker视频
docker·容器·音视频
Black蜡笔小新15 小时前
视频融合平台EasyCVR助力守护渔业牧区安全与增效
安全·音视频
Likeadust18 小时前
革新传统勘探:视频推流平台EasyDSS无人机推流如何赋能高效安全的地质考察?
安全·音视频·无人机
00后程序员张1 天前
Swift 应用加密工具的全面方案,从源码混淆到 IPA 成品加固的多层安全实践
安全·ios·小程序·uni-app·ssh·iphone·swift
AI浩1 天前
Cambrian-S:迈向视频中的空间超感知
人工智能·目标检测·计算机视觉·音视频