• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 录像
2
3录像也是相机应用的最重要功能之一,录像是循环帧的捕获。对于录像的流畅度,开发者可以参考[拍照](camera-shooting.md)中的步骤4,设置分辨率、闪光灯、焦距、照片质量及旋转角度等信息。
4
5## 开发步骤
6
7详细的API说明请参考[Camera API参考](../reference/apis/js-apis-camera.md)。
8
91. 导入media模块。创建拍照输出流的SurfaceId以及拍照输出的数据,都需要用到系统提供的[media接口](../reference/apis/js-apis-media.md)能力,导入media接口的方法如下。
10
11   ```ts
12   import media from '@ohos.multimedia.media';
13   ```
14
152. 创建Surface。
16
17   系统提供的media接口可以创建一个录像AVRecorder实例,通过该实例的getInputSurface方法获取SurfaceId,与录像输出流做关联,处理录像输出流输出的数据。
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   // AVRecorderConfig可参考下一章节
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. 创建录像输出流。
48
49    通过CameraOutputCapability类中的videoProfiles,可获取当前设备支持的录像输出流。然后,定义创建录像的参数,通过createVideoOutput方法创建录像输出流。
50
51   ```ts
52   let videoProfilesArray = cameraOutputCapability.videoProfiles;
53   if (!videoProfilesArray) {
54       console.error("createOutput videoProfilesArray == null || undefined");
55   }
56
57   // 创建视频录制的参数
58   let videoConfig = {
59       videoSourceType: media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
60       profile: {
61           fileFormat : media.ContainerFormatType.CFT_MPEG_4, // 视频文件封装格式,只支持MP4
62           videoBitrate : 100000, // 视频比特率
63           videoCodec : media.CodecMimeType.VIDEO_MPEG4, // 视频文件编码格式,支持mpeg4和avc两种格式
64           videoFrameWidth : 640,  // 视频分辨率的宽
65           videoFrameHeight : 480, // 视频分辨率的高
66           videoFrameRate : 30 // 视频帧率
67       },
68       url: 'fd://35',
69       rotation: 0
70   }
71   // 创建avRecorder
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   // 设置视频录制的参数
82   avRecorder.prepare(videoConfig);
83   // 创建VideoOutput对象
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. 开始录像。
93
94   先通过videoOutput的start方法启动录像输出流,再通过avRecorder的start方法开始录像。
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. 停止录像。
111
112   先通过avRecorder的stop方法停止录像,再通过videoOutput的stop方法停止录像输出流。
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## 状态监听
130
131在相机应用开发过程中,可以随时监听录像输出流状态,包括录像开始、录像结束、录像流输出的错误。
132
133- 通过注册固定的frameStart回调函数获取监听录像开始结果,videoOutput创建成功时即可监听,录像第一次曝光时触发,有该事件返回结果则认为录像开始。
134
135  ```ts
136  videoOutput.on('frameStart', () => {
137      console.info('Video frame started');
138  })
139  ```
140
141- 通过注册固定的frameEnd回调函数获取监听录像结束结果,videoOutput创建成功时即可监听,录像完成最后一帧时触发,有该事件返回结果则认为录像流已结束。
142
143  ```ts
144  videoOutput.on('frameEnd', () => {
145      console.info('Video frame ended');
146  })
147  ```
148
149- 通过注册固定的error回调函数获取监听录像输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见[CameraErrorCode](../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