Ghi và phát lại một phiên thực tế tăng cường trên Android

API ghi và phát cho phép bạn quay video và dữ liệu thực tế tăng cường một lần trong một môi trường cụ thể và sử dụng nội dung đó để thay thế một phiên quay camera trực tiếp.

Điều kiện tiên quyết

Hãy đảm bảo bạn hiểu rõ các khái niệm cơ bản về AR và cách định cấu hình một phiên ARCore trước khi tiếp tục.

Khả năng tương thích với các API ARCore khác

Do cách xử lý dữ liệu phiên, API ARCore có thể tạo ra kết quả khác trong quá trình phát so với quan sát được trong quá trình ghi. Chúng cũng có thể tạo ra các kết quả khác nhau trong các phiên phát tiếp theo. Ví dụ: số lượng thiết bị có thể theo dõi được phát hiện, thời gian chính xác phát hiện chúng và tư thế của chúng theo thời gian có thể khác nhau trong khi phát.

Khả năng tương thích với máy ảnh dùng chung

Bạn có thể ghi lại những phiên sử dụng Máy ảnh dùng chung. Tuy nhiên, hiện không thể phát các phiên này bằng chế độ Máy ảnh dùng chung.

Khả năng tương thích với Cloud Neo

Bạn có thể lưu trữ và phân giải Cloud Anchors (Neo trên đám mây) trong khi ghi hoặc phát lại một phiên.

Đang ghi

Bắt đầu, dừng và kiểm tra trạng thái ghi phiên ARCore.

Ghi lại phiên ARCore

Để ghi lại một phiên ARCore, hãy định cấu hình phiên đó và cung cấp URI MP4 để ghi lại. Hãy gọi session.startRecording() trước cuộc gọi đầu tiên đến session.resume(). Quá trình ghi sẽ tự động bắt đầu khi phiên tiếp tục. Để tự động dừng quay khi phiên bị tạm dừng, hãy gọi RecordingConfig.setAutoStopOnPause(). Để ghi lại một phần của phiên, hãy gọi session.startRecording() khi phiên đó đang chạy.

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()

Dừng ghi phiên

Để dừng ghi mà không tạm dừng phiên thực tế tăng cường hiện đang chạy, hãy gọi session.stopRecording().

Java

try {
  session.stopRecording();  // Stop recording.
} catch (RecordingFailedException e) {
  Log.e(TAG, "Failed to stop recording", e);
}

Kotlin

session.stopRecording()

Kiểm tra trạng thái ghi

Bạn có thể sử dụng session.getRecordingStatus() bất cứ lúc nào để xác định RecordingStatus hiện tại.

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.
}

Phát

Phát lại các phiên thực tế tăng cường đã ghi trước đó. Các phiên sẽ phát lại theo thời gian thực, và không thể điều chỉnh tốc độ hay cách phát lại phiên.

Phát lại phiên đã ghi lại trước đó

Để phát lại một phiên đã ghi lại trước đó, hãy gọi session.setPlaybackDatasetUri() trước lệnh gọi đầu tiên đến session.resume().

Khi quá trình phát đã bắt đầu do lệnh gọi đầu tiên đến session.resume(), việc tạm dừng phiên bằng cách gọi session.pause() sẽ tạm ngừng xử lý tất cả khung hình ảnh của máy ảnh và mọi dữ liệu cảm biến đã ghi khác trong tập dữ liệu. Khung hình ảnh của máy ảnh và dữ liệu khung cảm biến bị loại bỏ theo cách này sẽ không được xử lý lại khi phiên được tiếp tục bằng cách gọi session.resume(). Hoạt động theo dõi thực tế tăng cường cho phiên thường sẽ bị ảnh hưởng do khoảng trống trong dữ liệu được xử lý.

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()

Bắt đầu phát lại từ đầu

Để bắt đầu phát lại từ đầu tập dữ liệu, hãy tạm dừng phiên và gọi session.setPlaybackDatasetUri(), chỉ định cùng một bản ghi MP4 trước khi tiếp tục phiên.

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.

Phát lại một phiên khác

Để phát lại một tập dữ liệu khác, hãy tạm dừng phiên và chỉ định tập dữ liệu mới trước khi tiếp tục phiên.

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.

Kiểm tra trạng thái phát

Sử dụng session.getPlaybackStatus() bất cứ lúc nào để xác định PlaybackStatus hiện tại.

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.
}

Bước tiếp theo