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.")
    }
}
相关推荐
Black蜡笔小新1 小时前
视频融合平台EasyCVR构筑智慧小区安全防护与智能管理的数字底座
安全·音视频
CoookeCola2 小时前
M.I.O: Interactive Intelligence for Digital Humans(交互式智能数字人)
论文阅读·人工智能·aigc·音视频
__WanG4 小时前
screen time api - FamilyActivityPicker 获取选中应用
ios·iphone·swift
特立独行的猫a4 小时前
鸿蒙PC三方库移植:x264视频编码库的移植适配实践
华为·音视频·harmonyos·三方库移植·鸿蒙pc
HyperAI超神经4 小时前
入选NeurIPS 2025,智源/北大/北邮提出多流控制视频生成框架,基于音频解混实现精确音画同步
人工智能·ai·音视频·视频生成·neurlps 2025
be or not to be14 小时前
HTML入门系列:从图片到表单,再到音视频的完整实践
前端·html·音视频
LCG米15 小时前
基于ESP32-S3与OV5640的高清低延迟无线视频传输系统构建教程
音视频
Angelina_Jolie16 小时前
基于 Retinex 的 TempRetinex:适用于不同光照条件下低光视频的无监督增强方法
计算机视觉·音视频
山海青风19 小时前
藏文TTS介绍:6 MMS 项目的多语言 TTS
人工智能·python·神经网络·音视频
Everbrilliant891 天前
FFmpeg解码OpenSL、ANativeWindow播放实现音视频同步
ffmpeg·音视频·opensl音视频同步播放·音视频同步播放·唇音同步·音视频时钟漂移·播放时钟同步