• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Video Recording Development
2
3## When to Use
4
5During video recording, audio and video signals are captured, encoded, and saved to files. You can specify parameters such as the encoding format, encapsulation format, and file path for video recording.
6
7**Figure 1** Video recording state transition
8
9![en-us_image_video_recorder_state_machine](figures/en-us_image_video_recorder_state_machine.png)
10
11
12
13**Figure 2** Layer 0 diagram of video recording
14
15![en-us_image_video_recorder_zero](figures/en-us_image_video_recorder_zero.png)
16
17## How to Develop
18
19For details about the APIs, see [VideoRecorder in the Media API](../reference/apis/js-apis-media.md).
20
21### Full-Process Scenario
22
23The full video recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources.
24
25```js
26import media from '@ohos.multimedia.media'
27import mediaLibrary from '@ohos.multimedia.mediaLibrary'
28export class VideoRecorderDemo {
29  private testFdNumber; // Used to save the FD address.
30  // pathName indicates the passed recording file name, for example, 01.mp4. The generated file address is /storage/media/100/local/files/Video/01.mp4.
31  // To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
32  async getFd(pathName) {
33    let displayName = pathName;
34    const mediaTest = mediaLibrary.getMediaLibrary();
35    let fileKeyObj = mediaLibrary.FileKey;
36    let mediaType = mediaLibrary.MediaType.VIDEO;
37    let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO);
38    let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath);
39    if (dataUri != undefined) {
40      let args = dataUri.id.toString();
41      let fetchOp = {
42        selections : fileKeyObj.ID + "=?",
43        selectionArgs : [args],
44      }
45      let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
46      let fileAsset = await fetchFileResult.getAllObject();
47      let fdNumber = await fileAsset[0].open('Rw');
48      this.testFdNumber = "fd://" + fdNumber.toString();
49    }
50  }
51
52  // Error callback triggered in the case of an error
53  failureCallback(error) {
54      console.info('error happened, error name is ' + error.name);
55      console.info('error happened, error code is ' + error.code);
56      console.info('error happened, error message is ' + error.message);
57  }
58
59  // Error callback triggered in the case of an exception
60  catchCallback(error) {
61      console.info('catch error happened, error name is ' + error.name);
62      console.info('catch error happened, error code is ' + error.code);
63      console.info('catch error happened, error message is ' + error.message);
64  }
65
66  async videoRecorderDemo() {
67    let videoRecorder = null; // videoRecorder is an empty object and assigned with a value after createVideoRecorder is successfully called.
68    let surfaceID = null; // Used to save the surface ID returned by getInputSurface.
69    // Obtain the FD address of the video to be recorded.
70    await this.getFd('01.mp4');
71    // Recording-related parameter settings
72    let videoProfile = {
73      audioBitrate : 48000,
74      audioChannels : 2,
75      audioCodec : 'audio/mp4a-latm',
76      audioSampleRate : 48000,
77      fileFormat : 'mp4',
78      videoBitrate : 48000,
79      videoCodec : 'video/mp4v-es',
80      videoFrameWidth : 640,
81      videoFrameHeight : 480,
82      videoFrameRate : 30
83    }
84
85    let videoConfig = {
86      audioSourceType : 1,
87      videoSourceType : 0,
88      profile : videoProfile,
89      url : this.testFdNumber, // testFdNumber is generated by getFd.
90      orientationHint : 0,
91      location : { latitude : 30, longitude : 130 },
92    }
93    // Create a VideoRecorder object.
94    await media.createVideoRecorder().then((recorder) => {
95      console.info('case createVideoRecorder called');
96      if (typeof (recorder) != 'undefined') {
97        videoRecorder = recorder;
98        console.info('createVideoRecorder success');
99      } else {
100        console.info('createVideoRecorder failed');
101      }
102    }, this.failureCallback).catch(this.catchCallback);
103
104    // Call the prepare API to prepare for video recording.
105    await videoRecorder.prepare(videoConfig).then(() => {
106      console.info('prepare success');
107    }, this.failureCallback).catch(this.catchCallback);
108
109    // Obtain the surface ID, save it, and pass it to camera-related APIs.
110    await videoRecorder.getInputSurface().then((surface) => {
111      console.info('getInputSurface success');
112      surfaceID = surface;
113    }, this.failureCallback).catch(this.catchCallback);
114
115    // Video recording depends on camera-related APIs. The following operations can be performed only after the video output start API is invoked. For details about how to call the camera APIs, see the samples.
116    // Start video recording.
117    await videoRecorder.start().then(() => {
118      console.info('start success');
119    }, this.failureCallback).catch(this.catchCallback);
120
121    // Pause video recording before the video output stop API of the camera is invoked.
122    await videoRecorder.pause().then(() => {
123      console.info('pause success');
124    }, this.failureCallback).catch(this.catchCallback);
125
126    // Resume video recording after the video output start API of the camera is invoked.
127    await videoRecorder.resume().then(() => {
128      console.info('resume success');
129    }, this.failureCallback).catch(this.catchCallback);
130
131    // Stop video recording after the video output stop API of the camera is invoked.
132    await videoRecorder.stop().then(() => {
133      console.info('stop success');
134    }, this.failureCallback).catch(this.catchCallback);
135
136    // Reset the recording configuration.
137    await videoRecorder.reset().then(() => {
138      console.info('reset success');
139    }, this.failureCallback).catch(this.catchCallback);
140
141    // Release the video recording resources and camera object resources.
142    await videoRecorder.release().then(() => {
143      console.info('release success');
144    }, this.failureCallback).catch(this.catchCallback);
145
146    // Set the related object to null.
147    videoRecorder = undefined;
148    surfaceID = undefined;
149  }
150}
151```
152