Sceneform SceneView 的视频录制
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本开发者指南将引导您完成相关步骤,以便让您的应用将 Sceneform SceneView
录制到本地视频文件中。它使用 VideoRecorder
类中提供的功能,该类是从 1.6.0 版 Sceneform SDK for Android 开始的 VideoRecording 示例示例中提供的。
构建和运行示例应用
如需构建并运行 VideoRecording 示例应用,请执行以下操作:
- 确保您在 Android Studio 中有一个 Sceneform 项目,并且您的 Android 设备已通过 USB 连接到开发机器。如需了解详细步骤,请参阅快速入门。
- 将 VideoRecording 示例导入您的项目。
- 在 Android Studio 中,点击 Run 图标
。然后,选择您的设备作为部署目标,并点击确定以在设备上启动示例应用。
- 当您移动设备并将 3D 对象放入周围空间时,请点击“录制”按钮以开始录制,点击“停止”按钮停止录制。
您可以通过设备上的相机胶卷、名为 Sceneform
的影集或路径中访问录制的视频:
/sdcard/Pictures/Sceneform/Sample<hex characters>.mp4
如需让应用录制 Sceneform 场景,需要:
- 请求应用权限
- 初始化录像机
- 开始和停止录制视频
1. 请求应用权限
为了能够将视频文件写入本地存储空间,您的应用必须向 AndroidManifest.xml
添加以下行来请求 WRITE_EXTERNAL_STORAGE
权限:
<application>
…
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2. 初始化录像机
VideoRecording 示例包含一个名为 VideoRecorder
的类,该类封装了使用 MediaRecorder 从 SceneView
对象捕获视频以创建视频所需的所有设置逻辑。
如需使用视频录制器,请在 Activity(例如 onCreate()
)中初始化 VideoRecorder 类。
// 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)
。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2022-09-26。
[null,null,["最后更新时间 (UTC):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))."]]