LoginSignup
2
6

More than 3 years have passed since last update.

画面をARコンテンツごと録画する方法です。
スクリーンショット 2020-10-20 12.07.35.png

実装

1、ReplayKitをインポート

ReplayKitを用います。

import ReplayKit

class ViewController: UIViewController,RPPreviewViewControllerDelegate...{

録画後プレビューのためのRPPreviewViewControllerDelegateも設定します。

2、ReplayKitの画面レコーダーを用意

let sharedRecorder = RPScreenRecorder.shared()

Info.plist の "Privacy - Microphone Usage Description" を追加してください。
追加せずにマイクを有効にすると、クラッシュします。

3、録画開始


sharedRecorder.isMicrophoneEnabled = true
sharedRecorder.startRecording(handler: { (error) in
    if let error = error {
        print(error)
    }
})

画面全体の収録を開始します。
スクリーンショット 2020-10-20 12.07.55.png

4、録画終了


sharedRecorder.stopRecording(handler: { (previewViewController, error) in
    previewViewController?.previewControllerDelegate = self
    self.present(previewViewController!, animated: true, completion: nil)
})

プレビューViewがポップアップします。
「保存」をタップで写真ライブラリに保存されます。
Info.plist の "Privacy - Photo Library Additions Usage Description" と "Privacy - Photo Library Usage Description"を追加してください。
追加せずに保存しようとすると、クラッシュします。

5、プレビューViewのデリゲート設定

func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
    DispatchQueue.main.async { [unowned previewController] in
        previewController.dismiss(animated: true, completion: nil)
    }
}

「キャンセル」をタップでプレビューViewが消えるためのデリゲートメソッドです。

Tips

・録画音の設定

import AudioToolbox
...
    AudioServicesPlaySystemSound(1117) // 録画開始音
    AudioServicesPlaySystemSound(1118) // 録画終了音

・録画開始時に、不要なボタンなどを隠すと、綺麗にAR画面のみ録画できます。
iPhoneの時間やバッテリー表示などは録画されません。

🐣


お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
Medium

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6