在 Android 设备上录制和播放 AR 现场录像
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
《The Recording》和借助 Playback API,您可以在给定环境中录制一次视频和 AR 数据,然后使用这些内容替换实时摄像头会话。
前提条件
确保您了解 AR 基础概念
以及如何在继续之前配置 ARCore 现场录像。
与其他 ARCore API 的兼容性
由于会话数据的处理方式不同,ARCore API 在播放期间产生的结果可能与记录期间观察到的结果不同。在后续播放会话中,它们也可能会产生不同的结果。例如,在播放过程中,检测到的可跟踪对象的数量、检测的精确时间以及它们的姿态随时间而变化。
与共享相机的兼容性
系统可以录制使用共享摄像头的会话。不过,目前无法使用“共享相机”模式播放这些会话。
与云锚点的兼容性
您可以在录制或回放会话时托管和解析云锚点。
正在录制
开始、停止和检查 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.
}
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe Recording & Playback API allows you to record video and AR data from an environment and reuse it to replace a live camera feed in subsequent sessions.\u003c/p\u003e\n"],["\u003cp\u003eARCore APIs may produce slightly different results during playback compared to live sessions due to data processing differences.\u003c/p\u003e\n"],["\u003cp\u003eYou can record and playback AR sessions, including those with Cloud Anchors, by configuring the session and providing an MP4 URI.\u003c/p\u003e\n"],["\u003cp\u003ePlayback sessions run in real-time and cannot be adjusted, but you can pause, resume, and restart them, or switch to a different recorded session.\u003c/p\u003e\n"],["\u003cp\u003eWhile Shared Camera sessions can be recorded, playback using Shared Camera mode is not currently supported.\u003c/p\u003e\n"]]],["The Recording & Playback API allows recording video and AR data into an MP4 URI, which can then replace a live camera session. To record, configure the session, provide the MP4 URI, and call `session.startRecording()` before `session.resume()`. Recording can be stopped via `session.stopRecording()`. Playback uses a recorded session via `session.setPlaybackDatasetUri()` before resuming. Pausing during playback discards camera frames and sensor data. Restart or switch playback sessions by pausing and setting the dataset URI again. You can check recording and playback statuses using `getRecordingStatus()` and `getPlaybackStatus()`, respectively.\n"],null,["# Record and play back an AR session on Android\n\nThe Recording \\& Playback API enables you to record video and AR data once within a given environment and use that content to replace a live camera session.\n\nPrerequisites\n-------------\n\nMake sure that you understand [fundamental AR concepts](/ar/develop/fundamentals)\nand how to [configure an ARCore session](/ar/develop/java/session-config) before proceeding.\n\nCompatibility with other ARCore APIs\n------------------------------------\n\nDue to the way session data is processed, ARCore APIs may produce different results during playback than observed during recording. They may also produce different results during subsequent playback sessions. For example, the number of detected trackables, the precise timing of their detection, and their poses over time may be different during playback.\n\n### Compatibility with Shared Camera\n\nSessions that use [Shared Camera](/ar/develop/java/camera-sharing) can be recorded. However, playback for these sessions using Shared Camera mode is currently unavailable.\n\n### Compatibility with Cloud Anchors\n\nYou can host and resolve [Cloud Anchors](/ar/develop/java/cloud-anchors/overview-android) while recording or playing back a session.\n\nRecording\n---------\n\nStart, stop, and check the status of an ARCore session recording.\n\n### Record an ARCore session\n\nTo record an ARCore session, configure the session and provide an MP4 URI for the recording. Call [`session.startRecording()`](/ar/reference/java/com/google/ar/core/Session#startRecording-recordingConfig) before the first call to [`session.resume()`](/ar/reference/java/com/google/ar/core/Session#resume-). Recording automatically starts when the session resumes. To automatically stop recording when the session is paused, call [`RecordingConfig.setAutoStopOnPause()`](/ar/reference/java/com/google/ar/core/RecordingConfig#getAutoStopOnPause()). To record a partial session, call [`session.startRecording()`](/ar/reference/java/com/google/ar/core/Session#startRecording-recordingConfig) while the session is running. \n\n### Java\n\n // Configure the ARCore session.\n Session session = new Session(context);\n Uri destination = Uri.fromFile(new File(context.getFilesDir(), \"recording.mp4\"));\n RecordingConfig recordingConfig =\n new RecordingConfig(session)\n .setMp4DatasetUri(destination)\n .setAutoStopOnPause(true);\n try {\n // Prepare the session for recording, but do not start recording yet.\n session.startRecording(recordingConfig);\n } catch (RecordingFailedException e) {\n Log.e(TAG, \"Failed to start recording\", e);\n }\n\n // Resume the ARCore session to start recording.\n session.resume();\n\n### Kotlin\n\n // Configure the ARCore session.\n val session = Session(context)\n val destination = Uri.fromFile(File(context.getFilesDir(), \"recording.mp4\"))\n val recordingConfig = RecordingConfig(session)\n .setMp4DatasetUri(destination)\n .setAutoStopOnPause(true)\n session.startRecording(recordingConfig)\n\n // Resume the ARCore session to start recording.\n session.resume()\n\n### Stop a session recording\n\nTo stop recording without pausing the currently running AR session, call [`session.stopRecording()`](/ar/reference/java/com/google/ar/core/Session#stopRecording-). \n\n### Java\n\n try {\n session.stopRecording(); // Stop recording.\n } catch (RecordingFailedException e) {\n Log.e(TAG, \"Failed to stop recording\", e);\n }\n\n### Kotlin\n\n session.stopRecording()\n\n### Check recording status\n\n[`session.getRecordingStatus()`](/ar/reference/java/com/google/ar/core/Session#getRecordingStatus-)\ncan be used at any time to determine the current [`RecordingStatus`](/ar/reference/java/com/google/ar/core/RecordingStatus). \n\n### Java\n\n // Use any time to determine current RecordingStatus.\n if (session.getRecordingStatus() == RecordingStatus.OK) {\n // Update the UI to show that the session is currently being recorded.\n }\n\n### Kotlin\n\n // Use any time to determine current RecordingStatus.\n if (session.recordingStatus == RecordingStatus.OK) {\n // Update the UI to show that the session is currently being recorded.\n }\n\nPlayback\n--------\n\nPlay back previously recorded AR sessions. Sessions play back in real time, and session playback or speed cannot be adjusted.\n\n### Play back a previously recorded session\n\nTo play back a previously recorded session, call [`session.setPlaybackDatasetUri()`](/ar/reference/java/com/google/ar/core/Session#setPlaybackDatasetUri-mp4DatasetUri) before the first call to [`session.resume()`](/ar/reference/java/com/google/ar/core/Session#resume()).\n\nOnce playback has started due to the first call to [`session.resume()`](/ar/reference/java/com/google/ar/core/Session#resume()), pausing the session by calling [`session.pause()`](/ar/reference/java/com/google/ar/core/Session#pause()) will suspend processing of all camera image frames and any other recorded sensor data in the dataset. Camera image frames and sensor frame data that is discarded in this way will not be reprocessed when the session is again resumed by calling [`session.resume()`](/ar/reference/java/com/google/ar/core/Session#resume()). AR tracking for the session will generally suffer due to the gap in processed data. \n\n### Java\n\n // Configure the ARCore session.\n Session session = new Session(context);\n\n // Specify the previously recorded MP4 file.\n Uri recordingUri = Uri.fromFile(new File(context.getFilesDir(), \"recording.mp4\"));\n session.setPlaybackDatasetUri(recordingUri);\n ...\n\n // Start playback from the beginning of the dataset.\n session.resume();\n ...\n\n // Pause the AR session, but silently continue MP4 playback. Camera frames\n // and other recorded sensor data is discarded while the session is paused.\n session.pause();\n ...\n\n // Resume the AR session. Camera frames and other sensor data from the MP4\n // that was silently played back while the session was paused is not\n // processed by ARCore.\n session.resume();\n\n### Kotlin\n\n // Configure the ARCore session.\n val session = Session(context)\n\n // Specify the previously recorded MP4 file.\n val recordingUri = Uri.fromFile(File(context.filesDir, \"recording.mp4\"))\n session.playbackDatasetUri = recordingUri\n ...\n\n // Start playback from the beginning of the dataset.\n session.resume()\n ...\n\n // Pause the AR session, but silently continue MP4 playback. Camera frames\n // and other recorded sensor data is discarded while the session is paused.\n session.pause()\n ...\n\n // Resume the AR session. Camera frames and other sensor data from the MP4\n // that was silently played back while the session was paused is not\n // processed by ARCore.\n session.resume()\n\n### Restart playback from the beginning\n\nTo restart a playback from the beginning of the dataset, pause the session and call [`session.setPlaybackDatasetUri()`](/ar/reference/java/com/google/ar/core/Session#setPlaybackDatasetUri-mp4DatasetUri), specifying the same MP4 recording before resuming the session. \n\n### Java\n\n session.pause();\n // Pause and specify the SAME dataset:\n session.setPlaybackDatasetUri(previousRecordingUri);\n session.resume(); // Playback starts from the BEGINNING of the dataset.\n\n### Kotlin\n\n session.pause()\n // Pause and specify the SAME dataset:\n session.playbackDatasetUri = previousRecordingUri\n session.resume() // Playback starts from the BEGINNING of the dataset.\n\n### Play back a different session\n\nTo play back a different dataset, pause the session and specify the new dataset before resuming the session. \n\n### Java\n\n // Switch to a different dataset.\n session.pause(); // Pause the playback of the first dataset.\n // Specify a different dataset to use.\n session.setPlaybackDatasetUri(newRecordingUri);\n session.resume(); // Start playback from the beginning of the new dataset.\n\n### Kotlin\n\n // Switch to a different dataset.\n session.pause() // Pause the playback of the first dataset.\n // Specify a different dataset to use.\n session.playbackDatasetUri = newRecordingUri\n session.resume() // Start playback from the beginning of the new dataset.\n\n### Check playback status\n\nUse [`session.getPlaybackStatus()`](/ar/reference/java/com/google/ar/core/Session#getPlaybackStatus-) at any time to determine the current\n[`PlaybackStatus`](/ar/reference/java/com/google/ar/core/PlaybackStatus). \n\n### Java\n\n // Use any time to determine current PlaybackStatus.\n if (session.getPlaybackStatus() != PlaybackStatus.OK) {\n // Update the UI to show that the session playback has finished.\n }\n\n### Kotlin\n\n // Use any time to determine current PlaybackStatus.\n if (session.playbackStatus != PlaybackStatus.OK) {\n // Update the UI to show that the session playback has finished.\n }\n\nWhat's next\n-----------\n\n- Learn how to [add custom data](/ar/develop/java/recording-and-playback/custom-data-track) to recorded sessions.\n- Try the [Introduction to the ARCore Recording and Playback API](https://codelabs.developers.google.com/codelabs/arcore-record-and-playback-intro) codelab."]]