Android で AR セッションを録画して再生する

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

前提条件

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

他の ARCore API との互換性

セッション データの処理方法によっては、ARCore API の再生中に生成される結果は録画時とは異なる場合があります。また、その後の再生セッションでも異なる結果になる場合があります。たとえば、検出されたトラッキング可能要素の数、検出の正確なタイミング、時間の経過に伴うポーズは、再生中に異なる場合があります。

共有カメラとの互換性

共有カメラを使用するセッションは録画できます。ただし、共有カメラ モードを使用したこれらのセッションは、現時点では再生できません。

Cloud Anchors との互換性

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

録画

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

ARCore セッションを録画する

ARCore セッションを録画するには、セッションを設定して、録音用の MP4 URI を指定します。最初の session.resume() 呼び出しの前に session.startRecording() を呼び出します。セッションが再開されると、録音が自動的に開始されます。セッションの一時停止時に録画を自動的に停止するには、RecordingConfig.setAutoStopOnPause() を呼び出します。部分的なセッションを記録するには、セッションの実行中に session.startRecording() を呼び出します。

Java

// Configure the ARCore session.
Session session = new Session(context);
Uri destination = Uri.fromFile(new File(context.getFilesDir(), "recording.mp4"));
RecordingConfig recordingConfig =
        new RecordingConfig(session)
        .setMp4DatasetUri(destination)
        .setAutoStopOnPause(true);
try {
  // Prepare the session for recording, but do not start recording yet.
  session.startRecording(recordingConfig);
} catch (RecordingFailedException e) {
  Log.e(TAG, "Failed to start recording", e);
}

// Resume the ARCore session to start recording.
session.resume();

Kotlin

// Configure the ARCore session.
val session = Session(context)
val destination = Uri.fromFile(File(context.getFilesDir(), "recording.mp4"))
val recordingConfig = RecordingConfig(session)
  .setMp4DatasetUri(destination)
  .setAutoStopOnPause(true)
session.startRecording(recordingConfig)

// Resume the ARCore session to start recording.
session.resume()

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

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

Java

try {
  session.stopRecording();  // Stop recording.
} catch (RecordingFailedException e) {
  Log.e(TAG, "Failed to stop recording", e);
}

Kotlin

session.stopRecording()

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

session.getRecordingStatus() を使用すると、いつでも現在の RecordingStatus を確認できます。

Java

// Use any time to determine current RecordingStatus.
if (session.getRecordingStatus() == RecordingStatus.OK) {
  // Update the UI to show that the session is currently being recorded.
}

Kotlin

// Use any time to determine current RecordingStatus.
if (session.recordingStatus == RecordingStatus.OK) {
  // Update the UI to show that the session is currently being recorded.
}

再生

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

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

以前に録音したセッションを再生するには、最初の session.resume() 呼び出しの前に session.setPlaybackDatasetUri() を呼び出します。

session.resume() への最初の呼び出しによって再生が開始された後、session.pause() を呼び出してセッションを一時停止すると、データセット内のすべてのカメラ画像フレームとその他の記録されたセンサーデータの処理が一時停止されます。この方法で破棄されたカメラ画像フレームとセンサー フレームデータは、session.resume() を呼び出してセッションが再開されたときに再処理されません。処理されたデータにギャップがあるため、セッションの AR トラッキングに悪影響が及ぶのが一般的です。

Java

// Configure the ARCore session.
Session session = new Session(context);

// Specify the previously recorded MP4 file.
Uri recordingUri = Uri.fromFile(new File(context.getFilesDir(), "recording.mp4"));
session.setPlaybackDatasetUri(recordingUri);
…

// Start playback from the beginning of the dataset.
session.resume();
…

// Pause the AR session, but silently continue MP4 playback. Camera frames
// and other recorded sensor data is discarded while the session is paused.
session.pause();
…

// Resume the AR session. Camera frames and other sensor data from the MP4
// that was silently played back while the session was paused is not
// processed by ARCore.
session.resume();

Kotlin

// Configure the ARCore session.
val session = Session(context)

// Specify the previously recorded MP4 file.
val recordingUri = Uri.fromFile(File(context.filesDir, "recording.mp4"))
session.playbackDatasetUri = recordingUri
…

// Start playback from the beginning of the dataset.
session.resume()
…

// Pause the AR session, but silently continue MP4 playback. Camera frames
// and other recorded sensor data is discarded while the session is paused.
session.pause()
…

// Resume the AR session. Camera frames and other sensor data from the MP4
// that was silently played back while the session was paused is not
// processed by ARCore.
session.resume()

再生を最初からやり直す

データセットの先頭から再生を再開するには、セッションを一時停止し、session.setPlaybackDatasetUri() を呼び出して、セッションを再開する前に同じ MP4 録音を指定します。

Java

session.pause();
// Pause and specify the SAME dataset:
session.setPlaybackDatasetUri(previousRecordingUri);
session.resume();  // Playback starts from the BEGINNING of the dataset.

Kotlin

session.pause()
// Pause and specify the SAME dataset:
session.playbackDatasetUri = previousRecordingUri
session.resume()  // Playback starts from the BEGINNING of the dataset.

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

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

Java

// Switch to a different dataset.
session.pause();   // Pause the playback of the first dataset.
// Specify a different dataset to use.
session.setPlaybackDatasetUri(newRecordingUri);
session.resume();  // Start playback from the beginning of the new dataset.

Kotlin

// Switch to a different dataset.
session.pause()   // Pause the playback of the first dataset.
// Specify a different dataset to use.
session.playbackDatasetUri = newRecordingUri
session.resume()  // Start playback from the beginning of the new dataset.

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

いつでも session.getPlaybackStatus() を使用して、現在の PlaybackStatus を確認できます。

Java

// Use any time to determine current PlaybackStatus.
if (session.getPlaybackStatus() != PlaybackStatus.OK) {
  // Update the UI to show that the session playback has finished.
}

Kotlin

// Use any time to determine current PlaybackStatus.
if (session.playbackStatus != PlaybackStatus.OK) {
  // Update the UI to show that the session playback has finished.
}

次のステップ