錄製與播放 API 可讓您在特定環境中錄製一次影片和 AR 資料,並利用該內容取代即時攝影機工作階段。
必要條件
請務必先瞭解基本 AR 概念,以及如何設定 ARCore 工作階段,然後再繼續操作。
與其他 ARCore API 的相容性
根據工作階段資料的處理方式,ARCore API 在播放期間產生的結果可能會與錄製期間所觀察到的結果不同。也可能會在後續的播放工作階段產生不同的結果。舉例來說,在播放期間,偵測到的可追蹤項目數量、偵測結果的確切時間,以及姿勢的長期姿勢都可能不同。
與共用相機的相容性
可以記錄使用共用相機的工作階段。不過,這類工作階段目前無法使用共用相機模式播放。
與 Cloud Anchor 的相容性
您可以在錄製或播放工作階段時託管及解析 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.
}
後續步驟
- 瞭解如何在已記錄的工作階段中新增自訂資料。
- 嘗試 ARCore Recording and Playback API 簡介程式碼研究室。