android 调用相机ar,在Android上录制并播放AR会话

这篇博客介绍了如何使用ARCore的Recording & Playback API来记录和回放增强现实会话。在记录过程中,会话数据可能与实时体验有所差异,如对象检测数量和定位精度。在回放时,会话会实时播放,但无法调整速度。文章还详细说明了开始、停止记录,检查记录和回放状态的步骤,以及如何处理会话的暂停和恢复。此外,讨论了与共享相机和云锚的兼容性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过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.

}

下一步是什么了解如何将自定义数据添加到录制的会话中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值