Sceneform SceneViews 的錄影
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本開發人員指南引導您完成這些步驟,以便讓應用程式將場景 SceneView
錄製到本機影片檔案中。其使用 VideoRecorder
類別提供的功能,該類別從 VideoRecording 範例 範例中開始,從 Android Sceneform SDK 1.6.0 版開始。
建構並執行範例應用程式
如要建構並執行 VideoRecording 範例應用程式:
- 確認 Android Studio 中有 Sceneform 專案,且您的 Android 裝置已透過 USB 連線至開發機器。如需詳細步驟,請參閱快速入門導覽課程。
- 將 VideoRecording 範例匯入專案。
- 在 Android Studio 中,按一下「執行」圖示
。然後選擇裝置做為部署目標,然後按一下「OK」 (啟動) 啟動範例應用程式。
- 移動裝置並將 3D 物件放在周圍的空間時,按一下「Record」按鈕即可開始錄製,點選「Stop」按鈕停止記錄。
你可以透過裝置相機膠卷、Sceneform
的相簿或路徑中存取錄製的影片:
/sdcard/Pictures/Sceneform/Sample<hex characters>.mp4
如要讓應用程式記錄場景場景,你必須:
- 要求應用程式權限
- 正在初始化錄影工具
- 開始及停止錄影
1. 要求應用程式權限
如要將影片檔案寫入本機儲存空間,應用程式必須在 AndroidManifest.xml
中新增下列程式碼,要求 WRITE_EXTERNAL_STORAGE
權限:
<application>
…
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2. 初始化錄音工具
VideoRecording Sample 包含名為 VideoRecorder
的類別,可封裝使用 MediaRecorder 從 SceneView
物件擷取影片所需的設定邏輯。
如要使用錄影工具,請在活動中初始化 VideoRecorder 類別,例如 onCreate()
。
// Create a new video recorder instance.
videoRecorder = new VideoRecorder();
// Specify the AR scene view to be recorded.
videoRecorder.setSceneView(arFragment.getArSceneView());
// Set video quality and recording orientation to match that of the device.
int orientation = getResources().getConfiguration().orientation;
videoRecorder.setVideoQuality(CamcorderProfile.QUALITY_2160P, orientation);
3. 建立影片錄製
如要開始錄製,請呼叫 onToggleRecord()
:
// Returns true if recording has started.
boolean recording = videoRecorder.onToggleRecord();
如要停止錄影,請再次呼叫 onToggleRecord()
:
// Returns false if recording has stopped.
boolean recording = videoRecorder.onToggleRecord();
如要擷取錄影的檔案路徑,請使用 getVideoPath()
:
// Determine absolute file path of video recording.
String videoPath = videoRecorder.getVideoPath().getAbsolutePath();
視需要使用 ADB 將錄製的檔案複製到開發機器中:
adb pull /sdcard/…/path/to/recorded/video.mp4 .
如要判斷圖片和影片的正確位置,以便讓相機正確顯示在相機膠卷中,VideoRecord
類別會使用 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2022-09-26 (世界標準時間)。
[null,null,["上次更新時間:2022-09-26 (世界標準時間)。"],[[["\u003cp\u003eThis guide explains how to record Sceneform \u003ccode\u003eSceneView\u003c/code\u003es to a local video file using the \u003ccode\u003eVideoRecorder\u003c/code\u003e class.\u003c/p\u003e\n"],["\u003cp\u003eYou can build and run the provided \u003cstrong\u003eVideoRecording Sample\u003c/strong\u003e app to experience the recording functionality.\u003c/p\u003e\n"],["\u003cp\u003eEnabling video recording in your app involves requesting permissions, initializing the \u003ccode\u003eVideoRecorder\u003c/code\u003e, and starting/stopping recording using \u003ccode\u003eonToggleRecord()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eRecorded videos are saved to the device's camera roll in a folder named \u003ccode\u003eSceneform\u003c/code\u003e and can be accessed via their file path.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eVideoRecorder\u003c/code\u003e class uses \u003ccode\u003eMediaRecorder\u003c/code\u003e to capture frames from the \u003ccode\u003eSceneView\u003c/code\u003e and save them as a video file.\u003c/p\u003e\n"]]],["To enable Sceneform video recording, first, request the `WRITE_EXTERNAL_STORAGE` permission in `AndroidManifest.xml`. Then, initialize the `VideoRecorder` class, setting the `SceneView` and video quality. Start recording by calling `onToggleRecord()`, and call it again to stop. Retrieve the video's file path with `getVideoPath()`. The video is saved in the device's camera roll within a `Sceneform` album or at `/sdcard/Pictures/Sceneform/`. You can also copy the video using adb pull.\n"],null,["# Video recording of Sceneform SceneViews\n\nThis developer guide takes you through the steps to enable your app to record\nSceneform [`SceneView`s](/sceneform/reference/com/google/ar/sceneform/SceneView) to a local video file. It uses functionality\navailable in the `VideoRecorder` class, which is available as part of the\n[VideoRecording Sample](//github.com/google-ar/sceneform-android-sdk/tree/v1.15.0/samples/videorecording)\nsample starting with version 1.6.0 of the Sceneform SDK for Android.\n\nBuild and run the sample app\n----------------------------\n\nTo build and run the **VideoRecording Sample** app:\n\n1. Make sure you have a Sceneform project in Android Studio, and that your Android device is connected to the development machine via USB. See the [quickstart](/sceneform/develop/android-quickstart) for detailed steps.\n2. Import the [VideoRecording Sample](//github.com/google-ar/sceneform-android-sdk/tree/v1.15.0/samples/videorecording) into your project.\n3. In Android Studio, click **Run** . Then, choose your device as the deployment target and click **OK** to launch the sample app on your device.\n4. As you move your device and place 3D objects in the space around you, click the Record button to begin recording, and the Stop button to stop recording.\n\nThe recorded video will be accessible via the camera roll on the device, in a\nphoto album named `Sceneform` or at the path: \n\n /sdcard/Pictures/Sceneform/Sample\u003chex characters\u003e.mp4\n\nOverview of enabling an app to support Sceneform video recording\n----------------------------------------------------------------\n\nEnabling your app to record Sceneform scenes requires:\n\n1. Requesting app permissions\n2. Initializing the video recorder\n3. Starting and stopping video recording\n\n### 1. Request app permissions\n\nIn order to be able to write the video file to local storage, you app must\nrequest the `WRITE_EXTERNAL_STORAGE` permission by adding the following line to\nyour `AndroidManifest.xml`: \n\n \u003capplication\u003e\n ...\n \u003c/application\u003e\n\n \u003cuses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"/\u003e\n\n### 2. Initialize the video recorder\n\nThe **VideoRecording Sample** includes a class named `VideoRecorder`, which\nencapsulates all the settings logic needed to use the\n[MediaRecorder](//developer.android.com/guide/topics/media/mediarecorder) to\ncapture frames from a `SceneView` object to create a video.\n\nTo use the video recorder, initialize the\n[VideoRecorder](//github.com/google-ar/sceneform-android-sdk/blob/v1.15.0/samples/videorecording/app/src/main/java/com/google/ar/sceneform/samples/videorecording/VideoRecorder.java)\nclass in your activity, for example in `onCreate()`. \n\n // Create a new video recorder instance.\n videoRecorder = new VideoRecorder();\n\n // Specify the AR scene view to be recorded.\n videoRecorder.setSceneView(arFragment.getArSceneView());\n\n // Set video quality and recording orientation to match that of the device.\n int orientation = getResources().getConfiguration().orientation;\n videoRecorder.setVideoQuality(CamcorderProfile.QUALITY_2160P, orientation);\n\n### 3. Create a video recording\n\n1. To begin recording, call `onToggleRecord()`:\n\n // Returns true if recording has started.\n boolean recording = videoRecorder.onToggleRecord();\n\n2. To stop recording, call `onToggleRecord()` a second time:\n\n // Returns false if recording has stopped.\n boolean recording = videoRecorder.onToggleRecord();\n\n3. To retrieve the file path of the video recording, use `getVideoPath()`:\n\n // Determine absolute file path of video recording.\n String videoPath = videoRecorder.getVideoPath().getAbsolutePath();\n\n4. Optionally, copy the recorded file to your development machine using adb:\n\n```\nadb pull /sdcard/…/path/to/recorded/video.mp4 .\n```\n\nTo determine the correct location for images and video so that they properly\nshow up on the camera roll, the `VideoRecord` class utilizes\n[`Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)`](//developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String))."]]