צילום והפעלה של סשן AR ב-AR Foundation שמטרגט ל-Android

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

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

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

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

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

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

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

מתבצע תיעוד

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

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

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

מה השלב הבא?