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.")
    }
}
相关推荐
ZENERGY-众壹9 小时前
30MW 渔光互补监控实录:高湿环境下视频与传感器集成的三个坑
音视频
霍霍哈嗨13 小时前
swiftUI框架基础
ios·swiftui·swift
美狐美颜SDK开放平台14 小时前
直播APP开发教程:视频美颜SDK接入需要注意哪些问题?
人工智能·音视频·美颜sdk·第三方美颜sdk·短视频美颜sdk
末代iOS程序员华仔14 小时前
iOS 开发到上架 App Store 全流程详解
ios·objective-c·swift
Elastic 中国社区官方博客15 小时前
从多模态 LLM 中引导构建音频嵌入
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·音视频
qetfw16 小时前
yt-dlp:下载公开视频、字幕、封面和音频的实用命令
python·音视频·开源项目·效率工具
美丽的欣情16 小时前
C++音频开发:什么是50Hz、100Hz测试音?以及如何生成16bit/8000Hz的WAV测试文件
开发语言·c++·音视频
AI服务老曹16 小时前
实战记录:H264/H265视频分析项目接入与性能评估指南
音视频
amongdata17 小时前
MyVoiceTyping:面向中文语音输入的本地优先开源方案
python·开源·音视频·语音识别
做萤石二次开发的哈哈17 小时前
如何接入萤石开放平台 ERTC Windows SDK?
windows·音视频·萤石开放平台