1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 3 Copyright 2014 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 --> 17 18 <sample> 19 <name>Camera2Video</name> 20 <group>Media</group> 21 <package>com.example.android.camera2video</package> 22 23 <minSdk>21</minSdk> 24 25 <strings> 26 <intro> 27 <![CDATA[ 28 This sample demonstrates how to record video using Camera2 API. 29 ]]> 30 </intro> 31 </strings> 32 33 <template src="base"/> 34 35 <metadata> 36 <status>PUBLISHED</status> 37 <categories>Media</categories> 38 <technologies>Android</technologies> 39 <languages>Java</languages> 40 <solutions>Mobile</solutions> 41 <level>ADVANCED</level> 42 <icon>screenshots/icon-web.png</icon> 43 <screenshots> 44 <img>screenshots/1-launch.png</img> 45 <img>screenshots/2-record.png</img> 46 <img>screenshots/3-save.png</img> 47 </screenshots> 48 <api_refs> 49 <android>android.hardware.camera2.CameraCaptureSession</android> 50 <android>android.hardware.camera2.CameraDevice</android> 51 <android>android.view.TextureView</android> 52 </api_refs> 53 <description> 54 <![CDATA[ 55 This sample shows how to record video using the new Camera2 API in Android Lollipop. 56 ]]> 57 </description> 58 59 <intro> 60 <![CDATA[ 61 Android Lollipop introduced a new camera API, called camera2. This sample uses [CameraDevice][1] 62 and [CameraCaptureSession][2] to record video. It also uses a custom [TextureView][3] to render the output. 63 64 The main steps are: 65 66 1. Create a custom TextureView class and add it to the layout. The purpose of the custom TextureView is 67 to be able to draw itself according to an aspect ratio, which is set via a public method. Additionally, 68 the `onMeasure(int widthMeasureSpec, int heightMeasureSpec)` method is overridden, using the aspect ratio. 69 2. Implement a `TextureView.SurfaceTextureListener` on your TextureView, and override its 70 `onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height)` method to calculate 71 the matrix to apply to the TextureView so the camera output fits. Use the method `setTransform(matrix)` on 72 the TextureView. 73 3. Implement a [`CameraDevice.StateCallback`][4] to receive events about changes of the state of the 74 camera device. Override its methods to set your CameraDevice instance, start the preview, and stop 75 and release the camera. 76 4. When starting the preview, set up the MediaRecorder to accept video format. 77 5. Then, set up a [`CaptureRequest.Builder`][5] using `createCaptureRequest(CameraDevice.TEMPLATE_RECORD)` 78 on your CameraDevice instance. 79 6. Then, implement a [`CameraCaptureSession.StateCallback`][6], using the method 80 `createCaptureSession(surfaces, new CameraCaptureSession.StateCallback(){})` on your CameraDevice instance, 81 where `surfaces` is a list consisting of the surface view of your TextureView and the surface of 82 your MediaRecorder instance. 83 7. Use `start()` and `stop()` methods on your MediaRecorder instance to actually start and stop the recording. 84 8. Lastly, set up and clean up your camera device in `onResume()` and `onPause()`. 85 86 87 [1]: https://developer.android.com/reference/android/hardware/camera2/CameraDevice.html 88 [2]: http://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.html 89 [3]: http://developer.android.com/reference/android/view/TextureView.html 90 [4]: https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback.html 91 [5]: http://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Builder.html 92 [6]: http://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.StateCallback.html 93 ]]> 94 </intro> 95 </metadata> 96 97 </sample> 98