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.")
    }
}
相关推荐
QWQ___qwq7 小时前
Swift中.gesture的用法
服务器·microsoft·swift
雨之小8 小时前
RV1106+es8388音频采集和播放调试
音视频·rv1106·es8388
EasyCVR8 小时前
不止于“看”:视频汇聚平台EasyCVR视频监控系统功能特点详解
音视频
来知晓13 小时前
语音处理:音频移形幻影,为何大振幅信号也无声
开发语言·音视频
阿酷tony13 小时前
开源项目:FlyCut Caption智能视频字幕裁剪工具
音视频·智能视频字幕裁剪·视频字幕裁剪
CodeJourney.13 小时前
Sora引爆AI视频革命
人工智能·音视频
2501_9209555713 小时前
MP4格式视频无法播放怎么修?4个修复方法,解决难题
音视频
ZEGO即构开发者13 小时前
【ZEGO即构开发者日报】谷歌推出新款视频生成模型 Veo 3.1;腾讯开源通用文本表示模型Youtu-Embedding;AI 陪伴赛道观察……
人工智能·音视频·实时音视频·业界资讯
给大佬递杯卡布奇诺14 小时前
FFmpeg 基本API avformat_open_input函数内部调用流程分析
c++·ffmpeg·音视频