Android をターゲットとする AR Foundation で AR セッションを録画して再生する

Recording & Playback API を使用すると、特定の環境内で動画と AR データを 1 回録画し、そのコンテンツを使用してライブカメラ セッションに代用できます。

前提条件

続行する前に、AR の基本コンセプトARCore セッションを構成する方法を理解してください。

他の ARCore API との互換性

セッション データの処理方法により、ARCore API では、録画中に観測された結果と再生中に得られる結果が異なる場合があります。また、後続の再生セッションで異なる結果が得られることもあります。たとえば、再生中に検出されるトラッケーブルの数、検出の正確なタイミング、時間の経過に伴うポーズが異なる場合があります。

Cloud Anchors との互換性

セッションの録画中または再生中に Cloud Anchor をホストして解決できます。

記録中

ARCore セッションの録画を開始、停止、ステータスを確認します。

ARCore セッションを録画する

ARCore セッションを録画するには、セッションを構成し、録画用の MP4 URI を指定します。セッションを再開する前に ARRecordingManager.StartRecording() を呼び出します。セッションが再開されると、録画が自動的に開始されます。セッションが一時停止されたときに録音を自動的に停止するには、ARRecordingConfig.AutoStopOnPause を呼び出します。部分的なセッションを録画するには、セッションの実行中に ARRecordingManager.StartRecording() を呼び出します。

ARCoreRecordingConfig recordingConfig = ScriptableObject.CreateInstance<ARCoreRecordingConfig>();
Uri datasetUri = new System.Uri("file:///uri/for/dataset.mp4");
recordingConfig
.Mp4DatasetUri = datasetUri.AbsoluteUri;

recordingManager
.StartRecording(recordingConfig);

セッションの録画を停止する

現在実行中の AR セッションを一時停止せずに録画を停止するには、ARRecordingManager.StopRecording() を呼び出します。

recordingManager.StopRecording();

録画ステータスを確認する

ARRecordingManager.RecordingStatus を使用すると、いつでも現在の録画ステータスを確認できます。

Debug.Log("Current Recording Status: " + recordingManager.RecordingStatus);

再生

以前に録画した AR セッションを再生します。セッションはリアルタイムで再生され、セッションの再生や速度を調整することはできません。

以前に録画したセッションを再生する

以前に録画したセッションを再生するには、ARPlaybackManager.SetPlaybackDatasetUri() を呼び出して、再生するデータセットの URI を指定します。この方法を使用するには、セッションを一時停止する必要があります。セッションを再開して変更を反映します。

セッションの再開により再生が開始された後、ARSession を無効にしてセッションを一時停止すると、データセット内のすべてのカメラ画像フレームとその他の記録されたセンサーデータの処理が一時停止します。この方法で破棄されたカメラ画像フレームとセンサー フレームデータは、セッションを再開してセッションが再開されたときに再処理されません。通常、処理されたデータのギャップにより、セッションの AR トラッキングが低下します。

// Disable the ARSession to pause the current AR session.
session
.enabled = false;

// In the next frame, provide a URI for the dataset you wish to play back.
Uri datasetUri = new System.Uri("file:///uri/for/dataset.mp4");
playbackManager
.SetPlaybackDatasetUri(datasetUri);

// In the frame after that, re-enable the ARSession to resume the session from
// the beginning of the dataset.
session
.enabled = true;

既知の問題と回避策

ARPlaybackManager.SetPlaybackDatasetUri() の呼び出しが ErrorPlaybackFailed を返すという既知の問題があります。これは、セッションが一時停止されるまでに数フレームかかることがあるためです。セッションが一時停止される前に ARPlaybackManager.SetPlaybackDatasetUri() が呼び出されると、セッションにアクセスできず、エラーが返されます。

次のコードは回避策として使用できます。

// Workaround for known issue where `playbackManager.SetPlaybackDatasetUri()`
// returns `ErrorPlaybackFailed` because it can take several frames for a
// session to be paused.

// Reference to the ARSession component in the scene.
ARSession session;

void PlaybackDataset()
{
    setPlaybackDataset
= true;

   
// Pause the current AR session.
    session
.enabled = false;

   
// Set a timeout for retrying playback retrieval.
    timeout
= 10f;
}

// Next frame
void Update()
{
   
...

   
if (setPlaybackDataset)
   
{
       
PlaybackResult result = playbackManager.SetPlaybackDatasetUri(datasetUri);
       
if (result == PlaybackResult.ErrorPlaybackFailed || result == PlaybackResult.SessionNotReady)
       
{
           
// Try to set the dataset again in the next frame.
            timeout
-= Time.deltaTime;
       
}
       
else
       
{
           
// Do not set the timeout if the result is something other than ErrorPlaybackFailed.
            timeout
= -1f;
       
}

       
if (timeout < 0.0f)
       
{
            setPlaybackDataset
= false;
           
// If playback is successful, proceed as usual.
           
// If playback is not successful, handle the error appropriately.
       
}
   
}

   
...
}

再生を停止する

再生を停止するには、ARPlaybackManager.SetPlaybackDatasetUri() を呼び出して、データセット URI を null に設定します。

// Disable the ARSession to pause the current AR session.
session
.enabled = false;

// In the next frame, unset the playback dataset URI.
playbackManager
.SetPlaybackDatasetUri(null);

// In the frame after that, re-enable the ARSession to resume the session using
// the device camera and other sensors.
session
.enabled = true;

最初から再生を再開する

データセットの先頭から再生を再開するには、ARPlaybackManager.SetPlaybackDatasetUri() を呼び出して同じ MP4 録画を指定してから、セッションを再開します。

// Disable the ARSession to pause the current AR session.
session
.enabled = false;

// In the next frame, specify the same dataset URI.
playbackManager
.SetPlaybackDatasetUri(datasetUri); // Same URI that was previously set.

// In the frame after that, re-enable the ARSession to resume the session from
// the beginning of the dataset.
session
.enabled = true;

別のセッションを再生する

別のデータセットを再生するには、セッションを一時停止して新しいデータセットを指定し、セッションを再開します。

// Disable the ARSession to pause the current AR session.
session
.enabled = false;

// In the next frame, specify a new dataset URI.
Uri newDatasetUri = new System.Uri("file:///uri/for/different/dataset.mp4");
playbackManager
.SetPlaybackDatasetUri(newDatasetUri); // Different URI than was previously set.

// In the frame after that, re-enable the ARSession to resume the session from
// the beginning of the new dataset.
session
.enabled = true;

再生ステータスを確認する

ARPlaybackManager.PlaybackStatus を使用すると、いつでも現在の再生ステータスを確認できます。

Debug.Log("Current Playback Status: " + playbackManager.PlaybackStatus);

次のステップ