js-apis-media.md 101.7 KB
Newer Older
W
wusongqing 已提交
1 2
# Media

W
wusongqing 已提交
3
> **NOTE**
4
>
W
wusongqing 已提交
5 6
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.

W
wusongqing 已提交
7 8 9 10 11
The multimedia subsystem provides a set of simple and easy-to-use APIs for you to access the system and use media resources.

This subsystem offers various media services covering audio and video, which provide the following capabilities:

- Audio playback ([AudioPlayer](#audioplayer))
W
wusongqing 已提交
12
- Video playback ([VideoPlayer](#videoplayer8))
W
wusongqing 已提交
13
- Audio recording ([AudioRecorder](#audiorecorder))
W
wusongqing 已提交
14
- Video recording ([VideoRecorder](#videorecorder9))
W
wusongqing 已提交
15

W
wusongqing 已提交
16
The following capabilities will be provided in later versions: data source audio/video playback, audio/video encoding and decoding, container encapsulation and decapsulation, and media capability query.
Z
zengyawen 已提交
17 18 19

## Modules to Import

W
wusongqing 已提交
20
```js
Z
zengyawen 已提交
21 22 23
import media from '@ohos.multimedia.media';
```

W
wusongqing 已提交
24
##  media.createAudioPlayer
Z
zengyawen 已提交
25

W
wusongqing 已提交
26
createAudioPlayer(): [AudioPlayer](#audioplayer)
Z
zengyawen 已提交
27

W
wusongqing 已提交
28
Creates an **AudioPlayer** instance in synchronous mode.
Z
zengyawen 已提交
29

W
wusongqing 已提交
30 31
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

W
wusongqing 已提交
32
**Return value**
Z
zengyawen 已提交
33

W
wusongqing 已提交
34
| Type                       | Description                                                        |
Z
zengyawen 已提交
35
| --------------------------- | ------------------------------------------------------------ |
W
wusongqing 已提交
36 37 38 39 40
| [AudioPlayer](#audioplayer) | Returns the **AudioPlayer** instance if the operation is successful; returns **null** otherwise. After the instance is created, you can start, pause, or stop audio playback.|

**Example**

```js
W
wusongqing 已提交
41
let audioPlayer = media.createAudioPlayer();
W
wusongqing 已提交
42 43
```

W
wusongqing 已提交
44
## media.createVideoPlayer<sup>8+</sup>
W
wusongqing 已提交
45

W
wusongqing 已提交
46
createVideoPlayer(callback: AsyncCallback\<[VideoPlayer](#videoplayer8)>): void
W
wusongqing 已提交
47

G
Gloria 已提交
48
Creates a **VideoPlayer** instance. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
49 50

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
W
wusongqing 已提交
51 52 53

**Parameters**

W
wusongqing 已提交
54 55
| Name  | Type                                       | Mandatory| Description                          |
| -------- | ------------------------------------------- | ---- | ------------------------------ |
56
| callback | AsyncCallback<[VideoPlayer](#videoplayer8)> | Yes  | Callback used to return the result. If the operation is successful, the **VideoPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to manage and play video.|
Z
zengyawen 已提交
57 58 59

**Example**

W
wusongqing 已提交
60
```js
W
wusongqing 已提交
61 62 63
let videoPlayer

media.createVideoPlayer((error, video) => {
W
wusongqing 已提交
64
   if (video != null) {
W
wusongqing 已提交
65 66
       videoPlayer = video;
       console.info('video createVideoPlayer success');
W
wusongqing 已提交
67
   } else {
G
Gloria 已提交
68
       console.info(`video createVideoPlayer fail, error:${error}`);
W
wusongqing 已提交
69 70
   }
});
Z
zengyawen 已提交
71
```
W
wusongqing 已提交
72

W
wusongqing 已提交
73
## media.createVideoPlayer<sup>8+</sup>
W
wusongqing 已提交
74

W
wusongqing 已提交
75
createVideoPlayer(): Promise<[VideoPlayer](#videoplayer8)>
W
wusongqing 已提交
76

G
Gloria 已提交
77
Creates a **VideoPlayer** instance. This API uses a promise to return the result.
W
wusongqing 已提交
78 79

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
W
wusongqing 已提交
80

W
wusongqing 已提交
81
**Return value**
W
wusongqing 已提交
82

G
Gloria 已提交
83 84
| Type                                 | Description                                                        |
| ------------------------------------- | ------------------------------------------------------------ |
85
| Promise<[VideoPlayer](#videoplayer8)> | Promise used to return the result. If the operation is successful, the **VideoPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to manage and play video.|
W
wusongqing 已提交
86 87 88 89

**Example**

```js
W
wusongqing 已提交
90 91
let videoPlayer

W
wusongqing 已提交
92
media.createVideoPlayer().then((video) => {
W
wusongqing 已提交
93
   if (video != null) {
W
wusongqing 已提交
94 95
       videoPlayer = video;
       console.info('video createVideoPlayer success');
W
wusongqing 已提交
96
   } else {
W
wusongqing 已提交
97
       console.info('video createVideoPlayer fail');
W
wusongqing 已提交
98
   }
W
wusongqing 已提交
99
}).catch((error) => {
G
Gloria 已提交
100
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
101
});
Z
zengyawen 已提交
102 103 104
```

## media.createAudioRecorder
W
wusongqing 已提交
105

Z
zengyawen 已提交
106 107
createAudioRecorder(): AudioRecorder

W
wusongqing 已提交
108
Creates an **AudioRecorder** instance to control audio recording.
109
Only one **AudioRecorder** instance can be created per device.
Z
zengyawen 已提交
110

W
wusongqing 已提交
111 112
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

W
wusongqing 已提交
113
**Return value**
Z
zengyawen 已提交
114

G
Gloria 已提交
115 116
| Type                           | Description                                                        |
| ------------------------------- | ------------------------------------------------------------ |
117
| [AudioRecorder](#audiorecorder) | Returns the **AudioRecorder** instance if the operation is successful; returns **null** otherwise. The instance can be used to record audio.|
Z
zengyawen 已提交
118 119 120

**Example**

W
wusongqing 已提交
121
```js
W
wusongqing 已提交
122
let audioRecorder = media.createAudioRecorder();
W
wusongqing 已提交
123 124
```

W
wusongqing 已提交
125
## media.createVideoRecorder<sup>9+</sup>
W
wusongqing 已提交
126

W
wusongqing 已提交
127
createVideoRecorder(callback: AsyncCallback\<[VideoRecorder](#videorecorder9)>): void
W
wusongqing 已提交
128

G
Gloria 已提交
129
Creates a **VideoRecorder** instance. This API uses an asynchronous callback to return the result.
130
Only one **AudioRecorder** instance can be created per device.
W
wusongqing 已提交
131 132 133

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

134 135
**System API**: This is a system API.

W
wusongqing 已提交
136 137 138 139
**Parameters**

| Name  | Type                                           | Mandatory| Description                          |
| -------- | ----------------------------------------------- | ---- | ------------------------------ |
140 141 142 143 144 145 146 147 148
| callback | AsyncCallback<[VideoRecorder](#videorecorder9)> | Yes  | Callback used to return the result. If the operation is successful, the **VideoRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record video.|

**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                      |
| -------- | ------------------------------ |
| 5400101  | No memory. Return by callback. |
W
wusongqing 已提交
149 150 151 152 153 154 155

**Example**

```js
let videoRecorder

media.createVideoRecorder((error, video) => {
W
wusongqing 已提交
156
   if (video != null) {
W
wusongqing 已提交
157 158 159
       videoRecorder = video;
       console.info('video createVideoRecorder success');
   } else {
G
Gloria 已提交
160
       console.info(`video createVideoRecorder fail, error:${error}`);
W
wusongqing 已提交
161 162
   }
});
Z
zengyawen 已提交
163
```
W
wusongqing 已提交
164

W
wusongqing 已提交
165
## media.createVideoRecorder<sup>9+</sup>
W
wusongqing 已提交
166

W
wusongqing 已提交
167
createVideoRecorder(): Promise<[VideoRecorder](#videorecorder9)>
W
wusongqing 已提交
168

G
Gloria 已提交
169
Creates a **VideoRecorder** instance. This API uses a promise to return the result.
170
Only one **AudioRecorder** instance can be created per device.
W
wusongqing 已提交
171 172 173

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

174 175
**System API**: This is a system API.

W
wusongqing 已提交
176 177
**Return value**

G
Gloria 已提交
178 179
| Type                                     | Description                                                        |
| ----------------------------------------- | ------------------------------------------------------------ |
180 181 182 183 184 185 186 187 188
| Promise<[VideoRecorder](#videorecorder9)> | Promise used to return the result. If the operation is successful, the **VideoRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record video.|

**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                     |
| -------- | ----------------------------- |
| 5400101  | No memory. Return by promise. |
W
wusongqing 已提交
189 190 191 192 193 194

**Example**

```js
let videoRecorder

W
wusongqing 已提交
195
media.createVideoRecorder().then((video) => {
W
wusongqing 已提交
196
    if (video != null) {
W
wusongqing 已提交
197 198 199 200 201
       videoRecorder = video;
       console.info('video createVideoRecorder success');
   } else {
       console.info('video createVideoRecorder fail');
   }
W
wusongqing 已提交
202
}).catch((error) => {
G
Gloria 已提交
203
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
204
});
Z
zengyawen 已提交
205 206
```

W
wusongqing 已提交
207 208


W
wusongqing 已提交
209 210 211 212
## MediaErrorCode<sup>8+</sup>

Enumerates the media error codes.

W
wusongqing 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226
**System capability**: SystemCapability.Multimedia.Media.Core

| Name                      | Value  | Description                                  |
| -------------------------- | ---- | -------------------------------------- |
| MSERR_OK                   | 0    | The operation is successful.                        |
| MSERR_NO_MEMORY            | 1    | Failed to allocate memory. The system may have no available memory.|
| MSERR_OPERATION_NOT_PERMIT | 2    | No permission to perform this operation.                |
| MSERR_INVALID_VAL          | 3    | Invalid input parameter.                    |
| MSERR_IO                   | 4    | An I/O error occurs.                      |
| MSERR_TIMEOUT              | 5    | The operation times out.                        |
| MSERR_UNKNOWN              | 6    | An unknown error occurs.                        |
| MSERR_SERVICE_DIED         | 7    | Invalid server.                      |
| MSERR_INVALID_STATE        | 8    | The operation is not allowed in the current state.  |
| MSERR_UNSUPPORTED          | 9    | The operation is not supported in the current version.      |
W
wusongqing 已提交
227 228 229 230 231

## MediaType<sup>8+</sup>

Enumerates the media types.

W
wusongqing 已提交
232 233 234 235 236 237
**System capability**: SystemCapability.Multimedia.Media.Core

| Name          | Value  | Description      |
| -------------- | ---- | ---------- |
| MEDIA_TYPE_AUD | 0    | Media.|
| MEDIA_TYPE_VID | 1    | Video.|
W
wusongqing 已提交
238 239 240 241 242

## CodecMimeType<sup>8+</sup>

Enumerates the codec MIME types.

W
wusongqing 已提交
243 244
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
245 246 247 248 249 250 251
| Name        | Value                   | Description                    |
| ------------ | --------------------- | ------------------------ |
| VIDEO_H263   | 'video/h263'          | Video in H.263 format.     |
| VIDEO_AVC    | 'video/avc'           | Video in AVC format.      |
| VIDEO_MPEG2  | 'video/mpeg2'         | Video in MPEG-2 format.    |
| VIDEO_MPEG4  | 'video/mp4v-es'       | Video in MPEG-4 format.    |
| VIDEO_VP8    | 'video/x-vnd.on2.vp8' | Video in VP8 format.      |
G
Gloria 已提交
252
| AUDIO_AAC    | 'audio/mp4a-latm'     | Audio in MP4A-LATM format.|
W
wusongqing 已提交
253 254
| AUDIO_VORBIS | 'audio/vorbis'        | Audio in Vorbis format.   |
| AUDIO_FLAC   | 'audio/flac'          | Audio in FLAC format.     |
W
wusongqing 已提交
255 256 257 258 259

## MediaDescriptionKey<sup>8+</sup>

Enumerates the media description keys.

W
wusongqing 已提交
260 261
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
262
| Name                    | Value             | Description                                                        |
W
wusongqing 已提交
263
| ------------------------ | --------------- | ------------------------------------------------------------ |
G
Gloria 已提交
264 265 266 267 268 269 270 271 272 273
| MD_KEY_TRACK_INDEX       | 'track_index'   | Track index, which is a number.                      |
| MD_KEY_TRACK_TYPE        | 'track_type'    | Track type, which is a number. For details, see [MediaType](#mediatype8).|
| MD_KEY_CODEC_MIME        | 'codec_mime'    | Codec MIME type, which is a string.                |
| MD_KEY_DURATION          | 'duration'      | Media duration, which is a number, in units of ms.    |
| MD_KEY_BITRATE           | 'bitrate'       | Bit rate, which is a number, in units of bit/s.   |
| MD_KEY_WIDTH             | 'width'         | Video width, which is a number, in units of pixel.    |
| MD_KEY_HEIGHT            | 'height'        | Video height, which is a number, in units of pixel.    |
| MD_KEY_FRAME_RATE        | 'frame_rate'    | Video frame rate, which is a number, in units of 100 fps.|
| MD_KEY_AUD_CHANNEL_COUNT | 'channel_count' | Number of audio channels, which is a number.                        |
| MD_KEY_AUD_SAMPLE_RATE   | 'sample_rate'   | Sampling rate, which is a number, in units of Hz.      |
W
wusongqing 已提交
274 275 276 277 278

## BufferingInfoType<sup>8+</sup>

Enumerates the buffering event types.

W
wusongqing 已提交
279 280
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
281 282 283 284 285
| Name             | Value  | Description                            |
| ----------------- | ---- | -------------------------------- |
| BUFFERING_START   | 1    | Buffering starts.                  |
| BUFFERING_END     | 2    | Buffering ends.                  |
| BUFFERING_PERCENT | 3    | Buffering progress, in percent.                |
G
Gloria 已提交
286
| CACHED_DURATION   | 4    | Cache duration, in ms.|
W
wusongqing 已提交
287

Z
zengyawen 已提交
288 289
## AudioPlayer

W
wusongqing 已提交
290
Provides APIs to manage and play audio. Before calling an API of **AudioPlayer**, you must use [createAudioPlayer()](#mediacreateaudioplayer) to create an **AudioPlayer** instance.
W
wusongqing 已提交
291

W
wusongqing 已提交
292
For details about the audio playback demo, see [Audio Playback Development](../../media/audio-playback.md).
Z
zengyawen 已提交
293

W
wusongqing 已提交
294 295 296
### Attributes<a name=audioplayer_attributes></a>

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
Z
zengyawen 已提交
297

298 299 300
| Name                           | Type                                                  | Readable| Writable| Description                                                        |
| ------------------------------- | ------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
| src                             | string                                                 | Yes  | Yes  | Audio file URI. The mainstream audio formats (M4A, AAC, MPEG-3, OGG, and WAV) are supported.<br>**Examples of supported URI schemes**:<br>1. FD: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx<br>**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.INTERNET|
301
| fdSrc<sup>9+</sup>              | [AVFileDescriptor](#avfiledescriptor9)                 | Yes  | Yes  | Description of the audio file. This attribute is required when audio resources of an application are continuously stored in a file.<br>**Example:**<br>Assume that a music file that stores continuous music resources consists of the following:<br>Music 1 (address offset: 0, byte length: 100)<br>Music 2 (address offset: 101; byte length: 50)<br>Music 3 (address offset: 151, byte length: 150)<br>1. To play music 1: AVFileDescriptor {fd = resource handle; offset = 0; length = 100; }<br>2. To play music 2: AVFileDescriptor {fd = resource handle; offset = 101; length = 50; }<br>3. To play music 3: AVFileDescriptor {fd = resource handle; offset = 151; length = 150; }<br>To play an independent music file, use **src=fd://xx**.<br>|
302 303 304 305 306
| loop                            | boolean                                                | Yes  | Yes  | Whether to loop audio playback. The value **true** means to loop audio playback, and **false** means the opposite.                |
| audioInterruptMode<sup>9+</sup> | [audio.InterruptMode](js-apis-audio.md#interruptmode9) | Yes  | Yes  | Audio interruption mode.                                              |
| currentTime                     | number                                                 | Yes  | No  | Current audio playback position, in ms.                      |
| duration                        | number                                                 | Yes  | No  | Audio duration, in ms.                                |
| state                           | [AudioState](#audiostate)                              | Yes  | No  | Audio playback state. This state cannot be used as the condition for triggering the call of **play()**, **pause()**, or **stop()**.|
W
wusongqing 已提交
307
### play<a name=audioplayer_play></a>
Z
zengyawen 已提交
308

W
wusongqing 已提交
309
play(): void
Z
zengyawen 已提交
310

W
wusongqing 已提交
311
Starts to play audio resources. This API can be called only after the [dataLoad](#audioplayer_on) event is triggered.
W
wusongqing 已提交
312 313

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
Z
zengyawen 已提交
314 315 316

**Example**

W
wusongqing 已提交
317 318 319
```js
audioPlayer.on('play', () => {    // Set the 'play' event callback.
    console.log('audio play success');
Z
zengyawen 已提交
320
});
W
wusongqing 已提交
321
audioPlayer.play();
Z
zengyawen 已提交
322 323
```

W
wusongqing 已提交
324
### pause<a name=audioplayer_pause></a>
Z
zengyawen 已提交
325

W
wusongqing 已提交
326
pause(): void
Z
zengyawen 已提交
327 328 329

Pauses audio playback.

W
wusongqing 已提交
330 331
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

Z
zengyawen 已提交
332 333
**Example**

W
wusongqing 已提交
334 335 336
```js
audioPlayer.on('pause', () => {    // Set the 'pause' event callback.
    console.log('audio pause success');
Z
zengyawen 已提交
337
});
W
wusongqing 已提交
338
audioPlayer.pause();
Z
zengyawen 已提交
339 340
```

W
wusongqing 已提交
341
### stop<a name=audioplayer_stop></a>
Z
zengyawen 已提交
342

W
wusongqing 已提交
343
stop(): void
Z
zengyawen 已提交
344 345 346

Stops audio playback.

W
wusongqing 已提交
347 348
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

Z
zengyawen 已提交
349 350
**Example**

W
wusongqing 已提交
351 352 353
```js
audioPlayer.on('stop', () => {    // Set the 'stop' event callback.
    console.log('audio stop success');
Z
zengyawen 已提交
354
});
W
wusongqing 已提交
355
audioPlayer.stop();
Z
zengyawen 已提交
356 357
```

W
wusongqing 已提交
358 359 360
### reset<sup>7+</sup><a name=audioplayer_reset></a>

reset(): void
Z
zengyawen 已提交
361

362
Resets the audio asset to be played.
Z
zengyawen 已提交
363

W
wusongqing 已提交
364 365
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

W
wusongqing 已提交
366 367 368 369 370 371 372 373 374 375 376 377
**Example**

```js
audioPlayer.on('reset', () => {    // Set the 'reset' event callback.
    console.log('audio reset success');
});
audioPlayer.reset();
```

### seek<a name=audioplayer_seek></a>

seek(timeMs: number): void
Z
zengyawen 已提交
378 379 380

Seeks to the specified playback position.

W
wusongqing 已提交
381 382
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

Z
zengyawen 已提交
383 384
**Parameters**

G
Gloria 已提交
385 386 387
| Name| Type  | Mandatory| Description                                                       |
| ------ | ------ | ---- | ----------------------------------------------------------- |
| timeMs | number | Yes  | Position to seek to, in ms. The value range is [0, duration].|
Z
zengyawen 已提交
388 389 390

**Example**

W
wusongqing 已提交
391 392
```js
audioPlayer.on('timeUpdate', (seekDoneTime) => {    // Set the 'timeUpdate' event callback.
W
wusongqing 已提交
393
    if (seekDoneTime == null) {
W
wusongqing 已提交
394 395 396 397
        console.info('audio seek fail');
        return;
    }
    console.log('audio seek success. seekDoneTime: ' + seekDoneTime);
Z
zengyawen 已提交
398
});
W
wusongqing 已提交
399
audioPlayer.seek(30000); // Seek to 30000 ms.
Z
zengyawen 已提交
400 401
```

W
wusongqing 已提交
402
### setVolume<a name=audioplayer_setvolume></a>
Z
zengyawen 已提交
403

W
wusongqing 已提交
404
setVolume(vol: number): void
Z
zengyawen 已提交
405 406 407

Sets the volume.

W
wusongqing 已提交
408 409
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

Z
zengyawen 已提交
410 411
**Parameters**

W
wusongqing 已提交
412
| Name| Type  | Mandatory| Description                                                        |
W
wusongqing 已提交
413
| ------ | ------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
414
| vol    | number | Yes  | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).|
Z
zengyawen 已提交
415 416 417

**Example**

W
wusongqing 已提交
418 419 420
```js
audioPlayer.on('volumeChange', () => {    // Set the 'volumeChange' event callback.
    console.log('audio volumeChange success');
Z
zengyawen 已提交
421
});
W
wusongqing 已提交
422
audioPlayer.setVolume(1);    // Set the volume to 100%.
Z
zengyawen 已提交
423 424
```

W
wusongqing 已提交
425
### release<a name=audioplayer_release></a>
Z
zengyawen 已提交
426

W
wusongqing 已提交
427
release(): void
Z
zengyawen 已提交
428

W
wusongqing 已提交
429 430 431
Releases the audio playback resource.

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
Z
zengyawen 已提交
432 433 434

**Example**

W
wusongqing 已提交
435 436 437
```js
audioPlayer.release();
audioPlayer = undefined;
Z
zengyawen 已提交
438 439
```

W
wusongqing 已提交
440 441
### getTrackDescription<sup>8+</sup><a name=audioplayer_gettrackdescription1></a>

W
wusongqing 已提交
442
getTrackDescription(callback: AsyncCallback<Array\<MediaDescription>>): void
Z
zengyawen 已提交
443

G
Gloria 已提交
444
Obtains the audio track information. This API uses an asynchronous callback to return the result. It can be called only after the [dataLoad](#audioplayer_on) event is triggered.
W
wusongqing 已提交
445 446

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
Z
zengyawen 已提交
447

W
wusongqing 已提交
448 449
**Parameters**

G
Gloria 已提交
450 451 452
| Name  | Type                                                        | Mandatory| Description                                      |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ |
| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes  | Callback used to return a **MediaDescription** array, which records the audio track information.|
Z
zengyawen 已提交
453 454 455

**Example**

W
wusongqing 已提交
456 457 458 459 460 461 462 463 464
```js
function printfDescription(obj) {
    for (let item in obj) {
        let property = obj[item];
        console.info('audio key is ' + item);
        console.info('audio value is ' + property);
    }
}

G
Gloria 已提交
465 466 467 468
audioPlayer.getTrackDescription((error, arrList) => {
    if (arrList != null) {
        for (let i = 0; i < arrList.length; i++) {
            printfDescription(arrList[i]);
W
wusongqing 已提交
469 470
        }
    } else {
G
Gloria 已提交
471
        console.log(`audio getTrackDescription fail, error:${error}`);
W
wusongqing 已提交
472 473
    }
});
Z
zengyawen 已提交
474
```
W
wusongqing 已提交
475 476 477

### getTrackDescription<sup>8+</sup><a name=audioplayer_gettrackdescription2></a>

W
wusongqing 已提交
478
getTrackDescription(): Promise<Array\<MediaDescription>>
W
wusongqing 已提交
479

W
wusongqing 已提交
480
Obtains the audio track information. This API uses a promise to return the result. It can be called only after the [dataLoad](#audioplayer_on) event is triggered.
W
wusongqing 已提交
481 482

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
W
wusongqing 已提交
483

W
wusongqing 已提交
484
**Return value**
W
wusongqing 已提交
485

G
Gloria 已提交
486 487 488
| Type                                                  | Description                                           |
| ------------------------------------------------------ | ----------------------------------------------- |
| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the audio track information.|
W
wusongqing 已提交
489 490 491 492 493 494 495 496 497 498 499

**Example**

```js
function printfDescription(obj) {
    for (let item in obj) {
        let property = obj[item];
        console.info('audio key is ' + item);
        console.info('audio value is ' + property);
    }
}
G
Gloria 已提交
500
let arrayDescription = null
G
Gloria 已提交
501 502 503
audioPlayer.getTrackDescription().then((arrList) => {
    if (arrList != null) {
        arrayDescription = arrList;
W
wusongqing 已提交
504 505 506
    } else {
        console.log('audio getTrackDescription fail');
    }
W
wusongqing 已提交
507
}).catch((error) => {
G
Gloria 已提交
508
   console.info(`audio catchCallback, error:${error}`);
W
wusongqing 已提交
509 510
});

W
wusongqing 已提交
511 512 513
for (let i = 0; i < arrayDescription.length; i++) {
    printfDescription(arrayDescription[i]);
}
Z
zengyawen 已提交
514 515
```

W
wusongqing 已提交
516
### on('bufferingUpdate')<sup>8+</sup>
Z
zengyawen 已提交
517

W
wusongqing 已提交
518
on(type: 'bufferingUpdate', callback: (infoType: [BufferingInfoType](#bufferinginfotype8), value: number) => void): void
Z
zengyawen 已提交
519

520
Subscribes to the audio buffering update event. This API works only under online playback.
Z
zengyawen 已提交
521

W
wusongqing 已提交
522 523
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

Z
zengyawen 已提交
524 525
**Parameters**

W
wusongqing 已提交
526 527
| Name  | Type    | Mandatory| Description                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
528
| type     | string   | Yes  | Event type, which is **'bufferingUpdate'** in this case.       |
W
wusongqing 已提交
529
| callback | function | Yes  | Callback invoked when the event is triggered.<br>When [BufferingInfoType](#bufferinginfotype8) is set to **BUFFERING_PERCENT** or **CACHED_DURATION**, **value** is valid. Otherwise, **value** is fixed at **0**.|
Z
zengyawen 已提交
530 531 532

**Example**

W
wusongqing 已提交
533 534 535 536 537
```js
audioPlayer.on('bufferingUpdate', (infoType, value) => {
    console.log('audio bufferingInfo type: ' + infoType);
    console.log('audio bufferingInfo value: ' + value);
});
Z
zengyawen 已提交
538
```
W
wusongqing 已提交
539

W
wusongqing 已提交
540
 ### on('play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange')<a name = audioplayer_on></a>
W
wusongqing 已提交
541 542 543 544 545

on(type: 'play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange', callback: () => void): void

Subscribes to the audio playback events.

W
wusongqing 已提交
546 547
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

W
wusongqing 已提交
548 549
**Parameters**

W
wusongqing 已提交
550
| Name  | Type      | Mandatory| Description                                                        |
W
wusongqing 已提交
551
| -------- | ---------- | ---- | ------------------------------------------------------------ |
G
Gloria 已提交
552
| type     | string     | Yes  | Event type. The following events are supported:<br>- 'play': triggered when the [play()](#audioplayer_play) API is called and audio playback starts.<br>- 'pause': triggered when the [pause()](#audioplayer_pause) API is called and audio playback is paused.<br>- 'stop': triggered when the [stop()](#audioplayer_stop) API is called and audio playback stops.<br>- 'reset': triggered when the [reset()](#audioplayer_reset) API is called and audio playback is reset.<br>- 'dataLoad': triggered when the audio data is loaded, that is, when the **src** attribute is configured.<br>- 'finish': triggered when the audio playback is finished.<br>- 'volumeChange': triggered when the [setVolume()](#audioplayer_setvolume) API is called and the playback volume is changed.|
W
wusongqing 已提交
553
| callback | () => void | Yes  | Callback invoked when the event is triggered.                                          |
W
wusongqing 已提交
554 555 556 557

**Example**

```js
G
Gloria 已提交
558 559
import fileio from '@ohos.fileio'

W
wusongqing 已提交
560 561
let audioPlayer = media.createAudioPlayer();  // Create an AudioPlayer instance.
audioPlayer.on('dataLoad', () => {            // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully.
W
wusongqing 已提交
562
    console.info('audio set source success');
W
wusongqing 已提交
563 564 565
    audioPlayer.play();                       // Start the playback and trigger the 'play' event callback.
});
audioPlayer.on('play', () => {                // Set the 'play' event callback.
W
wusongqing 已提交
566
    console.info('audio play success');
W
wusongqing 已提交
567
    audioPlayer.seek(30000);                  // Call the seek() API and trigger the 'timeUpdate' event callback.
Z
zengyawen 已提交
568
});
W
wusongqing 已提交
569
audioPlayer.on('pause', () => {               // Set the 'pause' event callback.
W
wusongqing 已提交
570
    console.info('audio pause success');
W
wusongqing 已提交
571 572 573
    audioPlayer.stop();                       // Stop the playback and trigger the 'stop' event callback.
});
audioPlayer.on('reset', () => {               // Set the 'reset' event callback.
W
wusongqing 已提交
574
    console.info('audio reset success');
W
wusongqing 已提交
575 576 577 578
    audioPlayer.release();                    // Release the AudioPlayer instance.
    audioPlayer = undefined;
});
audioPlayer.on('timeUpdate', (seekDoneTime) => {  // Set the 'timeUpdate' event callback.
W
wusongqing 已提交
579
    if (seekDoneTime == null) {
W
wusongqing 已提交
580 581 582 583 584 585 586
        console.info('audio seek fail');
        return;
    }
    console.info('audio seek success, and seek time is ' + seekDoneTime);
    audioPlayer.setVolume(0.5);                // Set the volume to 50% and trigger the 'volumeChange' event callback.
});
audioPlayer.on('volumeChange', () => {         // Set the 'volumeChange' event callback.
W
wusongqing 已提交
587
    console.info('audio volumeChange success');
W
wusongqing 已提交
588 589 590
    audioPlayer.pause();                       // Pause the playback and trigger the 'pause' event callback.
});
audioPlayer.on('finish', () => {               // Set the 'finish' event callback.
W
wusongqing 已提交
591
    console.info('audio play finish');
W
wusongqing 已提交
592 593 594
    audioPlayer.stop();                        // Stop the playback and trigger the 'stop' event callback.
});
audioPlayer.on('error', (error) => {           // Set the 'error' event callback.
G
Gloria 已提交
595
    console.info(`audio error called, error: ${error}`);
W
wusongqing 已提交
596
});
W
wusongqing 已提交
597 598

// Set the FD (local playback) of the video file selected by the user.
G
Gloria 已提交
599
let fdPath = 'fd://';
W
wusongqing 已提交
600 601
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/accounts/account_0/appdata" command.
let path = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
G
Gloria 已提交
602 603
fileio.open(path).then((fdValue) => {
   fdPath = fdPath + '' + fdValue;
JoyboyCZ's avatar
JoyboyCZ 已提交
604
   console.info('open fd success fd is' + fdPath);
W
wusongqing 已提交
605 606
}, (err) => {
   console.info('open fd failed err is' + err);
W
wusongqing 已提交
607
}).catch((err) => {
W
wusongqing 已提交
608 609 610
   console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath;  // Set the src attribute and trigger the 'dataLoad' event callback.
Z
zengyawen 已提交
611 612 613 614 615 616
```

### on('timeUpdate')

on(type: 'timeUpdate', callback: Callback\<number>): void

G
Gloria 已提交
617
Subscribes to the **'timeUpdate'** event. This event is reported every second when the audio playback is in progress.
W
wusongqing 已提交
618 619

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
Z
zengyawen 已提交
620 621 622

**Parameters**

W
wusongqing 已提交
623
| Name  | Type             | Mandatory| Description                                                        |
W
wusongqing 已提交
624
| -------- | ----------------- | ---- | ------------------------------------------------------------ |
G
Gloria 已提交
625 626
| type     | string            | Yes  | Event type, which is **'timeUpdate'** in this case.<br>The **'timeUpdate'** event is triggered when the audio playback starts after an audio playback timestamp update.|
| callback | Callback\<number> | Yes  | Callback invoked when the event is triggered. The input parameter is the updated timestamp.            |
Z
zengyawen 已提交
627 628 629

**Example**

W
wusongqing 已提交
630
```js
G
Gloria 已提交
631 632 633
audioPlayer.on('timeUpdate', (newTime) => {    // Set the 'timeUpdate' event callback.
    if (newTime == null) {
        console.info('audio timeUpadate fail');
W
wusongqing 已提交
634 635
        return;
    }
G
Gloria 已提交
636
    console.log('audio timeUpadate success. seekDoneTime: ' + newTime);
Z
zengyawen 已提交
637
});
G
Gloria 已提交
638
audioPlayer.play();    // The 'timeUpdate' event is triggered when the playback starts.
Z
zengyawen 已提交
639 640 641 642 643 644
```

### on('error')

on(type: 'error', callback: ErrorCallback): void

645
Subscribes to audio playback error events. After an error event is reported, you must handle the event and exit the playback.
Z
zengyawen 已提交
646

W
wusongqing 已提交
647 648
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

Z
zengyawen 已提交
649 650
**Parameters**

W
wusongqing 已提交
651
| Name  | Type         | Mandatory| Description                                                        |
W
wusongqing 已提交
652
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
653
| type     | string        | Yes  | Event type, which is **'error'** in this case.<br>The **'error'** event is triggered when an error occurs during audio playback.|
W
wusongqing 已提交
654
| callback | ErrorCallback | Yes  | Callback invoked when the event is triggered.                                      |
Z
zengyawen 已提交
655 656 657

**Example**

W
wusongqing 已提交
658
```js
659
audioPlayer.on('error', (error) => {      // Set the 'error' event callback.
G
Gloria 已提交
660
    console.info(`audio error called, error: ${error}`); 
Z
zengyawen 已提交
661
});
W
wusongqing 已提交
662
audioPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'error' event.
Z
zengyawen 已提交
663 664 665 666
```

## AudioState

W
wusongqing 已提交
667
Enumerates the audio playback states. You can obtain the state through the **state** attribute.
W
wusongqing 已提交
668

W
wusongqing 已提交
669 670
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

G
Gloria 已提交
671 672 673 674 675 676 677
| Name   | Type  | Description                                          |
| ------- | ------ | ---------------------------------------------- |
| idle    | string |  No audio playback is in progress. The audio player is in this state after the **'dataload'** or **'reset'** event is triggered.|
| playing | string | Audio playback is in progress. The audio player is in this state after the **'play'** event is triggered.          |
| paused  | string | Audio playback is paused. The audio player is in this state after the **'pause'** event is triggered.         |
| stopped | string | Audio playback is stopped. The audio player is in this state after the **'stop'** event is triggered.     |
| error   | string | Audio playback is in the error state.                                    |
678 679 680 681 682

## AVFileDescriptor<sup>9+</sup>

Describes audio and video file resources. It is used to specify a particular resource for playback based on its offset and length within a file.

G
Gloria 已提交
683
**System capability**: SystemCapability.Multimedia.Media.Core
684 685 686 687 688 689 690

| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| fd     | number | Yes  | Resource handle, which is obtained by calling **resourceManager.getRawFileDescriptor**.      |
| offset | number | Yes  | Resource offset, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.|
| length | number | Yes  | Resource length, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.|

W
wusongqing 已提交
691
## VideoPlayer<sup>8+</sup>
W
wusongqing 已提交
692

693
Provides APIs to manage and play video. Before calling an API of **VideoPlayer**, you must use [createVideoPlayer()](#mediacreatevideoplayer8) to create a [VideoPlayer](#videoplayer8) instance.
W
wusongqing 已提交
694

W
wusongqing 已提交
695
For details about the video playback demo, see [Video Playback Development](../../media/video-playback.md).
W
wusongqing 已提交
696

W
wusongqing 已提交
697 698
### Attributes<a name=videoplayer_attributes></a>

W
wusongqing 已提交
699 700
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

W
wusongqing 已提交
701 702
| Name                    | Type                              | Readable| Writable| Description                                                        |
| ------------------------ | ---------------------------------- | ---- | ---- | ------------------------------------------------------------ |
703
| url<sup>8+</sup>         | string                             | Yes  | Yes  | Video URL. The mainstream video formats (MPEG-4, MPEG-TS, WebM, and MKV) are supported.<br>**Example of supported URIs**:<br>1. FD: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx<br>|
G
Gloria 已提交
704
| fdSrc<sup>9+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes| Yes| Description of a video file. This attribute is required when video resources of an application are continuously stored in a file.<br>**Example:**<br>Assume that a music file that stores continuous music resources consists of the following:<br>Video 1 (address offset: 0, byte length: 100)<br>Video 2 (address offset: 101; byte length: 50)<br>Video 3 (address offset: 151, byte length: 150)<br>1. To play video 1: AVFileDescriptor {fd = resource handle; offset = 0; length = 100; }<br>2. To play video 2: AVFileDescriptor {fd = resource handle; offset = 101; length = 50; }<br>3. To play video 3: AVFileDescriptor {fd = resource handle; offset = 151; length = 150; }<br>To play an independent video file, use **src=fd://xx**.<br>|
W
wusongqing 已提交
705
| loop<sup>8+</sup>        | boolean                            | Yes  | Yes  | Whether to loop video playback. The value **true** means to loop video playback, and **false** means the opposite.                |
706
| videoScaleType<sup>9+</sup>        | [VideoScaleType](#videoscaletype9)                   | Yes  | Yes  | Video scale type.      |
G
Gloria 已提交
707
| audioInterruptMode<sup>9+</sup>        | [audio.InterruptMode](js-apis-audio.md#interruptmode9)  | Yes  | Yes  | Audio interruption mode.      |
708 709
| currentTime<sup>8+</sup> | number                             | Yes  | No  | Current video playback position, in ms.                      |
| duration<sup>8+</sup>    | number                             | Yes  | No  | Video duration, in ms. The value **-1** indicates the live mode.            |
W
wusongqing 已提交
710
| state<sup>8+</sup>       | [VideoPlayState](#videoplaystate8) | Yes  | No  | Video playback state.                                            |
711 712
| width<sup>8+</sup>       | number                             | Yes  | No  | Video width, in pixels.                                  |
| height<sup>8+</sup>      | number                             | Yes  | No  | Video height, in pixels.                                  |
W
wusongqing 已提交
713 714 715 716 717

### setDisplaySurface<sup>8+</sup>

setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void

G
Gloria 已提交
718
Sets **SurfaceId**. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
719

W
wusongqing 已提交
720 721
*Note: **SetDisplaySurface** must be called between the URL setting and the calling of **prepare**. A surface must be set for video streams without audio. Otherwise, the calling of **prepare** fails.

W
wusongqing 已提交
722 723 724 725
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

726 727 728 729
| Name   | Type                | Mandatory| Description                     |
| --------- | -------------------- | ---- | ------------------------- |
| surfaceId | string               | Yes  | Surface ID to set.                |
| callback  | AsyncCallback\<void> | Yes  | Callback used to return the result.|
W
wusongqing 已提交
730 731 732 733

**Example**

```js
G
Gloria 已提交
734
let surfaceId = null;
W
wusongqing 已提交
735
videoPlayer.setDisplaySurface(surfaceId, (err) => {
W
wusongqing 已提交
736
    if (err == null) {
W
wusongqing 已提交
737 738
        console.info('setDisplaySurface success!');
    } else {
W
wusongqing 已提交
739
        console.info('setDisplaySurface fail!');
W
wusongqing 已提交
740 741 742
    }
});
```
Z
zengyawen 已提交
743

W
wusongqing 已提交
744
### setDisplaySurface<sup>8+</sup>
Z
zengyawen 已提交
745

W
wusongqing 已提交
746
setDisplaySurface(surfaceId: string): Promise\<void>
Z
zengyawen 已提交
747

W
wusongqing 已提交
748
Sets **SurfaceId**. This API uses a promise to return the result.
Z
zengyawen 已提交
749

W
wusongqing 已提交
750 751
*Note: **SetDisplaySurface** must be called between the URL setting and the calling of **prepare**. A surface must be set for video streams without audio. Otherwise, the calling of **prepare** fails.

W
wusongqing 已提交
752
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Z
zengyawen 已提交
753

W
wusongqing 已提交
754 755 756 757 758 759 760 761
**Parameters**

| Name   | Type  | Mandatory| Description     |
| --------- | ------ | ---- | --------- |
| surfaceId | string | Yes  | Surface ID to set.|

**Return value**

W
wusongqing 已提交
762 763 764
| Type          | Description                          |
| -------------- | ------------------------------ |
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
765 766 767 768

**Example**

```js
G
Gloria 已提交
769
let surfaceId = null;
W
wusongqing 已提交
770
videoPlayer.setDisplaySurface(surfaceId).then(() => {
W
wusongqing 已提交
771
    console.info('setDisplaySurface success');
W
wusongqing 已提交
772
}).catch((error) => {
G
Gloria 已提交
773
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
774
});
W
wusongqing 已提交
775 776 777 778 779 780
```

### prepare<sup>8+</sup>

prepare(callback: AsyncCallback\<void>): void

G
Gloria 已提交
781
Prepares for video playback. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
782 783

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
W
wusongqing 已提交
784

Z
zengyawen 已提交
785 786
**Parameters**

787 788 789
| Name  | Type                | Mandatory| Description                    |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
790 791 792

**Example**

W
wusongqing 已提交
793 794
```js
videoPlayer.prepare((err) => {
W
wusongqing 已提交
795
    if (err == null) {
W
wusongqing 已提交
796 797
        console.info('prepare success!');
    } else {
W
wusongqing 已提交
798 799 800
        console.info('prepare fail!');
    }
});
W
wusongqing 已提交
801
```
W
wusongqing 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

### prepare<sup>8+</sup>

prepare(): Promise\<void>

Prepares for video playback. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Return value**

| Type          | Description                         |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
W
wusongqing 已提交
820
videoPlayer.prepare().then(() => {
W
wusongqing 已提交
821
    console.info('prepare success');
W
wusongqing 已提交
822
}).catch((error) => {
G
Gloria 已提交
823
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
824
});
W
wusongqing 已提交
825 826
```

W
wusongqing 已提交
827
### play<sup>8+</sup>
W
wusongqing 已提交
828

W
wusongqing 已提交
829
play(callback: AsyncCallback\<void>): void;
Z
zengyawen 已提交
830

G
Gloria 已提交
831
Starts to play video resources. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
832 833

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Z
zengyawen 已提交
834

W
wusongqing 已提交
835 836
**Parameters**

837 838 839
| Name  | Type                | Mandatory| Description                    |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
840 841 842

**Example**

W
wusongqing 已提交
843 844
```js
videoPlayer.play((err) => {
W
wusongqing 已提交
845
    if (err == null) {
W
wusongqing 已提交
846 847
        console.info('play success!');
    } else {
W
wusongqing 已提交
848 849 850
        console.info('play fail!');
    }
});
Z
zengyawen 已提交
851
```
W
wusongqing 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

### play<sup>8+</sup>

play(): Promise\<void>;

Starts to play video resources. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Return value**

| Type          | Description                         |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
W
wusongqing 已提交
870
videoPlayer.play().then(() => {
W
wusongqing 已提交
871
    console.info('play success');
W
wusongqing 已提交
872
}).catch((error) => {
G
Gloria 已提交
873
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
874
});
Z
zengyawen 已提交
875 876
```

W
wusongqing 已提交
877
### pause<sup>8+</sup>
Z
zengyawen 已提交
878

W
wusongqing 已提交
879
pause(callback: AsyncCallback\<void>): void
Z
zengyawen 已提交
880

G
Gloria 已提交
881
Pauses video playback. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
882 883 884 885 886

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

887 888 889
| Name  | Type                | Mandatory| Description                    |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
890 891 892

**Example**

W
wusongqing 已提交
893 894
```js
videoPlayer.pause((err) => {
W
wusongqing 已提交
895
    if (err == null) {
W
wusongqing 已提交
896 897
        console.info('pause success!');
    } else {
W
wusongqing 已提交
898 899 900
        console.info('pause fail!');
    }
});
Z
zengyawen 已提交
901
```
W
wusongqing 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

### pause<sup>8+</sup>

pause(): Promise\<void>

Pauses video playback. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Return value**

| Type          | Description                         |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
W
wusongqing 已提交
920
videoPlayer.pause().then(() => {
W
wusongqing 已提交
921
    console.info('pause success');
W
wusongqing 已提交
922
}).catch((error) => {
G
Gloria 已提交
923
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
924
});
Z
zengyawen 已提交
925 926
```

W
wusongqing 已提交
927
### stop<sup>8+</sup>
Z
zengyawen 已提交
928

W
wusongqing 已提交
929 930
stop(callback: AsyncCallback\<void>): void

G
Gloria 已提交
931
Stops video playback. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
932

W
wusongqing 已提交
933 934 935 936
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

937 938 939
| Name  | Type                | Mandatory| Description                    |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
W
wusongqing 已提交
940

Z
zengyawen 已提交
941 942
**Example**

W
wusongqing 已提交
943 944
```js
videoPlayer.stop((err) => {
W
wusongqing 已提交
945
    if (err == null) {
W
wusongqing 已提交
946 947
        console.info('stop success!');
    } else {
W
wusongqing 已提交
948 949 950
        console.info('stop fail!');
    }
});
Z
zengyawen 已提交
951
```
W
wusongqing 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969

### stop<sup>8+</sup>

stop(): Promise\<void>

Stops video playback. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Return value**

| Type          | Description                         |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
W
wusongqing 已提交
970
videoPlayer.stop().then(() => {
W
wusongqing 已提交
971
    console.info('stop success');
W
wusongqing 已提交
972
}).catch((error) => {
G
Gloria 已提交
973
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
974
});
Z
zengyawen 已提交
975 976
```

W
wusongqing 已提交
977
### reset<sup>8+</sup>
Z
zengyawen 已提交
978

W
wusongqing 已提交
979
reset(callback: AsyncCallback\<void>): void
Z
zengyawen 已提交
980

981
Resets the video asset to be played. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
982

W
wusongqing 已提交
983 984 985 986
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

987 988 989
| Name  | Type                | Mandatory| Description                    |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
990 991 992

**Example**

W
wusongqing 已提交
993 994
```js
videoPlayer.reset((err) => {
W
wusongqing 已提交
995
    if (err == null) {
W
wusongqing 已提交
996 997
        console.info('reset success!');
    } else {
W
wusongqing 已提交
998 999 1000
        console.info('reset fail!');
    }
});
Z
zengyawen 已提交
1001
```
W
wusongqing 已提交
1002 1003 1004 1005 1006

### reset<sup>8+</sup>

reset(): Promise\<void>

1007
Resets the video asset to be played. This API uses a promise to return the result.
W
wusongqing 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Return value**

| Type          | Description                         |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
W
wusongqing 已提交
1020
videoPlayer.reset().then(() => {
W
wusongqing 已提交
1021
    console.info('reset success');
W
wusongqing 已提交
1022
}).catch((error) => {
G
Gloria 已提交
1023
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
1024
});
Z
zengyawen 已提交
1025 1026
```

W
wusongqing 已提交
1027
### seek<sup>8+</sup>
Z
zengyawen 已提交
1028

W
wusongqing 已提交
1029
seek(timeMs: number, callback: AsyncCallback\<number>): void
Z
zengyawen 已提交
1030

G
Gloria 已提交
1031
Seeks to the specified playback position. The next key frame at the specified position is played. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1032 1033

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Z
zengyawen 已提交
1034 1035 1036

**Parameters**

1037 1038 1039 1040
| Name  | Type                  | Mandatory| Description                                                        |
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
| timeMs   | number                 | Yes  | Position to seek to, in ms. The value range is [0, duration].|
| callback | AsyncCallback\<number> | Yes  | Callback used to return the result.                              |
Z
zengyawen 已提交
1041 1042 1043

**Example**

W
wusongqing 已提交
1044
```js
W
wusongqing 已提交
1045 1046
let seekTime = 5000;
videoPlayer.seek(seekTime, (err, result) => {
W
wusongqing 已提交
1047
    if (err == null) {
W
wusongqing 已提交
1048 1049
        console.info('seek success!');
    } else {
W
wusongqing 已提交
1050 1051
        console.info('seek fail!');
    }
Z
zengyawen 已提交
1052 1053 1054
});
```

W
wusongqing 已提交
1055
### seek<sup>8+</sup>
Z
zengyawen 已提交
1056

W
wusongqing 已提交
1057
seek(timeMs: number, mode:SeekMode, callback: AsyncCallback\<number>): void
Z
zengyawen 已提交
1058

G
Gloria 已提交
1059
Seeks to the specified playback position. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1060 1061

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Z
zengyawen 已提交
1062 1063 1064

**Parameters**

G
Gloria 已提交
1065 1066 1067 1068
| Name  | Type                  | Mandatory| Description                                                        |
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
| timeMs   | number                 | Yes  | Position to seek to, in ms. The value range is [0, duration].|
| mode     | [SeekMode](#seekmode8) | Yes  | Seek mode.                                                  |
1069
| callback | AsyncCallback\<number> | Yes  | Callback used to return the result.                              |
W
wusongqing 已提交
1070

W
wusongqing 已提交
1071
**Example**
Z
zengyawen 已提交
1072

W
wusongqing 已提交
1073
```js
W
wusongqing 已提交
1074 1075
import media from '@ohos.multimedia.media'
let seekTime = 5000;
G
Gloria 已提交
1076
videoPlayer.seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC, (err, result) => {
W
wusongqing 已提交
1077
    if (err == null) {
W
wusongqing 已提交
1078 1079
        console.info('seek success!');
    } else {
W
wusongqing 已提交
1080 1081 1082 1083
        console.info('seek fail!');
    }
});
```
Z
zengyawen 已提交
1084

W
wusongqing 已提交
1085
### seek<sup>8+</sup>
Z
zengyawen 已提交
1086

W
wusongqing 已提交
1087
seek(timeMs: number, mode?:SeekMode): Promise\<number>
W
wusongqing 已提交
1088

W
wusongqing 已提交
1089
Seeks to the specified playback position. If **mode** is not specified, the next key frame at the specified position is played. This API uses a promise to return the result.
Z
zengyawen 已提交
1090

W
wusongqing 已提交
1091
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Z
zengyawen 已提交
1092

W
wusongqing 已提交
1093
**Parameters**
W
wusongqing 已提交
1094

G
Gloria 已提交
1095 1096 1097 1098
| Name| Type                  | Mandatory| Description                                                        |
| ------ | ---------------------- | ---- | ------------------------------------------------------------ |
| timeMs | number                 | Yes  | Position to seek to, in ms. The value range is [0, duration].|
| mode   | [SeekMode](#seekmode8) | No  | Seek mode.                                                  |
Z
zengyawen 已提交
1099

W
wusongqing 已提交
1100
**Return value**
Z
zengyawen 已提交
1101

G
Gloria 已提交
1102 1103
| Type          | Description                                       |
| -------------- | ------------------------------------------- |
1104
| Promise\<number>| Promise used to return the playback position, in ms.|
Z
zengyawen 已提交
1105

W
wusongqing 已提交
1106
**Example**
Z
zengyawen 已提交
1107

W
wusongqing 已提交
1108
```js
G
Gloria 已提交
1109
import media from '@ohos.multimedia.media'
W
wusongqing 已提交
1110
let seekTime = 5000;
W
wusongqing 已提交
1111
videoPlayer.seek(seekTime).then((seekDoneTime) => { // seekDoneTime indicates the position after the seek operation is complete.
W
wusongqing 已提交
1112
    console.info('seek success');
W
wusongqing 已提交
1113
}).catch((error) => {
G
Gloria 已提交
1114
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
1115
});
W
wusongqing 已提交
1116

G
Gloria 已提交
1117
videoPlayer.seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC).then((seekDoneTime) => {
W
wusongqing 已提交
1118
    console.info('seek success');
W
wusongqing 已提交
1119
}).catch((error) => {
G
Gloria 已提交
1120
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
1121
});
W
wusongqing 已提交
1122 1123 1124 1125 1126 1127
```

### setVolume<sup>8+</sup>

setVolume(vol: number, callback: AsyncCallback\<void>): void

G
Gloria 已提交
1128
Sets the volume. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1129 1130 1131 1132 1133

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

1134 1135 1136 1137
| Name  | Type                | Mandatory| Description                                                        |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
| vol      | number               | Yes  | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).|
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.                                        |
W
wusongqing 已提交
1138 1139 1140 1141

**Example**

```js
W
wusongqing 已提交
1142 1143
let vol = 0.5;
videoPlayer.setVolume(vol, (err, result) => {
W
wusongqing 已提交
1144
    if (err == null) {
W
wusongqing 已提交
1145 1146
        console.info('setVolume success!');
    } else {
W
wusongqing 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
        console.info('setVolume fail!');
    }
});
```

### setVolume<sup>8+</sup>

setVolume(vol: number): Promise\<void>

Sets the volume. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| vol    | number | Yes  | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).|

**Return value**

| Type          | Description                     |
| -------------- | ------------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
W
wusongqing 已提交
1175
let vol = 0.5;
G
Gloria 已提交
1176
videoPlayer.setVolume(vol).then(() => {
W
wusongqing 已提交
1177
    console.info('setVolume success');
W
wusongqing 已提交
1178
}).catch((error) => {
G
Gloria 已提交
1179
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
1180
});
W
wusongqing 已提交
1181 1182 1183 1184 1185 1186
```

### release<sup>8+</sup>

release(callback: AsyncCallback\<void>): void

G
Gloria 已提交
1187
Releases the video playback resource. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1188 1189 1190 1191 1192

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

1193 1194 1195
| Name  | Type                | Mandatory| Description                    |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
W
wusongqing 已提交
1196 1197 1198 1199 1200

**Example**

```js
videoPlayer.release((err) => {
W
wusongqing 已提交
1201
    if (err == null) {
W
wusongqing 已提交
1202 1203
        console.info('release success!');
    } else {
W
wusongqing 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
        console.info('release fail!');
    }
});
```

### release<sup>8+</sup>

release(): Promise\<void>

Releases the video playback resource. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Return value**

| Type          | Description                         |
| -------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

```js
G
Gloria 已提交
1226
videoPlayer.release().then(() => {
W
wusongqing 已提交
1227
    console.info('release success');
W
wusongqing 已提交
1228
}).catch((error) => {
G
Gloria 已提交
1229
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
1230
});
W
wusongqing 已提交
1231 1232 1233 1234
```

### getTrackDescription<sup>8+</sup>

W
wusongqing 已提交
1235
getTrackDescription(callback: AsyncCallback<Array\<MediaDescription>>): void
W
wusongqing 已提交
1236

G
Gloria 已提交
1237
Obtains the video track information. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1238 1239 1240 1241 1242

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

G
Gloria 已提交
1243 1244 1245
| Name  | Type                                                        | Mandatory| Description                                      |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ |
| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes  | Callback used to return a **MediaDescription** array, which records the video track information.|
W
wusongqing 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

**Example**

```js
function printfDescription(obj) {
    for (let item in obj) {
        let property = obj[item];
        console.info('video key is ' + item);
        console.info('video value is ' + property);
    }
}

G
Gloria 已提交
1258 1259 1260 1261
videoPlayer.getTrackDescription((error, arrList) => {
    if ((arrList) != null) {
        for (let i = 0; i < arrList.length; i++) {
            printfDescription(arrList[i]);
W
wusongqing 已提交
1262 1263
        }
    } else {
G
Gloria 已提交
1264
        console.log(`video getTrackDescription fail, error:${error}`);
W
wusongqing 已提交
1265 1266 1267 1268 1269 1270
    }
});
```

### getTrackDescription<sup>8+</sup>

W
wusongqing 已提交
1271
getTrackDescription(): Promise<Array\<MediaDescription>>
W
wusongqing 已提交
1272 1273 1274 1275 1276 1277 1278

Obtains the video track information. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Return value**

G
Gloria 已提交
1279 1280 1281
| Type                                                  | Description                                           |
| ------------------------------------------------------ | ----------------------------------------------- |
| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the video track information.|
W
wusongqing 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294

**Example**

```js
function printfDescription(obj) {
    for (let item in obj) {
        let property = obj[item];
        console.info('video key is ' + item);
        console.info('video value is ' + property);
    }
}

let arrayDescription;
G
Gloria 已提交
1295 1296 1297
videoPlayer.getTrackDescription().then((arrList) => {
    if (arrList != null) {
        arrayDescription = arrList;
W
wusongqing 已提交
1298 1299 1300
    } else {
        console.log('video getTrackDescription fail');
    }
W
wusongqing 已提交
1301
}).catch((error) => {
G
Gloria 已提交
1302
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
1303
});
W
wusongqing 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312
for (let i = 0; i < arrayDescription.length; i++) {
    printfDescription(arrayDescription[i]);
}
```

### setSpeed<sup>8+</sup>

setSpeed(speed:number, callback: AsyncCallback\<number>): void

G
Gloria 已提交
1313
Sets the video playback speed. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1314 1315 1316 1317 1318

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

1319 1320 1321 1322
| Name  | Type                  | Mandatory| Description                                                      |
| -------- | ---------------------- | ---- | ---------------------------------------------------------- |
| speed    | number                 | Yes  | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).|
| callback | AsyncCallback\<number> | Yes  | Callback used to return the result.                                  |
W
wusongqing 已提交
1323 1324 1325 1326

**Example**

```js
W
wusongqing 已提交
1327 1328 1329 1330
import media from '@ohos.multimedia.media'
let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X;

videoPlayer.setSpeed(speed, (err, result) => {
W
wusongqing 已提交
1331
    if (err == null) {
W
wusongqing 已提交
1332 1333
        console.info('setSpeed success!');
    } else {
W
wusongqing 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
        console.info('setSpeed fail!');
    }
});
```

### setSpeed<sup>8+</sup>

setSpeed(speed:number): Promise\<number>

Sets the video playback speed. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

| Name| Type  | Mandatory| Description                                                      |
| ------ | ------ | ---- | ---------------------------------------------------------- |
| speed  | number | Yes  | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).|

W
wusongqing 已提交
1353 1354
**Return value**

G
Gloria 已提交
1355 1356
| Type            | Description                                                        |
| ---------------- | ------------------------------------------------------------ |
1357
| Promise\<number>| Promise used to return playback speed. For details, see [PlaybackSpeed](#playbackspeed8).|
W
wusongqing 已提交
1358

W
wusongqing 已提交
1359 1360 1361
**Example**

```js
W
wusongqing 已提交
1362 1363 1364
import media from '@ohos.multimedia.media'
let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X;

G
Gloria 已提交
1365
videoPlayer.setSpeed(speed).then(() => {
W
wusongqing 已提交
1366
    console.info('setSpeed success');
W
wusongqing 已提交
1367
}).catch((error) => {
G
Gloria 已提交
1368
   console.info(`video catchCallback, error:${error}`);
W
wusongqing 已提交
1369
});
W
wusongqing 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
```

### on('playbackCompleted')<sup>8+</sup>

on(type: 'playbackCompleted', callback: Callback\<void>): void

Subscribes to the video playback completion event.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

| Name  | Type    | Mandatory| Description                                                       |
| -------- | -------- | ---- | ----------------------------------------------------------- |
W
wusongqing 已提交
1384
| type     | string   | Yes  | Event type, which is **'playbackCompleted'** in this case.|
W
wusongqing 已提交
1385 1386 1387 1388 1389 1390
| callback | function | Yes  | Callback invoked when the event is triggered.                                 |

**Example**

```js
videoPlayer.on('playbackCompleted', () => {
W
wusongqing 已提交
1391
    console.info('playbackCompleted success!');
W
wusongqing 已提交
1392 1393 1394 1395 1396 1397 1398
});
```

### on('bufferingUpdate')<sup>8+</sup>

on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void

G
Gloria 已提交
1399
Subscribes to the video buffering update event. Only network playback supports this subscription.
W
wusongqing 已提交
1400 1401 1402 1403 1404 1405 1406

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

| Name  | Type    | Mandatory| Description                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1407
| type     | string   | Yes  | Event type, which is **'bufferingUpdate'** in this case.       |
W
wusongqing 已提交
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
| callback | function | Yes  | Callback invoked when the event is triggered.<br>When [BufferingInfoType](#bufferinginfotype8) is set to **BUFFERING_PERCENT** or **CACHED_DURATION**, **value** is valid. Otherwise, **value** is fixed at **0**.|

**Example**

```js
videoPlayer.on('bufferingUpdate', (infoType, value) => {
    console.log('video bufferingInfo type: ' + infoType);
    console.log('video bufferingInfo value: ' + value);
});
```

### on('startRenderFrame')<sup>8+</sup>

on(type: 'startRenderFrame', callback: Callback\<void>): void

Subscribes to the frame rendering start event.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

W
wusongqing 已提交
1429 1430
| Name  | Type           | Mandatory| Description                                                        |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1431
| type     | string          | Yes  | Event type, which is **'startRenderFrame'** in this case.|
W
wusongqing 已提交
1432
| callback | Callback\<void> | Yes  | Callback invoked when the event is triggered.                          |
W
wusongqing 已提交
1433 1434 1435 1436 1437

**Example**

```js
videoPlayer.on('startRenderFrame', () => {
W
wusongqing 已提交
1438
    console.info('startRenderFrame success!');
W
wusongqing 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
});
```

### on('videoSizeChanged')<sup>8+</sup>

on(type: 'videoSizeChanged', callback: (width: number, height: number) => void): void

Subscribes to the video width and height change event.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

| Name  | Type    | Mandatory| Description                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1454
| type     | string   | Yes  | Event type, which is **'videoSizeChanged'** in this case.|
W
wusongqing 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
| callback | function | Yes  | Callback invoked when the event is triggered. **width** indicates the video width, and **height** indicates the video height.   |

**Example**

```js
videoPlayer.on('videoSizeChanged', (width, height) => {
    console.log('video width is: ' + width);
    console.log('video height is: ' + height);
});
```

### on('error')<sup>8+</sup>

on(type: 'error', callback: ErrorCallback): void

1470
Subscribes to video playback error events. After an error event is reported, you must handle the event and exit the playback.
W
wusongqing 已提交
1471 1472 1473 1474 1475

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

W
wusongqing 已提交
1476 1477
| Name  | Type         | Mandatory| Description                                                        |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1478
| type     | string        | Yes  | Event type, which is **'error'** in this case.<br>The **'error'** event is triggered when an error occurs during video playback.|
W
wusongqing 已提交
1479
| callback | ErrorCallback | Yes  | Callback invoked when the event is triggered.                                      |
W
wusongqing 已提交
1480 1481 1482 1483 1484

**Example**

```js
videoPlayer.on('error', (error) => {      // Set the 'error' event callback.
G
Gloria 已提交
1485
    console.info(`video error called, error: ${error}`);
W
wusongqing 已提交
1486
});
1487
videoPlayer.url = 'fd://error';  // Set an incorrect URL to trigger the 'error' event.
W
wusongqing 已提交
1488 1489
```

1490
### on('availableBitratesCollect')<sup>9+</sup>
W
wusongqing 已提交
1491

1492
on(type: 'availableBitratesCollect', callback: (bitrates: Array\<number>) => void): void
W
wusongqing 已提交
1493 1494 1495 1496 1497 1498 1499 1500 1501

Subscribes to the video playback bit rate reporting event.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

| Name  | Type    | Mandatory| Description                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
1502 1503
| type     | string   | Yes  | Event type, which is **'availableBitratesCollect'** in this case. This event is reported only once when the playback starts.|
| callback | function | Yes  | Callback used to return supported bit rates, in an array.          |
W
wusongqing 已提交
1504 1505 1506 1507

**Example**

```js
1508
videoPlayer.on('availableBitratesCollect', (bitrates) => {
W
wusongqing 已提交
1509
    for (let i = 0; i < bitrates.length; i++) {
1510
        console.info('case availableBitratesCollect bitrates: '  + bitrates[i]);  // Print bit rates.
W
wusongqing 已提交
1511 1512 1513 1514
    }
});
```

W
wusongqing 已提交
1515 1516 1517 1518
## VideoPlayState<sup>8+</sup>

Enumerates the video playback states. You can obtain the state through the **state** attribute.

W
wusongqing 已提交
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

| Name    | Type  | Description          |
| -------- | ------ | -------------- |
| idle     | string | The video player is idle.|
| prepared | string | Video playback is being prepared.|
| playing  | string | Video playback is in progress.|
| paused   | string | Video playback is paused.|
| stopped  | string | Video playback is stopped.|
| error    | string | Video playback is in the error state.    |
W
wusongqing 已提交
1529 1530 1531

## SeekMode<sup>8+</sup>

W
wusongqing 已提交
1532
Enumerates the video playback seek modes, which can be passed in the **seek** API.
W
wusongqing 已提交
1533

W
wusongqing 已提交
1534 1535
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
1536 1537
| Name          | Value  | Description                                                        |
| -------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1538 1539
| SEEK_NEXT_SYNC | 0    | Seeks to the next key frame at the specified position. You are advised to use this value for the rewind operation.|
| SEEK_PREV_SYNC | 1    | Seeks to the previous key frame at the specified position. You are advised to use this value for the fast-forward operation.|
W
wusongqing 已提交
1540 1541 1542

## PlaybackSpeed<sup>8+</sup>

W
wusongqing 已提交
1543
Enumerates the video playback speeds, which can be passed in the **setSpeed** API.
W
wusongqing 已提交
1544

W
wusongqing 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

| Name                | Value  | Description                          |
| -------------------- | ---- | ------------------------------ |
| SPEED_FORWARD_0_75_X | 0    | Plays the video at 0.75 times the normal speed.|
| SPEED_FORWARD_1_00_X | 1    | Plays the video at the normal speed.        |
| SPEED_FORWARD_1_25_X | 2    | Plays the video at 1.25 times the normal speed.|
| SPEED_FORWARD_1_75_X | 3    | Plays the video at 1.75 times the normal speed.|
| SPEED_FORWARD_2_00_X | 4    | Plays the video at 2.00 times the normal speed.|
W
wusongqing 已提交
1554

1555 1556 1557 1558 1559 1560
## VideoScaleType<sup>9+</sup>

Enumerates the video scale modes.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

1561
| Name                        | Value| Description    |
1562 1563 1564 1565
| ---------------------------- | ------ | ---------- |
| VIDEO_SCALE_TYPE_FIT     | 0      | The video will be stretched to fit the window.|
| VIDEO_SCALE_TYPE_FIT_CROP| 1      | The video will be stretched to fit the window, without changing its aspect ratio. The content may be cropped.    |

W
wusongqing 已提交
1566 1567 1568 1569
## MediaDescription<sup>8+</sup>

Defines media information in key-value mode.

W
wusongqing 已提交
1570 1571
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
1572 1573 1574
**Example**

```js
G
Gloria 已提交
1575
import media from '@ohos.multimedia.media'
W
wusongqing 已提交
1576 1577
function printfItemDescription(obj, key) {
    let property = obj[key];
G
Gloria 已提交
1578 1579
    console.info('audio key is ' + key); // Specify a key. For details about the keys, see [MediaDescriptionKey].
    console.info('audio value is ' + property); // Obtain the value of the key. The value can be any type. For details about the types, see [MediaDescriptionKey].
W
wusongqing 已提交
1580
}
G
Gloria 已提交
1581
let audioPlayer = media.createAudioPlayer();
G
Gloria 已提交
1582 1583 1584 1585
audioPlayer.getTrackDescription((error, arrList) => {
    if (arrList != null) {
        for (let i = 0; i < arrList.length; i++) {
            printfItemDescription(arrList[i], media.MediaDescriptionKey.MD_KEY_TRACK_TYPE);  // Print the MD_KEY_TRACK_TYPE value of each track.
W
wusongqing 已提交
1586 1587
        }
    } else {
G
Gloria 已提交
1588
        console.log(`audio getTrackDescription fail, error:${error}`);
W
wusongqing 已提交
1589 1590 1591 1592 1593 1594
    }
});
```

## AudioRecorder

1595
Implements audio recording. Before calling an API of **AudioRecorder**, you must use [createAudioRecorder()](#mediacreateaudiorecorder) to create an [AudioRecorder](#audiorecorder) instance.
W
wusongqing 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612

For details about the audio recording demo, see [Audio Recording Development](../../media/audio-recorder.md).

### prepare<a name=audiorecorder_prepare></a>

prepare(config: AudioRecorderConfig): void

Prepares for recording.

**Required permissions:** ohos.permission.MICROPHONE

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Parameters**

| Name| Type                                       | Mandatory| Description                                                        |
| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1613
| config | [AudioRecorderConfig](#audiorecorderconfig) | Yes  | Audio recording parameters, including the audio output URI, encoding format, sampling rate, number of audio channels, and output format.|
W
wusongqing 已提交
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637

**Example**

```js
let audioRecorderConfig = {
    audioEncoder : media.AudioEncoder.AAC_LC,
    audioEncodeBitRate : 22050,
    audioSampleRate : 22050,
    numberOfChannels : 2,
    format : media.AudioOutputFormat.AAC_ADTS,
    uri : 'fd://1',       // The file must be created by the caller and granted with proper permissions.
    location : { latitude : 30, longitude : 130},
}
audioRecorder.on('prepare', () => {    // Set the 'prepare' event callback.
    console.log('prepare success');
});
audioRecorder.prepare(audioRecorderConfig);
```


### start<a name=audiorecorder_start></a>

start(): void

W
wusongqing 已提交
1638
Starts audio recording. This API can be called only after the [prepare](#audiorecorder_on) event is triggered.
W
wusongqing 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Example**

```js
audioRecorder.on('start', () => {    // Set the 'start' event callback.
    console.log('audio recorder start success');
});
audioRecorder.start();
```

### pause<a name=audiorecorder_pause></a>

pause():void

W
wusongqing 已提交
1655
Pauses audio recording. This API can be called only after the [start](#audiorecorder_on) event is triggered.
W
wusongqing 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Example**

```js
audioRecorder.on('pause', () => {    // Set the 'pause' event callback.
    console.log('audio recorder pause success');
});
audioRecorder.pause();
```

### resume<a name=audiorecorder_resume></a>

resume():void

W
wusongqing 已提交
1672
Resumes audio recording. This API can be called only after the [pause](#audiorecorder_on) event is triggered.
W
wusongqing 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Example**

```js
audioRecorder.on('resume', () => { // Set the 'resume' event callback.
    console.log('audio recorder resume success');
});
audioRecorder.resume();
```

### stop<a name=audiorecorder_stop></a>

stop(): void

Stops audio recording.

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Example**

```js
audioRecorder.on('stop', () => {    // Set the 'stop' event callback.
    console.log('audio recorder stop success');
});
audioRecorder.stop();
```

### release<a name=audiorecorder_release></a>

release(): void

Releases the audio recording resource.

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Example**

```js
audioRecorder.on('release', () => {    // Set the 'release' event callback.
    console.log('audio recorder release success');
});
audioRecorder.release();
audioRecorder = undefined;
```

### reset<a name=audiorecorder_reset></a>

reset(): void

Resets audio recording.

Before resetting audio recording, you must call [stop()](#audiorecorder_stop) to stop recording. After audio recording is reset, you must call [prepare()](#audiorecorder_prepare) to set the recording parameters for another recording.

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Example**

```js
audioRecorder.on('reset', () => {    // Set the 'reset' event callback.
    console.log('audio recorder reset success');
});
audioRecorder.reset();
```

### on('prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset')<a name=audiorecorder_on></a>

on(type: 'prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset', callback: () => void): void

Subscribes to the audio recording events.

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Parameters**

| Name  | Type    | Mandatory| Description                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
G
Gloria 已提交
1751
| type     | string   | Yes  | Event type. The following events are supported:<br>- 'prepare': triggered when the [prepare](#audiorecorder_prepare) API is called and the audio recording parameters are set.<br>- 'start': triggered when the [start](#audiorecorder_start) API is called and audio recording starts.<br>- 'pause': triggered when the [pause](#audiorecorder_pause) API is called and audio recording is paused.<br>- 'resume': triggered when the [resume](#audiorecorder_resume) API is called and audio recording is resumed.<br>- 'stop': triggered when the [stop](#audiorecorder_stop) API is called and audio recording stops.<br>- 'release': triggered when the [release](#audiorecorder_release) API is called and the recording resource is released.<br>- 'reset': triggered when the [reset](#audiorecorder_reset) API is called and audio recording is reset.|
W
wusongqing 已提交
1752 1753 1754 1755 1756
| callback | ()=>void | Yes  | Callback invoked when the event is triggered.                                          |

**Example**

```js
G
Gloria 已提交
1757
let audioRecorder = media.createAudioRecorder();                                  // Create an AudioRecorder instance.
W
wusongqing 已提交
1758
let audioRecorderConfig = {
1759
    audioEncoder : media.AudioEncoder.AAC_LC,
W
wusongqing 已提交
1760 1761 1762 1763 1764 1765 1766
    audioEncodeBitRate : 22050,
    audioSampleRate : 22050,
    numberOfChannels : 2,
    format : media.AudioOutputFormat.AAC_ADTS,
    uri : 'fd://xx',                                                            // The file must be created by the caller and granted with proper permissions.
    location : { latitude : 30, longitude : 130},
}
1767
audioRecorder.on('error', (error) => {                                             // Set the 'error' event callback.
G
Gloria 已提交
1768
    console.info(`audio error called, error: ${error}`);
W
wusongqing 已提交
1769
});
1770
audioRecorder.on('prepare', () => {                                              // Set the 'prepare' event callback.
W
wusongqing 已提交
1771
    console.log('prepare success');
1772
    audioRecorder.start();                                                       // Start recording and trigger the 'start' event callback.
W
wusongqing 已提交
1773
});
1774
audioRecorder.on('start', () => {                                                 // Set the 'start' event callback.
W
wusongqing 已提交
1775 1776
    console.log('audio recorder start success');
});
1777
audioRecorder.on('pause', () => {                                                 // Set the 'pause' event callback.
W
wusongqing 已提交
1778 1779
    console.log('audio recorder pause success');
});
1780
audioRecorder.on('resume', () => {                                                 // Set the 'resume' event callback.
W
wusongqing 已提交
1781 1782
    console.log('audio recorder resume success');
});
1783
audioRecorder.on('stop', () => {                                                 // Set the 'stop' event callback.
W
wusongqing 已提交
1784 1785
    console.log('audio recorder stop success');
});
1786
audioRecorder.on('release', () => {                                                 // Set the 'release' event callback.
W
wusongqing 已提交
1787 1788
    console.log('audio recorder release success');
});
1789
audioRecorder.on('reset', () => {                                                 // Set the 'reset' event callback.
W
wusongqing 已提交
1790 1791
    console.log('audio recorder reset success');
});
1792
audioRecorder.prepare(audioRecorderConfig)                                       // Set recording parameters and trigger the 'prepare' event callback.
W
wusongqing 已提交
1793 1794 1795 1796 1797 1798
```

### on('error')

on(type: 'error', callback: ErrorCallback): void

1799
Subscribes to audio recording error events. After an error event is reported, you must handle the event and exit the recording.
W
wusongqing 已提交
1800 1801 1802 1803 1804 1805 1806

**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

**Parameters**

| Name  | Type         | Mandatory| Description                                                        |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1807
| type     | string        | Yes  | Event type, which is **'error'** in this case.<br>The **'error'** event is triggered when an error occurs during audio recording.|
W
wusongqing 已提交
1808 1809 1810 1811 1812
| callback | ErrorCallback | Yes  | Callback invoked when the event is triggered.                                      |

**Example**

```js
G
Gloria 已提交
1813
let audioRecorderConfig = {
1814
    audioEncoder : media.AudioEncoder.AAC_LC,
G
Gloria 已提交
1815 1816 1817 1818 1819 1820 1821
    audioEncodeBitRate : 22050,
    audioSampleRate : 22050,
    numberOfChannels : 2,
    format : media.AudioOutputFormat.AAC_ADTS,
    uri : 'fd://xx',                                                     // The file must be created by the caller and granted with proper permissions.
    location : { latitude : 30, longitude : 130},
}
1822
audioRecorder.on('error', (error) => {                                  // Set the 'error' event callback.
G
Gloria 已提交
1823
    console.info(`audio error called, error: ${error}`); 
W
wusongqing 已提交
1824
});
G
Gloria 已提交
1825
audioRecorder.prepare(audioRecorderConfig);                            // Do no set any parameter in prepare and trigger the 'error' event callback.
W
wusongqing 已提交
1826 1827 1828 1829 1830 1831
```

## AudioRecorderConfig

Describes audio recording configurations.

W
wusongqing 已提交
1832 1833
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

W
wusongqing 已提交
1834 1835
| Name                 | Type                               | Mandatory| Description                                                        |
| --------------------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1836
| audioEncoder<sup>(deprecated)</sup>          | [AudioEncoder](#audioencoder)           | No  | Audio encoding format. The default value is **AAC_LC**.<br>**Note**: This parameter is deprecated since API version 8. Use **audioEncoderMime** instead.                            |
W
wusongqing 已提交
1837 1838 1839
| audioEncodeBitRate    | number                                  | No  | Audio encoding bit rate. The default value is **48000**.                             |
| audioSampleRate       | number                                  | No  | Audio sampling rate. The default value is **48000**.                             |
| numberOfChannels      | number                                  | No  | Number of audio channels. The default value is **2**.                                 |
W
wusongqing 已提交
1840
| format<sup>(deprecated)</sup>                | [AudioOutputFormat](#audiooutputformat) | No  | Audio output format. The default value is **MPEG_4**.<br>**Note**: This parameter is deprecated since API version 8. Use **fileFormat** instead.                        |
W
wusongqing 已提交
1841
| location              | [Location](#location)                   | No  | Geographical location of the recorded audio.                                        |
W
wusongqing 已提交
1842 1843 1844
| uri                   | string                                  | Yes  | Audio output URI. Supported: fd://xx (fd number)<br>![](figures/en-us_image_url.png) <br>The file must be created by the caller and granted with proper permissions.|
| audioEncoderMime<sup>8+</sup>      | [CodecMimeType](#codecmimetype8)        | No  | Audio encoding format.          |
| fileFormat<sup>8+</sup>      | [ContainerFormatType](#containerformattype8)        | No  | Audio encoding format.        |
W
wusongqing 已提交
1845

W
wusongqing 已提交
1846 1847 1848 1849
## AudioEncoder<sup>(deprecated)</sup>

> **NOTE**
> This API is deprecated since API version 8. You are advised to use [CodecMimeType](#codecmimetype8) instead.
W
wusongqing 已提交
1850 1851 1852

Enumerates the audio encoding formats.

W
wusongqing 已提交
1853 1854
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

1855 1856 1857 1858 1859 1860 1861
| Name   | Value  | Description                                                        |
| ------- | ---- | ------------------------------------------------------------ |
| DEFAULT | 0    | Default encoding format.<br>This API is defined but not implemented yet.             |
| AMR_NB  | 1    | AMR-NB.<br>This API is defined but not implemented yet.|
| AMR_WB  | 2    | Adaptive Multi Rate-Wide Band Speech Codec (AMR-WB).<br>This API is defined but not implemented yet.|
| AAC_LC  | 3    | Advanced Audio Coding Low Complexity (AAC-LC).|
| HE_AAC  | 4    | High-Efficiency Advanced Audio Coding (HE_AAC).<br>This API is defined but not implemented yet.|
W
wusongqing 已提交
1862 1863


W
wusongqing 已提交
1864 1865 1866
## AudioOutputFormat<sup>(deprecated)</sup>

> **NOTE**
W
wusongqing 已提交
1867
> This API is deprecated since API version 8. You are advised to use [ContainerFormatType](#containerformattype8) instead.
W
wusongqing 已提交
1868 1869 1870

Enumerates the audio output formats.

W
wusongqing 已提交
1871 1872
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

1873 1874 1875 1876 1877 1878 1879
| Name    | Value  | Description                                                        |
| -------- | ---- | ------------------------------------------------------------ |
| DEFAULT  | 0    | Default encapsulation format.<br>This API is defined but not implemented yet.             |
| MPEG_4   | 2    | MPEG-4.                                          |
| AMR_NB   | 3    | AMR_NB.<br>This API is defined but not implemented yet.         |
| AMR_WB   | 4    | AMR_WB.<br>This API is defined but not implemented yet.         |
| AAC_ADTS | 6    | Audio Data Transport Stream (ADTS), which is a transport stream format of AAC-based audio.|
W
wusongqing 已提交
1880

W
wusongqing 已提交
1881
## VideoRecorder<sup>9+</sup>
W
wusongqing 已提交
1882

W
wusongqing 已提交
1883
Implements video recording. Before calling an API of the **VideoRecorder** class, you must call [createVideoRecorder()](#mediacreatevideorecorder9) to create a [VideoRecorder](#videorecorder9) instance.
W
wusongqing 已提交
1884 1885 1886 1887 1888

For details about the video recording demo, see [Video Recording Development](../../media/video-recorder.md).

### Attributes

W
wusongqing 已提交
1889 1890
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

1891 1892
**System API**: This is a system API.

W
wusongqing 已提交
1893 1894
| Name              | Type                                  | Readable| Writable| Description            |
| ------------------ | -------------------------------------- | ---- | ---- | ---------------- |
W
wusongqing 已提交
1895
| state<sup>9+</sup> | [VideoRecordState](#videorecordstate9) | Yes  | No  | Video recording state.|
W
wusongqing 已提交
1896

W
wusongqing 已提交
1897
### prepare<sup>9+</sup><a name=videorecorder_prepare1></a>
W
wusongqing 已提交
1898 1899 1900

prepare(config: VideoRecorderConfig, callback: AsyncCallback\<void>): void;

G
Gloria 已提交
1901
Sets video recording parameters. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1902

W
wusongqing 已提交
1903
**Required permissions:** ohos.permission.MICROPHONE
W
wusongqing 已提交
1904 1905 1906

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

1907 1908
**System API**: This is a system API.

W
wusongqing 已提交
1909 1910
**Parameters**

W
wusongqing 已提交
1911 1912
| Name  | Type                                        | Mandatory| Description                               |
| -------- | -------------------------------------------- | ---- | ----------------------------------- |
W
wusongqing 已提交
1913
| config   | [VideoRecorderConfig](#videorecorderconfig9) | Yes  | Video recording parameters to set.           |
W
wusongqing 已提交
1914
| callback | AsyncCallback\<void>                         | Yes  | Callback used to return the result.|
W
wusongqing 已提交
1915

1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                               |
| -------- | --------------------------------------- |
| 201      | Permission denied. Return by callback.  |
| 401      | Parameter error. Return by callback.    |
| 5400102  | Operate not permit. Return by callback. |
| 5400105  | Service died. Return by callback.       |

W
wusongqing 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
**Example**

```js
let videoProfile = {
    audioBitrate : 48000,
    audioChannels : 2,
    audioCodec : 'audio/mp4a-latm',
    audioSampleRate : 48000,
    fileFormat : 'mp4',
    videoBitrate : 48000,
    videoCodec : 'video/mp4v-es',
    videoFrameWidth : 640,
    videoFrameHeight : 480,
    videoFrameRate : 30
}

let videoConfig = {
    audioSourceType : 1,
    videoSourceType : 0,
    profile : videoProfile,
    url : 'fd://xx',   // The file must be created by the caller and granted with proper permissions.
    orientationHint : 0,
    location : { latitude : 30, longitude : 130 },
}

// asyncallback
1953 1954 1955
videoRecorder.prepare(videoConfig, (err) => {
    if (err == null) {
        console.info('prepare success');
W
wusongqing 已提交
1956
    } else {
1957
        console.info('prepare failed and error is ' + err.message);
W
wusongqing 已提交
1958
    }
1959
})
W
wusongqing 已提交
1960 1961
```

W
wusongqing 已提交
1962
### prepare<sup>9+</sup><a name=videorecorder_prepare2></a>
W
wusongqing 已提交
1963 1964 1965

prepare(config: VideoRecorderConfig): Promise\<void>;

G
Gloria 已提交
1966
Sets video recording parameters. This API uses a promise to return the result.
W
wusongqing 已提交
1967

1968
**Required permissions:** ohos.permission.MICROPHONE
W
wusongqing 已提交
1969 1970 1971

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

1972 1973
**System API**: This is a system API.

W
wusongqing 已提交
1974 1975
**Parameters**

W
wusongqing 已提交
1976 1977
| Name| Type                                        | Mandatory| Description                    |
| ------ | -------------------------------------------- | ---- | ------------------------ |
W
wusongqing 已提交
1978
| config | [VideoRecorderConfig](#videorecorderconfig9) | Yes  | Video recording parameters to set.|
W
wusongqing 已提交
1979 1980 1981 1982 1983 1984 1985

**Return value**

| Type          | Description                                    |
| -------------- | ---------------------------------------- |
| Promise\<void> | Promise used to return the result.|

1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                              |
| -------- | -------------------------------------- |
| 201      | Permission denied. Return by promise.  |
| 401      | Parameter error. Return by promise.    |
| 5400102  | Operate not permit. Return by promise. |
| 5400105  | Service died. Return by promise.       |

W
wusongqing 已提交
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
**Example**

```js
let videoProfile = {
    audioBitrate : 48000,
    audioChannels : 2,
    audioCodec : 'audio/mp4a-latm',
    audioSampleRate : 48000,
    fileFormat : 'mp4',
    videoBitrate : 48000,
    videoCodec : 'video/mp4v-es',
    videoFrameWidth : 640,
    videoFrameHeight : 480,
    videoFrameRate : 30
}

let videoConfig = {
    audioSourceType : 1,
    videoSourceType : 0,
    profile : videoProfile,
    url : 'fd://xx',   // The file must be created by the caller and granted with proper permissions.
    orientationHint : 0,
    location : { latitude : 30, longitude : 130 },
}

// promise
2023 2024
videoRecorder.prepare(videoConfig).then(() => {
    console.info('prepare success');
W
wusongqing 已提交
2025
}).catch((err) => {
2026
    console.info('prepare failed and catch error is ' + err.message);
W
wusongqing 已提交
2027 2028 2029
});
```

W
wusongqing 已提交
2030
### getInputSurface<sup>9+</sup>
W
wusongqing 已提交
2031 2032 2033 2034 2035 2036 2037

getInputSurface(callback: AsyncCallback\<string>): void;

Obtains the surface required for recording in asynchronous mode. This surface is provided for the caller. The caller obtains the **surfaceBuffer** from this surface and fills in the corresponding data.

Note that the video data must carry the timestamp (in ns) and buffer size, and the start time of the timestamp is based on the system startup time.

W
wusongqing 已提交
2038
This API can be called only after [prepare()](#videorecorder_prepare1) is called.
W
wusongqing 已提交
2039 2040 2041

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2042 2043
**System API**: This is a system API.

W
wusongqing 已提交
2044 2045 2046 2047 2048 2049
**Parameters**

| Name  | Type                  | Mandatory| Description                       |
| -------- | ---------------------- | ---- | --------------------------- |
| callback | AsyncCallback\<string> | Yes  | Callback used to obtain the result.|

2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                               |
| -------- | --------------------------------------- |
| 5400102  | Operate not permit. Return by callback. |
| 5400103  | IO error. Return by callback.           |
| 5400105  | Service died. Return by callback.       |

W
wusongqing 已提交
2060 2061 2062 2063
**Example**

```js
// asyncallback
W
wusongqing 已提交
2064
let surfaceID = null;                                               // Surface ID passed to the external system.
W
wusongqing 已提交
2065
videoRecorder.getInputSurface((err, surfaceId) => {
W
wusongqing 已提交
2066
    if (err == null) {
W
wusongqing 已提交
2067 2068 2069 2070 2071 2072 2073 2074
        console.info('getInputSurface success');
        surfaceID = surfaceId;
    } else {
        console.info('getInputSurface failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2075
### getInputSurface<sup>9+</sup>
W
wusongqing 已提交
2076 2077 2078 2079 2080 2081 2082

getInputSurface(): Promise\<string>;

 Obtains the surface required for recording in asynchronous mode. This surface is provided for the caller. The caller obtains the **surfaceBuffer** from this surface and fills in the corresponding data.

Note that the video data must carry the timestamp (in ns) and buffer size, and the start time of the timestamp is based on the system startup time.

W
wusongqing 已提交
2083
This API can be called only after [prepare()](#videorecorder_prepare1) is called.
W
wusongqing 已提交
2084 2085 2086

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2087 2088
**System API**: This is a system API.

W
wusongqing 已提交
2089 2090 2091 2092 2093 2094
**Return value**

| Type            | Description                            |
| ---------------- | -------------------------------- |
| Promise\<string> | Promise used to return the result.|

2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                              |
| -------- | -------------------------------------- |
| 5400102  | Operate not permit. Return by promise. |
| 5400103  | IO error. Return by promise.           |
| 5400105  | Service died. Return by promise.       |

W
wusongqing 已提交
2105 2106 2107 2108
**Example**

```js
// promise
W
wusongqing 已提交
2109
let surfaceID = null;                                               // Surface ID passed to the external system.
W
wusongqing 已提交
2110
videoRecorder.getInputSurface().then((surfaceId) => {
W
wusongqing 已提交
2111 2112 2113 2114 2115 2116 2117
    console.info('getInputSurface success');
    surfaceID = surfaceId;
}).catch((err) => {
    console.info('getInputSurface failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2118
### start<sup>9+</sup><a name=videorecorder_start1></a>
W
wusongqing 已提交
2119 2120 2121

start(callback: AsyncCallback\<void>): void;

G
Gloria 已提交
2122
Starts video recording. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
2123

2124
This API can be called only after [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) are called, because the data source must pass data to the surface first.
W
wusongqing 已提交
2125 2126 2127

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2128 2129
**System API**: This is a system API.

W
wusongqing 已提交
2130 2131 2132 2133 2134 2135
**Parameters**

| Name  | Type                | Mandatory| Description                        |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                               |
| -------- | --------------------------------------- |
| 5400102  | Operate not permit. Return by callback. |
| 5400103  | IO error. Return by callback.           |
| 5400105  | Service died. Return by callback.       |

W
wusongqing 已提交
2146 2147 2148 2149 2150
**Example**

```js
// asyncallback
videoRecorder.start((err) => {
W
wusongqing 已提交
2151
    if (err == null) {
W
wusongqing 已提交
2152 2153 2154 2155 2156 2157 2158
        console.info('start videorecorder success');
    } else {
        console.info('start videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2159
### start<sup>9+</sup><a name=videorecorder_start2></a>
W
wusongqing 已提交
2160 2161 2162

start(): Promise\<void>;

G
Gloria 已提交
2163
Starts video recording. This API uses a promise to return the result.
W
wusongqing 已提交
2164

2165
This API can be called only after [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) are called, because the data source must pass data to the surface first.
W
wusongqing 已提交
2166 2167 2168

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2169 2170
**System API**: This is a system API.

W
wusongqing 已提交
2171 2172 2173 2174 2175 2176
**Return value**

| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|

2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                              |
| -------- | -------------------------------------- |
| 5400102  | Operate not permit. Return by promise. |
| 5400103  | IO error. Return by promise.           |
| 5400105  | Service died. Return by promise.       |

W
wusongqing 已提交
2187 2188 2189 2190
**Example**

```js
// promise
W
wusongqing 已提交
2191
videoRecorder.start().then(() => {
W
wusongqing 已提交
2192 2193 2194 2195 2196 2197
    console.info('start videorecorder success');
}).catch((err) => {
    console.info('start videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2198
### pause<sup>9+</sup><a name=videorecorder_pause1></a>
W
wusongqing 已提交
2199 2200 2201

pause(callback: AsyncCallback\<void>): void;

G
Gloria 已提交
2202
Pauses video recording. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
2203

W
wusongqing 已提交
2204
This API can be called only after [start()](#videorecorder_start1) is called. You can resume recording by calling [resume()](#videorecorder_resume1).
W
wusongqing 已提交
2205 2206 2207

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2208 2209
**System API**: This is a system API.

W
wusongqing 已提交
2210 2211 2212 2213 2214 2215
**Parameters**

| Name  | Type                | Mandatory| Description                        |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                               |
| -------- | --------------------------------------- |
| 5400102  | Operate not permit. Return by callback. |
| 5400103  | IO error. Return by callback.           |
| 5400105  | Service died. Return by callback.       |

W
wusongqing 已提交
2226 2227 2228 2229 2230
**Example**

```js
// asyncallback
videoRecorder.pause((err) => {
W
wusongqing 已提交
2231
    if (err == null) {
W
wusongqing 已提交
2232 2233 2234 2235 2236 2237 2238
        console.info('pause videorecorder success');
    } else {
        console.info('pause videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2239
### pause<sup>9+</sup><a name=videorecorder_pause2></a>
W
wusongqing 已提交
2240 2241 2242

pause(): Promise\<void>;

G
Gloria 已提交
2243
Pauses video recording. This API uses a promise to return the result.
W
wusongqing 已提交
2244

W
wusongqing 已提交
2245
This API can be called only after [start()](#videorecorder_start1) is called. You can resume recording by calling [resume()](#videorecorder_resume1).
W
wusongqing 已提交
2246 2247 2248

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2249 2250
**System API**: This is a system API.

W
wusongqing 已提交
2251 2252 2253 2254 2255 2256
**Return value**

| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|

2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                              |
| -------- | -------------------------------------- |
| 5400102  | Operate not permit. Return by promise. |
| 5400103  | IO error. Return by promise.           |
| 5400105  | Service died. Return by promise.       |

W
wusongqing 已提交
2267 2268 2269 2270
**Example**

```js
// promise
W
wusongqing 已提交
2271
videoRecorder.pause().then(() => {
W
wusongqing 已提交
2272 2273 2274 2275 2276 2277
    console.info('pause videorecorder success');
}).catch((err) => {
    console.info('pause videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2278
### resume<sup>9+</sup><a name=videorecorder_resume1></a>
W
wusongqing 已提交
2279 2280 2281

resume(callback: AsyncCallback\<void>): void;

G
Gloria 已提交
2282
Resumes video recording. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
2283 2284 2285

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2286 2287
**System API**: This is a system API.

W
wusongqing 已提交
2288 2289 2290 2291 2292 2293
**Parameters**

| Name  | Type                | Mandatory| Description                        |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                               |
| -------- | --------------------------------------- |
| 5400102  | Operate not permit. Return by callback. |
| 5400103  | IO error. Return by callback.           |
| 5400105  | Service died. Return by callback.       |

W
wusongqing 已提交
2304 2305 2306 2307 2308
**Example**

```js
// asyncallback
videoRecorder.resume((err) => {
W
wusongqing 已提交
2309
    if (err == null) {
W
wusongqing 已提交
2310 2311 2312 2313 2314 2315 2316
        console.info('resume videorecorder success');
    } else {
        console.info('resume videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2317
### resume<sup>9+</sup><a name=videorecorder_resume2></a>
W
wusongqing 已提交
2318 2319 2320

resume(): Promise\<void>;

G
Gloria 已提交
2321
Resumes video recording. This API uses a promise to return the result.
W
wusongqing 已提交
2322 2323 2324

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2325 2326
**System API**: This is a system API.

W
wusongqing 已提交
2327 2328 2329 2330 2331 2332
**Return value**

| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|

2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                              |
| -------- | -------------------------------------- |
| 5400102  | Operate not permit. Return by promise. |
| 5400103  | IO error. Return by promise.           |
| 5400105  | Service died. Return by promise.       |

W
wusongqing 已提交
2343 2344 2345 2346
**Example**

```js
// promise
W
wusongqing 已提交
2347
videoRecorder.resume().then(() => {
W
wusongqing 已提交
2348 2349 2350 2351 2352 2353
    console.info('resume videorecorder success');
}).catch((err) => {
    console.info('resume videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2354
### stop<sup>9+</sup><a name=videorecorder_stop1></a>
W
wusongqing 已提交
2355 2356 2357

stop(callback: AsyncCallback\<void>): void;

G
Gloria 已提交
2358
Stops video recording. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
2359

2360
To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) again.
W
wusongqing 已提交
2361 2362 2363

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2364 2365
**System API**: This is a system API.

W
wusongqing 已提交
2366 2367 2368 2369 2370 2371
**Parameters**

| Name  | Type                | Mandatory| Description                        |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                               |
| -------- | --------------------------------------- |
| 5400102  | Operate not permit. Return by callback. |
| 5400103  | IO error. Return by callback.           |
| 5400105  | Service died. Return by callback.       |

W
wusongqing 已提交
2382 2383 2384 2385 2386
**Example**

```js
// asyncallback
videoRecorder.stop((err) => {
W
wusongqing 已提交
2387
    if (err == null) {
W
wusongqing 已提交
2388 2389 2390 2391 2392 2393 2394
        console.info('stop videorecorder success');
    } else {
        console.info('stop videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2395
### stop<sup>9+</sup><a name=videorecorder_stop2></a>
W
wusongqing 已提交
2396 2397 2398

stop(): Promise\<void>;

G
Gloria 已提交
2399
Stops video recording. This API uses a promise to return the result.
W
wusongqing 已提交
2400

2401
To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) again.
W
wusongqing 已提交
2402 2403 2404

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2405 2406
**System API**: This is a system API.

W
wusongqing 已提交
2407 2408 2409 2410 2411 2412
**Return value**

| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|

2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                              |
| -------- | -------------------------------------- |
| 5400102  | Operate not permit. Return by promise. |
| 5400103  | IO error. Return by promise.           |
| 5400105  | Service died. Return by promise.       |

W
wusongqing 已提交
2423 2424 2425 2426
**Example**

```js
// promise
W
wusongqing 已提交
2427
videoRecorder.stop().then(() => {
W
wusongqing 已提交
2428 2429 2430 2431 2432 2433
    console.info('stop videorecorder success');
}).catch((err) => {
    console.info('stop videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2434
### release<sup>9+</sup><a name=videorecorder_release1></a>
W
wusongqing 已提交
2435 2436 2437

release(callback: AsyncCallback\<void>): void;

G
Gloria 已提交
2438
Releases the video recording resource. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
2439 2440 2441

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2442 2443
**System API**: This is a system API.

W
wusongqing 已提交
2444 2445 2446 2447 2448 2449
**Parameters**

| Name  | Type                | Mandatory| Description                            |
| -------- | -------------------- | ---- | -------------------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

2450 2451 2452 2453 2454 2455 2456 2457
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                         |
| -------- | --------------------------------- |
| 5400105  | Service died. Return by callback. |

W
wusongqing 已提交
2458 2459 2460 2461 2462
**Example**

```js
// asyncallback
videoRecorder.release((err) => {
W
wusongqing 已提交
2463
    if (err == null) {
W
wusongqing 已提交
2464 2465 2466 2467 2468 2469 2470
        console.info('release videorecorder success');
    } else {
        console.info('release videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2471
### release<sup>9+</sup><a name=videorecorder_release2></a>
W
wusongqing 已提交
2472 2473 2474

release(): Promise\<void>;

G
Gloria 已提交
2475
Releases the video recording resource. This API uses a promise to return the result.
W
wusongqing 已提交
2476 2477 2478

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2479 2480
**System API**: This is a system API.

W
wusongqing 已提交
2481 2482 2483 2484 2485 2486
**Return value**

| Type          | Description                                     |
| -------------- | ----------------------------------------- |
| Promise\<void> | Promise used to return the result.|

2487 2488 2489 2490 2491 2492 2493 2494
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                         |
| -------- | --------------------------------- |
| 5400105  | Service died. Return by callback. |

W
wusongqing 已提交
2495 2496 2497 2498
**Example**

```js
// promise
W
wusongqing 已提交
2499
videoRecorder.release().then(() => {
W
wusongqing 已提交
2500 2501 2502 2503 2504 2505
    console.info('release videorecorder success');
}).catch((err) => {
    console.info('release videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2506
### reset<sup>9+</sup><a name=videorecorder_reset1></a>
W
wusongqing 已提交
2507 2508 2509

reset(callback: AsyncCallback\<void>): void;

G
Gloria 已提交
2510
Resets video recording. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
2511

2512
To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) again.
W
wusongqing 已提交
2513 2514 2515

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2516 2517
**System API**: This is a system API.

W
wusongqing 已提交
2518 2519 2520 2521 2522 2523
**Parameters**

| Name  | Type                | Mandatory| Description                        |
| -------- | -------------------- | ---- | ---------------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

2524 2525 2526 2527 2528 2529 2530 2531 2532
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                         |
| -------- | --------------------------------- |
| 5400103  | IO error. Return by callback.     |
| 5400105  | Service died. Return by callback. |

W
wusongqing 已提交
2533 2534 2535 2536 2537
**Example**

```js
// asyncallback
videoRecorder.reset((err) => {
W
wusongqing 已提交
2538
    if (err == null) {
W
wusongqing 已提交
2539 2540 2541 2542 2543 2544 2545
        console.info('reset videorecorder success');
    } else {
        console.info('reset videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2546
### reset<sup>9+</sup><a name=videorecorder_reset2></a>
W
wusongqing 已提交
2547 2548 2549

reset(): Promise\<void>;

G
Gloria 已提交
2550
Resets video recording. This API uses a promise to return the result.
W
wusongqing 已提交
2551

2552
To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) again.
W
wusongqing 已提交
2553 2554 2555

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2556 2557
**System API**: This is a system API.

W
wusongqing 已提交
2558 2559 2560 2561 2562 2563
**Return value**

| Type          | Description                                 |
| -------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|

2564 2565 2566 2567 2568 2569 2570 2571 2572
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                        |
| -------- | -------------------------------- |
| 5400103  | IO error. Return by promise.     |
| 5400105  | Service died. Return by promise. |

W
wusongqing 已提交
2573 2574 2575 2576
**Example**

```js
// promise
W
wusongqing 已提交
2577
videoRecorder.reset().then(() => {
W
wusongqing 已提交
2578 2579 2580 2581 2582 2583
    console.info('reset videorecorder success');
}).catch((err) => {
    console.info('reset videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2584
### on('error')<sup>9+</sup>
W
wusongqing 已提交
2585 2586 2587

on(type: 'error', callback: ErrorCallback): void

2588
Subscribes to video recording error events. After an error event is reported, you must handle the event and exit the recording.
W
wusongqing 已提交
2589 2590 2591 2592 2593 2594 2595

**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

**Parameters**

| Name  | Type         | Mandatory| Description                                                        |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
2596
| type     | string        | Yes  | Event type, which is **'error'** in this case.<br>The **'error'** event is triggered when an error occurs during video recording.|
W
wusongqing 已提交
2597 2598
| callback | ErrorCallback | Yes  | Callback invoked when the event is triggered.                                      |

2599 2600 2601 2602 2603 2604 2605 2606 2607
**Error codes**

For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md).

| ID| Error Message                         |
| -------- | --------------------------------- |
| 5400103  | IO error. Return by callback.     |
| 5400105  | Service died. Return by callback. |

W
wusongqing 已提交
2608 2609 2610
**Example**

```js
2611
// This event is reported when an error occurs during the retrieval of videoRecordState.
W
wusongqing 已提交
2612
videoRecorder.on('error', (error) => {                                  // Set the 'error' event callback.
G
Gloria 已提交
2613
    console.info(`audio error called, error: ${error}`); 
2614
})
W
wusongqing 已提交
2615 2616
```

W
wusongqing 已提交
2617
## VideoRecordState<sup>9+</sup>
W
wusongqing 已提交
2618 2619 2620

Enumerates the video recording states. You can obtain the state through the **state** attribute.

W
wusongqing 已提交
2621 2622
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2623 2624
**System API**: This is a system API.

W
wusongqing 已提交
2625 2626 2627 2628 2629 2630 2631
| Name    | Type  | Description                  |
| -------- | ------ | ---------------------- |
| idle     | string | The video recorder is idle.        |
| prepared | string | The video recording parameters are set.|
| playing  | string | Video recording is in progress.        |
| paused   | string | Video recording is paused.        |
| stopped  | string | Video recording is stopped.        |
W
wusongqing 已提交
2632
| error    | string | Video recording is in the error state.            |
W
wusongqing 已提交
2633

W
wusongqing 已提交
2634
## VideoRecorderConfig<sup>9+</sup>
W
wusongqing 已提交
2635 2636 2637

Describes the video recording parameters.

W
wusongqing 已提交
2638 2639
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2640 2641 2642
**System API**: This is a system API.

| Name           | Type                                          | Mandatory| Description                                                        |
W
wusongqing 已提交
2643 2644 2645 2646 2647 2648
| --------------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ |
| audioSourceType | [AudioSourceType](#audiosourcetype9)           | Yes  | Type of the audio source for video recording.                                      |
| videoSourceType | [VideoSourceType](#videosourcetype9)           | Yes  | Type of the video source for video recording.                                      |
| profile         | [VideoRecorderProfile](#videorecorderprofile9) | Yes  | Video recording profile.                                         |
| rotation        | number                                         | No  | Rotation angle of the recorded video.                                        |
| location        | [Location](#location)                          | No  | Geographical location of the recorded video.                                        |
G
Gloria 已提交
2649
| url             | string                                         | Yes  | Video output URL. Supported: fd://xx (fd number)<br>![](figures/en-us_image_url.png) |
W
wusongqing 已提交
2650

W
wusongqing 已提交
2651
## AudioSourceType<sup>9+</sup>
W
wusongqing 已提交
2652 2653 2654

Enumerates the audio source types for video recording.

W
wusongqing 已提交
2655 2656
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2657 2658
**System API**: This is a system API.

W
wusongqing 已提交
2659 2660 2661 2662
| Name                     | Value  | Description                  |
| ------------------------- | ---- | ---------------------- |
| AUDIO_SOURCE_TYPE_DEFAULT | 0    | Default audio input source.|
| AUDIO_SOURCE_TYPE_MIC     | 1    | Mic audio input source. |
W
wusongqing 已提交
2663

W
wusongqing 已提交
2664
## VideoSourceType<sup>9+</sup>
W
wusongqing 已提交
2665 2666 2667

Enumerates the video source types for video recording.

W
wusongqing 已提交
2668 2669
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2670 2671
**System API**: This is a system API.

W
wusongqing 已提交
2672 2673 2674 2675
| Name                         | Value  | Description                           |
| ----------------------------- | ---- | ------------------------------- |
| VIDEO_SOURCE_TYPE_SURFACE_YUV | 0    | The input surface carries raw data.|
| VIDEO_SOURCE_TYPE_SURFACE_ES  | 1    | The input surface carries ES data. |
W
wusongqing 已提交
2676

W
wusongqing 已提交
2677
## VideoRecorderProfile<sup>9+</sup>
W
wusongqing 已提交
2678 2679 2680

Describes the video recording profile.

W
wusongqing 已提交
2681 2682
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

2683 2684 2685
**System API**: This is a system API.

| Name            | Type                                        | Mandatory| Description            |
W
wusongqing 已提交
2686 2687 2688
| ---------------- | -------------------------------------------- | ---- | ---------------- |
| audioBitrate     | number                                       | Yes  | Audio encoding bit rate.|
| audioChannels    | number                                       | Yes  | Number of audio channels.|
W
wusongqing 已提交
2689
| audioCodec       | [CodecMimeType](#codecmimetype8)             | Yes  | Audio encoding format.  |
W
wusongqing 已提交
2690 2691
| audioSampleRate  | number                                       | Yes  | Audio sampling rate.    |
| fileFormat       | [ContainerFormatType](#containerformattype8) | Yes  | Container format of a file.|
W
wusongqing 已提交
2692
| videoBitrate     | number                                       | Yes  | Video encoding bit rate.|
W
wusongqing 已提交
2693
| videoCodec       | [CodecMimeType](#codecmimetype8)             | Yes  | Video encoding format.  |
W
wusongqing 已提交
2694 2695
| videoFrameWidth  | number                                       | Yes  | Width of the recorded video frame.|
| videoFrameHeight | number                                       | Yes  | Height of the recorded video frame.|
W
wusongqing 已提交
2696
| videoFrameRate   | number                                       | Yes  | Video frame rate.  |
W
wusongqing 已提交
2697 2698 2699 2700 2701

## ContainerFormatType<sup>8+</sup>

Enumerates the container format types (CFTs).

W
wusongqing 已提交
2702 2703 2704 2705
**System capability**: SystemCapability.Multimedia.Media.Core

| Name       | Value   | Description                 |
| ----------- | ----- | --------------------- |
G
Gloria 已提交
2706 2707
| CFT_MPEG_4  | 'mp4' | Video container format MPEG-4.|
| CFT_MPEG_4A | 'm4a' | Audio container format M4A.|
W
wusongqing 已提交
2708

W
wusongqing 已提交
2709
## Location
W
wusongqing 已提交
2710 2711 2712

Describes the geographical location of the recorded video.

W
wusongqing 已提交
2713 2714
**System capability**: SystemCapability.Multimedia.Media.Core

2715 2716 2717 2718
| Name     | Type  | Mandatory| Description            |
| --------- | ------ | ---- | ---------------- |
| latitude  | number | Yes  | Latitude of the geographical location.|
| longitude | number | Yes  | Longitude of the geographical location.|