通过Recording&Playback API,您可以在给定的环境中记录一次视频和AR数据,并使用该内容替换实时摄像头会话。
先决条件
在继续之前,请确保您了解基本的AR概念以及如何配置ARCore会话。
与其他ARCore API的兼容性
由于会话数据的处理方式,ARCore API可能在回放过程中产生与记录过程中所观察到的结果不同的结果。在随后的播放会话中,它们也可能产生不同的结果。例如,在回放过程中,检测到的可跟踪对象的数量,检测到的精确定时以及它们随时间的姿势可能会有所不同。
与共享相机的兼容性
可以记录使用共享摄像机的会话。但是,当前无法使用“共享照相机”模式播放这些会话。
与云锚的兼容性
您可以在录制或回放会话时托管和解析Cloud Anchors 。
记录
开始,停止并检查ARCore会话记录的状态。
记录ARCore会话
爪哇// Configure the ARCore session.
Session session = new Session(context);
String destination = new File(context.getFilesDir(), "recording.mp4").getAbsolutePath();
RecordingConfig recordingConfig =
new RecordingConfig(session)
.setMp4DatasetFilePath(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();科特林// Configure the ARCore session.
val session = Session(context)
val destination = File(context.getFilesDir(), "recording.mp4").getAbsolutePath()
val recordingConfig = RecordingConfig(session)
.setMp4DatasetFilePath(destination)
.setAutoStopOnPause(true)
session.startRecording(recordingConfig)
// Resume the ARCore session to start recording.
session.resume()
停止会话记录
要在不暂停当前运行的AR会话的情况下停止记录,请调用session.stopRecording() 。
爪哇try {
session.stopRecording(); // Stop recording.
} catch (RecordingFailedException e) {
Log.e(TAG, "Failed to stop recording", e);
}科特林session.stopRecording()
检查录音状态
爪哇// Use any time to determine current RecordingStatus.
if (session.getRecordingStatus() == RecordingStatus.OK) {
// Update the UI to show that the session is currently being recorded.
}科特林// 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.pause()暂停会话将暂停对数据集中所有相机图像帧和任何其他记录的传感器数据的处理。通过调用session.resume()再次恢复会话时,不会重新处理以这种方式丢弃的相机图像帧和传感器帧数据。由于处理数据的差异,会话的AR跟踪通常会受到影响。
爪哇// Configure the ARCore session.
Session session = new Session(context);
// Specify the previously recorded MP4 file.
String recordingPath = new File(context.getFilesDir(), "recording.mp4").getAbsolutePath();
session.setPlaybackDataset(recordingPath);
…
// 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();科特林// Configure the ARCore session.
val session = Session(context)
// Specify the previously recorded MP4 file.
val recordingPath = File(context.filesDir, "recording.mp4").absolutePath
session.playbackDataset = recordingPath
…
// 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.setPlaybackDataset() ,然后在恢复会话之前指定相同的MP4录音。
爪哇session.pause();
// Pause and specify the SAME dataset:
session.setPlaybackDataset(previousRecordingPath);
session.resume(); // Playback starts from the BEGINNING of the dataset.科特林session.pause()
// Pause and specify the SAME dataset:
session.playbackDataset = previousRecordingPath
session.resume() // Playback starts from the BEGINNING of the dataset.
播放不同的会话
要播放其他数据集,请在恢复会话之前暂停会话并指定新的数据集。
爪哇第0342章科特林// Switch to a different dataset.
session.pause() // Pause the playback of the first dataset.
// Specify a different dataset to use.
session.playbackDataset = newRecordingPath
session.resume() // Start playback from the beginning of the new dataset.
检查播放状态
爪哇// Use any time to determine current PlaybackStatus.
if (session.getPlaybackStatus() != PlaybackStatus.OK) {
// Update the UI to show that the session playback has finished.
}科特林// Use any time to determine current PlaybackStatus.
if (session.playbackStatus != PlaybackStatus.OK) {
// Update the UI to show that the session playback has finished.
}
下一步是什么了解如何将自定义数据添加到录制的会话中。