הקלטה והפעלה של סשן AR ב-Android

ממשק ה-API להקלטה ולהפעלה מאפשר להקליט נתוני וידאו ו-AR פעם אחת בסביבה נתונה, ולהשתמש בתוכן הזה כדי להחליף סשן מצלמה בשידור חי.

דרישות מוקדמות

לפני שממשיכים, חשוב לוודא שאתם מבינים את המושגים הבסיסיים של AR ואת האופן שבו מגדירים סשן של ARCore.

תאימות לממשקי API אחרים של ARCore

בגלל אופן העיבוד של נתוני סשנים, ממשקי ה-API של ARCore עשויים להניב תוצאות שונות במהלך ההפעלה מאלו שתועדו במהלך ההקלטה. הם עשויים גם להניב תוצאות שונות בהפעלות הבאות של ההפעלה. לדוגמה, מספר קובצי המעקב שזוהו, התזמון המדויק של הזיהוי שלהם ותנוחותיהם לאורך זמן עשויים להיות שונים במהלך ההפעלה.

תאימות למצלמה משותפת

ניתן להקליט סשנים שנעשה בהם שימוש במצלמה משותפת. עם זאת, כרגע אי אפשר להפעיל את השידורים האלה באמצעות מצב 'מצלמה משותפת'.

תאימות לעוגנים בענן

אפשר לארח ולטפל במודעות עוגן בענן בזמן הקלטה או הפעלה של סשן.

מתבצע תיעוד

להתחיל, להפסיק ולבדוק את הסטטוס של הקלטה של סשן ARCore.

הקלטה של סשן ב-ARCore

כדי להקליט סשן ARCore, צריך להגדיר את הסשן ולספק MP4 URI להקלטה. יש להתקשר למספר 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.
}

מה השלב הבא?