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

W
wusongqing 已提交
3 4 5
> **NOTE**
> 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 已提交
6 7 8 9 10
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 已提交
11
- Video playback ([VideoPlayer](#videoplayer8))
W
wusongqing 已提交
12
- Audio recording ([AudioRecorder](#audiorecorder))
W
wusongqing 已提交
13
- Video recording ([VideoRecorder](#videorecorder9))
W
wusongqing 已提交
14

W
wusongqing 已提交
15
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 已提交
16 17 18

## Modules to Import

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

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

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

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

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

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

W
wusongqing 已提交
33
| Type                       | Description                                                        |
Z
zengyawen 已提交
34
| --------------------------- | ------------------------------------------------------------ |
W
wusongqing 已提交
35 36 37 38 39
| [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 已提交
40
let audioPlayer = media.createAudioPlayer();
W
wusongqing 已提交
41 42
```

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

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

W
wusongqing 已提交
47 48 49
Creates a **VideoPlayer** instance in asynchronous mode. This API uses a callback to return the result.

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

**Parameters**

W
wusongqing 已提交
53 54 55
| Name  | Type                                       | Mandatory| Description                          |
| -------- | ------------------------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback<[VideoPlayer](#videoplayer8)> | Yes  | Callback used to return the **VideoPlayer** instance created.|
Z
zengyawen 已提交
56 57 58

**Example**

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

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

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

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

W
wusongqing 已提交
76 77 78
Creates a **VideoPlayer** instance in asynchronous mode. This API uses a promise to return the result.

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

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

W
wusongqing 已提交
82 83 84
| Type                                 | Description                               |
| ------------------------------------- | ----------------------------------- |
| Promise<[VideoPlayer](#videoplayer8)> | Promise used to return the **VideoPlayer** instance created.|
W
wusongqing 已提交
85 86 87 88

**Example**

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

W
wusongqing 已提交
91
function failureCallback(error) {
W
wusongqing 已提交
92
    console.info(`video failureCallback, error:${error.message}`);
W
wusongqing 已提交
93 94
}
function catchCallback(error) {
W
wusongqing 已提交
95
    console.info(`video catchCallback, error:${error.message}`);
W
wusongqing 已提交
96 97
}

W
wusongqing 已提交
98 99 100 101
await media.createVideoPlayer.then((video) => {
    if (typeof(video) != 'undefined') {
       videoPlayer = video;
       console.info('video createVideoPlayer success');
W
wusongqing 已提交
102
   } else {
W
wusongqing 已提交
103
       console.info('video createVideoPlayer fail');
W
wusongqing 已提交
104 105
   }
}, failureCallback).catch(catchCallback);
Z
zengyawen 已提交
106 107 108
```

## media.createAudioRecorder
W
wusongqing 已提交
109

Z
zengyawen 已提交
110 111
createAudioRecorder(): AudioRecorder

W
wusongqing 已提交
112
Creates an **AudioRecorder** instance to control audio recording.
Z
zengyawen 已提交
113

W
wusongqing 已提交
114 115
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

W
wusongqing 已提交
116
**Return value**
Z
zengyawen 已提交
117

W
wusongqing 已提交
118
| Type                           | Description                                     |
W
wusongqing 已提交
119 120
| ------------------------------- | ----------------------------------------- |
| [AudioRecorder](#audiorecorder) | Returns the **AudioRecorder** instance if the operation is successful; returns **null** otherwise.|
Z
zengyawen 已提交
121 122 123

**Example**

W
wusongqing 已提交
124
```js
W
wusongqing 已提交
125
let audiorecorder = media.createAudioRecorder();
W
wusongqing 已提交
126 127
```

W
wusongqing 已提交
128
## media.createVideoRecorder<sup>9+</sup>
W
wusongqing 已提交
129

W
wusongqing 已提交
130
createVideoRecorder(callback: AsyncCallback\<[VideoRecorder](#videorecorder9)>): void
W
wusongqing 已提交
131 132 133 134 135 136 137 138 139

Creates a **VideoRecorder** instance in asynchronous mode. This API uses a callback to return the result.

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

**Parameters**

| Name  | Type                                           | Mandatory| Description                          |
| -------- | ----------------------------------------------- | ---- | ------------------------------ |
W
wusongqing 已提交
140
| callback | AsyncCallback<[VideoRecorder](#videorecorder9)> | Yes  | Callback used to return the **VideoRecorder** instance created.|
W
wusongqing 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154

**Example**

```js
let videoRecorder

media.createVideoRecorder((error, video) => {
   if (typeof(video) != 'undefined') {
       videoRecorder = video;
       console.info('video createVideoRecorder success');
   } else {
       console.info(`video createVideoRecorder fail, error:${error.message}`);
   }
});
Z
zengyawen 已提交
155
```
W
wusongqing 已提交
156

W
wusongqing 已提交
157
## media.createVideoRecorder<sup>9+</sup>
W
wusongqing 已提交
158

W
wusongqing 已提交
159
createVideoRecorder(): Promise<[VideoRecorder](#videorecorder9)>
W
wusongqing 已提交
160 161 162 163 164 165 166 167 168

Creates a **VideoRecorder** instance in asynchronous mode. This API uses a promise to return the result.

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

**Return value**

| Type                                     | Description                               |
| ----------------------------------------- | ----------------------------------- |
W
wusongqing 已提交
169
| Promise<[VideoRecorder](#videorecorder9)> | Promise used to return the **VideoRecorder** instance created.|
W
wusongqing 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

**Example**

```js
let videoRecorder

function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}

await media.createVideoRecorder.then((video) => {
    if (typeof(video) != 'undefined') {
       videoRecorder = video;
       console.info('video createVideoRecorder success');
   } else {
       console.info('video createVideoRecorder fail');
   }
}, failureCallback).catch(catchCallback);
Z
zengyawen 已提交
191 192
```

W
wusongqing 已提交
193 194


W
wusongqing 已提交
195 196 197 198
## MediaErrorCode<sup>8+</sup>

Enumerates the media error codes.

W
wusongqing 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212
**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 已提交
213 214 215 216 217

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

Enumerates the media types.

W
wusongqing 已提交
218 219 220 221 222 223
**System capability**: SystemCapability.Multimedia.Media.Core

| Name          | Value  | Description      |
| -------------- | ---- | ---------- |
| MEDIA_TYPE_AUD | 0    | Media.|
| MEDIA_TYPE_VID | 1    | Video.|
W
wusongqing 已提交
224 225 226 227 228

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

Enumerates the codec MIME types.

W
wusongqing 已提交
229 230
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
231 232 233 234 235 236 237
| 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.      |
W
wusongqing 已提交
238
| AUDIO_AAC    | "audio/mp4a-latm"     | Audio in MP4A-LATM format.|
W
wusongqing 已提交
239 240
| AUDIO_VORBIS | 'audio/vorbis'        | Audio in Vorbis format.   |
| AUDIO_FLAC   | 'audio/flac'          | Audio in FLAC format.     |
W
wusongqing 已提交
241 242 243 244 245

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

Enumerates the media description keys.

W
wusongqing 已提交
246 247
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
248
| Name                    | Value             | Description                                                        |
W
wusongqing 已提交
249
| ------------------------ | --------------- | ------------------------------------------------------------ |
W
wusongqing 已提交
250 251 252
| 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.                |
W
wusongqing 已提交
253 254 255 256 257
| 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.|
W
wusongqing 已提交
258
| MD_KEY_AUD_CHANNEL_COUNT | "channel_count" | Number of audio channels, which is a number.                        |
W
wusongqing 已提交
259
| MD_KEY_AUD_SAMPLE_RATE   | "sample_rate"   | Sampling rate, which is a number, in units of Hz.      |
W
wusongqing 已提交
260 261 262 263 264

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

Enumerates the buffering event types.

W
wusongqing 已提交
265 266
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
267 268 269 270 271
| Name             | Value  | Description                            |
| ----------------- | ---- | -------------------------------- |
| BUFFERING_START   | 1    | Buffering starts.                  |
| BUFFERING_END     | 2    | Buffering ends.                  |
| BUFFERING_PERCENT | 3    | Buffering progress, in percent.                |
W
wusongqing 已提交
272
| CACHED_DURATION   | 4    | Cache duration, in milliseconds.|
W
wusongqing 已提交
273

Z
zengyawen 已提交
274 275
## AudioPlayer

W
wusongqing 已提交
276
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 已提交
277

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

W
wusongqing 已提交
280 281 282
### Attributes<a name=audioplayer_attributes></a>

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

W
wusongqing 已提交
284
| Name       | Type                     | Readable| Writable| Description                                                        |
W
wusongqing 已提交
285
| ----------- | ------------------------- | ---- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
286
| src         | string                    | Yes  | Yes  | Audio media URI. The mainstream audio formats (MPEG-4, AAC, MPEG-3, OGG, and WAV) are supported.<br>**Example of supported URIs**:<br>1. FD playback: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP network playback: http://xx<br>3. HLS network playback path (under development)<br>**Note**:<br>To use media materials, you must declare the read permission. Otherwise, the media materials cannot be played properly.|
W
wusongqing 已提交
287 288 289 290
| loop        | boolean                   | Yes  | Yes  | Whether to loop audio playback. The value **true** means to loop audio playback, and **false** means the opposite.                |
| currentTime | number                    | Yes  | No  | Current audio playback position.                                        |
| duration    | number                    | Yes  | No  | Audio duration.                                                  |
| state       | [AudioState](#audiostate) | Yes  | No  | Audio playback state.                                            |
Z
zengyawen 已提交
291

W
wusongqing 已提交
292
### play<a name=audioplayer_play></a>
Z
zengyawen 已提交
293

W
wusongqing 已提交
294
play(): void
Z
zengyawen 已提交
295

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

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
Z
zengyawen 已提交
299 300 301

**Example**

W
wusongqing 已提交
302 303 304
```js
audioPlayer.on('play', () => {    // Set the 'play' event callback.
    console.log('audio play success');
Z
zengyawen 已提交
305
});
W
wusongqing 已提交
306
audioPlayer.play();
Z
zengyawen 已提交
307 308
```

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

W
wusongqing 已提交
311
pause(): void
Z
zengyawen 已提交
312 313 314

Pauses audio playback.

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

Z
zengyawen 已提交
317 318
**Example**

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

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

W
wusongqing 已提交
328
stop(): void
Z
zengyawen 已提交
329 330 331

Stops audio playback.

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

Z
zengyawen 已提交
334 335
**Example**

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

W
wusongqing 已提交
343 344 345
### reset<sup>7+</sup><a name=audioplayer_reset></a>

reset(): void
Z
zengyawen 已提交
346

W
wusongqing 已提交
347
Switches the audio resource to be played.
Z
zengyawen 已提交
348

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

W
wusongqing 已提交
351 352 353 354 355 356 357 358 359 360 361 362
**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 已提交
363 364 365

Seeks to the specified playback position.

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

Z
zengyawen 已提交
368 369
**Parameters**

W
wusongqing 已提交
370 371
| Name| Type  | Mandatory| Description                                |
| ------ | ------ | ---- | ------------------------------------ |
W
wusongqing 已提交
372
| timeMs | number | Yes  | Position to seek to, in milliseconds.|
Z
zengyawen 已提交
373 374 375

**Example**

W
wusongqing 已提交
376 377 378 379 380 381 382
```js
audioPlayer.on('timeUpdate', (seekDoneTime) => {    // Set the 'timeUpdate' event callback.
    if (typeof (seekDoneTime) == 'undefined') {
        console.info('audio seek fail');
        return;
    }
    console.log('audio seek success. seekDoneTime: ' + seekDoneTime);
Z
zengyawen 已提交
383
});
W
wusongqing 已提交
384
audioPlayer.seek(30000); // Seek to 30000 ms.
Z
zengyawen 已提交
385 386
```

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

W
wusongqing 已提交
389
setVolume(vol: number): void
Z
zengyawen 已提交
390 391 392

Sets the volume.

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

Z
zengyawen 已提交
395 396
**Parameters**

W
wusongqing 已提交
397
| Name| Type  | Mandatory| Description                                                        |
W
wusongqing 已提交
398
| ------ | ------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
399
| vol    | number | Yes  | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).|
Z
zengyawen 已提交
400 401 402

**Example**

W
wusongqing 已提交
403 404 405
```js
audioPlayer.on('volumeChange', () => {    // Set the 'volumeChange' event callback.
    console.log('audio volumeChange success');
Z
zengyawen 已提交
406
});
W
wusongqing 已提交
407
audioPlayer.setVolume(1);    // Set the volume to 100%.
Z
zengyawen 已提交
408 409
```

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

W
wusongqing 已提交
412
release(): void
Z
zengyawen 已提交
413

W
wusongqing 已提交
414 415 416
Releases the audio playback resource.

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
Z
zengyawen 已提交
417 418 419

**Example**

W
wusongqing 已提交
420 421 422
```js
audioPlayer.release();
audioPlayer = undefined;
Z
zengyawen 已提交
423 424
```

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

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

W
wusongqing 已提交
429 430 431
Obtains the audio track information. This API uses a callback to return the result.

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

W
wusongqing 已提交
433 434
**Parameters**

W
wusongqing 已提交
435
| Name  | Type                                                        | Mandatory| Description                      |
W
wusongqing 已提交
436
| -------- | ------------------------------------------------------------ | ---- | -------------------------- |
W
wusongqing 已提交
437
| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes  | Callback used to return the audio track information obtained.|
Z
zengyawen 已提交
438 439 440

**Example**

W
wusongqing 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
```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);
    }
}

audioPlayer.getTrackDescription((error, arrlist) => {
    if (typeof (arrlist) != 'undefined') {
        for (let i = 0; i < arrlist.length; i++) {
            printfDescription(arrlist[i]);
        }
    } else {
        console.log(`audio getTrackDescription fail, error:${error.message}`);
    }
});
Z
zengyawen 已提交
459
```
W
wusongqing 已提交
460 461 462

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

W
wusongqing 已提交
463
getTrackDescription(): Promise<Array\<MediaDescription>>
W
wusongqing 已提交
464

W
wusongqing 已提交
465 466 467
Obtains the audio track information. This API uses a promise to return the result.

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

W
wusongqing 已提交
469
**Return value**
W
wusongqing 已提交
470

W
wusongqing 已提交
471
| Type                                                  | Description                           |
W
wusongqing 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
| ------------------------------------------------------ | ------------------------------- |
| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return the audio track information obtained.|

**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);
    }
}
function failureCallback(error) {
    console.info(`audio failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`audio catchCallback, error:${error.message}`);
}

await audioPlayer.getTrackDescription.then((arrlist) => {
    if (typeof (arrlist) != 'undefined') {
        arrayDescription = arrlist;
    } else {
        console.log('audio getTrackDescription fail');
    }
}, failureCallback).catch(catchCallback);
for (let i = 0; i < arrayDescription.length; i++) {
    printfDescription(arrayDescription[i]);
}
Z
zengyawen 已提交
502 503
```

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

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

W
wusongqing 已提交
508
Subscribes to the audio buffering update event.
Z
zengyawen 已提交
509

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

Z
zengyawen 已提交
512 513
**Parameters**

W
wusongqing 已提交
514 515 516 517
| Name  | Type    | Mandatory| Description                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| type     | string   | Yes  | Type of the event to subscribe to, which is 'bufferingUpdate' in this example.       |
| 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 已提交
518 519 520

**Example**

W
wusongqing 已提交
521 522 523 524 525
```js
audioPlayer.on('bufferingUpdate', (infoType, value) => {
    console.log('audio bufferingInfo type: ' + infoType);
    console.log('audio bufferingInfo value: ' + value);
});
Z
zengyawen 已提交
526
```
W
wusongqing 已提交
527

W
wusongqing 已提交
528
 ### on('play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange')<a name = audioplayer_on></a>
W
wusongqing 已提交
529 530 531 532 533

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

Subscribes to the audio playback events.

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

W
wusongqing 已提交
536 537
**Parameters**

W
wusongqing 已提交
538
| Name  | Type      | Mandatory| Description                                                        |
W
wusongqing 已提交
539
| -------- | ---------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
540
| type     | string     | Yes  | Type of the event to subscribe to. The following events are supported: 'play' \| 'pause' \| 'stop' \| 'reset' \| 'dataLoad' \| 'finish' \| 'volumeChange'<br>- The 'play' event is triggered when the [play()](#audioplayer_play) API is called and audio playback starts.<br>- The 'pause' event is triggered when the [pause()](#audioplayer_pause) API is called and audio playback is paused.<br>- The 'stop' event is triggered when the [stop()](#audioplayer_stop) API is called and audio playback stops.<br>- The 'reset' event is triggered when the [reset()](#audioplayer_reset) API is called and audio playback is reset.<br>- The 'dataLoad' event is triggered when the audio data is loaded, that is, when the **src** attribute is configured.<br>- The 'finish' event is triggered when the audio playback is finished.<br>- The 'volumeChange' event is triggered when the [setVolume()](#audioplayer_setvolume) API is called and the playback volume is changed.|
W
wusongqing 已提交
541
| callback | () => void | Yes  | Callback invoked when the event is triggered.                                          |
W
wusongqing 已提交
542 543 544 545 546 547

**Example**

```js
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 已提交
548
    console.info('audio set source success');
W
wusongqing 已提交
549 550 551
    audioPlayer.play();                       // Start the playback and trigger the 'play' event callback.
});
audioPlayer.on('play', () => {                // Set the 'play' event callback.
W
wusongqing 已提交
552
    console.info('audio play success');
W
wusongqing 已提交
553
    audioPlayer.seek(30000);                  // Call the seek() API and trigger the 'timeUpdate' event callback.
Z
zengyawen 已提交
554
});
W
wusongqing 已提交
555
audioPlayer.on('pause', () => {               // Set the 'pause' event callback.
W
wusongqing 已提交
556
    console.info('audio pause success');
W
wusongqing 已提交
557 558 559
    audioPlayer.stop();                       // Stop the playback and trigger the 'stop' event callback.
});
audioPlayer.on('reset', () => {               // Set the 'reset' event callback.
W
wusongqing 已提交
560
    console.info('audio reset success');
W
wusongqing 已提交
561 562 563 564
    audioPlayer.release();                    // Release the AudioPlayer instance.
    audioPlayer = undefined;
});
audioPlayer.on('timeUpdate', (seekDoneTime) => {  // Set the 'timeUpdate' event callback.
W
wusongqing 已提交
565
    if (typeof(seekDoneTime) == "undefined") {
W
wusongqing 已提交
566 567 568 569 570 571 572
        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 已提交
573
    console.info('audio volumeChange success');
W
wusongqing 已提交
574 575 576
    audioPlayer.pause();                       // Pause the playback and trigger the 'pause' event callback.
});
audioPlayer.on('finish', () => {               // Set the 'finish' event callback.
W
wusongqing 已提交
577
    console.info('audio play finish');
W
wusongqing 已提交
578 579 580
    audioPlayer.stop();                        // Stop the playback and trigger the 'stop' event callback.
});
audioPlayer.on('error', (error) => {           // Set the 'error' event callback.
W
wusongqing 已提交
581
    console.info(`audio error called, errName is ${error.name}`);
W
wusongqing 已提交
582 583 584
    console.info(`audio error called, errCode is ${error.code}`);
    console.info(`audio error called, errMessage is ${error.message}`);
});
W
wusongqing 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597

// Set the FD (local playback) of the video file selected by the user.
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath;  // Set the src attribute and trigger the 'dataLoad' event callback.
Z
zengyawen 已提交
598 599 600 601 602 603
```

### on('timeUpdate')

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

W
wusongqing 已提交
604 605 606
Subscribes to the 'timeUpdate' event.

**System capability**: SystemCapability.Multimedia.Media.AudioPlayer
Z
zengyawen 已提交
607 608 609

**Parameters**

W
wusongqing 已提交
610
| Name  | Type             | Mandatory| Description                                                        |
W
wusongqing 已提交
611
| -------- | ----------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
612
| type     | string            | Yes  | Type of the event to subscribe to, which is 'timeUpdate' in this API.<br>The 'timeUpdate' event is triggered when the [seek()](#audioplayer_seek) API is called.|
W
wusongqing 已提交
613
| callback | Callback\<number> | Yes  | Callback invoked when the event is triggered. The input parameter of the callback is the time when the seek operation is successful.            |
Z
zengyawen 已提交
614 615 616

**Example**

W
wusongqing 已提交
617 618 619 620 621 622 623
```js
audioPlayer.on('timeUpdate', (seekDoneTime) => {    // Set the 'timeUpdate' event callback.
    if (typeof (seekDoneTime) == 'undefined') {
        console.info('audio seek fail');
        return;
    }
    console.log('audio seek success. seekDoneTime: ' + seekDoneTime);
Z
zengyawen 已提交
624
});
W
wusongqing 已提交
625
audioPlayer.seek(30000); // Seek to 30000 ms.
Z
zengyawen 已提交
626 627 628 629 630 631
```

### on('error')

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

W
wusongqing 已提交
632
Subscribes to the audio playback error event.
Z
zengyawen 已提交
633

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

Z
zengyawen 已提交
636 637
**Parameters**

W
wusongqing 已提交
638
| Name  | Type         | Mandatory| Description                                                        |
W
wusongqing 已提交
639
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
640
| type     | string        | Yes  | Type of the event to subscribe to, which is 'error' in this API.<br>The 'error' event is triggered when an error occurs during audio playback.|
W
wusongqing 已提交
641
| callback | ErrorCallback | Yes  | Callback invoked when the event is triggered.                                      |
Z
zengyawen 已提交
642 643 644

**Example**

W
wusongqing 已提交
645 646
```js
audioPlayer.on('error', (error) => {      // Set the error event callback.
W
wusongqing 已提交
647
    console.info(`audio error called, errName is ${error.name}`);      // Print the error name.
W
wusongqing 已提交
648 649
    console.info(`audio error called, errCode is ${error.code}`);      // Print the error code.
    console.info(`audio error called, errMessage is ${error.message}`);// Print the detailed description of the error type.
Z
zengyawen 已提交
650
});
W
wusongqing 已提交
651
audioPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'error' event.
Z
zengyawen 已提交
652 653 654 655
```

## AudioState

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

W
wusongqing 已提交
658 659 660 661 662 663 664 665 666
**System capability**: SystemCapability.Multimedia.Media.AudioPlayer

| Name              | Type  | Description          |
| ------------------ | ------ | -------------- |
| idle               | string | The audio player is idle.|
| playing            | string | Audio playback is in progress.|
| paused             | string | Audio playback is paused.|
| stopped            | string | Audio playback is stopped.|
| error<sup>8+</sup> | string | Audio playback is in the error state.    |
Z
zengyawen 已提交
667

W
wusongqing 已提交
668
## VideoPlayer<sup>8+</sup>
W
wusongqing 已提交
669

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

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

W
wusongqing 已提交
674 675
### Attributes<a name=videoplayer_attributes></a>

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

W
wusongqing 已提交
678 679
| Name                    | Type                              | Readable| Writable| Description                                                        |
| ------------------------ | ---------------------------------- | ---- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
680
| url<sup>8+</sup>         | string                             | Yes  | Yes  | Video media URL. The mainstream video formats (MPEG-4, MPEG-TS, WebM, and MKV) are supported.<br>**Example of supported URIs**:<br>1. FD playback: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP network playback: http://xx<br>3. HLS network playback path (under development)<br>**Note**:<br>To use media materials, you must declare the read permission. Otherwise, the media materials cannot be played properly.|
W
wusongqing 已提交
681 682 683 684 685 686
| 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.                |
| currentTime<sup>8+</sup> | number                             | Yes  | No  | Current video playback position.                                        |
| duration<sup>8+</sup>    | number                             | Yes  | No  | Video duration. The value **-1** indicates the live streaming mode.                              |
| state<sup>8+</sup>       | [VideoPlayState](#videoplaystate8) | Yes  | No  | Video playback state.                                            |
| width<sup>8+</sup>       | number                             | Yes  | No  | Video width.                                                    |
| height<sup>8+</sup>      | number                             | Yes  | No  | Video height.                                                    |
W
wusongqing 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700

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

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

Sets **SurfaceId**. This API uses a callback to return the result.

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

**Parameters**

| Name   | Type    | Mandatory| Description                     |
| --------- | -------- | ---- | ------------------------- |
| surfaceId | string   | Yes  | Surface ID to set.                |
W
wusongqing 已提交
701
| callback  | function | Yes  | Callback used to return the result.|
W
wusongqing 已提交
702 703 704 705

**Example**

```js
W
wusongqing 已提交
706
videoPlayer.setDisplaySurface(surfaceId, (err) => {
W
wusongqing 已提交
707 708 709
    if (typeof (err) == 'undefined') {
        console.info('setDisplaySurface success!');
    } else {
W
wusongqing 已提交
710
        console.info('setDisplaySurface fail!');
W
wusongqing 已提交
711 712 713
    }
});
```
Z
zengyawen 已提交
714

W
wusongqing 已提交
715
### setDisplaySurface<sup>8+</sup>
Z
zengyawen 已提交
716

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

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

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

W
wusongqing 已提交
723 724 725 726 727 728 729 730 731 732
**Parameters**

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

**Return value**

| Type         | Description                          |
| ------------- | ------------------------------ |
W
wusongqing 已提交
733
| Promise<void> | Promise used to return the result.|
W
wusongqing 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

**Example**

```js
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.setDisplaySurface(surfaceId).then(() => {
    console.info('setDisplaySurface success');
}, failureCallback).catch(catchCallback);
```

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

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

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

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

Z
zengyawen 已提交
757 758
**Parameters**

W
wusongqing 已提交
759 760 761
| Name  | Type    | Mandatory| Description                    |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
762 763 764

**Example**

W
wusongqing 已提交
765 766
```js
videoPlayer.prepare((err) => {
W
wusongqing 已提交
767 768 769
    if (typeof (err) == 'undefined') {
        console.info('prepare success!');
    } else {
W
wusongqing 已提交
770 771 772
        console.info('prepare fail!');
    }
});
W
wusongqing 已提交
773
```
W
wusongqing 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

### 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
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
W
wusongqing 已提交
797
}
W
wusongqing 已提交
798 799 800
await videoPlayer.prepare().then(() => {
    console.info('prepare success');
}, failureCallback).catch(catchCallback);
W
wusongqing 已提交
801 802
```

W
wusongqing 已提交
803
### play<sup>8+</sup>
W
wusongqing 已提交
804

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

W
wusongqing 已提交
807 808 809
Starts to play video resources. This API uses a callback to return the result.

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

W
wusongqing 已提交
811 812 813 814 815
**Parameters**

| Name  | Type    | Mandatory| Description                    |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
816 817 818

**Example**

W
wusongqing 已提交
819 820
```js
videoPlayer.play((err) => {
W
wusongqing 已提交
821 822 823
    if (typeof (err) == 'undefined') {
        console.info('play success!');
    } else {
W
wusongqing 已提交
824 825 826
        console.info('play fail!');
    }
});
Z
zengyawen 已提交
827
```
W
wusongqing 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

### 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
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.play().then(() => {
    console.info('play success');
}, failureCallback).catch(catchCallback);
Z
zengyawen 已提交
855 856
```

W
wusongqing 已提交
857
### pause<sup>8+</sup>
Z
zengyawen 已提交
858

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

W
wusongqing 已提交
861 862 863 864 865 866 867 868 869
Pauses video playback. This API uses a callback to return the result.

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

**Parameters**

| Name  | Type    | Mandatory| Description                    |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
870 871 872

**Example**

W
wusongqing 已提交
873 874
```js
videoPlayer.pause((err) => {
W
wusongqing 已提交
875 876 877
    if (typeof (err) == 'undefined') {
        console.info('pause success!');
    } else {
W
wusongqing 已提交
878 879 880
        console.info('pause fail!');
    }
});
Z
zengyawen 已提交
881
```
W
wusongqing 已提交
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

### 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
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.pause().then(() => {
    console.info('pause success');
}, failureCallback).catch(catchCallback);
Z
zengyawen 已提交
909 910
```

W
wusongqing 已提交
911
### stop<sup>8+</sup>
Z
zengyawen 已提交
912

W
wusongqing 已提交
913 914 915
stop(callback: AsyncCallback\<void>): void

Stops video playback. This API uses a callback to return the result.
Z
zengyawen 已提交
916

W
wusongqing 已提交
917 918 919 920 921 922 923
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

| Name  | Type    | Mandatory| Description                    |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes  | Callback used to return the result.|
W
wusongqing 已提交
924

Z
zengyawen 已提交
925 926
**Example**

W
wusongqing 已提交
927 928
```js
videoPlayer.stop((err) => {
W
wusongqing 已提交
929 930 931
    if (typeof (err) == 'undefined') {
        console.info('stop success!');
    } else {
W
wusongqing 已提交
932 933 934
        console.info('stop fail!');
    }
});
Z
zengyawen 已提交
935
```
W
wusongqing 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962

### 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
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.stop().then(() => {
    console.info('stop success');
}, failureCallback).catch(catchCallback);
Z
zengyawen 已提交
963 964
```

W
wusongqing 已提交
965
### reset<sup>8+</sup>
Z
zengyawen 已提交
966

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

W
wusongqing 已提交
969
Switches the video resource to be played. This API uses a callback to return the result.
Z
zengyawen 已提交
970

W
wusongqing 已提交
971 972 973 974 975 976 977
**System capability**: SystemCapability.Multimedia.Media.VideoPlayer

**Parameters**

| Name  | Type    | Mandatory| Description                    |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
978 979 980

**Example**

W
wusongqing 已提交
981 982
```js
videoPlayer.reset((err) => {
W
wusongqing 已提交
983 984 985
    if (typeof (err) == 'undefined') {
        console.info('reset success!');
    } else {
W
wusongqing 已提交
986 987 988
        console.info('reset fail!');
    }
});
Z
zengyawen 已提交
989
```
W
wusongqing 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

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

reset(): Promise\<void>

Switches the video resource to be played. 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
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.reset().then(() => {
    console.info('reset success');
}, failureCallback).catch(catchCallback);
Z
zengyawen 已提交
1017 1018
```

W
wusongqing 已提交
1019
### seek<sup>8+</sup>
Z
zengyawen 已提交
1020

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

W
wusongqing 已提交
1023 1024 1025
Seeks to the specified playback position. The next key frame at the specified position is played. This API uses a callback to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Z
zengyawen 已提交
1026 1027 1028

**Parameters**

W
wusongqing 已提交
1029 1030
| Name  | Type    | Mandatory| Description                                |
| -------- | -------- | ---- | ------------------------------------ |
W
wusongqing 已提交
1031
| timeMs   | number   | Yes  | Position to seek to, in milliseconds.|
W
wusongqing 已提交
1032
| callback | function | Yes  | Callback used to return the result.      |
Z
zengyawen 已提交
1033 1034 1035

**Example**

W
wusongqing 已提交
1036 1037
```js
videoPlayer.seek((seekTime, err) => {
W
wusongqing 已提交
1038 1039 1040
    if (typeof (err) == 'undefined') {
        console.info('seek success!');
    } else {
W
wusongqing 已提交
1041 1042
        console.info('seek fail!');
    }
Z
zengyawen 已提交
1043 1044 1045
});
```

W
wusongqing 已提交
1046
### seek<sup>8+</sup>
Z
zengyawen 已提交
1047

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

W
wusongqing 已提交
1050 1051 1052
Seeks to the specified playback position. This API uses a callback to return the result.

**System capability**: SystemCapability.Multimedia.Media.VideoPlayer
Z
zengyawen 已提交
1053 1054 1055

**Parameters**

W
wusongqing 已提交
1056 1057
| Name  | Type                  | Mandatory| Description                                |
| -------- | ---------------------- | ---- | ------------------------------------ |
W
wusongqing 已提交
1058
| timeMs   | number                 | Yes  | Position to seek to, in milliseconds.|
W
wusongqing 已提交
1059 1060
| mode     | [SeekMode](#seekmode8) | Yes  | Seek mode.                          |
| callback | function               | Yes  | Callback used to return the result.      |
W
wusongqing 已提交
1061

W
wusongqing 已提交
1062
**Example**
Z
zengyawen 已提交
1063

W
wusongqing 已提交
1064 1065
```js
videoPlayer.seek((seekTime, seekMode, err) => {
W
wusongqing 已提交
1066 1067 1068
    if (typeof (err) == 'undefined') {
        console.info('seek success!');
    } else {
W
wusongqing 已提交
1069 1070 1071 1072
        console.info('seek fail!');
    }
});
```
Z
zengyawen 已提交
1073

W
wusongqing 已提交
1074
### seek<sup>8+</sup>
Z
zengyawen 已提交
1075

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

W
wusongqing 已提交
1078
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 已提交
1079

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

W
wusongqing 已提交
1082
**Parameters**
W
wusongqing 已提交
1083

W
wusongqing 已提交
1084 1085
| Name| Type                  | Mandatory| Description                                |
| ------ | ---------------------- | ---- | ------------------------------------ |
W
wusongqing 已提交
1086
| timeMs | number                 | Yes  | Position to seek to, in milliseconds.|
W
wusongqing 已提交
1087
| mode   | [SeekMode](#seekmode8) | No  | Seek mode.                          |
Z
zengyawen 已提交
1088

W
wusongqing 已提交
1089
**Return value**
Z
zengyawen 已提交
1090

W
wusongqing 已提交
1091 1092 1093
| Type          | Description                               |
| -------------- | ----------------------------------- |
| Promise\<void> | Promise used to return the result.|
Z
zengyawen 已提交
1094

W
wusongqing 已提交
1095
**Example**
Z
zengyawen 已提交
1096

W
wusongqing 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
```js
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.seek(seekTime).then((seekDoneTime) => { // seekDoneTime indicates the position after the seek operation is complete.
    console.info('seek success');
}, failureCallback).catch(catchCallback);

await videoPlayer.seek(seekTime, seekMode).then((seekDoneTime) => {
    console.info('seek success');
}, failureCallback).catch(catchCallback);
```

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

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

Sets the volume. This API uses a callback 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%).|
| callback | function | Yes  | Callback used to return the result.                                        |

**Example**

```js
videoPlayer.setVolume((vol, err) => {
W
wusongqing 已提交
1132 1133 1134
    if (typeof (err) == 'undefined') {
        console.info('setVolume success!');
    } else {
W
wusongqing 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 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 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
        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
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.setVolume(vol).then() => {
    console.info('setVolume success');
}, failureCallback).catch(catchCallback);
```

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

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

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

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

**Parameters**

| Name  | Type    | Mandatory| Description                    |
| -------- | -------- | ---- | ------------------------ |
| callback | function | Yes  | Callback used to return the result.|

**Example**

```js
videoPlayer.release((err) => {
W
wusongqing 已提交
1192 1193 1194
    if (typeof (err) == 'undefined') {
        console.info('release success!');
    } else {
W
wusongqing 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
        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
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.release().then() => {
    console.info('release success');
}, failureCallback).catch(catchCallback);
```

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

W
wusongqing 已提交
1230
getTrackDescription(callback: AsyncCallback<Array\<MediaDescription>>): void
W
wusongqing 已提交
1231 1232 1233 1234 1235 1236 1237

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

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

**Parameters**

W
wusongqing 已提交
1238 1239 1240
| Name  | Type                                                        | Mandatory| Description                      |
| -------- | ------------------------------------------------------------ | ---- | -------------------------- |
| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes  | Callback used to return the video track information obtained.|
W
wusongqing 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265

**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);
    }
}

videoPlayer.getTrackDescription((error, arrlist) => {
    if (typeof (arrlist) != 'undefined') {
        for (let i = 0; i < arrlist.length; i++) {
            printfDescription(arrlist[i]);
        }
    } else {
        console.log(`video getTrackDescription fail, error:${error.message}`);
    }
});
```

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

W
wusongqing 已提交
1266
getTrackDescription(): Promise<Array\<MediaDescription>>
W
wusongqing 已提交
1267 1268 1269 1270 1271 1272 1273

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

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

**Return value**

W
wusongqing 已提交
1274 1275 1276
| Type                                                  | Description                           |
| ------------------------------------------------------ | ------------------------------- |
| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return the video track information obtained.|
W
wusongqing 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

**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);
    }
}
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}

let arrayDescription;
await videoPlayer.getTrackDescription().then((arrlist) => {
    if (typeof (arrlist) != 'undefined') {
        arrayDescription = arrlist;
    } else {
        console.log('video getTrackDescription fail');
    }
}, failureCallback).catch(catchCallback);
for (let i = 0; i < arrayDescription.length; i++) {
    printfDescription(arrayDescription[i]);
}
```

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

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

Sets the video playback speed. This API uses a callback 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).|
| callback | function | Yes  | Callback used to return the result.                                  |

**Example**

```js
videoPlayer.setSpeed((speed:number, err) => {
W
wusongqing 已提交
1327 1328 1329
    if (typeof (err) == 'undefined') {
        console.info('setSpeed success!');
    } else {
W
wusongqing 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
        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 已提交
1349 1350 1351 1352 1353 1354
**Return value**

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

W
wusongqing 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
**Example**

```js
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
await videoPlayer.setSpeed(speed).then() => {
    console.info('setSpeed success');
}, failureCallback).catch(catchCallback);
```

### 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                                                       |
| -------- | -------- | ---- | ----------------------------------------------------------- |
| type     | string   | Yes  | Type of the event to subscribe to, which is 'playbackCompleted' in this example.|
| callback | function | Yes  | Callback invoked when the event is triggered.                                 |

**Example**

```js
videoPlayer.on('playbackCompleted', () => {
W
wusongqing 已提交
1388
    console.info('playbackCompleted success!');
W
wusongqing 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
});
```

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

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

Subscribes to the video buffering update event.

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

**Parameters**

| Name  | Type    | Mandatory| Description                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| type     | string   | Yes  | Type of the event to subscribe to, which is 'bufferingUpdate' in this example.       |
| 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 已提交
1426 1427 1428 1429
| Name  | Type           | Mandatory| Description                                                        |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
| type     | string          | Yes  | Type of the event to subscribe to, which is 'startRenderFrame' in this example.|
| callback | Callback\<void> | Yes  | Callback invoked when the event is triggered.                          |
W
wusongqing 已提交
1430 1431 1432 1433 1434

**Example**

```js
videoPlayer.on('startRenderFrame', () => {
W
wusongqing 已提交
1435
    console.info('startRenderFrame success!');
W
wusongqing 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
});
```

### 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                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| type     | string   | Yes  | Type of the event to subscribe to, which is 'videoSizeChanged' in this example.|
| 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

Subscribes to the video playback error event.

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

**Parameters**

W
wusongqing 已提交
1473 1474 1475 1476
| Name  | Type         | Mandatory| Description                                                        |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
| type     | string        | Yes  | Type of the event to subscribe to, which is 'error' in this API.<br>The 'error' event is triggered when an error occurs during video playback.|
| callback | ErrorCallback | Yes  | Callback invoked when the event is triggered.                                      |
W
wusongqing 已提交
1477 1478 1479 1480 1481

**Example**

```js
videoPlayer.on('error', (error) => {      // Set the 'error' event callback.
W
wusongqing 已提交
1482
    console.info(`video error called, errName is ${error.name}`);      // Print the error name.
W
wusongqing 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
    console.info(`video error called, errCode is ${error.code}`);      // Print the error code.
    console.info(`video error called, errMessage is ${error.message}`);// Print the detailed description of the error type.
});
videoPlayer.setVolume(3);  // Set volume to an invalid value to trigger the 'error' event.
```

## VideoPlayState<sup>8+</sup>

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

W
wusongqing 已提交
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
**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 已提交
1503 1504 1505

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

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

W
wusongqing 已提交
1508 1509
**System capability**: SystemCapability.Multimedia.Media.Core

W
wusongqing 已提交
1510 1511
| Name          | Value  | Description                                                        |
| -------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1512 1513
| 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 已提交
1514 1515 1516

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

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

W
wusongqing 已提交
1519 1520 1521 1522 1523 1524 1525 1526 1527
**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 已提交
1528 1529 1530

## MediaDescription<sup>8+</sup>

W
wusongqing 已提交
1531
### [key : string] : Object
W
wusongqing 已提交
1532 1533 1534

Defines media information in key-value mode.

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

W
wusongqing 已提交
1537 1538
| Name | Type  | Description                                                        |
| ----- | ------ | ------------------------------------------------------------ |
W
wusongqing 已提交
1539 1540
| key   | string | Key of the media information. For details about the keys, see [MediaDescriptionKey](#mediadescriptionkey8).|
| value | any    | Value of the key. For details about the values, see [MediaDescriptionKey](#mediadescriptionkey8).|
W
wusongqing 已提交
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563

**Example**

```js
function printfItemDescription(obj, key) {
    let property = obj[key];
    console.info('audio key is ' + key);
    console.info('audio value is ' + property);
}

audioPlayer.getTrackDescription((error, arrlist) => {
    if (typeof (arrlist) != 'undefined') {
        for (let i = 0; i < arrlist.length; i++) {
            printfItemDescription(arrlist[i], MD_KEY_TRACK_TYPE);  // Print the MD_KEY_TRACK_TYPE value of each track.
        }
    } else {
        console.log(`audio getTrackDescription fail, error:${error.message}`);
    }
});
```

## AudioRecorder

W
wusongqing 已提交
1564
Implements audio recording. Before calling an API of the **AudioRecorder** class, you must call [createAudioRecorder()](#mediacreateaudiorecorder) to create an [AudioRecorder](#audiorecorder) instance.
W
wusongqing 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

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                                                        |
| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ |
| config | [AudioRecorderConfig](#audiorecorderconfig) | Yes  | Audio recording parameters, including the audio output URI, [encoding format](#audioencoder), sampling rate, number of audio channels, and [output format](#audiooutputformat).|

**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 已提交
1607
Starts audio recording. This API can be called only after the [prepare](#audiorecorder_on) event is triggered.
W
wusongqing 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623

**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 已提交
1624
Pauses audio recording. This API can be called only after the [start](#audiorecorder_on) event is triggered.
W
wusongqing 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640

**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 已提交
1641
Resumes audio recording. This API can be called only after the [pause](#audiorecorder_on) event is triggered.
W
wusongqing 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 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

**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                                                        |
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1720
| type     | string   | Yes  | Type of the event to subscribe to. The following events are supported: 'prepare'\|'start'\|  'pause' \| 'resume' \|'stop'\|'release'\|'reset'<br>- The 'prepare' event is triggered when the [prepare](#audiorecorder_prepare) API is called and the audio recording parameters are set.<br>- The 'start' event is triggered when the [start](#audiorecorder_start) API is called and audio recording starts.<br>- The 'pause' event is triggered when the [pause](#audiorecorder_pause) API is called and audio recording is paused.<br>- The 'resume' event is triggered when the [resume](#audiorecorder_resume) API is called and audio recording is resumed.<br>- The 'stop' event is triggered when the [stop](#audiorecorder_stop) API is called and audio recording stops.<br>- The 'release' event is triggered when the [release](#audiorecorder_release) API is called and the recording resource is released.<br>- The 'reset' event is triggered when the [reset](#audiorecorder_reset) API is called and audio recording is reset.|
W
wusongqing 已提交
1721 1722 1723 1724 1725
| callback | ()=>void | Yes  | Callback invoked when the event is triggered.                                          |

**Example**

```js
W
wusongqing 已提交
1726
let audiorecorder = media.createAudioRecorder();                                  // Create an AudioRecorder instance.
W
wusongqing 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735
let audioRecorderConfig = {
    audioEncoder : media.AudioEncoder.AAC_LC, ,
    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},
}
W
wusongqing 已提交
1736 1737
audioRecorder.on('error', (error) => {                                             // Set the 'error' event callback.
    console.info(`audio error called, errName is ${error.name}`);
W
wusongqing 已提交
1738 1739 1740
    console.info(`audio error called, errCode is ${error.code}`);
    console.info(`audio error called, errMessage is ${error.message}`);
});
W
wusongqing 已提交
1741
audioRecorder.on('prepare', () => {                                              // Set the 'prepare' event callback.
W
wusongqing 已提交
1742
    console.log('prepare success');
W
wusongqing 已提交
1743
    audioRecorder.start();                                                       // Start recording and trigger the 'start' event callback.
W
wusongqing 已提交
1744
});
W
wusongqing 已提交
1745
audioRecorder.on('start', () => {                                                 // Set the 'start' event callback.
W
wusongqing 已提交
1746 1747
    console.log('audio recorder start success');
});
W
wusongqing 已提交
1748
audioRecorder.on('pause', () => {                                                 // Set the 'pause' event callback.
W
wusongqing 已提交
1749 1750
    console.log('audio recorder pause success');
});
W
wusongqing 已提交
1751
audioRecorder.on('resume', () => {                                                 // Set the 'resume' event callback.
W
wusongqing 已提交
1752 1753
    console.log('audio recorder resume success');
});
W
wusongqing 已提交
1754
audioRecorder.on('stop', () => {                                                 // Set the 'stop' event callback.
W
wusongqing 已提交
1755 1756
    console.log('audio recorder stop success');
});
W
wusongqing 已提交
1757
audioRecorder.on('release', () => {                                                 // Set the 'release' event callback.
W
wusongqing 已提交
1758 1759
    console.log('audio recorder release success');
});
W
wusongqing 已提交
1760
audioRecorder.on('reset', () => {                                                 // Set the 'reset' event callback.
W
wusongqing 已提交
1761 1762
    console.log('audio recorder reset success');
});
W
wusongqing 已提交
1763
audioRecorder.prepare(audioRecorderConfig)                                       // Set recording parameters and trigger the 'prepare' event callback.
W
wusongqing 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
```

### on('error')

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

Subscribes to the audio recording error event.

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

**Parameters**

| Name  | Type         | Mandatory| Description                                                        |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1778
| type     | string        | Yes  | Type of the event to subscribe to, which is 'error' in this API.<br>The 'error' event is triggered when an error occurs during audio recording.|
W
wusongqing 已提交
1779 1780 1781 1782 1783
| callback | ErrorCallback | Yes  | Callback invoked when the event is triggered.                                      |

**Example**

```js
W
wusongqing 已提交
1784 1785
audioRecorder.on('error', (error) => {                                  // Set the 'error' event callback.
    console.info(`audio error called, errName is ${error.name}`);       // Print the error name.
W
wusongqing 已提交
1786 1787 1788
    console.info(`audio error called, errCode is ${error.code}`);       // Print the error code.
    console.info(`audio error called, errMessage is ${error.message}`); // Print the detailed description of the error type.
});
W
wusongqing 已提交
1789
audioRecorder.prepare();                                                  // Do no set any parameter in prepare and trigger the 'error' event callback.
W
wusongqing 已提交
1790 1791 1792 1793 1794 1795
```

## AudioRecorderConfig

Describes audio recording configurations.

W
wusongqing 已提交
1796 1797
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

W
wusongqing 已提交
1798 1799
| Name                 | Type                               | Mandatory| Description                                                        |
| --------------------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1800 1801 1802 1803 1804
| audioEncoder          | [AudioEncoder](#audioencoder)           | No  | Audio encoding format. The default value is **AAC_LC**.                            |
| 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**.                                 |
| format                | [AudioOutputFormat](#audiooutputformat) | No  | Audio output format. The default value is **MPEG_4**.                        |
W
wusongqing 已提交
1805
| location              | [Location](#location)                   | No  | Geographical location of the recorded audio.                                        |
W
wusongqing 已提交
1806
| uri                   | string                                  | Yes  | Audio output URI. Supported: fd://xx&nbsp;(fd&nbsp;number)<br>![en-us_image_0000001164217678](figures/en-us_image_url.png) <br>The file must be created by the caller and granted with proper permissions.|
W
wusongqing 已提交
1807
| audioEncoderMime      | [CodecMimeType](#codecmimetype8)        | No  | Audio encoding format.                                              |
W
wusongqing 已提交
1808 1809


W
wusongqing 已提交
1810 1811 1812 1813
## AudioEncoder<sup>(deprecated)</sup>

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

Enumerates the audio encoding formats.

W
wusongqing 已提交
1817 1818
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

W
wusongqing 已提交
1819 1820
| Name   | Default Value| Description                                                        |
| ------- | ------ | ------------------------------------------------------------ |
W
wusongqing 已提交
1821 1822 1823
| DEFAULT | 0      | Default audio encoding format, which is Adaptive Multi Rate-Narrow Band Speech Codec (AMR-NB).<br>This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.|
| AMR_NB  | 1      | AMR-NB.<br>This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.|
| AMR_WB  | 2      | Adaptive Multi Rate-Wide Band Speech Codec (AMR-WB).<br>This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.|
W
wusongqing 已提交
1824
| AAC_LC  | 3      | Advanced Audio Coding Low Complexity (AAC-LC).|
W
wusongqing 已提交
1825
| HE_AAC  | 4      | High-Efficiency Advanced&nbsp;Audio&nbsp;Coding (HE_AAC).<br>This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.|
W
wusongqing 已提交
1826 1827


W
wusongqing 已提交
1828 1829 1830 1831
## AudioOutputFormat<sup>(deprecated)</sup>

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

Enumerates the audio output formats.

W
wusongqing 已提交
1835 1836
**System capability**: SystemCapability.Multimedia.Media.AudioRecorder

W
wusongqing 已提交
1837 1838
| Name    | Default Value| Description                                                        |
| -------- | ------ | ------------------------------------------------------------ |
W
wusongqing 已提交
1839
| DEFAULT  | 0      | Default encapsulation format, which is MPEG-4.<br>This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.|
W
wusongqing 已提交
1840
| MPEG_4   | 2      | MPEG-4.                                          |
W
wusongqing 已提交
1841 1842
| AMR_NB   | 3      | AMR_NB.<br>This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.|
| AMR_WB   | 4      | AMR_WB.<br>This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR.|
W
wusongqing 已提交
1843
| AAC_ADTS | 6      | Audio Data Transport Stream (ADTS), which is a transport stream format of AAC-based audio.|
W
wusongqing 已提交
1844

W
wusongqing 已提交
1845
## VideoRecorder<sup>9+</sup>
W
wusongqing 已提交
1846

W
wusongqing 已提交
1847
Implements video recording. Before calling an API of the **VideoRecorder** class, you must call [createVideoRecorder()](#mediacreatevideorecorder9) to create a [VideoRecorder](#videorecorder9) instance.
W
wusongqing 已提交
1848 1849 1850 1851 1852

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

### Attributes

W
wusongqing 已提交
1853 1854 1855 1856 1857
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

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

W
wusongqing 已提交
1859
### prepare<sup>9+</sup><a name=videorecorder_prepare1></a>
W
wusongqing 已提交
1860 1861 1862 1863 1864

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

Sets video recording parameters in asynchronous mode. This API uses a callback to return the result.

W
wusongqing 已提交
1865
**Required permissions:** ohos.permission.MICROPHONE
W
wusongqing 已提交
1866 1867 1868 1869 1870

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

**Parameters**

W
wusongqing 已提交
1871 1872
| Name  | Type                                        | Mandatory| Description                               |
| -------- | -------------------------------------------- | ---- | ----------------------------------- |
W
wusongqing 已提交
1873
| config   | [VideoRecorderConfig](#videorecorderconfig9) | Yes  | Video recording parameters to set.           |
W
wusongqing 已提交
1874
| callback | AsyncCallback\<void>                         | Yes  | Callback used to return the result.|
W
wusongqing 已提交
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903

**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
let videoRecorder = null;
let events = require('events');
W
wusongqing 已提交
1904
let eventEmitter = new events.EventEmitter();
W
wusongqing 已提交
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926

eventEmitter.on('prepare', () => {
    videoRecorder.prepare(videoConfig, (err) => {
        if (typeof (err) == 'undefined') {
            console.info('prepare success');
        } else {
            console.info('prepare failed and error is ' + err.message);
        }
    });
});

media.createVideoRecorder((err, recorder) => {
    if (typeof (err) == 'undefined' && typeof (recorder) != 'undefined') {
        videoRecorder = recorder;
        console.info('createVideoRecorder success');
        eventEmitter.emit('prepare');                                        // Trigger the 'prepare' event.
    } else {
        console.info('createVideoRecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
1927
### prepare<sup>9+</sup><a name=videorecorder_prepare2></a>
W
wusongqing 已提交
1928 1929 1930 1931 1932

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

Sets video recording parameters in asynchronous mode. This API uses a promise to return the result.

W
wusongqing 已提交
1933
**Required permissions:** ohos.permission.MICROPHONE and ohos.permission.CAMERA
W
wusongqing 已提交
1934 1935 1936 1937 1938

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

**Parameters**

W
wusongqing 已提交
1939 1940
| Name| Type                                        | Mandatory| Description                    |
| ------ | -------------------------------------------- | ---- | ------------------------ |
W
wusongqing 已提交
1941
| config | [VideoRecorderConfig](#videorecorderconfig9) | Yes  | Video recording parameters to set.|
W
wusongqing 已提交
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997

**Return value**

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

**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
let videoRecorder = null;
await media.createVideoRecorder().then((recorder) => {
    if (typeof (recorder) != 'undefined') {
        videoRecorder = recorder;
        console.info('createVideoRecorder success');
    } else {
        console.info('createVideoRecorder failed');
    }
}, (err) => {
    console.info('error hanppend message is ' + err.message);
}).catch((err) => {
    console.info('catch err error message is ' + err.message);
});

await videoRecorder.prepare(videoConfig).then(() => {
    console.info('prepare success');
}, (err) => {
    console.info('prepare failed and error is ' + err.message);
}).catch((err) => {
    console.info('prepare failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
1998
### getInputSurface<sup>9+</sup>
W
wusongqing 已提交
1999 2000 2001 2002 2003 2004 2005

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 已提交
2006
This API can be called only after [prepare()](#videorecorder_prepare1) is called.
W
wusongqing 已提交
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019

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

**Parameters**

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

**Example**

```js
// asyncallback
W
wusongqing 已提交
2020
let surfaceID = null;                                               // Surface ID passed to the external system.
W
wusongqing 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
videoRecorder.getInputSurface((err, surfaceId) => {
    if (typeof (err) == 'undefined') {
        console.info('getInputSurface success');
        surfaceID = surfaceId;
    } else {
        console.info('getInputSurface failed and error is ' + err.message);
    }
});
```

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

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 已提交
2039
This API can be called only after [prepare()](#videorecorder_prepare1) is called.
W
wusongqing 已提交
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052

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

**Return value**

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

**Example**

```js
// promise
W
wusongqing 已提交
2053
let surfaceID = null;                                               // Surface ID passed to the external system.
W
wusongqing 已提交
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
await videoRecorder.getInputSurface().then((surfaceId) => {
    console.info('getInputSurface success');
    surfaceID = surfaceId;
}, (err) => {
    console.info('getInputSurface failed and error is ' + err.message);
}).catch((err) => {
    console.info('getInputSurface failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2064
### start<sup>9+</sup><a name=videorecorder_start1></a>
W
wusongqing 已提交
2065 2066 2067 2068 2069

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

Starts video recording in asynchronous mode. This API uses a callback to return the result.

W
wusongqing 已提交
2070
This API can be called only after [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) are called, because the data source must pass data to the surface first.
W
wusongqing 已提交
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092

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

**Parameters**

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

**Example**

```js
// asyncallback
videoRecorder.start((err) => {
    if (typeof (err) == 'undefined') {
        console.info('start videorecorder success');
    } else {
        console.info('start videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2093
### start<sup>9+</sup><a name=videorecorder_start2></a>
W
wusongqing 已提交
2094 2095 2096 2097 2098

start(): Promise\<void>;

Starts video recording in asynchronous mode. This API uses a promise to return the result.

W
wusongqing 已提交
2099
This API can be called only after [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) are called, because the data source must pass data to the surface first.
W
wusongqing 已提交
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121

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

**Return value**

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

**Example**

```js
// promise
await videoRecorder.start().then(() => {
    console.info('start videorecorder success');
}, (err) => {
    console.info('start videorecorder failed and error is ' + err.message);
}).catch((err) => {
    console.info('start videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2122
### pause<sup>9+</sup><a name=videorecorder_pause1></a>
W
wusongqing 已提交
2123 2124 2125 2126 2127

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

Pauses video recording in asynchronous mode. This API uses a callback to return the result.

W
wusongqing 已提交
2128
This API can be called only after [start()](#videorecorder_start1) is called. You can resume recording by calling [resume()](#videorecorder_resume1).
W
wusongqing 已提交
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150

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

**Parameters**

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

**Example**

```js
// asyncallback
videoRecorder.pause((err) => {
    if (typeof (err) == 'undefined') {
        console.info('pause videorecorder success');
    } else {
        console.info('pause videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2151
### pause<sup>9+</sup><a name=videorecorder_pause2></a>
W
wusongqing 已提交
2152 2153 2154 2155 2156

pause(): Promise\<void>;

Pauses video recording in asynchronous mode. This API uses a promise to return the result.

W
wusongqing 已提交
2157
This API can be called only after [start()](#videorecorder_start1) is called. You can resume recording by calling [resume()](#videorecorder_resume1).
W
wusongqing 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179

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

**Return value**

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

**Example**

```js
// promise
await videoRecorder.pause().then(() => {
    console.info('pause videorecorder success');
}, (err) => {
    console.info('pause videorecorder failed and error is ' + err.message);
}).catch((err) => {
    console.info('pause videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2180
### resume<sup>9+</sup><a name=videorecorder_resume1></a>
W
wusongqing 已提交
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206

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

Resumes video recording in asynchronous mode. This API uses a callback to return the result.

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

**Parameters**

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

**Example**

```js
// asyncallback
videoRecorder.resume((err) => {
    if (typeof (err) == 'undefined') {
        console.info('resume videorecorder success');
    } else {
        console.info('resume videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2207
### resume<sup>9+</sup><a name=videorecorder_resume2></a>
W
wusongqing 已提交
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233

resume(): Promise\<void>;

Resumes video recording in asynchronous mode. This API uses a promise to return the result.

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

**Return value**

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

**Example**

```js
// promise
await videoRecorder.resume().then(() => {
    console.info('resume videorecorder success');
}, (err) => {
    console.info('resume videorecorder failed and error is ' + err.message);
}).catch((err) => {
    console.info('resume videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2234
### stop<sup>9+</sup><a name=videorecorder_stop1></a>
W
wusongqing 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262

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

Stops video recording in asynchronous mode. This API uses a callback to return the result.

To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) again.

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

**Parameters**

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

**Example**

```js
// asyncallback
videoRecorder.stop((err) => {
    if (typeof (err) == 'undefined') {
        console.info('stop videorecorder success');
    } else {
        console.info('stop videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2263
### stop<sup>9+</sup><a name=videorecorder_stop2></a>
W
wusongqing 已提交
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291

stop(): Promise\<void>;

Stops video recording in asynchronous mode. This API uses a promise to return the result.

To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) again.

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

**Return value**

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

**Example**

```js
// promise
await videoRecorder.stop().then(() => {
    console.info('stop videorecorder success');
}, (err) => {
    console.info('stop videorecorder failed and error is ' + err.message);
}).catch((err) => {
    console.info('stop videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2292
### release<sup>9+</sup><a name=videorecorder_release1></a>
W
wusongqing 已提交
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318

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

Releases the video recording resource in asynchronous mode. This API uses a callback to return the result.

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

**Parameters**

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

**Example**

```js
// asyncallback
videoRecorder.release((err) => {
    if (typeof (err) == 'undefined') {
        console.info('release videorecorder success');
    } else {
        console.info('release videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2319
### release<sup>9+</sup><a name=videorecorder_release2></a>
W
wusongqing 已提交
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345

release(): Promise\<void>;

Releases the video recording resource in asynchronous mode. This API uses a promise to return the result.

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

**Return value**

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

**Example**

```js
// promise
await videoRecorder.release().then(() => {
    console.info('release videorecorder success');
}, (err) => {
    console.info('release videorecorder failed and error is ' + err.message);
}).catch((err) => {
    console.info('release videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2346
### reset<sup>9+</sup><a name=videorecorder_reset1></a>
W
wusongqing 已提交
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374

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

Resets video recording in asynchronous mode. This API uses a callback to return the result.

To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) again.

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

**Parameters**

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

**Example**

```js
// asyncallback
videoRecorder.reset((err) => {
    if (typeof (err) == 'undefined') {
        console.info('reset videorecorder success');
    } else {
        console.info('reset videorecorder failed and error is ' + err.message);
    }
});
```

W
wusongqing 已提交
2375
### reset<sup>9+</sup><a name=videorecorder_reset2></a>
W
wusongqing 已提交
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403

reset(): Promise\<void>;

Resets video recording in asynchronous mode. This API uses a promise to return the result.

To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface8) again.

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

**Return value**

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

**Example**

```js
// promise
await videoRecorder.reset().then(() => {
    console.info('reset videorecorder success');
}, (err) => {
    console.info('reset videorecorder failed and error is ' + err.message);
}).catch((err) => {
    console.info('reset videorecorder failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
2404
### on('error')<sup>9+</sup>
W
wusongqing 已提交
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415

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

Subscribes to the video recording error event.

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

**Parameters**

| Name  | Type         | Mandatory| Description                                                        |
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
2416
| type     | string        | Yes  | Type of the event to subscribe to, which is 'error' in this API.<br>The 'error' event is triggered when an error occurs during video recording.|
W
wusongqing 已提交
2417 2418 2419 2420 2421
| callback | ErrorCallback | Yes  | Callback invoked when the event is triggered.                                      |

**Example**

```js
W
wusongqing 已提交
2422 2423
videoRecorder.on('error', (error) => {                                  // Set the 'error' event callback.
    console.info(`audio error called, errName is ${error.name}`);       // Print the error name.
W
wusongqing 已提交
2424 2425 2426 2427 2428 2429
    console.info(`audio error called, errCode is ${error.code}`);       // Print the error code.
    console.info(`audio error called, errMessage is ${error.message}`); // Print the detailed description of the error type.
});
// This event is reported when an error occurs during the retrieval of videoRecordState.
```

W
wusongqing 已提交
2430
## VideoRecordState<sup>9+</sup>
W
wusongqing 已提交
2431 2432 2433

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

W
wusongqing 已提交
2434 2435 2436 2437 2438 2439 2440 2441 2442
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

| 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 已提交
2443
| error    | string | Video recording is in the error state.            |
W
wusongqing 已提交
2444

W
wusongqing 已提交
2445
## VideoRecorderConfig<sup>9+</sup>
W
wusongqing 已提交
2446 2447 2448

Describes the video recording parameters.

W
wusongqing 已提交
2449 2450
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

W
wusongqing 已提交
2451 2452 2453 2454 2455 2456 2457
| Name           | Type                                      | Mandatory| Description                                                        |
| --------------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ |
| 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.                                        |
Z
zengyawen 已提交
2458
| url             | string                                         | Yes  | Video output URL. Supported: fd://xx&nbsp;(fd&nbsp;number)<br>![](figures/en-us_image_url.png) <br>The file must be created by the caller and granted with proper permissions. |
W
wusongqing 已提交
2459

W
wusongqing 已提交
2460
## AudioSourceType<sup>9+</sup>
W
wusongqing 已提交
2461 2462 2463

Enumerates the audio source types for video recording.

W
wusongqing 已提交
2464 2465 2466 2467 2468 2469
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

| Name                     | Value  | Description                  |
| ------------------------- | ---- | ---------------------- |
| AUDIO_SOURCE_TYPE_DEFAULT | 0    | Default audio input source.|
| AUDIO_SOURCE_TYPE_MIC     | 1    | Mic audio input source. |
W
wusongqing 已提交
2470

W
wusongqing 已提交
2471
## VideoSourceType<sup>9+</sup>
W
wusongqing 已提交
2472 2473 2474

Enumerates the video source types for video recording.

W
wusongqing 已提交
2475 2476 2477 2478 2479 2480
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

| 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 已提交
2481

W
wusongqing 已提交
2482
## VideoRecorderProfile<sup>9+</sup>
W
wusongqing 已提交
2483 2484 2485

Describes the video recording profile.

W
wusongqing 已提交
2486 2487 2488 2489 2490 2491
**System capability**: SystemCapability.Multimedia.Media.VideoRecorder

| Name            | Type                                    | Mandatory| Description            |
| ---------------- | -------------------------------------------- | ---- | ---------------- |
| audioBitrate     | number                                       | Yes  | Audio encoding bit rate.|
| audioChannels    | number                                       | Yes  | Number of audio channels.|
W
wusongqing 已提交
2492
| audioCodec       | [CodecMimeType](#codecmimetype8)             | Yes  | Audio encoding format.  |
W
wusongqing 已提交
2493 2494
| audioSampleRate  | number                                       | Yes  | Audio sampling rate.    |
| fileFormat       | [ContainerFormatType](#containerformattype8) | Yes  | Container format of a file.|
W
wusongqing 已提交
2495
| videoBitrate     | number                                       | Yes  | Video encoding bit rate.|
W
wusongqing 已提交
2496
| videoCodec       | [CodecMimeType](#codecmimetype8)             | Yes  | Video encoding format.  |
W
wusongqing 已提交
2497 2498
| videoFrameWidth  | number                                       | Yes  | Width of the recorded video frame.|
| videoFrameHeight | number                                       | Yes  | Height of the recorded video frame.|
W
wusongqing 已提交
2499
| videoFrameRate   | number                                       | Yes  | Video frame rate.  |
W
wusongqing 已提交
2500 2501 2502 2503 2504

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

Enumerates the container format types (CFTs).

W
wusongqing 已提交
2505 2506 2507 2508
**System capability**: SystemCapability.Multimedia.Media.Core

| Name       | Value   | Description                 |
| ----------- | ----- | --------------------- |
W
wusongqing 已提交
2509
| CFT_MPEG_4  | "mp4" | Video container format MPEG-4 .|
W
wusongqing 已提交
2510
| CFT_MPEG_4A | "m4a" | Audio container format M4A.|
W
wusongqing 已提交
2511

W
wusongqing 已提交
2512
## Location
W
wusongqing 已提交
2513 2514 2515

Describes the geographical location of the recorded video.

W
wusongqing 已提交
2516 2517 2518 2519 2520 2521
**System capability**: SystemCapability.Multimedia.Media.Core

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