在 Android 设备上录制时添加自定义数据
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
《The Recording》和借助 Playback API,您可以录制现场录像并使用它来代替实时摄像头画面。不过,这些记录仅包含视频和传感器数据。您还可以向时段录制添加自定义数据,并在播放期间将该数据返回给您,就好像该数据是相机图片的一部分一样。
ARCore 不会自动在录制内容中包含任何自定义数据。而是允许您在录制期间向 ARCore 帧添加自定义数据,并在播放期间从该帧检索相同的数据。您需要对应用进行编程,确保用户在回放会话时能够获得预期的数据。
自定义数据的使用场景
向录制内容添加自定义数据可增加 AR 应用的可能性。以下是一些具体用例。
随时随地使用 AR
过去,用户只能在合适的地点和时间获享 AR 体验。如果他们想在客厅放置 AR 灯具,必须亲身站在拍摄地看看 AR 灯具在那里的效果。借助自定义曲目,他们可以在客厅只录制一次视频,然后就可以随时为场景添加虚拟家具。
共同打造 AR 体验
由于没有现场会话要求,用户就拥有更多 AR 编辑选项,因而能够随时随地创建和访问独特的 AR 内容。例如,他们可以录制特定环境、添加增强现实效果并与好友分享。
前提条件
确保您了解 AR 基础概念
以及如何在继续之前配置 ARCore 现场录像。
使用自定义数据录制
使用自定义数据创建时段录音。
使用自定义数据初始化记录
如需使用自定义数据初始化录音,请按以下步骤操作。如需开始、停止和检查录制会话,请参阅录制和播放 AR 会话。
- 获取
RecordingConfig
。
- 使用自定义 UUID 创建一个新的
Track
。所有自定义数据都将保存在此处。
- 将
Track
添加到您在会话配置期间创建的 RecordingConfig
。
Java
// Initialize a new track with a custom UUID.
// Make sure to save the UUID because it is the ID that you will use
// to get your data back during playback.
UUID trackUUID = UUID.fromString("de5ec7a4-09ec-4c48-b2c3-a98b66e71893"); // from UUID generator
Track track = new Track(session).setId(trackUUID);
// Add the Track to the recordingConfig.
// recordingConfig must already be configured.
recordingConfig.addTrack(track);
Kotlin
// Initialize a new track with a custom UUID.
// Make sure to save the UUID because it is the ID that you will use
// to get your data back during playback.
val trackUUID = UUID.fromString("de5ec7a4-09ec-4c48-b2c3-a98b66e71893") // from UUID generator
val track = Track(session).setId(trackUUID)
// Add the Track to the recordingConfig.
// recordingConfig must already be configured.
recordingConfig.addTrack(track)
所有新曲目均被视为单独的录音,每个录制的曲目占用自己的 UUID。
为方便日后识别,您可以使用其他数据来配置轨道,以描述现场录像。例如,您可以通过添加备注来说明您录制现场录像的位置和时间:“本次现场录像是在下午在商场录制的”,以此“标记”曲目。
Java
// Set additional data on this track.
// For example, describe where you recorded the session.
byte[] customTrackData = "airport".getBytes(StandardCharsets.UTF_8);
track.setMetadata(ByteBuffer.wrap(customTrackData));
Kotlin
// Set additional data on this track.
// For example, describe where you recorded the session.
val customTrackData: ByteArray = "airport".toByteArray()
track.setMetadata(ByteBuffer.wrap(customTrackData))
如果您的应用需要与外部工具兼容,您可以使用 MIME 类型配置轨道,描述轨道中记录的数据类型。如果您未指定类型,数据将归类为 application/text
。读取数据时,ARCore 会忽略 MIME 类型。
Java
// Set a MIME type for compatibility with external tools.
track.setMimeType("text/csv");
Kotlin
// Set a MIME type for compatibility with external tools.
track.setMimeType("text/csv")
录制自定义数据轨道
所有自定义轨道数据都会记录到 Frame
上。AR 会话使用 session.update()
获取帧。将数据录制到帧上的时间与播放过程中返回数据的时间相同。例如,如果您调用 recordTrackData()
并将值“A”设为 00:07:02
,则播放曲目时,您将在 00:07:02
标记处得到“A”。
如需录制自定义数据轨道,请将数据转换为 ByteBuffer
并调用 recordTrackData()
。
Java
// Place an AR lamp in a room.
if (placeLampButtonWasPressed) {
Lamp lampProduct = Lamp.FLOOR; // a floor lamp
// Convert the lamp data into a byte array.
ByteBuffer lampData = ByteBuffer.wrap(new byte[] {(byte) lampProduct.ordinal()});
frame.recordTrackData(trackUUID, lampData);
}
Kotlin
// Place an AR lamp in a room.
if (placeLampButtonWasPressed) {
val lampProduct = Lamp.FLOOR // a floor lamp
// Convert the lamp data into a byte array.
val lampData = ByteBuffer.wrap(byteArrayOf(lampProduct.ordinal.toByte()))
frame.recordTrackData(trackUUID, lampData)
}
播放自定义数据轨道
在播放期间从现场录像中提取自定义数据。
初始化播放
使用自定义数据初始化播放与初始化常规时段录音的播放相同。
返回自定义数据
调用 getUpdatedTrackData()
可检索在帧上记录的自定义数据。您可以从同一帧中检索多个轨道数据。例如,如果您在录制期间对同一帧调用了两次 recordTrackData()
,则会在播放期间返回两个 TrackData
实例。
Java
// Fetch the data recorded on a select frame and place it in a container object.
Collection<TrackData> trackDataList = frame.getUpdatedTrackData(trackUUID);
Kotlin
// Fetch the data recorded on a select frame and place it in a container object.
val trackDataList: Collection<TrackData> = frame.getUpdatedTrackData(trackUUID)
TrackData
位于容器对象中后,提取自定义数据的字节。
Java
// Extract the bytes of custom data from the list of track data.
for (TrackData trackData : trackDataList) {
ByteBuffer bytes = trackData.getData();
Lamp lamp = Lamp.values()[bytes.get()]; // this is the lamp!
}
Kotlin
// Extract the bytes of custom data from the list of track data.
for (trackData in trackDataList) {
val bytes = trackData.data
val lamp = Lamp.values()[bytes.get().toInt()] // this is the lamp!
}
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe Recording & Playback API lets you record AR sessions, including video, sensor, and custom data, for later use, replacing the need for a live camera feed during playback.\u003c/p\u003e\n"],["\u003cp\u003eCustom data enhances AR experiences, enabling on-the-go AR usage and collaborative AR content creation by recording environments and adding virtual elements.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers need to understand fundamental AR concepts and session configuration before utilizing the API to add, record, and playback custom data tracks within ARCore frames.\u003c/p\u003e\n"],["\u003cp\u003eCustom data is added to recordings using tracks identified by unique UUIDs and recorded onto frames, allowing data retrieval during playback at the corresponding frame timestamps.\u003c/p\u003e\n"],["\u003cp\u003eDuring playback, developers can extract custom data from frames using the track UUID and process it to recreate the AR experience as intended during recording.\u003c/p\u003e\n"]]],[],null,["# Add custom data while recording on Android\n\nThe Recording \\& Playback API allows you to record a session and use it in place of a real-time camera feed. However, these recordings only contain video and sensor data. You can also add custom data to a session recording and have the data returned to you during playback, as though it is part of a camera image.\n\nARCore does not automatically include any custom data in recordings. Rather, it allows you to add custom data to an ARCore frame during recording, and retrieve that same data from the frame during playback. **It is up to you to program the app in such a way that the user will get back the data they expect when they play their session back.**\n\nUse cases for custom data\n-------------------------\n\nAdding custom data to recordings expands the possibilities for your AR apps. The following are some specific use cases.\n\n### Use AR on the go\n\nIn the past, users could only access AR experiences at the right place and the right time. If they wanted to place an AR lamp in their living room, they had to physically stand at the location to see how the lamp may look there. With custom tracks, they can record their living room once and add virtual furniture to the scene whenever they feel like it.\n\n### Co-create AR experiences\n\nWithout a live session requirement, users have many more options for AR editing, allowing them to create and access unique AR content at any place and time. For example, they can record a given environment, add augmented-reality effects, and share them with friends.\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\nRecord with custom data\n-----------------------\n\nCreate a session recording with custom data.\n\n### Initialize a recording with custom data\n\nFollow these steps to initialize a recording with custom data. To start, stop, and check a recording session, please see [Record and play back an AR session](/ar/develop/java/recording-and-playback/developer-guide).\n\n1. Obtain a [`RecordingConfig`](/ar/reference/java/com/google/ar/core/RecordingConfig).\n2. Create a new [`Track`](/ar/reference/java/com/google/ar/core/Track) with a [custom UUID](https://developer.android.com/reference/java/util/UUID). All custom data will be saved here.\n3. Add the [`Track`](/ar/reference/java/com/google/ar/core/Track) to the [`RecordingConfig`](/ar/develop/java/recording-and-playback/developer-guide#record_an_arcore_session) that you created during session configuration.\n\n### Java\n\n```java\n// Initialize a new track with a custom UUID.\n// Make sure to save the UUID because it is the ID that you will use\n// to get your data back during playback.\nUUID trackUUID = UUID.fromString(\"de5ec7a4-09ec-4c48-b2c3-a98b66e71893\"); // from UUID generator\nTrack track = new Track(session).setId(trackUUID);\n\n// Add the Track to the recordingConfig.\n// recordingConfig must already be configured.\nrecordingConfig.addTrack(track);\n```\n\n### Kotlin\n\n```kotlin\n// Initialize a new track with a custom UUID.\n// Make sure to save the UUID because it is the ID that you will use\n// to get your data back during playback.\nval trackUUID = UUID.fromString(\"de5ec7a4-09ec-4c48-b2c3-a98b66e71893\") // from UUID generator\nval track = Track(session).setId(trackUUID)\n\n// Add the Track to the recordingConfig.\n// recordingConfig must already be configured.\nrecordingConfig.addTrack(track)\n```\n\nAll new tracks are treated as separate recordings, with each recorded track occupying its own UUID.\n\n### Optional: Configure the track with additional data\n\nIn the case that you want to identify it later, you can configure a track with additional data that describes the session recording. For example, you can \"tag\" a track by adding a note that describes the location and time at which you recorded the session: \"This session was recorded at the mall in the afternoon.\" \n\n### Java\n\n```java\n// Set additional data on this track.\n// For example, describe where you recorded the session.\nbyte[] customTrackData = \"airport\".getBytes(StandardCharsets.UTF_8);\ntrack.setMetadata(ByteBuffer.wrap(customTrackData));\n```\n\n### Kotlin\n\n```kotlin\n// Set additional data on this track.\n// For example, describe where you recorded the session.\nval customTrackData: ByteArray = \"airport\".toByteArray()\ntrack.setMetadata(ByteBuffer.wrap(customTrackData))\n```\n\n### Optional: Configure the track with a MIME type\n\nIf your app needs to be compatible with external tools, you can configure a track with a [MIME type](https://en.wikipedia.org/wiki/Media_type) that describes the type of data recorded in the track. If you do not specify a type, the data will be categorized as `application/text`. ARCore ignores the MIME type when reading data. \n\n### Java\n\n```java\n// Set a MIME type for compatibility with external tools.\ntrack.setMimeType(\"text/csv\");\n```\n\n### Kotlin\n\n```kotlin\n// Set a MIME type for compatibility with external tools.\ntrack.setMimeType(\"text/csv\")\n```\n\n### Record custom data tracks\n\nAll custom track data is recorded onto [`Frame`](/ar/reference/java/com/google/ar/core/Frame)s. AR sessions use [`session.update()`](/ar/reference/java/com/google/ar/core/Session#update-) to get a frame. The time at which you record data onto a frame is the same time at which the data will be returned during playback. For example, if you call [`recordTrackData()`](/ar/reference/java/com/google/ar/core/Frame#recordTrackData-trackId-sample) with the value \"A\" at `00:07:02`, you'll get \"A\" back at the `00:07:02` mark when the track is played back.\n\nTo record a custom data track, convert the data into a [`ByteBuffer`](https://developer.android.com/reference/java/nio/ByteBuffer) and call [`recordTrackData()`](/ar/reference/java/com/google/ar/core/Frame#recordTrackData-trackId-sample). \n\n### Java\n\n```java\n// Place an AR lamp in a room.\nif (placeLampButtonWasPressed) {\n Lamp lampProduct = Lamp.FLOOR; // a floor lamp\n // Convert the lamp data into a byte array.\n ByteBuffer lampData = ByteBuffer.wrap(new byte[] {(byte) lampProduct.ordinal()});\n frame.recordTrackData(trackUUID, lampData);\n}\n```\n\n### Kotlin\n\n```kotlin\n// Place an AR lamp in a room.\nif (placeLampButtonWasPressed) {\n val lampProduct = Lamp.FLOOR // a floor lamp\n // Convert the lamp data into a byte array.\n val lampData = ByteBuffer.wrap(byteArrayOf(lampProduct.ordinal.toByte()))\n frame.recordTrackData(trackUUID, lampData)\n}\n```\n\nPlay back custom data tracks\n----------------------------\n\nExtract custom data from a session recording during playback.\n\n### Initialize a playback\n\nInitializing a playback with custom data is the same as [initializing a playback of a regular session recording](/ar/develop/java/recording-and-playback/developer-guide#play_back_a_previously_recorded_session).\n\n### Return custom data\n\nCall [`getUpdatedTrackData()`](/ar/reference/java/com/google/ar/core/Frame#getUpdatedTrackData-trackUuid) to retrieve the custom data recorded on a frame. It is possible to retrieve multiple track data from the same frame. For example, if you called [`recordTrackData()`](/ar/reference/java/com/google/ar/core/Frame#recordTrackData-trackId-sample) two times on the same frame during recording, you will get back two instances of [`TrackData`](/ar/reference/java/com/google/ar/core/TrackData) during playback. \n\n### Java\n\n```java\n// Fetch the data recorded on a select frame and place it in a container object.\nCollection\u003cTrackData\u003e trackDataList = frame.getUpdatedTrackData(trackUUID);\n```\n\n### Kotlin\n\n```kotlin\n// Fetch the data recorded on a select frame and place it in a container object.\nval trackDataList: Collection\u003cTrackData\u003e = frame.getUpdatedTrackData(trackUUID)\n```\n\nOnce the [`TrackData`](/ar/reference/java/com/google/ar/core/TrackData) is in a container object, extract the bytes of custom data. \n\n### Java\n\n```java\n// Extract the bytes of custom data from the list of track data.\nfor (TrackData trackData : trackDataList) {\n ByteBuffer bytes = trackData.getData();\n Lamp lamp = Lamp.values()[bytes.get()]; // this is the lamp!\n}\n```\n\n### Kotlin\n\n```kotlin\n// Extract the bytes of custom data from the list of track data.\nfor (trackData in trackDataList) {\n val bytes = trackData.data\n val lamp = Lamp.values()[bytes.get().toInt()] // this is the lamp!\n}\n```\n\nWhat's next\n-----------\n\n- Learn how to build your own app with Recording \\& Playback by going through the [Recording \\& Playback codelab](https://codelabs.developers.google.com/codelabs/arcore-record-and-playback-intro#0)."]]