Nagraj i odtwórz sesję AR w aplikacji AR Foundation kierowana na Androida

Interfejs Logging & Playback API umożliwia jednorazowe nagranie danych wideo i AR w danym środowisku i wykorzystanie ich zamiast sesji kamery na żywo.

Wymagania wstępne

Zanim przejdziesz dalej, upewnij się, że znasz podstawowe pojęcia związane z AR i wiesz, jak skonfigurować sesję ARCore.

Zgodność z innymi interfejsami API ARCore

Ze względu na sposób przetwarzania danych sesji interfejsy ARCore API mogą zwracać inne wyniki podczas odtwarzania niż zaobserwowane podczas nagrywania. Mogą one również przynosić inne wyniki podczas kolejnych sesji odtwarzania. Na przykład liczba wykrytych elementów do śledzenia, dokładny czas wykrycia i pozycje w czasie mogą się różnić podczas odtwarzania.

Zgodność z kotwicami w chmurze

Zakotwiczone w chmurze możesz hostować i rozwiązywać problemy podczas nagrywania lub odtwarzania sesji.

Rejestrowanie

Uruchamiaj, zatrzymuj i sprawdzaj stan nagrywania sesji ARCore.

Nagraj sesję ARCore

Aby nagrać sesję ARCore, skonfiguruj sesję i podaj identyfikator URI nagrania MP4. Przed wznowieniem sesji wywołaj ARRecordingManager.StartRecording(). Nagrywanie rozpocznie się automatycznie po wznowieniu sesji. Aby automatycznie zatrzymać nagrywanie po wstrzymaniu sesji, zadzwoń pod numer ARRecordingConfig.AutoStopOnPause. Aby nagrać częściową sesję, wywołaj ARRecordingManager.StartRecording() w trakcie jej trwania.

ARCoreRecordingConfig recordingConfig = ScriptableObject.CreateInstance<ARCoreRecordingConfig>();
Uri datasetUri = new System.Uri("file:///uri/for/dataset.mp4");
recordingConfig.Mp4DatasetUri = datasetUri.AbsoluteUri;

recordingManager.StartRecording(recordingConfig);

Zatrzymaj nagrywanie sesji

Aby zatrzymać nagrywanie bez wstrzymywania trwającej sesji AR, zadzwoń pod numer ARRecordingManager.StopRecording().

recordingManager.StopRecording();

Sprawdzanie stanu nagrywania

Za pomocą aplikacji ARRecordingManager.RecordingStatus w każdej chwili możesz sprawdzić bieżący stan nagrywania.

Debug.Log("Current Recording Status: " + recordingManager.RecordingStatus);

Odtwarzanie

Odtwarzanie nagranych wcześniej sesji AR. Sesje są odtwarzane w czasie rzeczywistym, a odtwarzanie i szybkość sesji nie mogą zostać zmienione.

Odtwarzanie wcześniej nagranej sesji

Aby odtworzyć wcześniej zarejestrowaną sesję, wywołaj metodę ARPlaybackManager.SetPlaybackDatasetUri() i podaj identyfikator URI zbioru danych, który chcesz odtworzyć. Aby użyć tej metody, musisz wstrzymać sesję. Wznów sesję, aby zastosować zmiany.

Po rozpoczęciu odtwarzania w związku z wznowieniem sesji wstrzymanie sesji przez wyłączenie zasady ARSession spowoduje wstrzymanie przetwarzania wszystkich klatek obrazu z kamery i innych zapisanych danych z czujnika znajdujących się w zbiorze danych. Odrzucone w ten sposób ramki na zdjęcia z aparatu i przetworzone dane z czujnika nie zostaną przetworzone ponownie po wznowieniu sesji przez jej wznowienie. Ogólnie rzecz biorąc, śledzenie AR podczas sesji nie będzie działać prawidłowo z powodu luk w przetwarzanych danych.

// 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;

Znany problem i sposób jego obejścia

Istnieje znany problem polegający na tym, że wywołania ARPlaybackManager.SetPlaybackDatasetUri() zwracają ErrorPlaybackFailed. Dzieje się tak, ponieważ wstrzymanie sesji może potrwać kilka klatek. Jeśli metoda ARPlaybackManager.SetPlaybackDatasetUri() zostanie wywołana przed wstrzymaniem sesji, nie uzyska do niej dostępu, więc zwróci błąd.

Aby obejść ten problem, możesz użyć poniższego kodu.

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

    ...
}

Zatrzymywanie odtwarzania

Aby zatrzymać odtwarzanie, wywołaj ARPlaybackManager.SetPlaybackDatasetUri() i ustaw identyfikator URI zbioru danych na 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;

Wznów odtwarzanie od początku

Aby ponownie uruchomić odtwarzanie od początku zbioru danych, wywołaj ARPlaybackManager.SetPlaybackDatasetUri() i przed wznowieniem sesji określ to samo nagranie 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;

Odtwarzanie innej sesji

Aby odtworzyć inny zbiór danych, wstrzymaj sesję i podaj nowy zbiór danych przed jej wznowieniem.

// 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;

Sprawdzanie stanu odtwarzania

Za pomocą ARPlaybackManager.PlaybackStatus w każdej chwili możesz określić bieżący stan odtwarzania.

Debug.Log("Current Playback Status: " + playbackManager.PlaybackStatus);

Co dalej?