บันทึกและเล่นเซสชัน AR ใน Android

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

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

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

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

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

ความเข้ากันได้กับกล้องที่แชร์

เซสชันที่ใช้กล้องที่แชร์จะบันทึกได้ แต่การเล่นเซสชันเหล่านี้โดยใช้โหมดกล้องที่แชร์ยังไม่พร้อมใช้งานในขณะนี้

ความเข้ากันได้กับ Cloud Anchor

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

กำลังบันทึก

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

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

หากต้องการบันทึกเซสชัน ARCore ให้กำหนดค่าเซสชันและระบุ URI ของ MP4 สำหรับการบันทึก โทรหา session.startRecording() ก่อนการโทรครั้งแรกไปยัง session.resume() การบันทึกจะเริ่มต้นโดยอัตโนมัติเมื่อเซสชันกลับมาทำงานอีกครั้ง หากต้องการหยุดบันทึกโดยอัตโนมัติเมื่อหยุดเซสชันชั่วคราว ให้โทรหา 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.setPlaybackDatasetUri() ก่อนการโทรไปยัง session.resume() ครั้งแรก

เมื่อเริ่มเล่นเนื่องจากมีการเรียกใช้ 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.
}

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