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.")
    }
}
相关推荐
REDcker21 小时前
Web 音视频流媒体 API 全景
前端·音视频
奔跑吧 android21 小时前
【车载Audio】【AudioHal 05】【高通音频架构】【audio_hw_device 核心接口解析】
音视频·qcom·aosp15·车载音频·audio hal·audio_hw_device
疯笔码良1 天前
【swiftUI】实现自定义的底部TabBar组件
ios·swiftui·swift
音视频牛哥1 天前
RTSP协议规范深度解析与SmartMediaKit的RTSP播放器工程实践
人工智能·计算机视觉·音视频·大牛直播sdk·rtsp播放器·超低延迟rtsp播放器·rtspplayer
sweetone2 天前
故障现象 并非玄学——LINN(莲)MAJIK功放维修记录
经验分享·音视频
不吃鱼的猫7482 天前
【从零手写播放器:FFmpeg 音视频开发实战】04-封装格式与多媒体容器
c++·ffmpeg·音视频
九丝城主2 天前
1V1音视频对话4--FLUTTER实现
flutter·音视频
张张说点啥2 天前
能做影视级可商业视频的AI工具,Seedance 2.0 全球首发实测
人工智能·音视频
东坡肘子2 天前
祝大家马年新春快乐! -- 肘子的 Swift 周报 #123
人工智能·swiftui·swift
qq_433502182 天前
收集了一些免费视频背景映月素材网站分享记录
经验分享·学习·音视频·生活