錄音與Playback API 可讓你錄製會議過程,並用來取代即時攝影機畫面。但這些錄音內容只包含影片和感應器資料。您也可將自訂資料新增至會話錄製中,並在播放期間將資料傳回給您,就像是相機影像的一部分。
ARCore 不會自動在錄製內容中加入任何自訂資料。相反地,此元件可在錄製時將自訂資料新增至 ARCore 影格,並在播放過程中從影格擷取相同的資料。您可以自行設計應用程式的程式,讓使用者能在繼續播放工作階段時取回所需資料。
自訂資料的用途
在錄製中加入自訂資料,為 AR 應用程式增添更多可能。以下是一些特定用途。
隨時隨地使用 AR
以往使用者只能在適當的位置使用 AR 功能。如果想將 AR 燈具放在客廳裡,他們必須實際站在那裡,看看燈具在那裡可能呈現的樣子。有了客製化軌道,他們只要錄製一次客廳內容,就能隨時將虛擬家具加到場景中。
共同打造 AR 體驗
如果沒有即時工作階段需求,使用者還能使用更多 AR 編輯功能,隨時隨地製作並存取獨特的 AR 內容。例如錄製指定環境、加入擴增實境效果,並與好友分享。
必要條件
請務必瞭解基本 AR 概念 以及如何在繼續操作前設定 ARCore 工作階段。
使用自訂資料記錄
建立含有自訂資料的工作階段錄製內容。
使用自訂資料初始化錄製內容
請按照下列步驟,使用自訂資料初始化錄製。如要開始、停止及查看錄製工作階段,請參閱「錄製及播放 AR 工作階段」。
- 取得
RecordingConfig
。 - 使用自訂 UUID 建立新的
Track
。所有自訂資料都會儲存在這裡。 - 將
Track
新增至您在工作階段設定期間建立的RecordingConfig
。
// 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);
// 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。
選用:使用其他資料設定測試群組
為方便日後識別,您可以使用描述工作階段記錄的其他資料來設定測試群組。舉例來說,您可以新增附註,說明活動錄製地點和時間,藉此「標記」曲目:「下午在購物中心錄製這場活動」。
// 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));
// 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 類型設定音軌
如果您的應用程式需要與外部工具相容,您可以使用 MIME 類型設定測試群組,說明測試群組中記錄的資料類型。如果您未指定類型,系統會將資料歸類為 application/text
。ARCore 會在讀取資料時忽略 MIME 類型。
// Set a MIME type for compatibility with external tools.
track.setMimeType("text/csv");
// Set a MIME type for compatibility with external tools.
track.setMimeType("text/csv")
記錄自訂數據軌
所有自訂追蹤資料都會記錄到 Frame
AR 工作階段會使用 session.update()
取得影格。將資料記錄在影格中的時間與播放期間資料傳回的時間相同。舉例來說,如果您在 00:07:02
呼叫 recordTrackData()
並傳回值「A」,則在播放音軌時,您會在 00:07:02
標記處收到「A」。
如要記錄自訂資料軌,請將資料轉換為 ByteBuffer
並呼叫 recordTrackData()
。
// 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);
}
// 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
例項。
// Fetch the data recorded on a select frame and place it in a container object.
Collection<TrackData> trackDataList = frame.getUpdatedTrackData(trackUUID);
// Fetch the data recorded on a select frame and place it in a container object.
val trackDataList: Collection<TrackData> = frame.getUpdatedTrackData(trackUUID)
一旦 TrackData
位於容器物件中,請擷取自訂資料的位元組。
// 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!
}
// 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!
}
後續步驟
- 請參閱錄製與播放程式碼研究室,瞭解如何使用錄製與播放功能建構自己的應用程式。