Home | History | Annotate | Download | only in Camera2Video
      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     <dependency>com.android.support:appcompat-v7:23.0.0</dependency>
     24     <minSdk>21</minSdk>
     25 
     26     <strings>
     27         <intro>
     28             <![CDATA[
     29             This sample demonstrates how to record video using Camera2 API.
     30             ]]>
     31         </intro>
     32     </strings>
     33 
     34     <template src="base"/>
     35 
     36     <metadata>
     37         <status>PUBLISHED</status>
     38         <categories>Media</categories>
     39         <technologies>Android</technologies>
     40         <languages>Java</languages>
     41         <solutions>Mobile</solutions>
     42         <level>ADVANCED</level>
     43         <icon>screenshots/icon-web.png</icon>
     44         <screenshots>
     45             <img>screenshots/1-launch.png</img>
     46             <img>screenshots/2-record.png</img>
     47             <img>screenshots/3-save.png</img>
     48         </screenshots>
     49         <api_refs>
     50             <android>android.hardware.camera2.CameraCaptureSession</android>
     51             <android>android.hardware.camera2.CameraDevice</android>
     52             <android>android.view.TextureView</android>
     53         </api_refs>
     54         <description>
     55 <![CDATA[
     56 This sample shows how to record video using the new Camera2 API in Android Lollipop.
     57 ]]>
     58         </description>
     59 
     60         <intro>
     61 <![CDATA[
     62 Android Lollipop introduced a new camera API, called camera2. This sample uses [CameraDevice][1]
     63 and [CameraCaptureSession][2] to record video. It also uses a custom [TextureView][3] to render the output.
     64 
     65 The main steps are:
     66 
     67 1. Create a custom TextureView class and add it to the layout. The purpose of the custom TextureView is
     68 to be able to draw itself according to an aspect ratio, which is set via a public method. Additionally,
     69 the `onMeasure(int widthMeasureSpec, int heightMeasureSpec)` method is overridden, using the aspect ratio.
     70 2. Implement a `TextureView.SurfaceTextureListener` on your TextureView, and override its
     71 `onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height)` method to calculate
     72 the matrix to apply to the TextureView so the camera output fits. Use the method `setTransform(matrix)` on
     73 the TextureView.
     74 3. Implement a [`CameraDevice.StateCallback`][4] to receive events about changes of the state of the
     75 camera device. Override its methods to set your CameraDevice instance, start the preview, and stop
     76 and release the camera.
     77 4. When starting the preview, set up the MediaRecorder to accept video format.
     78 5. Then, set up a [`CaptureRequest.Builder`][5] using `createCaptureRequest(CameraDevice.TEMPLATE_RECORD)`
     79 on your CameraDevice instance.
     80 6. Then, implement a [`CameraCaptureSession.StateCallback`][6], using the method
     81 `createCaptureSession(surfaces, new CameraCaptureSession.StateCallback(){})` on your CameraDevice instance,
     82 where `surfaces` is a list consisting of the surface view of your TextureView and the surface of
     83 your MediaRecorder instance.
     84 7. Use `start()` and `stop()` methods on your MediaRecorder instance to actually start and stop the recording.
     85 8. Lastly, set up and clean up your camera device in `onResume()` and `onPause()`.
     86 
     87 
     88 [1]: https://developer.android.com/reference/android/hardware/camera2/CameraDevice.html
     89 [2]: http://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.html
     90 [3]: http://developer.android.com/reference/android/view/TextureView.html
     91 [4]: https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback.html
     92 [5]: http://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Builder.html
     93 [6]: http://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.StateCallback.html
     94 ]]>
     95         </intro>
     96     </metadata>
     97 
     98 </sample>
     99