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.")
    }
}
相关推荐
上海合宙LuatOS1 天前
LuatOS核心库API——【hmeta 】硬件元数据
单片机·嵌入式硬件·物联网·算法·音视频·硬件工程·哈希算法
追夢秋陽1 天前
Cocoa 使用NSCollectionView显示列表,数据不足布局异常处理
macos·objective-c·cocoa·swift·collectionview
一个人旅程~1 天前
QQ音乐、potplayer、VLC等对音乐格式的支持和加密文件格式的转换有哪些方法?potplayer的音质相对于VLC有哪些特点?
windows·经验分享·电脑·音视频
音视频牛哥1 天前
从协议栈到工程落地:SmartMediaKit全链路模块化架构深度解析
音视频·rtsp播放器·rtmp播放器·安卓rtsp服务器·安卓轻量级rtsp服务·安卓rtsp摄像头推流·安卓摄像头rtmp推流
奔跑吧 android1 天前
【车载Audio】【AudioHal 04】【高通音频架构】【从 AHAL adev_open 到 PAL XML 解析:30微秒内的调用链深度追踪】
音视频·audiohal·车载audio·高通音频架构·ahal·audiofinger
REDcker1 天前
FFmpeg完整文档
linux·服务器·c++·ffmpeg·音视频·c·后端开发
linux_cfan1 天前
WordPress 视频播放痛点解决方案:支持 RTSP/WebRTC 与字幕检索的 ZWPlayer 插件实测
php·音视频·webrtc
新缸中之脑1 天前
SaaS 大灭绝
开发语言·ios·swift
Swift社区1 天前
LeetCode 389 找不同 - Swift 题解
算法·leetcode·swift
AI周红伟2 天前
周红伟:字节官方发布Seedance 2.0 视频模型,技术实现过程和 技术分解
音视频