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

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

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

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

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

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

การใช้งานร่วมกับกล้องที่ใช้ร่วมกัน

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

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

คุณสามารถโฮสต์และแก้ไข 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.
}

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