บันทึกและเล่นเซสชัน AR บน AR Foundation ที่กำหนดเป้าหมายเป็น Android

Recording & Playback API ช่วยให้คุณบันทึกวิดีโอและข้อมูล AR ได้ครั้งเดียวภายในสภาพแวดล้อมหนึ่งๆ และใช้เนื้อหานั้นแทนเซสชันกล้องสด

ข้อกำหนดเบื้องต้น

โปรดทำความเข้าใจแนวคิดพื้นฐานเกี่ยวกับ AR และวิธีกำหนดค่าเซสชัน ARCore ก่อนดำเนินการต่อ

ความเข้ากันได้กับ ARCore API อื่นๆ

ARCore API อาจให้ผลลัพธ์ที่แตกต่างออกไประหว่างการเล่นกับที่สังเกตเห็นระหว่างการบันทึก เนื่องจากวิธีการประมวลผลข้อมูลเซสชัน และอาจให้ผลลัพธ์ที่แตกต่างกันในเซสชันการเล่นที่ตามมา ตัวอย่างเช่น จำนวนรายการที่ติดตามได้ เวลาที่ตรวจพบอย่างแม่นยำ และท่าทางของรายการเมื่อเวลาผ่านไปอาจแตกต่างกันไประหว่างการเล่น

ใช้งานร่วมกับ Cloud Anchor ได้

คุณสามารถโฮสต์และแก้ไขหมุด Cloud ขณะบันทึกหรือเล่นเซสชันซ้ำได้

กำลังบันทึก

เริ่ม หยุด และตรวจสอบสถานะการบันทึกเซสชัน ARCore

บันทึกเซสชัน ARCore

หากต้องการบันทึกเซสชัน ARCore ให้กําหนดค่าเซสชันและระบุ URI ของ MP4 สำหรับการบันทึก โทรหา ARRecordingManager.StartRecording() ก่อนกลับมาที่เซสชัน การบันทึกจะเริ่มขึ้นโดยอัตโนมัติเมื่อเซสชันกลับมาทำงานอีกครั้ง หากต้องการหยุดบันทึกโดยอัตโนมัติเมื่อหยุดเซสชันชั่วคราว ให้โทรหา ARRecordingConfig.AutoStopOnPause หากต้องการบันทึกเซสชันบางส่วน ให้กด ARRecordingManager.StartRecording() ขณะเซสชันทำงานอยู่

ARCoreRecordingConfig recordingConfig = ScriptableObject.CreateInstance<ARCoreRecordingConfig>();
Uri datasetUri = new System.Uri("file:///uri/for/dataset.mp4");
recordingConfig
.Mp4DatasetUri = datasetUri.AbsoluteUri;

recordingManager
.StartRecording(recordingConfig);

หยุดการบันทึกเซสชัน

หากต้องการหยุดบันทึกโดยไม่หยุดเซสชัน AR ที่ทำงานอยู่ชั่วคราว ให้เรียกใช้ ARRecordingManager.StopRecording()

recordingManager.StopRecording();

ตรวจสอบสถานะการบันทึก

คุณใช้ ARRecordingManager.RecordingStatus ได้ทุกเมื่อเพื่อดูสถานะการบันทึกปัจจุบัน

Debug.Log("Current Recording Status: " + recordingManager.RecordingStatus);

การเล่น

เล่นเซสชัน AR ที่บันทึกไว้ก่อนหน้านี้ เซสชันจะเล่นแบบเรียลไทม์ และไม่สามารถปรับความเร็วหรือการเล่นเซสชันได้

เล่นเซสชันที่บันทึกไว้ก่อนหน้านี้

หากต้องการเล่นเซสชันที่บันทึกไว้ก่อนหน้านี้ ให้เรียกใช้ ARPlaybackManager.SetPlaybackDatasetUri() และระบุ URI ของชุดข้อมูลที่ต้องการเล่น คุณต้องหยุดเซสชันชั่วคราวเพื่อใช้วิธีการนี้ ดำเนินเซสชันต่อเพื่อให้การเปลี่ยนแปลงมีผล

เมื่อเริ่มเล่นเนื่องจากมีการกลับมาทำงานต่อของเซสชัน การหยุดเซสชันชั่วคราวโดยการปิดใช้ARSessionจะระงับการประมวลผลเฟรมรูปภาพจากกล้องทั้งหมดและข้อมูลเซ็นเซอร์อื่นๆ ที่บันทึกไว้ในชุดข้อมูล ระบบจะไม่ประมวลผลเฟรมรูปภาพจากกล้องและข้อมูลเฟรมเซ็นเซอร์ที่ถูกทิ้งด้วยวิธีนี้อีกครั้งเมื่อเซสชันกลับมาทำงานอีกครั้งโดยกลับมาทำงานต่อ โดยปกติแล้ว การติดตาม AR สำหรับเซสชันจะได้รับผลกระทบเนื่องจากช่องว่างของข้อมูลที่ประมวลผล

// Disable the ARSession to pause the current AR session.
session
.enabled = false;

// In the next frame, provide a URI for the dataset you wish to play back.
Uri datasetUri = new System.Uri("file:///uri/for/dataset.mp4");
playbackManager
.SetPlaybackDatasetUri(datasetUri);

// In the frame after that, re-enable the ARSession to resume the session from
// the beginning of the dataset.
session
.enabled = true;

ปัญหาที่ทราบและวิธีแก้ปัญหาชั่วคราว

ปัญหาที่ทราบคือเมื่อเรียกใช้ ARPlaybackManager.SetPlaybackDatasetUri() ระบบจะแสดงผล ErrorPlaybackFailed ปัญหานี้เกิดขึ้นเนื่องจากเซสชันอาจใช้เวลาหลายเฟรมในการหยุดชั่วคราว หากเรียกใช้ ARPlaybackManager.SetPlaybackDatasetUri() ก่อนหยุดเซสชันไว้ชั่วคราว ระบบจะเข้าถึงเซสชันไม่ได้ จึงจะแสดงข้อผิดพลาด

คุณสามารถใช้โค้ดต่อไปนี้เป็นวิธีแก้ปัญหาชั่วคราวได้

// Workaround for known issue where `playbackManager.SetPlaybackDatasetUri()`
// returns `ErrorPlaybackFailed` because it can take several frames for a
// session to be paused.

// Reference to the ARSession component in the scene.
ARSession session;

void PlaybackDataset()
{
    setPlaybackDataset
= true;

   
// Pause the current AR session.
    session
.enabled = false;

   
// Set a timeout for retrying playback retrieval.
    timeout
= 10f;
}

// Next frame
void Update()
{
   
...

   
if (setPlaybackDataset)
   
{
       
PlaybackResult result = playbackManager.SetPlaybackDatasetUri(datasetUri);
       
if (result == PlaybackResult.ErrorPlaybackFailed || result == PlaybackResult.SessionNotReady)
       
{
           
// Try to set the dataset again in the next frame.
            timeout
-= Time.deltaTime;
       
}
       
else
       
{
           
// Do not set the timeout if the result is something other than ErrorPlaybackFailed.
            timeout
= -1f;
       
}

       
if (timeout < 0.0f)
       
{
            setPlaybackDataset
= false;
           
// If playback is successful, proceed as usual.
           
// If playback is not successful, handle the error appropriately.
       
}
   
}

   
...
}

หยุดเล่น

หากต้องการหยุดการเล่น ให้เรียกใช้ ARPlaybackManager.SetPlaybackDatasetUri() และตั้งค่า URI ของชุดข้อมูลเป็น null

// Disable the ARSession to pause the current AR session.
session
.enabled = false;

// In the next frame, unset the playback dataset URI.
playbackManager
.SetPlaybackDatasetUri(null);

// In the frame after that, re-enable the ARSession to resume the session using
// the device camera and other sensors.
session
.enabled = true;

เริ่มเล่นใหม่ตั้งแต่ต้น

หากต้องการเริ่มเล่นอีกครั้งจากจุดเริ่มต้นของชุดข้อมูล ให้เรียกใช้ ARPlaybackManager.SetPlaybackDatasetUri() และระบุไฟล์บันทึก MP4 เดียวกันก่อนกลับมาเล่นเซสชันต่อ

// Disable the ARSession to pause the current AR session.
session
.enabled = false;

// In the next frame, specify the same dataset URI.
playbackManager
.SetPlaybackDatasetUri(datasetUri); // Same URI that was previously set.

// In the frame after that, re-enable the ARSession to resume the session from
// the beginning of the dataset.
session
.enabled = true;

เล่นเซสชันอื่น

หากต้องการเล่นชุดข้อมูลอื่น ให้หยุดเซสชันชั่วคราวและระบุชุดข้อมูลใหม่ก่อนเล่นเซสชันต่อ

// Disable the ARSession to pause the current AR session.
session
.enabled = false;

// In the next frame, specify a new dataset URI.
Uri newDatasetUri = new System.Uri("file:///uri/for/different/dataset.mp4");
playbackManager
.SetPlaybackDatasetUri(newDatasetUri); // Different URI than was previously set.

// In the frame after that, re-enable the ARSession to resume the session from
// the beginning of the new dataset.
session
.enabled = true;

ตรวจสอบสถานะการเล่น

ARPlaybackManager.PlaybackStatus ใช้ได้ทุกเมื่อเพื่อระบุสถานะการเล่นปัจจุบัน

Debug.Log("Current Playback Status: " + playbackManager.PlaybackStatus);

สิ่งที่จะเกิดขึ้นหลังจากนี้