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.")
    }
}
相关推荐
sakiko_6 小时前
UIKit学习笔记3-布局、滚动视图、隐藏或显示视图
前端·笔记·学习·objective-c·swift·uikit
jinglong.zha7 小时前
AI萌宠短剧实战:从0孵化动物IP,用AI制作爆款短视频
人工智能·ai·音视频·网赚教程·萌宠
大学生小郑11 小时前
CMOS 传感器堆叠结构
图像处理·学习·音视频·视频
luoqice11 小时前
利用librtmp实现h264和AAC音频的推流
音视频·aac
byte轻骑兵11 小时前
蓝牙CAP规范解析:构建多设备协同的通用音频新生态
人工智能·音视频·le audio·低功耗音频·蓝牙通话
大象AI共学12 小时前
我让AI写了个网页,它自动变成了视频
人工智能·音视频
Prannt12 小时前
星朗智能语音——视频音色替换
ai·音视频·语音识别
sakiko_1 天前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
朗清风1 天前
“\“在字符串表示正则语义中的作用
swift
时空自由民.1 天前
音视频图片压缩
音视频