• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Camera Recording
2
3Video recording is also an important function of the camera application. Video recording is the process of cyclic capturing of frames. To smooth videos, you can follow step 4 in [Camera Photographing](camera-shooting.md) to set the resolution, flash, focal length, photo quality, and rotation angle.
4
5## How to Develop
6
7Read [Camera](../reference/apis/js-apis-camera.md) for the API reference.
8
91. Import the media module. The [APIs](../reference/apis/js-apis-media.md) provided by this module are used to obtain the surface ID and create a photo output stream.
10
11   ```ts
12   import media from '@ohos.multimedia.media';
13   ```
14
152. Create a surface.
16
17   Call **createAVRecorder()** of the media module to create an **AVRecorder** instance, and call **getInputSurface()** of the instance to obtain the surface ID, which is associated with the view output stream to process the data output by the stream.
18
19   ```ts
20   let AVRecorder;
21   media.createAVRecorder((error, recorder) => {
22      if (recorder != null) {
23          AVRecorder = recorder;
24          console.info('createAVRecorder success');
25      } else {
26          console.info(`createAVRecorder fail, error:${error}`);
27      }
28   });
29   // For details about AVRecorderConfig, see the next section.
30   AVRecorder.prepare(AVRecorderConfig, (err) => {
31      if (err == null) {
32          console.log('prepare success');
33      } else {
34          console.log('prepare failed and error is ' + err.message);
35      }
36   })
37
38   let videoSurfaceId = null;
39   AVRecorder.getInputSurface().then((surfaceId) => {
40      console.info('getInputSurface success');
41      videoSurfaceId = surfaceId;
42   }).catch((err) => {
43      console.info('getInputSurface failed and catch error is ' + err.message);
44   });
45   ```
46
473. Create a video output stream.
48
49    Obtain the video output streams supported by the current device from **videoProfiles** in the **CameraOutputCapability** class. Then, define video recording parameters and use **createVideoOutput()** to create a video output stream.
50
51   ```ts
52   let videoProfilesArray = cameraOutputCapability.videoProfiles;
53   if (!videoProfilesArray) {
54       console.error("createOutput videoProfilesArray == null || undefined");
55   }
56
57   // Define video recording parameters.
58   let videoConfig = {
59       videoSourceType: media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
60       profile: {
61           fileFormat: media.ContainerFormatType.CFT_MPEG_4, // Video file encapsulation format. Only MP4 is supported.
62           videoBitrate: 100000, // Video bit rate.
63           videoCodec: media.CodecMimeType.VIDEO_MPEG4, // Video file encoding format. Both MPEG-4 and AVC are supported.
64           videoFrameWidth: 640, // Video frame width.
65           videoFrameHeight: 480, // Video frame height.
66           videoFrameRate: 30 // Video frame rate.
67       },
68       url: 'fd://35',
69       rotation: 0
70   }
71   // Create an AVRecorder instance.
72   let avRecorder;
73   media.createAVRecorder((error, recorder) => {
74     if (recorder != null) {
75         avRecorder = recorder;
76         console.info('createAVRecorder success');
77     } else {
78         console.info(`createAVRecorder fail, error:${error}`);
79     }
80    });
81   // Set video recording parameters.
82   avRecorder.prepare(videoConfig);
83   // Create a VideoOutput instance.
84   let videoOutput;
85   try {
86       videoOutput = cameraManager.createVideoOutput(videoProfilesArray[0], videoSurfaceId);
87   } catch (error) {
88       console.error('Failed to create the videoOutput instance. errorCode = ' + error.code);
89   }
90   ```
91
924. Start video recording.
93
94   Call **start()** of the **VideoOutput** instance to start the video output stream, and then call **start()** of the **AVRecorder** instance to start recording.
95
96   ```
97   videoOutput.start(async (err) => {
98       if (err) {
99           console.error('Failed to start the video output ${err.message}');
100           return;
101       }
102       console.info('Callback invoked to indicate the video output start success.');
103   });
104
105   avRecorder.start().then(() => {
106       console.info('avRecorder start success');
107   }
108   ```
109
1105. Stop video recording.
111
112   Call **stop()** of the **AVRecorder** instance to stop recording, and then call **stop()** of the **VideoOutput** instance to stop the video output stream.
113
114   ```ts
115   videoRecorder.stop().then(() => {
116       console.info('stop success');
117   }
118
119   videoOutput.stop((err) => {
120       if (err) {
121           console.error('Failed to stop the video output ${err.message}');
122           return;
123       }
124       console.info('Callback invoked to indicate the video output stop success.');
125   });
126   ```
127
128
129## Status Listening
130
131During camera application development, you can listen for the status of the video output stream, including recording start, recording end, and recording stream output errors.
132
133- Register the 'frameStart' event to listen for recording start events. This event can be registered when a **VideoOutput** object is created and is triggered when the bottom layer starts exposure for recording for the first time. Video recording is started as long as a result is returned.
134
135  ```ts
136  videoOutput.on('frameStart', () => {
137      console.info('Video frame started');
138  })
139  ```
140
141- Register the 'frameEnd' event to listen for recording end events. This event can be registered when a **VideoOutput** object is created and is triggered when the last frame of recording ends. Video recording ends as long as a result is returned.
142
143  ```ts
144  videoOutput.on('frameEnd', () => {
145      console.info('Video frame ended');
146  })
147  ```
148
149- Register the 'error' event to listen for video output errors. The callback function returns an error code when an API is incorrectly used. For details about the error code types, see [Camera Error Codes](../reference/apis/js-apis-camera.md#cameraerrorcode).
150
151  ```ts
152  videoOutput.on('error', (error) => {
153      console.info(`Video output error code: ${error.code}`);
154  })
155  ```
156