באמצעות Recording & Playback API אפשר להקליט סרטונים ונתוני AR פעם אחת בסביבה נתונה, ולהשתמש בתוכן הזה כדי להחליף סשן מצלמה בשידור חי.
דרישות מוקדמות
לפני שממשיכים, חשוב להבין את המושגים הבסיסיים של AR ואת הדרך להגדיר סשן ARCore.
תאימות לממשקי API אחרים של ARCore
בגלל אופן העיבוד של נתוני סשנים, ממשקי ARCore API עשויים להפיק תוצאות שונות במהלך ההפעלה מאלה שנצפו במהלך ההקלטה. ייתכן שהן גם יפיקו תוצאות שונות בסשנים הבאים של ההפעלה. לדוגמה: יכול להיות שיהיו הבדלים במהלך ההפעלה: מספר הפריטים למעקב שזוהו, התזמון המדויק של הזיהוי והתנוחה שלהם לאורך זמן.
תאימות למצלמה משותפת
אפשר להקליט סשנים שמשתמשים במצלמה משותפת. עם זאת, הפעלתה בסשנים האלה באמצעות מצב 'מצלמה משותפת' לא זמינה כרגע.
תאימות לעוגנים בענן
אפשר לארח מודעות עוגן בענן ולפתור אותן בזמן הקלטה או הפעלה של סשן.
מתבצע תיעוד
התחלה, עצירה ובדיקת הסטטוס של הקלטת סשן ב-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.
}
מה השלב הבא?
- כך מוסיפים נתונים בהתאמה אישית לסשנים מוקלטים.
- מומלץ לנסות את קורס ה-codelab Introduction to the ARCore Recording and Playback API.