js-apis-media.md 85.6 KB
Newer Older
W
wusongqing 已提交
1
# 媒体服务
2

W
wusongqing 已提交
3
媒体子系统为开发者提供一套简单且易于理解的接口,使得开发者能够方便接入系统并使用系统的媒体资源。
Z
zengyawen 已提交
4

W
wusongqing 已提交
5
媒体子系统包含了音视频相关媒体业务,提供以下常用功能:
6

W
wusongqing 已提交
7 8 9 10
- 音频播放([AudioPlayer](#audioplayer)
- 视频播放([VideoPlayer](#videoplayer8)
- 音频录制([AudioRecorder](#audiorecorder)
- 视频录制([VideoRecorder](#VideoRecorder<sup>8+</sup>))
11

W
wusongqing 已提交
12
后续将提供以下功能:DataSource音视频播放、音视频编解码、容器封装解封装、媒体能力查询等功能。
13

W
wusongqing 已提交
14
## 导入模块
Z
zengyawen 已提交
15

16
```js
Z
zengyawen 已提交
17 18 19
import media from '@ohos.multimedia.media';
```

20
##  media.createAudioPlayer
Z
zengyawen 已提交
21

22
createAudioPlayer(): [AudioPlayer](#audioplayer)
Z
zengyawen 已提交
23

W
wusongqing 已提交
24 25
同步方式创建音频播放实例。

Z
zengyawen 已提交
26

27

W
wusongqing 已提交
28
**返回值:**
Z
zengyawen 已提交
29

W
wusongqing 已提交
30
| 类型                        | 说明                                                         |
31
| --------------------------- | ------------------------------------------------------------ |
W
wusongqing 已提交
32
| [AudioPlayer](#audioplayer) | 返回AudioPlayer类实例,失败时返回null。可用于音频播放、暂停、停止等操作。 |
Z
zengyawen 已提交
33

W
wusongqing 已提交
34
**示例:**
Z
zengyawen 已提交
35

36
```js
W
wusongqing 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
var audioPlayer = media.createAudioPlayer();
```

## media.createAudioPlayerAsync<sup>8+</sup>

createAudioPlayerAsync(callback: AsyncCallback\<[AudioPlayer](#audioplayer)>): void

异步方式创建音频播放实例。通过注册回调函数获取返回值。

**参数:**

| 参数名   | 类型                                       | 必填 | 说明                           |
| -------- | ------------------------------------------ | ---- | ------------------------------ |
| callback | AsyncCallback<[AudioPlayer](#audioplayer)> | 是   | 异步创建音频播放实例回调方法。 |

**示例:**

```js
media.createAudioPlayerAsync((error, audio) => {
   if (typeof(audio) != 'undefined') {
       audioPlayer = audio;
       console.info('audio createAudioPlayerAsync success');
   } else {
       console.info(`audio createAudioPlayerAsync fail, error:${error.message}`);
   }
});
```

## media.createAudioPlayerAsync<sup>8+</sup>

createAudioPlayerAsync: Promise<[AudioPlayer](#audioplayer)>

异步方式创建音频播放实例。通过Promise获取返回值。

**返回值:**

| 类型                                 | 说明                                |
| ------------------------------------ | ----------------------------------- |
| Promise<[AudioPlayer](#audioplayer)> | 异步创建音频播放实例Promise返回值。 |

**示例:**

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

await media.createAudioPlayerAsync.then((audio) => {
    if (typeof(audio) != 'undefined') {
       audioPlayer = audio;
       console.info('audio createAudioPlayerAsync success');
   } else {
       console.info('audio createAudioPlayerAsync fail');
   }
}, failureCallback).catch(catchCallback);
Z
zengyawen 已提交
95
```
96

97 98 99 100
## media.createVideoPlayer<sup>8+</sup>

createVideoPlayer(callback: AsyncCallback\<[VideoPlayer](#videoplayer8)>): void

W
wusongqing 已提交
101
异步方式创建视频播放实例,通过注册回调函数获取返回值。
102

W
wusongqing 已提交
103
**参数:**
Z
zengyawen 已提交
104

W
wusongqing 已提交
105
| 参数名   | 类型                                        | 必填 | 说明                           |
Z
zengyawen 已提交
106
| -------- | ------------------------------------------- | ---- | ------------------------------ |
W
wusongqing 已提交
107
| callback | AsyncCallback<[VideoPlayer](#videoplayer8)> | 是   | 异步创建视频播放实例回调方法。 |
108

W
wusongqing 已提交
109
**示例:**
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

```js
media.createVideoPlayer((error, video) => {
   if (typeof(video) != 'undefined') {
       videoPlayer = video;
       console.info('video createVideoPlayer success');
   } else {
       console.info(`video createVideoPlayer fail, error:${error.message}`);
   }
});
```

## media.createVideoPlayer<sup>8+</sup>

createVideoPlayer: Promise<[VideoPlayer](#videoplayer8)>

W
wusongqing 已提交
126
异步方式创建视频播放实例,通过Promise获取返回值。
127

W
wusongqing 已提交
128
**返回值:**
Z
zengyawen 已提交
129

W
wusongqing 已提交
130
| 类型                                  | 说明                                |
Z
zengyawen 已提交
131
| ------------------------------------- | ----------------------------------- |
W
wusongqing 已提交
132
| Promise<[VideoPlayer](#videoplayer8)> | 异步创建视频播放实例Promise返回值。 |
133

W
wusongqing 已提交
134
**示例:**
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

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

await media.createVideoPlayer.then((video) => {
    if (typeof(video) != 'undefined') {
       videoPlayer = video;
       console.info('video createVideoPlayer success');
   } else {
       console.info('video createVideoPlayer fail');
   }
}, failureCallback).catch(catchCallback);
```

Z
zengyawen 已提交
154
## media.createAudioRecorder
155

Z
zengyawen 已提交
156
createAudioRecorder(): AudioRecorder
Z
zengyawen 已提交
157

W
wusongqing 已提交
158
创建音频录制的实例来控制音频的录制。
Z
zengyawen 已提交
159

W
wusongqing 已提交
160
**返回值:**
Z
zengyawen 已提交
161

W
wusongqing 已提交
162
| 类型                            | 说明                                      |
Z
zengyawen 已提交
163
| ------------------------------- | ----------------------------------------- |
W
wusongqing 已提交
164
| [AudioRecorder](#audiorecorder) | 返回AudioRecorder类实例,失败时返回null。 |
Z
zengyawen 已提交
165

W
wusongqing 已提交
166
**示例:**
167

168
```js
B
bird_j 已提交
169
let audiorecorder = media.createAudioRecorder(); 
Z
zengyawen 已提交
170
```
Z
zengyawen 已提交
171

W
wusongqing 已提交
172 173 174 175 176 177 178 179 180 181 182
## media.createAudioRecorderAsync<sup>8+</sup>

createAudioRecorderAsync(callback: AsyncCallback\<[AudioRecorder](#audiorecorder)>): void

异步方式创建音频录制实例。通过注册回调函数获取返回值。

**参数:**

| 参数名   | 类型                                           | 必填 | 说明                           |
| -------- | ---------------------------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback<[AudioRecorder](#audiorecorder)> | 是   | 异步创建音频录制实例回调方法。 |
183

W
wusongqing 已提交
184
**示例:**
185

W
wusongqing 已提交
186 187 188 189 190 191 192 193 194 195
```js
media.createAudioRecorderAsync((error, audio) => {
   if (typeof(audio) != 'undefined') {
       audioRecorder = audio;
       console.info('audio createAudioRecorderAsync success');
   } else {
       console.info(`audio createAudioRecorderAsync fail, error:${error.message}`);
   }
});
```
196

W
wusongqing 已提交
197
## media.createAudioRecorderAsync<sup>8+</sup>
Z
zengyawen 已提交
198

W
wusongqing 已提交
199
createAudioRecorderAsync: Promise<[AudioRecorder](#audiorecorder)>
200

W
wusongqing 已提交
201
异步方式创建音频录制实例。通过Promise获取返回值。
202

W
wusongqing 已提交
203 204 205 206 207 208 209
**返回值:**

| 类型                                     | 说明                                |
| ---------------------------------------- | ----------------------------------- |
| Promise<[AudioRecorder](#audiorecorder)> | 异步创建音频录制实例Promise返回值。 |

**示例:**
210 211

```js
W
wusongqing 已提交
212 213 214 215 216 217
function failureCallback(error) {
    console.info(`audio failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`audio catchCallback, error:${error.message}`);
}
218

W
wusongqing 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
await media.createAudioRecorderAsync.then((audio) => {
    if (typeof(audio) != 'undefined') {
       audioRecorder = audio;
       console.info('audio createAudioRecorderAsync success');
   } else {
       console.info('audio createAudioRecorderAsync fail');
   }
}, failureCallback).catch(catchCallback);
```

## media.createVideoRecorderAsync<sup>8+</sup>

createVideoRecorderAsync(callback: AsyncCallback\<[VideoRecorder](#videorecorder8)>): void

异步方式创建视频录制实例。通过注册回调函数获取返回值。

**参数:**

| 参数名   | 类型                                                        | 必填 | 说明                           |
| -------- | ----------------------------------------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback<[VideoRecorder](#videorecorder8)> | 是   | 异步创建视频录制实例回调方法。 |

**示例:**

```js
media.createVideoRecorderAsync((error, video) => {
245 246
   if (typeof(video) != 'undefined') {
       videoRecorder = video;
W
wusongqing 已提交
247
       console.info('video createVideoRecorderAsync success');
248
   } else {
W
wusongqing 已提交
249
       console.info(`video createVideoRecorderAsync fail, error:${error.message}`);
250 251 252 253
   }
});
```

W
wusongqing 已提交
254
## media.createVideoRecorderAsync<sup>8+</sup>
255

W
wusongqing 已提交
256
createVideoRecorderAsync: Promise<[VideoRecorder](#videorecorder8)>
257

W
wusongqing 已提交
258
异步方式创建视频录制实例。通过Promise获取返回值。
Z
zengyawen 已提交
259

W
wusongqing 已提交
260
**返回值:**
261

W
wusongqing 已提交
262 263 264
| 类型                                                  | 说明                                |
| ----------------------------------------------------- | ----------------------------------- |
| Promise<[VideoRecorder](#videorecorder8)> | 异步创建视频录制实例Promise返回值。 |
265

W
wusongqing 已提交
266
**示例:**
267 268 269 270 271 272 273 274 275

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

W
wusongqing 已提交
276
await media.createVideoRecorderAsync.then((video) => {
277 278
    if (typeof(video) != 'undefined') {
       videoRecorder = video;
W
wusongqing 已提交
279
       console.info('video createVideoRecorderAsync success');
280
   } else {
W
wusongqing 已提交
281
       console.info('video createVideoRecorderAsync fail');
282 283 284 285 286 287
   }
}, failureCallback).catch(catchCallback);
```



288 289
## MediaErrorCode<sup>8+</sup>

W
wusongqing 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303
媒体服务错误类型枚举

| 名称                       | 值   | 说明                                   |
| -------------------------- | ---- | -------------------------------------- |
| MSERR_OK                   | 0    | 表示操作成功。                         |
| MSERR_NO_MEMORY            | 1    | 表示申请内存失败,系统可能无可用内存。 |
| MSERR_OPERATION_NOT_PERMIT | 2    | 表示无权限执行此操作。                 |
| MSERR_INVALID_VAL          | 3    | 表示传入入参无效。                     |
| MSERR_IO                   | 4    | 表示发生IO错误。                       |
| MSERR_TIMEOUT              | 5    | 表示操作超时。                         |
| MSERR_UNKNOWN              | 6    | 表示未知错误。                         |
| MSERR_SERVICE_DIED         | 7    | 表示服务端失效。                       |
| MSERR_INVALID_STATE        | 8    | 表示在当前状态下,不允许执行此操作。   |
| MSERR_UNSUPPORTED          | 9    | 表示在当前版本下,不支持此操作。       |
304 305 306

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

W
wusongqing 已提交
307
媒体类型枚举
308

W
wusongqing 已提交
309 310 311 312 313
| 名称                | 值   | 说明               |
| ------------------- | ---- | ------------------ |
| MEDIA_TYPE_AUD      | 0    | 表示音频。         |
| MEDIA_TYPE_VID      | 1    | 表示视频。         |
| MEDIA_TYPE_SUBTITLE | 2    | 表示字幕:开发中。 |
314 315 316

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

W
wusongqing 已提交
317
Codec MIME类型枚举
318

W
wusongqing 已提交
319 320 321 322 323 324 325
| 名称         | 值                | 说明                     |
| ------------ | ----------------- | ------------------------ |
| VIDEO_MPEG4  | ”video/mp4v-es“   | 表示视频/mpeg4类型。     |
| AUDIO_MPEG   | "audio/mpeg"      | 表示音频/mpeg类型。      |
| AUDIO_AAC    | "audio/mp4a-latm" | 表示音频/mp4a-latm类型。 |
| AUDIO_VORBIS | "audio/vorbis"    | 表示音频/vorbis类型。    |
| AUDIO_FLAC   | "audio/flac"      | 表示音频/flac类型。      |
326 327 328

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

W
wusongqing 已提交
329
媒体信息描述枚举
330

W
wusongqing 已提交
331
| 名称                     | 值              | 说明                                                         |
332
| ------------------------ | --------------- | ------------------------------------------------------------ |
W
wusongqing 已提交
333 334 335 336 337 338 339 340 341 342
| MD_KEY_TRACK_INDEX       | "track_index"   | 表示轨道序号,其对应键值类型为number。                       |
| MD_KEY_TRACK_TYPE        | "track_type"    | 表示轨道类型,其对应键值类型为number,参考[MediaType](#mediatype8)。 |
| MD_KEY_CODEC_MIME        | "codec_mime"    | 表示codec_mime类型,其对应键值类型为string。                 |
| MD_KEY_DURATION          | "duration"      | 表示媒体时长,其对应键值类型为number,单位为ms。             |
| MD_KEY_BITRATE           | "bitrate"       | 表示比特率,其对应键值类型为number,单位为bps。              |
| MD_KEY_WIDTH             | "width"         | 表示视频宽度,其对应键值类型为number,单位为像素。           |
| MD_KEY_HEIGHT            | "height"        | 表示视频高度,其对应键值类型为number,单位为像素。           |
| MD_KEY_FRAME_RATE        | "frame_rate"    | 表示视频帧率,其对应键值类型为number,单位为100fps。         |
| MD_KEY_AUD_CHANNEL_COUNT | "channel_count" | 表示声道数,其对应键值类型为number。                         |
| MD_KEY_AUD_SAMPLE_RATE   | "sample_rate"   | 表示采样率,其对应键值类型为number,单位为HZ。               |
343 344 345

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

W
wusongqing 已提交
346
缓存事件类型枚举
347

W
wusongqing 已提交
348 349 350 351 352 353
| 名称              | 值   | 说明                       |
| ----------------- | ---- | -------------------------- |
| BUFFERING_START   | 1    | 表示开始缓存。             |
| BUFFERING_END     | 2    | 表示结束缓存。             |
| BUFFERING_PERCENT | 3    | 表示缓存百分比。           |
| CACHED_DURATION   | 4    | 表示缓存时长,单位为毫秒。 |
354

Z
zengyawen 已提交
355
## AudioPlayer
Z
zengyawen 已提交
356

W
wusongqing 已提交
357
音频播放管理类,用于管理和播放音频媒体。在调用AudioPlayer的方法前,需要先通过[createAudioPlayer()](#media.createaudioplayer)[createAudioPlayerAsync()](#media.createaudioplayerasync8)构建一个[AudioPlayer](#audioplayer)实例。
Z
zengyawen 已提交
358

W
wusongqing 已提交
359
音频播放demo可参考:[音频播放开发指导](../../media/audio-playback.md)
Z
zengyawen 已提交
360

W
wusongqing 已提交
361
### 属性<a name=audioplayer_属性></a>
Z
zengyawen 已提交
362

W
wusongqing 已提交
363
| 名称        | 类型                      | 可读 | 可写 | 说明                                                         |
364
| ----------- | ------------------------- | ---- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
365 366 367 368 369
| src         | string                    | 是   | 是   | 音频媒体URI,支持当前主流的音频格式(mp4、aac、mp3、ogg)。<br>**支持路径示例**<br>1、本地绝对路径:file:///data/data/ohos.xxx.xxx/files/test.mp4<br>![zh-cn_image_0000001164217678](figures/zh-cn_image_0000001164217678.png)<br>2、http网络播放路径:开发中<br>3、hls网络播放路径:开发中<br>4、fd类型播放:开发中<br>**注意事项**<br>媒体素材需至少赋予读权限后,才可正常播放 |
| loop        | boolean                   | 是   | 是   | 音频循环播放属性,设置为'true'表示循环播放。                 |
| currentTime | number                    | 是   | 否   | 音频的当前播放位置。                                         |
| duration    | number                    | 是   | 否   | 音频时长。                                                   |
| state       | [AudioState](#audiostate) | 是   | 否   | 音频播放的状态。                                             |
Z
zengyawen 已提交
370

371
### play<a name=audioplayer_play></a>
Z
zengyawen 已提交
372

Z
zengyawen 已提交
373
play(): void
Z
zengyawen 已提交
374

W
wusongqing 已提交
375
开始播放音频资源,需在[dataLoad](#on('play' | 'pause' | 'stop' | 'reset' | 'dataload' | 'finish' | 'volumechange'))事件成功触发后,才能调用play方法。
Z
zengyawen 已提交
376

W
wusongqing 已提交
377
**示例:**
Z
zengyawen 已提交
378

379
```js
W
wusongqing 已提交
380
audioPlayer.on('play', () => {    //设置'play'事件回调
381
    console.log('audio play success');
Z
zengyawen 已提交
382
});
383
audioPlayer.play();
Z
zengyawen 已提交
384
```
Z
zengyawen 已提交
385

386
### pause<a name=audioplayer_pause></a>
Z
zengyawen 已提交
387

Z
zengyawen 已提交
388
pause(): void
Z
zengyawen 已提交
389

W
wusongqing 已提交
390
暂停播放音频资源。
B
bird_j 已提交
391

W
wusongqing 已提交
392
**示例:**
Z
zengyawen 已提交
393

394
```js
W
wusongqing 已提交
395
audioPlayer.on('pause', () => {    //设置'pause'事件回调
396
    console.log('audio pause success');
Z
zengyawen 已提交
397
});
398
audioPlayer.pause();
Z
zengyawen 已提交
399
```
Z
zengyawen 已提交
400

401
### stop<a name=audioplayer_stop></a>
Z
zengyawen 已提交
402

Z
zengyawen 已提交
403
stop(): void
Z
zengyawen 已提交
404

W
wusongqing 已提交
405
停止播放音频资源。
Z
zengyawen 已提交
406

W
wusongqing 已提交
407
**示例:**
Z
zengyawen 已提交
408

409
```js
W
wusongqing 已提交
410
audioPlayer.on('stop', () => {    //设置'stop'事件回调
411 412 413
    console.log('audio stop success');
});
audioPlayer.stop();
Z
zengyawen 已提交
414
```
415 416 417 418 419

### reset<sup>7+</sup><a name=audioplayer_reset></a>

reset(): void

W
wusongqing 已提交
420
切换播放音频资源。
B
bird_j 已提交
421

W
wusongqing 已提交
422
**示例:**
423 424

```js
W
wusongqing 已提交
425
audioPlayer.on('reset', () => {    //设置'reset'事件回调
426
    console.log('audio reset success');
Z
zengyawen 已提交
427
});
428
audioPlayer.reset();
Z
zengyawen 已提交
429
```
Z
zengyawen 已提交
430

431
### seek<a name=audioplayer_seek></a>
Z
zengyawen 已提交
432

Z
zengyawen 已提交
433
seek(timeMs: number): void
Z
zengyawen 已提交
434

W
wusongqing 已提交
435
跳转到指定播放位置。
Z
zengyawen 已提交
436

W
wusongqing 已提交
437
**参数:**
B
bird_j 已提交
438

W
wusongqing 已提交
439
| 参数名 | 类型   | 必填 | 说明                           |
440
| ------ | ------ | ---- | ------------------------------ |
W
wusongqing 已提交
441
| timeMs | number | 是   | 指定的跳转时间节点,单位毫秒。 |
Z
zengyawen 已提交
442

W
wusongqing 已提交
443
**示例:**
Z
zengyawen 已提交
444

445
```js
W
wusongqing 已提交
446
audioPlayer.on('timeUpdate', (seekDoneTime) => {    //设置'timeUpdate'事件回调
447 448 449 450 451
    if (typeof (seekDoneTime) == 'undefined') {
        console.info('audio seek fail');
        return;
    }
    console.log('audio seek success. seekDoneTime: ' + seekDoneTime);
Z
zengyawen 已提交
452
});
W
wusongqing 已提交
453
audioPlayer.seek(30000);    //seek到30000ms的位置
Z
zengyawen 已提交
454
```
Z
zengyawen 已提交
455

456
### setVolume<a name=audioplayer_setvolume></a>
Z
zengyawen 已提交
457

Z
zengyawen 已提交
458
setVolume(vol: number): void
Z
zengyawen 已提交
459

W
wusongqing 已提交
460
设置音量。
B
bird_j 已提交
461

W
wusongqing 已提交
462
**参数:**
Z
zengyawen 已提交
463

W
wusongqing 已提交
464
| 参数名 | 类型   | 必填 | 说明                                                         |
465
| ------ | ------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
466
| vol    | number | 是   | 指定的相对音量大小,取值范围为[0.00-1.00],1表示最大音量,即100%。 |
Z
zengyawen 已提交
467

W
wusongqing 已提交
468
**示例:**
Z
zengyawen 已提交
469

470
```js
W
wusongqing 已提交
471
audioPlayer.on('volumeChange', () => {    //设置'volumeChange'事件回调
472
    console.log('audio volumeChange success');
Z
zengyawen 已提交
473
});
W
wusongqing 已提交
474
audioPlayer.setVolume(1);    //设置音量到100%
Z
zengyawen 已提交
475
```
Z
zengyawen 已提交
476

477
### release<a name=audioplayer_release></a>
Z
zengyawen 已提交
478

479
release(): void
Z
zengyawen 已提交
480

W
wusongqing 已提交
481
释放音频资源。
B
bird_j 已提交
482

W
wusongqing 已提交
483
**示例:**
Z
zengyawen 已提交
484

485 486 487
```js
audioPlayer.release();
audioPlayer = undefined;
Z
zengyawen 已提交
488
```
Z
zengyawen 已提交
489

490
### getTrackDescription<sup>8+</sup><a name=audioplayer_gettrackdescription1></a>
Z
zengyawen 已提交
491

492
getTrackDescription(callback: AsyncCallback<Array<[MediaDescription](#mediadescription8)>>): void
Z
zengyawen 已提交
493

W
wusongqing 已提交
494
通过回调方式获取音频轨道信息。
495

W
wusongqing 已提交
496
**参数:**
B
bird_j 已提交
497

W
wusongqing 已提交
498
| 参数名   | 类型                                                         | 必填 | 说明                       |
499
| -------- | ------------------------------------------------------------ | ---- | -------------------------- |
W
wusongqing 已提交
500
| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | 是   | 获取音频轨道信息回调方法。 |
Z
zengyawen 已提交
501

W
wusongqing 已提交
502
**示例:**
Z
zengyawen 已提交
503

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
```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 已提交
522
```
523 524 525 526 527

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

getTrackDescription(): Promise<Array<[MediaDescription](#mediadescription8)>>

W
wusongqing 已提交
528
通过Promise方式获取音频轨道信息。
B
bird_j 已提交
529

W
wusongqing 已提交
530
**返回值:**
531

W
wusongqing 已提交
532
| 类型                                                   | 说明                            |
533
| ------------------------------------------------------ | ------------------------------- |
W
wusongqing 已提交
534
| Promise<Array<[MediaDescription](#mediadescription8)>> | 获取音频轨道信息Promise返回值。 |
535

W
wusongqing 已提交
536
**示例:**
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

```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 已提交
563 564
```

565
### on('bufferingUpdate')<sup>8+</sup>
Z
zengyawen 已提交
566

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

W
wusongqing 已提交
569
开始订阅音频缓存更新事件。
Z
zengyawen 已提交
570

W
wusongqing 已提交
571
**参数:**
B
bird_j 已提交
572

W
wusongqing 已提交
573
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
574
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
575 576
| type     | string                                                       | 是   | 音频缓存事件回调类型,支持的事件:'bufferingUpdate'。        |
| callback | (infoType: [BufferingInfoType](#bufferinginfotype8), value: number) => void | 是   | 音频缓存事件回调方法。<br>[BufferingInfoType](#bufferinginfotype8)为BUFFERING_PERCENT或CACHED_DURATION时,value值有效,否则固定为0。 |
Z
zengyawen 已提交
577

W
wusongqing 已提交
578
**示例:**
Z
zengyawen 已提交
579

580 581 582 583 584
```js
audioPlayer.on('bufferingUpdate', (infoType, value) => {
    console.log('audio bufferingInfo type: ' + infoType);
    console.log('audio bufferingInfo value: ' + value);
});
Z
zengyawen 已提交
585
```
586 587 588 589 590

 ### on('play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange')

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

W
wusongqing 已提交
591
开始订阅音频播放事件。
B
bird_j 已提交
592

W
wusongqing 已提交
593
**参数:**
594

W
wusongqing 已提交
595
| 参数名   | 类型       | 必填 | 说明                                                         |
596
| -------- | ---------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
597 598
| type     | string     | 是   | 播放事件回调类型,支持的事件包括:'play' \| 'pause' \| 'stop' \| 'reset' \| 'dataLoad' \| 'finish' \| 'volumeChange'。<br>- 'play':完成[play()](#play)调用,音频开始播放,触发该事件。<br>- 'pause':完成[pause()](#pause)调用,音频暂停播放,触发该事件。<br>- 'stop':完成[stop()](#stop)调用,音频停止播放,触发该事件。<br>- 'reset':完成[reset()](#reset7)调用,播放器重置,触发该事件。<br>- 'dataLoad':完成音频数据加载后触发该事件,即src属性设置完成后触发该事件。<br>- 'finish':完成音频播放后触发该事件。<br>- 'volumeChange':完成[setVolume()](#setvolume)调用,播放音量改变后触发该事件。 |
| callback | () => void | 是   | 播放事件回调方法。                                           |
599

W
wusongqing 已提交
600
**示例:**
601 602

```js
W
wusongqing 已提交
603 604
let audioPlayer = media.createAudioPlayer();  //创建一个音频播放实例
audioPlayer.on('dataLoad', () => {            //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
605
	console.info('audio set source success');
W
wusongqing 已提交
606
    audioPlayer.play();                       //开始播放,并触发'play'事件回调
607
});
W
wusongqing 已提交
608
audioPlayer.on('play', () => {                //设置'play'事件回调
609
	console.info('audio play success');
W
wusongqing 已提交
610
    audioPlayer.seek(30000);                  //调用seek方法,并触发'timeUpdate'事件回调
611
});
W
wusongqing 已提交
612
audioPlayer.on('pause', () => {               //设置'pause'事件回调
613
	console.info('audio pause success');
W
wusongqing 已提交
614
    audioPlayer.stop();                       //停止播放,并触发'stop'事件回调
615
});
W
wusongqing 已提交
616
audioPlayer.on('reset', () => {               //设置'reset'事件回调
617
	console.info('audio reset success');
W
wusongqing 已提交
618
    audioPlayer.release();                    //释放播放实例资源
619 620
    audioPlayer = undefined;
});
W
wusongqing 已提交
621
audioPlayer.on('timeUpdate', (seekDoneTime) => {  //设置'timeUpdate'事件回调
622 623 624 625 626
	if (typeof(seekDoneTime) == "undefined") {
        console.info('audio seek fail');
        return;
    }
    console.info('audio seek success, and seek time is ' + seekDoneTime);
W
wusongqing 已提交
627
    audioPlayer.setVolume(0.5);                //设置音量为50%,并触发'volumeChange'事件回调
628
});
W
wusongqing 已提交
629
audioPlayer.on('volumeChange', () => {         //设置'volumeChange'事件回调
630
	console.info('audio volumeChange success');
W
wusongqing 已提交
631
    audioPlayer.pause();                       //暂停播放,并触发'pause'事件回调
632
});
W
wusongqing 已提交
633
audioPlayer.on('finish', () => {               //设置'finish'事件回调
634
	console.info('audio play finish');
W
wusongqing 已提交
635
    audioPlayer.stop();                        //停止播放,并触发'stop'事件回调
636
});
W
wusongqing 已提交
637
audioPlayer.on('error', (error) => {           //设置'error'事件回调
638 639 640
	console.info(`audio error called, errName is ${error.name}`);
    console.info(`audio error called, errCode is ${error.code}`);
    console.info(`audio error called, errMessage is ${error.message}`);
Z
zengyawen 已提交
641
});
W
wusongqing 已提交
642
audioPlayer.src = 'file:///data/data/ohos.xxx.xxx/files/test.mp4';  //设置src属性,并触发'dataLoad'事件回调
Z
zengyawen 已提交
643 644 645 646 647
```

### on('timeUpdate')

on(type: 'timeUpdate', callback: Callback\<number>): void
Z
zengyawen 已提交
648

W
wusongqing 已提交
649
开始订阅音频播放[seek()](#seek)时间更新事件。
Z
zengyawen 已提交
650

W
wusongqing 已提交
651
**参数:**
B
bird_j 已提交
652

W
wusongqing 已提交
653
| 参数名   | 类型              | 必填 | 说明                                                         |
654
| -------- | ----------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
655 656
| type     | string            | 是   | 播放事件回调类型,支持的事件包括:'timeUpdate'。<br>- 'timeUpdate':[seek()](#seek)调用完成,触发该事件。 |
| callback | Callback\<number> | 是   | 播放事件回调方法。回调方法入参为成功seek的时间。             |
Z
zengyawen 已提交
657

W
wusongqing 已提交
658
**示例:**
Z
zengyawen 已提交
659

660
```js
W
wusongqing 已提交
661
audioPlayer.on('timeUpdate', (seekDoneTime) => {    //设置'timeUpdate'事件回调
662 663 664 665 666
    if (typeof (seekDoneTime) == 'undefined') {
        console.info('audio seek fail');
        return;
    }
    console.log('audio seek success. seekDoneTime: ' + seekDoneTime);
Z
zengyawen 已提交
667
});
W
wusongqing 已提交
668
audioPlayer.seek(30000);    //seek到30000ms的位置
Z
zengyawen 已提交
669 670 671 672 673
```

### on('error')

on(type: 'error', callback: ErrorCallback): void
Z
zengyawen 已提交
674

W
wusongqing 已提交
675
开始订阅音频播放错误事件。
B
bird_j 已提交
676

W
wusongqing 已提交
677
**参数:**
Z
zengyawen 已提交
678

W
wusongqing 已提交
679
| 参数名   | 类型          | 必填 | 说明                                                         |
680
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
681 682
| type     | string        | 是   | 播放错误事件回调类型,支持的事件包括:'error'。<br>- 'error':音频播放中发生错误,触发该事件。 |
| callback | ErrorCallback | 是   | 播放错误事件回调方法。                                       |
Z
zengyawen 已提交
683

W
wusongqing 已提交
684
**示例:**
Z
zengyawen 已提交
685

686
```js
W
wusongqing 已提交
687 688 689 690
audioPlayer.on('error', (error) => {      //设置'error'事件回调
	console.info(`audio error called, errName is ${error.name}`);      //打印错误类型名称
    console.info(`audio error called, errCode is ${error.code}`);      //打印错误码
    console.info(`audio error called, errMessage is ${error.message}`);//打印错误类型详细描述
Z
zengyawen 已提交
691
});
W
wusongqing 已提交
692
audioPlayer.setVolume(3);  //设置volume为无效值,触发'error'事件
Z
zengyawen 已提交
693 694 695
```

## AudioState
Z
zengyawen 已提交
696

W
wusongqing 已提交
697
音频播放的状态机。可通过state属性获取当前状态。
698

W
wusongqing 已提交
699 700 701 702 703 704 705
| 名称               | 类型   | 描述           |
| ------------------ | ------ | -------------- |
| idle               | string | 音频播放空闲。 |
| playing            | string | 音频正在播放。 |
| paused             | string | 音频暂停播放。 |
| stopped            | string | 音频播放停止。 |
| error<sup>8+</sup> | string | 错误状态。     |
706

707 708
## VideoPlayer<sup>8+</sup>

W
wusongqing 已提交
709
视频播放管理类,用于管理和播放视频媒体。在调用VideoPlayer的方法前,需要先通过[createVideoPlayer()](#media.createvideoplayer8)构建一个[VideoPlayer](#videoplayer8)实例。
710

W
wusongqing 已提交
711
视频播放demo可参考:[视频播放开发指导](../../media/video-playback.md)
712

W
wusongqing 已提交
713
### 属性<a name=videoplayer_属性></a><sup>8+</sup>
714

W
wusongqing 已提交
715 716 717 718 719 720 721 722 723
| 名称        | 类型                               | 可读 | 可写 | 说明                                                         |
| ----------- | ---------------------------------- | ---- | ---- | ------------------------------------------------------------ |
| url         | string                             | 是   | 是   | 视频媒体URL,支持当前主流的视频格式(mp4、mpeg-ts、webm、mkv)。<br>**支持路径示例**<br>1. 本地绝对路径:file:///data/data/ohos.xxx.xxx/files/test.mp4<br>![zh-cn_image_0000001164217678](figures/zh-cn_image_0000001164217678.png)<br>**注意事项**<br>媒体素材需至少赋予读权限后,才可正常播放 |
| loop        | boolean                            | 是   | 是   | 视频循环播放属性,设置为'true'表示循环播放。                 |
| currentTime | number                             | 是   | 否   | 视频的当前播放位置。                                         |
| duration    | number                             | 是   | 否   | 视频时长,返回-1表示直播模式                                 |
| state       | [VideoPlayState](#videoplaystate8) | 是   | 否   | 视频播放的状态。                                             |
| width       | number                             | 是   | 否   | 视频宽。                                                     |
| height      | number                             | 是   | 否   | 视频高。                                                     |
724 725 726 727 728

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

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

W
wusongqing 已提交
729
通过回调方式设置SurfaceId。
B
bird_j 已提交
730

W
wusongqing 已提交
731
**参数:**
732

W
wusongqing 已提交
733
| 参数名    | 类型     | 必填 | 说明                      |
734
| --------- | -------- | ---- | ------------------------- |
W
wusongqing 已提交
735 736
| surfaceId | string   | 是   | SurfaceId                 |
| callback  | function | 是   | 设置SurfaceId的回调方法。 |
737

W
wusongqing 已提交
738
**示例:**
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

```js
videoPlayer.setDisplaySurface(surfaceId, (err) => {
	if (typeof (err) == 'undefined') {
		console.info('setDisplaySurface success!');
	} else {
        console.info('setDisplaySurface fail!');
    }
});
```

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

setDisplaySurface(surfaceId: string): Promise\<void>

W
wusongqing 已提交
754
通过Promise方式设置SurfaceId。
755

W
wusongqing 已提交
756
**参数:**
B
bird_j 已提交
757

W
wusongqing 已提交
758
| 参数名    | 类型   | 必填 | 说明      |
759
| --------- | ------ | ---- | --------- |
W
wusongqing 已提交
760
| surfaceId | string | 是   | SurfaceId |
761

W
wusongqing 已提交
762
**返回值:**
763

W
wusongqing 已提交
764
| 类型          | 说明                           |
765
| ------------- | ------------------------------ |
W
wusongqing 已提交
766
| Promise<void> | 设置SurfaceId的Promise返回值。 |
767

W
wusongqing 已提交
768
**示例:**
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785

```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

W
wusongqing 已提交
786
通过回调方式准备播放视频。
B
bird_j 已提交
787

W
wusongqing 已提交
788
**参数:**
789

W
wusongqing 已提交
790
| 参数名   | 类型     | 必填 | 说明                     |
791
| -------- | -------- | ---- | ------------------------ |
W
wusongqing 已提交
792
| callback | function | 是   | 准备播放视频的回调方法。 |
793

W
wusongqing 已提交
794
**示例:**
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809

```js
videoPlayer.prepare((err) => {
	if (typeof (err) == 'undefined') {
		console.info('prepare success!');
	} else {
        console.info('prepare fail!');
    }
});
```

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

prepare(): Promise\<void>

W
wusongqing 已提交
810
通过Promise方式准备播放视频。
811

W
wusongqing 已提交
812
**返回值:**
B
bird_j 已提交
813

W
wusongqing 已提交
814
| 类型           | 说明                          |
815
| -------------- | ----------------------------- |
W
wusongqing 已提交
816
| Promise\<void> | 准备播放视频的Promise返回值。 |
817

W
wusongqing 已提交
818
**示例:**
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

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

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

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

W
wusongqing 已提交
836
通过回调方式开始播放视频。
B
bird_j 已提交
837

W
wusongqing 已提交
838
**参数:**
839

W
wusongqing 已提交
840
| 参数名   | 类型     | 必填 | 说明                     |
841
| -------- | -------- | ---- | ------------------------ |
W
wusongqing 已提交
842
| callback | function | 是   | 开始播放视频的回调方法。 |
843

W
wusongqing 已提交
844
**示例:**
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

```js
videoPlayer.play((err) => {
	if (typeof (err) == 'undefined') {
		console.info('play success!');
	} else {
        console.info('play fail!');
    }
});
```

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

play(): Promise\<void>;

W
wusongqing 已提交
860
通过Promise方式开始播放视频。
861

W
wusongqing 已提交
862
**返回值:**
B
bird_j 已提交
863

W
wusongqing 已提交
864
| 类型           | 说明                          |
865
| -------------- | ----------------------------- |
W
wusongqing 已提交
866
| Promise\<void> | 开始播放视频的Promise返回值。 |
867

W
wusongqing 已提交
868
**示例:**
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

```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);
```

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

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

W
wusongqing 已提交
886
通过回调方式暂停播放视频。
B
bird_j 已提交
887

W
wusongqing 已提交
888
**参数:**
889

W
wusongqing 已提交
890
| 参数名   | 类型     | 必填 | 说明                     |
891
| -------- | -------- | ---- | ------------------------ |
W
wusongqing 已提交
892
| callback | function | 是   | 暂停播放视频的回调方法。 |
893

W
wusongqing 已提交
894
**示例:**
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

```js
videoPlayer.pause((err) => {
	if (typeof (err) == 'undefined') {
		console.info('pause success!');
	} else {
        console.info('pause fail!');
    }
});
```

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

pause(): Promise\<void>

W
wusongqing 已提交
910
通过Promise方式暂停播放视频。
911

W
wusongqing 已提交
912
**返回值:**
B
bird_j 已提交
913

W
wusongqing 已提交
914
| 类型           | 说明                          |
915
| -------------- | ----------------------------- |
W
wusongqing 已提交
916
| Promise\<void> | 暂停播放视频的Promise返回值。 |
917

W
wusongqing 已提交
918
**示例:**
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

```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);
```

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

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

W
wusongqing 已提交
936
通过回调方式停止播放视频。
B
bird_j 已提交
937

W
wusongqing 已提交
938
**参数:**
939

W
wusongqing 已提交
940
| 参数名   | 类型     | 必填 | 说明                     |
941
| -------- | -------- | ---- | ------------------------ |
W
wusongqing 已提交
942
| callback | function | 是   | 停止播放视频的回调方法。 |
943

W
wusongqing 已提交
944
**示例:**
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

```js
videoPlayer.stop((err) => {
	if (typeof (err) == 'undefined') {
		console.info('stop success!');
	} else {
        console.info('stop fail!');
    }
});
```

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

stop(): Promise\<void>

W
wusongqing 已提交
960
通过Promise方式停止播放视频。
B
bird_j 已提交
961

W
wusongqing 已提交
962
**返回值:**
963

W
wusongqing 已提交
964
| 类型           | 说明                          |
965
| -------------- | ----------------------------- |
W
wusongqing 已提交
966
| Promise\<void> | 停止播放视频的Promise返回值。 |
967

W
wusongqing 已提交
968
**示例:**
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

```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);
```

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

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

W
wusongqing 已提交
986
通过回调方式切换播放视频。
987

W
wusongqing 已提交
988
**参数:**
B
bird_j 已提交
989

W
wusongqing 已提交
990
| 参数名   | 类型     | 必填 | 说明                     |
991
| -------- | -------- | ---- | ------------------------ |
W
wusongqing 已提交
992
| callback | function | 是   | 切换播放视频的回调方法。 |
993

W
wusongqing 已提交
994
**示例:**
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

```js
videoPlayer.reset((err) => {
	if (typeof (err) == 'undefined') {
		console.info('reset success!');
	} else {
        console.info('reset fail!');
    }
});
```

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

reset(): Promise\<void>

W
wusongqing 已提交
1010
通过Promise方式切换播放视频。
B
bird_j 已提交
1011

W
wusongqing 已提交
1012
**返回值:**
1013

W
wusongqing 已提交
1014
| 类型           | 说明                          |
1015
| -------------- | ----------------------------- |
W
wusongqing 已提交
1016
| Promise\<void> | 切换播放视频的Promise返回值。 |
1017

W
wusongqing 已提交
1018
**示例:**
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

```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);
```

### seek<sup>8+</sup>

seek(timeMs: number, callback: AsyncCallback\<number>): void

W
wusongqing 已提交
1036
通过回调方式跳转到指定播放位置,默认跳转到指定时间点的下一个关键帧。
1037

W
wusongqing 已提交
1038
**参数:**
B
bird_j 已提交
1039

W
wusongqing 已提交
1040
| 参数名   | 类型     | 必填 | 说明                           |
1041
| -------- | -------- | ---- | ------------------------------ |
W
wusongqing 已提交
1042 1043
| timeMs   | number   | 是   | 指定的跳转时间节点,单位毫秒。 |
| callback | function | 是   | 跳转到指定播放位置的回调方法。 |
1044

W
wusongqing 已提交
1045
**示例:**
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

```js
videoPlayer.seek((seekTime, err) => {
	if (typeof (err) == 'undefined') {
		console.info('seek success!');
	} else {
        console.info('seek fail!');
    }
});
```

### seek<sup>8+</sup>

seek(timeMs: number, mode:SeekMode, callback: AsyncCallback\<number>): void

W
wusongqing 已提交
1061
通过回调方式跳转到指定播放位置。
B
bird_j 已提交
1062

W
wusongqing 已提交
1063
**参数:**
1064

W
wusongqing 已提交
1065
| 参数名   | 类型     | 必填 | 说明                                     |
1066
| -------- | -------- | ---- | ---------------------------------------- |
W
wusongqing 已提交
1067 1068 1069
| timeMs   | number   | 是   | 指定的跳转时间节点,单位毫秒。           |
| mode     | SeekMode | 是   | 跳转模式,具体见[SeekMode](#seekmode8)。 |
| callback | function | 是   | 跳转到指定播放位置的回调方法。           |
1070

W
wusongqing 已提交
1071
**示例:**
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

```js
videoPlayer.seek((seekTime, seekMode, err) => {
	if (typeof (err) == 'undefined') {
		console.info('seek success!');
	} else {
        console.info('seek fail!');
    }
});
```

### seek<sup>8+</sup>

seek(timeMs: number, mode?:SeekMode): Promise\<number>

W
wusongqing 已提交
1087
通过Promise方式跳转到指定播放位置,如果没有设置mode则跳转到指定时间点的下一个关键帧。
1088

W
wusongqing 已提交
1089
**参数:**
B
bird_j 已提交
1090

W
wusongqing 已提交
1091
| 参数名 | 类型     | 必填 | 说明                                   |
1092
| ------ | -------- | ---- | -------------------------------------- |
W
wusongqing 已提交
1093 1094
| timeMs | number   | 是   | 指定的跳转时间节点,单位毫秒。         |
| mode   | SeekMode | 否   | 跳转模式,具体见[SeekMode](#seekmode8) |
1095

W
wusongqing 已提交
1096
**返回值:**
1097

W
wusongqing 已提交
1098
| 类型           | 说明                                |
1099
| -------------- | ----------------------------------- |
W
wusongqing 已提交
1100
| Promise\<void> | 跳转到指定播放位置的Promise返回值。 |
1101

W
wusongqing 已提交
1102
**示例:**
1103 1104 1105 1106 1107 1108 1109 1110

```js
function failureCallback(error) {
    console.info(`video failureCallback, error:${error.message}`);
}
function catchCallback(error) {
    console.info(`video catchCallback, error:${error.message}`);
}
W
wusongqing 已提交
1111
await videoPlayer.seek(seekTime).then((seekDoneTime) => { // seekDoneTime表示seek完成后的时间点
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    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

W
wusongqing 已提交
1124
通过回调方式设置音量。
B
bird_j 已提交
1125

W
wusongqing 已提交
1126
**参数:**
1127

W
wusongqing 已提交
1128
| 参数名   | 类型     | 必填 | 说明                                                         |
1129
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1130 1131
| vol      | number   | 是   | 指定的相对音量大小,取值范围为[0.00-1.00],1表示最大音量,即100%。 |
| callback | function | 是   | 设置音量的回调方法。                                         |
1132

W
wusongqing 已提交
1133
**示例:**
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

```js
videoPlayer.setVolume((vol, err) => {
	if (typeof (err) == 'undefined') {
		console.info('setVolume success!');
	} else {
        console.info('setVolume fail!');
    }
});
```

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

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

W
wusongqing 已提交
1149
通过Promise方式设置音量。
1150

W
wusongqing 已提交
1151
**参数:**
B
bird_j 已提交
1152

W
wusongqing 已提交
1153
| 参数名 | 类型   | 必填 | 说明                                                         |
1154
| ------ | ------ | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1155
| vol    | number | 是   | 指定的相对音量大小,取值范围为[0.00-1.00],1表示最大音量,即100%。 |
1156

W
wusongqing 已提交
1157
**返回值:**
1158

W
wusongqing 已提交
1159
| 类型           | 说明                      |
1160
| -------------- | ------------------------- |
W
wusongqing 已提交
1161
| Promise\<void> | 设置音量的Promise返回值。 |
1162

W
wusongqing 已提交
1163
**示例:**
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

```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

W
wusongqing 已提交
1181
通过回调方式释放视频资源。
B
bird_j 已提交
1182

W
wusongqing 已提交
1183
**参数:**
1184

W
wusongqing 已提交
1185
| 参数名   | 类型     | 必填 | 说明                     |
1186
| -------- | -------- | ---- | ------------------------ |
W
wusongqing 已提交
1187
| callback | function | 是   | 释放视频资源的回调方法。 |
1188

W
wusongqing 已提交
1189
**示例:**
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

```js
videoPlayer.release((err) => {
	if (typeof (err) == 'undefined') {
		console.info('release success!');
	} else {
        console.info('release fail!');
    }
});
```

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

release(): Promise\<void>

W
wusongqing 已提交
1205
通过Promise方式释放视频资源。
B
bird_j 已提交
1206

W
wusongqing 已提交
1207
**返回值:**
1208

W
wusongqing 已提交
1209
| 类型           | 说明                          |
1210
| -------------- | ----------------------------- |
W
wusongqing 已提交
1211
| Promise\<void> | 释放视频资源的Promise返回值。 |
1212

W
wusongqing 已提交
1213
**示例:**
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

```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>

getTrackDescription(callback: AsyncCallback<Array<[MediaDescription](#mediadescription8>>)>>): void

W
wusongqing 已提交
1231
通过回调方式获取视频轨道信息。
1232

W
wusongqing 已提交
1233
**参数:**
B
bird_j 已提交
1234

W
wusongqing 已提交
1235
| 参数名   | 类型     | 必填 | 说明                       |
1236
| -------- | -------- | ---- | -------------------------- |
W
wusongqing 已提交
1237
| callback | function | 是   | 获取视频轨道信息回调方法。 |
1238

W
wusongqing 已提交
1239
**示例:**
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

```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>

getTrackDescription(): Promise<Array<[MediaDescription](#mediadescription8>>)>>

W
wusongqing 已提交
1265
通过Promise方式获取视频轨道信息。
B
bird_j 已提交
1266

W
wusongqing 已提交
1267
**返回值:**
1268

W
wusongqing 已提交
1269
| 类型                                                     | 说明                            |
1270
| -------------------------------------------------------- | ------------------------------- |
W
wusongqing 已提交
1271
| Promise<Array<[MediaDescription](#mediadescription8>>)>> | 获取视频轨道信息Promise返回值。 |
1272

W
wusongqing 已提交
1273
**示例:**
1274 1275 1276 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

```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

W
wusongqing 已提交
1307
通过回调方式设置播放速度。
1308

W
wusongqing 已提交
1309
**参数:**
B
bird_j 已提交
1310

W
wusongqing 已提交
1311
| 参数名   | 类型     | 必填 | 说明                                                       |
1312
| -------- | -------- | ---- | ---------------------------------------------------------- |
W
wusongqing 已提交
1313 1314
| speed    | number   | 是   | 指定播放视频速度,具体见[PlaybackSpeed](#playbackspeed8)。 |
| callback | function | 是   | 设置播放速度的回调方法。                                   |
1315

W
wusongqing 已提交
1316
**示例:**
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331

```js
videoPlayer.setSpeed((speed:number, err) => {
	if (typeof (err) == 'undefined') {
		console.info('setSpeed success!');
	} else {
        console.info('setSpeed fail!');
    }
});
```

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

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

W
wusongqing 已提交
1332
通过Promise方式设置播放速度。
B
bird_j 已提交
1333

W
wusongqing 已提交
1334
**参数:**
1335

W
wusongqing 已提交
1336
| 参数名 | 类型   | 必填 | 说明                                                       |
1337
| ------ | ------ | ---- | ---------------------------------------------------------- |
W
wusongqing 已提交
1338
| speed  | number | 是   | 指定播放视频速度,具体见[PlaybackSpeed](#playbackspeed8)。 |
1339

W
wusongqing 已提交
1340
**示例:**
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

```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

W
wusongqing 已提交
1358
开始监听视频播放完成事件。
1359

W
wusongqing 已提交
1360
**参数:**
B
bird_j 已提交
1361

W
wusongqing 已提交
1362
| 参数名   | 类型     | 必填 | 说明                                                        |
1363
| -------- | -------- | ---- | ----------------------------------------------------------- |
W
wusongqing 已提交
1364 1365
| type     | string   | 是   | 视频播放完成事件回调类型,支持的事件:'playbackCompleted'。 |
| callback | function | 是   | 视频播放完成事件回调方法。                                  |
1366

W
wusongqing 已提交
1367
**示例:**
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

```js
videoPlayer.on('playbackCompleted', () => {
	console.info('playbackCompleted success!');
});
```

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

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

W
wusongqing 已提交
1379
开始监听视频缓存更新事件。
B
bird_j 已提交
1380

W
wusongqing 已提交
1381
**参数:**
1382

W
wusongqing 已提交
1383
| 参数名   | 类型     | 必填 | 说明                                                         |
1384
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1385 1386
| type     | string   | 是   | 视频缓存事件回调类型,支持的事件:'bufferingUpdate'。        |
| callback | function | 是   | 视频缓存事件回调方法。<br>[BufferingInfoType](#bufferinginfotype8)为BUFFERING_PERCENT或CACHED_DURATION时,value值有效,否则固定为0。 |
1387

W
wusongqing 已提交
1388
**示例:**
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400

```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

W
wusongqing 已提交
1401
开始监听视频播放首帧送显上报事件。
1402

W
wusongqing 已提交
1403
**参数:**
B
bird_j 已提交
1404

W
wusongqing 已提交
1405
| 参数名   | 类型     | 必填 | 说明                                                         |
1406
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1407 1408
| type     | string   | 是   | 视频播放首帧送显上报事件回调类型,支持的事件:'startRenderFrame'。 |
| callback | function | 是   | 视频播放首帧送显上报事件回调方法。                           |
1409

W
wusongqing 已提交
1410
**示例:**
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421

```js
videoPlayer.on('startRenderFrame', () => {
	console.info('startRenderFrame success!');
});
```

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

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

W
wusongqing 已提交
1422
开始监听视频播放宽高变化事件。
B
bird_j 已提交
1423

W
wusongqing 已提交
1424
**参数:**
1425

W
wusongqing 已提交
1426
| 参数名   | 类型     | 必填 | 说明                                                         |
1427
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1428 1429
| type     | string   | 是   | 视频播放宽高变化事件回调类型,支持的事件:'videoSizeChanged'。 |
| callback | function | 是   | 视频播放宽高变化事件回调方法,width表示宽,height表示高。    |
1430

W
wusongqing 已提交
1431
**示例:**
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443

```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

W
wusongqing 已提交
1444
开始监听视频播放错误事件。
B
bird_j 已提交
1445

W
wusongqing 已提交
1446
**参数:**
1447

W
wusongqing 已提交
1448
| 参数名   | 类型     | 必填 | 说明                                                         |
1449
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1450 1451
| type     | string   | 是   | 播放错误事件回调类型,支持的事件包括:'error'。<br>- 'error':视频播放中发生错误,触发该事件。 |
| callback | function | 是   | 播放错误事件回调方法。                                       |
1452

W
wusongqing 已提交
1453
**示例:**
1454 1455

```js
W
wusongqing 已提交
1456 1457 1458 1459
videoPlayer.on('error', (error) => {      // 设置'error'事件回调
	console.info(`video error called, errName is ${error.name}`);      // 打印错误类型名称
    console.info(`video error called, errCode is ${error.code}`);      // 打印错误码
    console.info(`video error called, errMessage is ${error.message}`);// 打印错误类型详细描述
1460
});
W
wusongqing 已提交
1461
videoPlayer.setVolume(3);  //设置volume为无效值,触发'error'事件
1462 1463 1464 1465
```

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

W
wusongqing 已提交
1466
视频播放的状态机,可通过state属性获取当前状态。
1467

W
wusongqing 已提交
1468 1469 1470 1471 1472 1473 1474 1475
| 名称     | 类型   | 描述           |
| -------- | ------ | -------------- |
| idle     | string | 视频播放空闲。 |
| prepared | string | 视频播放准备。 |
| playing  | string | 视频正在播放。 |
| paused   | string | 视频暂停播放。 |
| stopped  | string | 视频播放停止。 |
| error    | string | 错误状态。     |
1476 1477 1478

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

W
wusongqing 已提交
1479
视频播放的Seek模式枚举,可通过seek方法作为参数传递下去。
1480

W
wusongqing 已提交
1481 1482 1483 1484 1485 1486
| 名称              | 值   | 描述                                                         |
| ----------------- | ---- | ------------------------------------------------------------ |
| SEEK_NEXT_SYNC    | 0    | 表示跳转到指定时间点的下一个关键帧,建议向后快进的时候用这个枚举值 |
| SEEK_PREV_SYNC    | 1    | 表示跳转到指定时间点的上一个关键帧,建议向前快进的时候用这个枚举值 |
| SEEK_CLOSEST_SYNC | 2    | 表示跳转到指定时间点最近的关键帧。                           |
| SEEK_CLOSEST      | 3    | 表示精确跳转到指定时间点。                                   |
1487 1488 1489

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

W
wusongqing 已提交
1490
视频播放的倍速枚举,可通过setSpeed方法作为参数传递下去。
1491

W
wusongqing 已提交
1492 1493 1494 1495 1496 1497 1498
| 名称                 | 值   | 描述                           |
| -------------------- | ---- | ------------------------------ |
| SPEED_FORWARD_0_75_X | 0    | 表示视频播放正常播速的0.75倍。 |
| SPEED_FORWARD_1_00_X | 1    | 表示视频播放正常播速。         |
| SPEED_FORWARD_1_25_X | 2    | 表示视频播放正常播速的1.25倍。 |
| SPEED_FORWARD_1_75_X | 3    | 表示视频播放正常播速的1.75倍。 |
| SPEED_FORWARD_2_00_X | 4    | 表示视频播放正常播速的2.00倍。 |
1499

1500 1501 1502
## MediaDescription<sup>8+</sup>

### [key : string] : any
Z
zengyawen 已提交
1503

W
wusongqing 已提交
1504
通过key-value方式获取媒体信息
1505

W
wusongqing 已提交
1506
| 名称  | 类型   | 说明                                                         |
1507
| ----- | ------ | ------------------------------------------------------------ |
W
wusongqing 已提交
1508 1509
| key   | string | 通过key值获取对应的value。key值具体可见[MediaDescriptionKey](#mediadescriptionkey8)。 |
| value | any    | 对应key值得value。其类型可为任意类型,具体key对应value的类型可参考[MediaDescriptionKey](#mediadescriptionkey8)的描述信息。 |
1510

W
wusongqing 已提交
1511
**示例:**
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522

```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++) {
W
wusongqing 已提交
1523
            printfItemDescription(arrlist[i], MD_KEY_TRACK_TYPE);  //打印出每条轨道MD_KEY_TRACK_TYPE的值
1524 1525 1526 1527 1528 1529
        }
    } else {
        console.log(`audio getTrackDescription fail, error:${error.message}`);
    }
});
```
Z
zengyawen 已提交
1530 1531 1532

## AudioRecorder

W
wusongqing 已提交
1533
音频录制管理类,用于录制音频媒体。在调用AudioRecorder的方法前,需要先通过[createAudioRecorder()](#media.createaudiorecorder)[createAudioRecorderAsync()](#media.createaudiorecorderasync8)构建一个[AudioRecorder](#audiorecorder)实例。
Z
zengyawen 已提交
1534

W
wusongqing 已提交
1535
音频录制demo可参考:[音频录制开发指导](../../media/audio-recorder.md)
1536 1537

### prepare<a name=audiorecorder_prepare></a>
Z
zengyawen 已提交
1538 1539 1540

prepare(config: AudioRecorderConfig): void

W
wusongqing 已提交
1541
录音准备。
Z
zengyawen 已提交
1542

W
wusongqing 已提交
1543
**参数:**
B
bird_j 已提交
1544

W
wusongqing 已提交
1545
| 参数名 | 类型                                        | 必填 | 说明                                                         |
Z
zengyawen 已提交
1546
| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1547
| config | [AudioRecorderConfig](#audiorecorderconfig) | 是   | 配置录音的相关参数,包括音频输出URI、[编码格式](#audioencoder)、采样率、声道数、[输出格式](#audiooutputformat)等。 |
Z
zengyawen 已提交
1548

W
wusongqing 已提交
1549
**示例:**
Z
zengyawen 已提交
1550

1551
```js
Z
zengyawen 已提交
1552
let audioRecorderConfig = {
1553
    audioEncoder : media.AudioEncoder.AAC_LC,
Z
zengyawen 已提交
1554 1555 1556
    audioEncodeBitRate : 22050,
    audioSampleRate : 22050,
    numberOfChannels : 2,
1557
    format : media.AudioOutputFormat.AAC_ADTS,
W
wusongqing 已提交
1558
    uri : 'file:///data/accounts/account_0/appdata/appdata/recorder/test.m4a',       // 文件需先由调用者创建,并给予适当的权限
1559
    location : { latitude : 30, longitude : 130},
Z
zengyawen 已提交
1560
}
W
wusongqing 已提交
1561
audioRecorder.on('prepare', () => {    //设置'prepare'事件回调
1562 1563
    console.log('prepare success');
});
B
bird_j 已提交
1564
audioRecorder.prepare(audioRecorderConfig);
Z
zengyawen 已提交
1565 1566 1567
```


1568
### start<a name=audiorecorder_start></a>
Z
zengyawen 已提交
1569 1570 1571

start(): void

W
wusongqing 已提交
1572
开始录制,需在[prepare](#audiorecorder_on)事件成功触发后,才能调用start方法。
B
bird_j 已提交
1573

W
wusongqing 已提交
1574
**示例:**
Z
zengyawen 已提交
1575

1576
```js
W
wusongqing 已提交
1577
audioRecorder.on('start', () => {    //设置'start'事件回调
1578 1579 1580
    console.log('audio recorder start success');
});
audioRecorder.start();
Z
zengyawen 已提交
1581
```
1582 1583 1584 1585 1586

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

pause():void

W
wusongqing 已提交
1587
暂停录制,需要在[start](#audiorecorder_on)事件成功触发后,才能调用pause方法。
1588

W
wusongqing 已提交
1589
**示例:**
1590 1591

```js
W
wusongqing 已提交
1592
audioRecorder.on('pause', () => {    //设置'pause'事件回调
1593 1594 1595 1596 1597 1598 1599 1600 1601
    console.log('audio recorder pause success');
});
audioRecorder.pause();
```

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

resume():void

W
wusongqing 已提交
1602
暂停录制,需要在[pause](#audiorecorder_on)事件成功触发后,才能调用resume方法。
B
bird_j 已提交
1603

W
wusongqing 已提交
1604
**示例:**
1605 1606

```js
W
wusongqing 已提交
1607
audioRecorder.on('resume', () => {    //设置'resume'事件回调
1608 1609 1610
    console.log('audio recorder resume success');
});
audioRecorder.resume();
Z
zengyawen 已提交
1611 1612
```

1613
### stop<a name=audiorecorder_stop></a>
Z
zengyawen 已提交
1614 1615 1616

stop(): void

W
wusongqing 已提交
1617
停止录音。
Z
zengyawen 已提交
1618

W
wusongqing 已提交
1619
**示例:**
Z
zengyawen 已提交
1620

1621
```js
W
wusongqing 已提交
1622
audioRecorder.on('stop', () => {    //设置'stop'事件回调
1623 1624 1625
    console.log('audio recorder stop success');
});
audioRecorder.stop();
Z
zengyawen 已提交
1626 1627
```

1628
### release<a name=audiorecorder_release></a>
Z
zengyawen 已提交
1629 1630 1631

release(): void

W
wusongqing 已提交
1632
释放录音资源。
B
bird_j 已提交
1633

W
wusongqing 已提交
1634
**示例:**
Z
zengyawen 已提交
1635

1636
```js
W
wusongqing 已提交
1637
audioRecorder.on('release', () => {    //设置'release'事件回调
B
bird_j 已提交
1638 1639
    console.log('audio recorder release success');
});
1640 1641
audioRecorder.release();
audioRecorder = undefined;
Z
zengyawen 已提交
1642 1643
```

1644
### reset<a name=audiorecorder_reset></a>
Z
zengyawen 已提交
1645 1646 1647

reset(): void

W
wusongqing 已提交
1648
重置录音。
Z
zengyawen 已提交
1649

W
wusongqing 已提交
1650
进行重置录音之前,需要先调用[stop()](#audiorecorder_stop)停止录音。重置录音之后,需要调用[prepare()](#audiorecorder_prepare)设置录音参数项,才能再次进行录音。
B
bird_j 已提交
1651

W
wusongqing 已提交
1652
**示例:**
Z
zengyawen 已提交
1653

B
bird_j 已提交
1654
```js
W
wusongqing 已提交
1655
audioRecorder.on('reset', () => {    //设置'reset'事件回调
B
bird_j 已提交
1656 1657 1658
    console.log('audio recorder reset success');
});
audioRecorder.reset();
Z
zengyawen 已提交
1659 1660
```

B
bird_j 已提交
1661
### on('prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset')<a name=audiorecorder_on></a>
Z
zengyawen 已提交
1662

1663
on(type: 'prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset', callback: () => void): void
Z
zengyawen 已提交
1664

W
wusongqing 已提交
1665
开始订阅音频录制事件。
Z
zengyawen 已提交
1666

W
wusongqing 已提交
1667
**参数:**
B
bird_j 已提交
1668

W
wusongqing 已提交
1669
| 参数名   | 类型     | 必填 | 说明                                                         |
Z
zengyawen 已提交
1670
| -------- | -------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1671 1672
| type     | string   | 是   | 录制事件回调类型,支持的事件包括:'prepare'&nbsp;\|&nbsp;'start'&nbsp;\|  'pause' \| ’resume‘ \|&nbsp;'stop'&nbsp;\|&nbsp;'release'&nbsp;\|&nbsp;'reset'。<br/>-&nbsp;'prepare'&nbsp;:完成[prepare](#audiorecorder_prepare)调用,音频录制参数设置完成,触发该事件。<br/>-&nbsp;'start'&nbsp;:完成[start](#audiorecorder_start)调用,音频录制开始,触发该事件。<br/>-&nbsp;'pause': 完成[pause](#audiorecorder_pause)调用,音频暂停录制,触发该事件。<br/>-&nbsp;'resume': 完成[resume](#audiorecorder_resume)调用,音频恢复录制,触发该事件。<br/>-&nbsp;'stop'&nbsp;:完成[stop](#audiorecorder_stop)调用,音频停止录制,触发该事件。<br/>-&nbsp;'release'&nbsp;:完成[release](#audiorecorder_release)调用,音频释放录制资源,触发该事件。<br/>-&nbsp;'reset':完成[reset](#audiorecorder_reset)调用,音频重置为初始状态,触发该事件。 |
| callback | ()=>void | 是   | 录制事件回调方法。                                           |
Z
zengyawen 已提交
1673

W
wusongqing 已提交
1674
**示例:**
Z
zengyawen 已提交
1675

1676
```js
W
wusongqing 已提交
1677
let audiorecorder = media.createAudioRecorder();  								// 创建一个音频录制实例
1678 1679 1680 1681 1682 1683
let audioRecorderConfig = {
    audioEncoder : media.AudioEncoder.AAC_LC, ,
    audioEncodeBitRate : 22050,
    audioSampleRate : 22050,
    numberOfChannels : 2,
    format : media.AudioOutputFormat.AAC_ADTS,
W
wusongqing 已提交
1684
    uri : 'file:///data/accounts/account_0/appdata/appdata/recorder/test.m4a',  // 文件需先由调用者创建,并给予适当的权限
1685 1686
    location : { latitude : 30, longitude : 130},
}
W
wusongqing 已提交
1687
audioRecorder.on('error', (error) => {             								// 设置'error'事件回调
1688 1689 1690 1691
	console.info(`audio error called, errName is ${error.name}`);
    console.info(`audio error called, errCode is ${error.code}`);
    console.info(`audio error called, errMessage is ${error.message}`);
});
W
wusongqing 已提交
1692
audioRecorder.on('prepare', () => {              								// 设置'prepare'事件回调
1693
    console.log('prepare success');
W
wusongqing 已提交
1694
    audioRecorder.start();                       								// 开始录制,并触发'start'事件回调
1695
});
W
wusongqing 已提交
1696
audioRecorder.on('start', () => {    		     								// 设置'start'事件回调
1697 1698
    console.log('audio recorder start success');
});
W
wusongqing 已提交
1699
audioRecorder.on('pause', () => {    		     								// 设置'pause'事件回调
1700 1701
    console.log('audio recorder pause success');
});
W
wusongqing 已提交
1702
audioRecorder.on('resume', () => {    		     								// 设置'resume'事件回调
1703 1704
    console.log('audio recorder resume success');
});
W
wusongqing 已提交
1705
audioRecorder.on('stop', () => {    		     								// 设置'stop'事件回调
1706 1707
    console.log('audio recorder stop success');
});
W
wusongqing 已提交
1708
audioRecorder.on('release', () => {    		     								// 设置'release'事件回调
1709 1710
    console.log('audio recorder release success');
});
W
wusongqing 已提交
1711
audioRecorder.on('reset', () => {    		     								// 设置'reset'事件回调
1712
    console.log('audio recorder reset success');
Z
zengyawen 已提交
1713
});
W
wusongqing 已提交
1714
audioRecorder.prepare(audioRecorderConfig)       								// 设置录制参数 ,并触发'prepare'事件回调
Z
zengyawen 已提交
1715 1716 1717 1718 1719 1720
```

### on('error')

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

W
wusongqing 已提交
1721
开始订阅音频录制错误事件。
B
bird_j 已提交
1722

W
wusongqing 已提交
1723
**参数:**
Z
zengyawen 已提交
1724

W
wusongqing 已提交
1725
| 参数名   | 类型          | 必填 | 说明                                                         |
Z
zengyawen 已提交
1726
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1727 1728
| type     | string        | 是   | 录制错误事件回调类型'error'。<br/>-&nbsp;'error':音频录制过程中发生错误,触发该事件。 |
| callback | ErrorCallback | 是   | 录制错误事件回调方法。                                       |
Z
zengyawen 已提交
1729

W
wusongqing 已提交
1730
**示例:**
1731 1732

```js
W
wusongqing 已提交
1733 1734 1735 1736
audioRecorder.on('error', (error) => {      							// 设置'error'事件回调
	console.info(`audio error called, errName is ${error.name}`);       // 打印错误类型名称
    console.info(`audio error called, errCode is ${error.code}`);       // 打印错误码
    console.info(`audio error called, errMessage is ${error.message}`); // 打印错误类型详细描述
1737
});
W
wusongqing 已提交
1738
audioRecorder.prepare();  												// prepare不设置参数,触发'error'事件
1739
```
Z
zengyawen 已提交
1740 1741 1742

## AudioRecorderConfig

W
wusongqing 已提交
1743
表示音频的录音配置。
Z
zengyawen 已提交
1744

W
wusongqing 已提交
1745
| 名称                  | 参数类型                                | 必填 | 说明                                                         |
1746
| --------------------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
1747 1748 1749 1750 1751 1752 1753
| audioEncoder          | [AudioEncoder](#audioencoder)           | 否   | 音频编码格式,默认设置为AAC_LC。                             |
| audioEncodeBitRate    | number                                  | 否   | 音频编码比特率,默认值为48000。                              |
| audioSampleRate       | number                                  | 否   | 音频采集采样率,默认值为48000。                              |
| numberOfChannels      | number                                  | 否   | 音频采集声道数,默认值为2。                                  |
| format                | [AudioOutputFormat](#audiooutputformat) | 否   | 音量输出封装格式,默认设置为MPEG_4。                         |
| location<sup>8+</sup> | [Location](#location8)                  | 否   | 音频采集的地理位置。                                         |
| uri                   | string                                  | 是   | 音频输出URI。支持:<br/>1.&nbsp;文件的绝对路径:file:///data/data/ohos.xxx.xxx/cache/test.mp4![zh-cn_image_0000001164217678](figures/zh-cn_image_0000001164217678.png)<br/>2.&nbsp;文件的fd路径:file://1&nbsp;(fd&nbsp;number)<br/> 文件需要由调用者创建,并赋予适当的权限。 |
Z
zengyawen 已提交
1754 1755 1756 1757


## AudioEncoder

W
wusongqing 已提交
1758
表示音频编码格式的枚举。
Z
zengyawen 已提交
1759

W
wusongqing 已提交
1760
| 名称    | 默认值 | 说明                                                         |
B
bird_j 已提交
1761
| ------- | ------ | ------------------------------------------------------------ |
W
wusongqing 已提交
1762 1763 1764 1765 1766
| DEFAULT | 0      | Default audio encoding format is AMR_NB。本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
| AMR_NB  | 1      | AMR-NB(Adaptive Multi Rate-Narrow Band Speech Codec) 编码格式。本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
| AMR_WB  | 2      | AMR-WB(Adaptive Multi Rate-Wide Band Speech Codec) 编码格式。本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
| AAC_LC  | 3      | AAC-LC(Advanced&nbsp;Audio&nbsp;Coding&nbsp;Low&nbsp;Complexity)编码格式。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
| HE_AAC  | 4      | HE_AAC(High-Efficiency Advanced&nbsp;Audio&nbsp;Coding)编码格式。本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
Z
zengyawen 已提交
1767 1768 1769 1770


## AudioOutputFormat

W
wusongqing 已提交
1771
表示音频封装格式的枚举。
Z
zengyawen 已提交
1772

W
wusongqing 已提交
1773
| 名称     | 默认值 | 说明                                                         |
Z
zengyawen 已提交
1774
| -------- | ------ | ------------------------------------------------------------ |
W
wusongqing 已提交
1775 1776 1777 1778 1779
| DEFAULT  | 0      | 默认封装格式为MPEG-4。本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
| MPEG_4   | 2      | 封装为MPEG-4格式。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
| AMR_NB   | 3      | 封装为AMR_NB格式。本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
| AMR_WB   | 4      | 封装为AMR_WB格式。本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
| AAC_ADTS | 6      | 封装为ADTS(Audio&nbsp;Data&nbsp;Transport&nbsp;Stream)格式,是AAC音频的传输流格式。<br/>**系统能力:**SystemCapability.Multimedia.Media.AudioRecorder |
1780 1781 1782

## VideoRecorder<sup>8+</sup>

W
wusongqing 已提交
1783
视频录制管理类,用于录制视频媒体。在调用VideoRecorder的方法前,需要先通过[createVideoRecorderAsync()](#media.createvideorecorderasync8)构建一个[VideoRecorder](#videorecorder8)实例。
1784

W
wusongqing 已提交
1785
视频录制demo可参考:[视频录制开发指导](../../media/video-recorder.md)
1786

W
wusongqing 已提交
1787
### 属性
1788

W
wusongqing 已提交
1789 1790 1791
| 名称  | 类型                                  | 可读 | 可写 | 说明             |
| ----- | ------------------------------------- | ---- | ---- | ---------------- |
| state | [VideoRecordState](#videorecordstate) | 是   | 否   | 视频录制的状态。 |
1792

W
wusongqing 已提交
1793
### prepare<a name=videorecorder_prepare1></a>
1794 1795 1796

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

W
wusongqing 已提交
1797
异步方式进行视频录制的参数设置。通过注册回调函数获取返回值。
1798

W
wusongqing 已提交
1799
**参数:**
B
bird_j 已提交
1800

W
wusongqing 已提交
1801
| 参数名   | 类型                                        | 必填 | 说明                                |
1802
| -------- | ------------------------------------------- | ---- | ----------------------------------- |
W
wusongqing 已提交
1803 1804
| config   | [VideoRecorderConfig](#videorecorderconfig) | 是   | 配置视频录制的相关参数。            |
| callback | AsyncCallback\<void>                        | 是   | 异步视频录制prepare方法的回调方法。 |
1805

W
wusongqing 已提交
1806
**示例:**
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825

```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,
W
wusongqing 已提交
1826
    url : 'file:///data/accounts/account_0/appdata/appdata/recorder/test.mp4',   // 文件需先由调用者创建,并给予适当的权限
1827 1828 1829 1830 1831 1832 1833
    orientationHint : 0,
    location : { latitude : 30, longitude : 130 },
}

// asyncallback
let videoRecorder = null;
let events = require('events');
B
bird_j 已提交
1834
let eventEmitter = new events.EventEmitter();                              
1835 1836 1837 1838

eventEmitter.on('prepare', () => {
    videoRecorder.prepare(videoConfig, (err) => {
        if (typeof (err) == 'undefined') {
B
bird_j 已提交
1839
            console.info('prepare success');
1840
        } else {
B
bird_j 已提交
1841
            console.info('prepare failed and error is ' + err.message);
1842 1843 1844 1845 1846 1847
        }
    });
});

media.createVideoRecorder((err, recorder) => {
    if (typeof (err) == 'undefined' && typeof (recorder) != 'undefined') {
B
bird_j 已提交
1848 1849
        videoRecorder = recorder;
        console.info('createVideoRecorder success');
W
wusongqing 已提交
1850
        eventEmitter.emit('prepare');                                        // prepare事件触发
1851
    } else {
B
bird_j 已提交
1852
        console.info('createVideoRecorder failed and error is ' + err.message);
1853 1854 1855 1856
    }
});
```

W
wusongqing 已提交
1857
### prepare<a name=videorecorder_prepare2></a>
1858 1859 1860

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

W
wusongqing 已提交
1861
异步方式进行视频录制的参数设置。通过Promise获取返回值。
B
bird_j 已提交
1862

W
wusongqing 已提交
1863
**参数:**
1864

W
wusongqing 已提交
1865
| 参数名 | 类型                                        | 必填 | 说明                     |
1866
| ------ | ------------------------------------------- | ---- | ------------------------ |
W
wusongqing 已提交
1867
| config | [VideoRecorderConfig](#videorecorderconfig) | 是   | 配置视频录制的相关参数。 |
1868

W
wusongqing 已提交
1869
**返回值:**
1870

W
wusongqing 已提交
1871
| 类型           | 说明                                     |
1872
| -------------- | ---------------------------------------- |
W
wusongqing 已提交
1873
| Promise\<void> | 异步视频录制prepare方法的Promise返回值。 |
1874

W
wusongqing 已提交
1875
**示例:**
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894

```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,
W
wusongqing 已提交
1895
    url : 'file:///data/accounts/account_0/appdata/appdata/recorder/test.mp4',   // 文件需先由调用者创建,并给予适当的权限
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
    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 已提交
1924
### getInputSurface
1925 1926 1927

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

W
wusongqing 已提交
1928
异步方式获得录制需要的surface。此surface提供给调用者,调用者从此surface中获取surfaceBuffer,填入相应的数据。
1929

W
wusongqing 已提交
1930
应当注意,填入的视频数据需要携带时间戳(单位ns),buffersize。时间戳的起始时间请以系统启动时间为基准。
1931

W
wusongqing 已提交
1932
只能在[prepare()](#videorecorder_prepare1)接口调用后调用。
1933

W
wusongqing 已提交
1934
**参数:**
B
bird_j 已提交
1935

W
wusongqing 已提交
1936
| 参数名   | 类型                   | 必填 | 说明                        |
1937
| -------- | ---------------------- | ---- | --------------------------- |
W
wusongqing 已提交
1938
| callback | AsyncCallback\<string> | 是   | 异步获得surface的回调方法。 |
1939

W
wusongqing 已提交
1940
**示例:**
1941 1942 1943

```js
// asyncallback
W
wusongqing 已提交
1944
let surfaceID = null;   											// 传递给外界的surfaceID
B
bird_j 已提交
1945 1946 1947
videoRecorder.getInputSurface((err, surfaceId) => {
    if (typeof (err) == 'undefined') {
        console.info('getInputSurface success');
B
bird_j 已提交
1948
        surfaceID = surfaceId;
B
bird_j 已提交
1949 1950 1951
    } else {
        console.info('getInputSurface failed and error is ' + err.message);
    }
1952 1953 1954
});
```

W
wusongqing 已提交
1955
### getInputSurface
1956 1957 1958

getInputSurface(): Promise\<string>;

W
wusongqing 已提交
1959
 异步方式获得录制需要的surface。此surface提供给调用者,调用者从此surface中获取surfaceBuffer,填入相应的数据。
1960

W
wusongqing 已提交
1961
应当注意,填入的视频数据需要携带时间戳(单位ns),buffersize。时间戳的起始时间请以系统启动时间为基准。
1962

W
wusongqing 已提交
1963
只能在[prepare()](#videorecorder_prepare1)接口调用后调用。
B
bird_j 已提交
1964

W
wusongqing 已提交
1965
**返回值:**
1966

W
wusongqing 已提交
1967
| 类型             | 说明                             |
1968
| ---------------- | -------------------------------- |
W
wusongqing 已提交
1969
| Promise\<string> | 异步获得surface的Promise返回值。 |
1970

W
wusongqing 已提交
1971
**示例:**
1972 1973 1974

```js
// promise
W
wusongqing 已提交
1975
let surfaceID = null;   											// 传递给外界的surfaceID
B
bird_j 已提交
1976
await videoRecorder.getInputSurface().then((surfaceId) => {
1977
    console.info('getInputSurface success');
B
bird_j 已提交
1978
    surfaceID = surfaceId;
1979 1980 1981 1982 1983 1984 1985
}, (err) => {
    console.info('getInputSurface failed and error is ' + err.message);
}).catch((err) => {
    console.info('getInputSurface failed and catch error is ' + err.message);
});
```

W
wusongqing 已提交
1986
### start<a name=videorecorder_start1></a>
1987 1988 1989

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

W
wusongqing 已提交
1990
异步方式开始视频录制。通过注册回调函数获取返回值。
1991

W
wusongqing 已提交
1992
[prepare()](#videorecorder_prepare1)[getInputSurface()](#getinputsurface)后调用,需要依赖数据源先给surface传递数据。
B
bird_j 已提交
1993

W
wusongqing 已提交
1994
**参数:**
1995

W
wusongqing 已提交
1996
| 参数名   | 类型                 | 必填 | 说明                         |
1997
| -------- | -------------------- | ---- | ---------------------------- |
W
wusongqing 已提交
1998
| callback | AsyncCallback\<void> | 是   | 异步开始视频录制的回调方法。 |
1999

W
wusongqing 已提交
2000
**示例:**
2001 2002 2003

```js
// asyncallback
B
bird_j 已提交
2004 2005 2006 2007 2008 2009
videoRecorder.start((err) => {
    if (typeof (err) == 'undefined') {
        console.info('start videorecorder success');
    } else {
        console.info('start videorecorder failed and error is ' + err.message);
    }
2010 2011 2012
});
```

W
wusongqing 已提交
2013
### start<a name=videorecorder_start2></a>
2014 2015 2016

start(): Promise\<void>;

W
wusongqing 已提交
2017
异步方式开始视频录制。通过Promise获取返回值。
2018

W
wusongqing 已提交
2019
[prepare()](#videorecorder_prepare1)[getInputSurface()](#getinputsurface)后调用,需要依赖数据源先给surface传递数据。
2020

W
wusongqing 已提交
2021
**返回值:**
B
bird_j 已提交
2022

W
wusongqing 已提交
2023
| 类型           | 说明                                  |
2024
| -------------- | ------------------------------------- |
W
wusongqing 已提交
2025
| Promise\<void> | 异步开始视频录制方法的Promise返回值。 |
2026

W
wusongqing 已提交
2027
**示例:**
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039

```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 已提交
2040
### pause<a name=videorecorder_pause1></a>
2041 2042 2043

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

W
wusongqing 已提交
2044
异步方式暂停视频录制。通过注册回调函数获取返回值。
2045

W
wusongqing 已提交
2046
[start()](#videorecorder_start1)后调用。可以通过调用[resume()](#videorecorder_resume1)接口来恢复录制。
B
bird_j 已提交
2047

W
wusongqing 已提交
2048
**参数:**
2049

W
wusongqing 已提交
2050
| 参数名   | 类型                 | 必填 | 说明                         |
2051
| -------- | -------------------- | ---- | ---------------------------- |
W
wusongqing 已提交
2052
| callback | AsyncCallback\<void> | 是   | 异步暂停视频录制的回调方法。 |
2053

W
wusongqing 已提交
2054
**示例:**
2055 2056 2057

```js
// asyncallback
B
bird_j 已提交
2058 2059 2060 2061 2062 2063
videoRecorder.pause((err) => {
    if (typeof (err) == 'undefined') {
        console.info('pause videorecorder success');
    } else {
        console.info('pause videorecorder failed and error is ' + err.message);
    }
2064 2065 2066
});
```

W
wusongqing 已提交
2067
### pause<a name=videorecorder_pause2></a>
2068 2069 2070

pause(): Promise\<void>;

W
wusongqing 已提交
2071
异步方式暂停视频录制。通过Promise获取返回值。
2072

W
wusongqing 已提交
2073
[start()](#videorecorder_start1)后调用。可以通过调用[resume()](#videorecorder_resume1)接口来恢复录制。
2074

W
wusongqing 已提交
2075
**返回值:**
B
bird_j 已提交
2076

W
wusongqing 已提交
2077
| 类型           | 说明                                  |
2078
| -------------- | ------------------------------------- |
W
wusongqing 已提交
2079
| Promise\<void> | 异步暂停视频录制方法的Promise返回值。 |
2080

W
wusongqing 已提交
2081
**示例:**
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093

```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 已提交
2094
### resume<a name=videorecorder_resume1></a>
2095 2096 2097

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

W
wusongqing 已提交
2098
异步方式恢复视频录制。通过注册回调函数获取返回值。
B
bird_j 已提交
2099

W
wusongqing 已提交
2100
**参数:**
2101

W
wusongqing 已提交
2102
| 参数名   | 类型                 | 必填 | 说明                         |
2103
| -------- | -------------------- | ---- | ---------------------------- |
W
wusongqing 已提交
2104
| callback | AsyncCallback\<void> | 是   | 异步恢复视频录制的回调方法。 |
2105

W
wusongqing 已提交
2106
**示例:**
2107 2108 2109

```js
// asyncallback
B
bird_j 已提交
2110 2111 2112 2113 2114 2115
videoRecorder.resume((err) => {
    if (typeof (err) == 'undefined') {
        console.info('resume videorecorder success');
    } else {
        console.info('resume videorecorder failed and error is ' + err.message);
    }
2116 2117 2118
});
```

W
wusongqing 已提交
2119
### resume<a name=videorecorder_resume2></a>
2120 2121 2122

resume(): Promise\<void>;

W
wusongqing 已提交
2123
异步方式恢复视频录制。通过Promise获取返回值。
2124

W
wusongqing 已提交
2125
**返回值:**
B
bird_j 已提交
2126

W
wusongqing 已提交
2127
| 类型           | 说明                                  |
2128
| -------------- | ------------------------------------- |
W
wusongqing 已提交
2129
| Promise\<void> | 异步恢复视频录制方法的Promise返回值。 |
2130

W
wusongqing 已提交
2131
**示例:**
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143

```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 已提交
2144
### stop<a name=videorecorder_stop1></a>
2145 2146 2147

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

W
wusongqing 已提交
2148
异步方式停止视频录制。通过注册回调函数获取返回值。
2149

W
wusongqing 已提交
2150
需要重新调用[prepare()](#videorecorder_prepare1)[getInputSurface()](#getinputsurface)接口才能重新录制。
B
bird_j 已提交
2151

W
wusongqing 已提交
2152
**参数:**
2153

W
wusongqing 已提交
2154
| 参数名   | 类型                 | 必填 | 说明                         |
2155
| -------- | -------------------- | ---- | ---------------------------- |
W
wusongqing 已提交
2156
| callback | AsyncCallback\<void> | 是   | 异步停止视频录制的回调方法。 |
2157

W
wusongqing 已提交
2158
**示例:**
2159 2160 2161

```js
// asyncallback
B
bird_j 已提交
2162 2163 2164 2165 2166 2167
videoRecorder.stop((err) => {
    if (typeof (err) == 'undefined') {
        console.info('stop videorecorder success');
    } else {
        console.info('stop videorecorder failed and error is ' + err.message);
    }
2168 2169 2170
});
```

W
wusongqing 已提交
2171
### stop<a name=videorecorder_stop2></a>
2172 2173 2174

stop(): Promise\<void>;

W
wusongqing 已提交
2175
异步方式停止视频录制。通过Promise获取返回值。
2176

W
wusongqing 已提交
2177
需要重新调用[prepare()](#videorecorder_prepare1)[getInputSurface()](#getinputsurface)接口才能重新录制。
2178

W
wusongqing 已提交
2179
**返回值:**
B
bird_j 已提交
2180

W
wusongqing 已提交
2181
| 类型           | 说明                                  |
2182
| -------------- | ------------------------------------- |
W
wusongqing 已提交
2183
| Promise\<void> | 异步停止视频录制方法的Promise返回值。 |
2184

W
wusongqing 已提交
2185
**示例:**
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197

```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 已提交
2198
### release<a name=videorecorder_release1></a>
2199 2200 2201

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

W
wusongqing 已提交
2202
异步方式释放视频录制资源。通过注册回调函数获取返回值。
B
bird_j 已提交
2203

W
wusongqing 已提交
2204
**参数:**
2205

W
wusongqing 已提交
2206
| 参数名   | 类型                 | 必填 | 说明                             |
2207
| -------- | -------------------- | ---- | -------------------------------- |
W
wusongqing 已提交
2208
| callback | AsyncCallback\<void> | 是   | 异步释放视频录制资源的回调方法。 |
2209

W
wusongqing 已提交
2210
**示例:**
2211 2212 2213

```js
// asyncallback
B
bird_j 已提交
2214 2215 2216 2217 2218 2219
videoRecorder.release((err) => {
    if (typeof (err) == 'undefined') {
        console.info('release videorecorder success');
    } else {
        console.info('release videorecorder failed and error is ' + err.message);
    }
2220 2221 2222
});
```

W
wusongqing 已提交
2223
### release<a name=videorecorder_release2></a>
2224 2225 2226

release(): Promise\<void>;

W
wusongqing 已提交
2227
异步方式释放视频录制资源。通过Promise获取返回值。
B
bird_j 已提交
2228

W
wusongqing 已提交
2229
**返回值:**
2230

W
wusongqing 已提交
2231
| 类型           | 说明                                      |
2232
| -------------- | ----------------------------------------- |
W
wusongqing 已提交
2233
| Promise\<void> | 异步释放视频录制资源方法的Promise返回值。 |
2234

W
wusongqing 已提交
2235
**示例:**
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247

```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 已提交
2248
### reset<a name=videorecorder_reset1></a>
2249 2250 2251

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

W
wusongqing 已提交
2252
异步方式重置视频录制。通过注册回调函数获取返回值。
2253

W
wusongqing 已提交
2254
需要重新调用[prepare()](#videorecorder_prepare1)[getInputSurface()](#getinputsurface)接口才能重新录制。
2255

W
wusongqing 已提交
2256
**参数:**
B
bird_j 已提交
2257

W
wusongqing 已提交
2258
| 参数名   | 类型                 | 必填 | 说明                         |
2259
| -------- | -------------------- | ---- | ---------------------------- |
W
wusongqing 已提交
2260
| callback | AsyncCallback\<void> | 是   | 异步重置视频录制的回调方法。 |
2261

W
wusongqing 已提交
2262
**示例:**
2263 2264 2265

```js
// asyncallback
B
bird_j 已提交
2266 2267 2268 2269 2270 2271
videoRecorder.reset((err) => {
    if (typeof (err) == 'undefined') {
        console.info('reset videorecorder success');
    } else {
        console.info('reset videorecorder failed and error is ' + err.message);
    }
2272 2273 2274
});
```

W
wusongqing 已提交
2275
### reset<a name=videorecorder_reset2></a>
2276 2277 2278

reset(): Promise\<void>;

W
wusongqing 已提交
2279
异步方式重置视频录制。通过Promise获取返回值。
2280

W
wusongqing 已提交
2281
需要重新调用[prepare()](#videorecorder_prepare1)[getInputSurface()](#getinputsurface)接口才能重新录制。
B
bird_j 已提交
2282

W
wusongqing 已提交
2283
**返回值:**
2284

W
wusongqing 已提交
2285
| 类型           | 说明                                  |
2286
| -------------- | ------------------------------------- |
W
wusongqing 已提交
2287
| Promise\<void> | 异步重置视频录制方法的Promise返回值。 |
2288

W
wusongqing 已提交
2289
**示例:**
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301

```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 已提交
2302
### on('error')
2303 2304 2305

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

W
wusongqing 已提交
2306
开始订阅视频录制错误事件。
2307

W
wusongqing 已提交
2308
**参数:**
B
bird_j 已提交
2309

W
wusongqing 已提交
2310
| 参数名   | 类型          | 必填 | 说明                                                         |
2311
| -------- | ------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
2312 2313
| type     | string        | 是   | 录制错误事件回调类型'error'。<br/>-&nbsp;'error':音频录制过程中发生错误,触发该事件。 |
| callback | ErrorCallback | 是   | 录制错误事件回调方法。                                       |
2314

W
wusongqing 已提交
2315
**示例:**
2316 2317

```js
W
wusongqing 已提交
2318 2319 2320 2321
videoRecorder.on('error', (error) => {      							// 设置'error'事件回调
	console.info(`audio error called, errName is ${error.name}`);       // 打印错误类型名称
    console.info(`audio error called, errCode is ${error.code}`);       // 打印错误码
    console.info(`audio error called, errMessage is ${error.message}`); // 打印错误类型详细描述
2322
});
W
wusongqing 已提交
2323
// 当获取videoRecordState接口出错时通过此订阅事件上报
2324 2325 2326 2327
```

## VideoRecordState<sup>8+</sup>

W
wusongqing 已提交
2328
视频录制的状态机。可通过state属性获取当前状态。
2329

W
wusongqing 已提交
2330 2331 2332 2333 2334 2335 2336 2337
| 名称     | 类型   | 描述                   |
| -------- | ------ | ---------------------- |
| idle     | string | 视频录制空闲。         |
| prepared | string | 视频录制参数设置完成。 |
| playing  | string | 视频正在录制。         |
| paused   | string | 视频暂停录制。         |
| stopped  | string | 视频录制停止。         |
| error    | string | 错误状态。             |
2338 2339 2340

## VideoRecorderConfig<sup>8+</sup>

W
wusongqing 已提交
2341
表示视频录制的参数设置。
2342

W
wusongqing 已提交
2343
| 名称            | 参数类型                                                   | 必填 | 说明                                                         |
2344
| --------------- | ---------------------------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
2345 2346 2347 2348 2349 2350
| audioSourceType | [AudioSourceType](#audiosourcetype<sup>8+</sup>)           | 是   | 视频录制的音频源类型。                                       |
| videoSourceType | [VideoSourceType](#videosourcetype<sup>8+</sup>)           | 是   | 视频录制的视频源类型。                                       |
| profile         | [VideoRecorderProfile](#videorecorderprofile<sup>8+</sup>) | 是   | 视频录制的profile。                                          |
| orientationHint | number                                                     | 否   | 录制视频的旋转角度。                                         |
| location        | [Location](#location8)                                     | 否   | 录制视频的地理位置。                                         |
| uri             | string                                                     | 是   | 视频输出URI。支持:<br/>1.&nbsp;文件的绝对路径:file:///data/data/ohos.xxx.xxx/cache/test.mp4![zh-cn_image_0000001164217678](figures/zh-cn_image_0000001164217678.png)<br/>2.&nbsp;文件的fd路径:file://1&nbsp;(fd&nbsp;number)<br/> 文件需要由调用者创建,并赋予适当的权限。 |
2351 2352 2353

## AudioSourceType<sup>8+</sup>

W
wusongqing 已提交
2354
表示视频录制中音频源类型的枚举。
2355

W
wusongqing 已提交
2356 2357 2358 2359
| 名称                       | 值   | 说明                   |
| -------------------------- | ---- | ---------------------- |
| AUDIO_SOURCE_TYPE_DEFAULT0 | 0    | 默认的音频输入源类型。 |
| AUDIO_SOURCE_TYPE_MIC      | 1    | 表示MIC的音频输入源。  |
2360 2361 2362

## VideoSourceType<sup>8+</sup>

W
wusongqing 已提交
2363
表示视频录制中视频源类型的枚举。
2364

W
wusongqing 已提交
2365 2366 2367 2368
| 名称                          | 值   | 说明                            |
| ----------------------------- | ---- | ------------------------------- |
| VIDEO_SOURCE_TYPE_SURFACE_YUV | 0    | 输入surface中携带的是raw data。 |
| VIDEO_SOURCE_TYPE_SURFACE_ES  | 1    | 输入surface中携带的是ES data。  |
2369 2370 2371

## VideoRecorderProfile<sup>8+</sup>

W
wusongqing 已提交
2372
视频录制的配置文件。
2373

W
wusongqing 已提交
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
| 名称             | 参数类型                                     | 必填 | 说明             |
| ---------------- | -------------------------------------------- | ---- | ---------------- |
| audioBitrate     | number                                       | 是   | 音频编码比特率。 |
| audioChannels    | number                                       | 是   | 音频采集声道数。 |
| audioCodec       | [CodecMimeType](#CodecMimeType8)             | 是   | 音频编码格式。   |
| audioSampleRate  | number                                       | 是   | 音频采样率。     |
| fileFormat       | [ContainerFormatType](#containerformattype8) | 是   | 文件的容器格式。 |
| videoCodec       | [CodecMimeType](#CodecMimeType8)             | 是   | 视频编码格式。   |
| videoFrameWidth  | number                                       | 是   | 录制视频帧的宽。 |
| videoFrameHeight | number                                       | 是   | 录制视频帧的高。 |
2384 2385 2386

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

W
wusongqing 已提交
2387
表示容器格式类型的枚举,缩写为CFT。
2388

W
wusongqing 已提交
2389 2390 2391 2392
| 名称        | 值    | 说明                  |
| ----------- | ----- | --------------------- |
| CFT_MPEG_4  | "mp4" | 视频的容器格式,MP4。 |
| CFT_MPEG_4A | "m4a" | 音频的容器格式,M4A。 |
2393 2394 2395

## Location<sup>8+</sup>

W
wusongqing 已提交
2396
视频录制的地理位置。
2397

W
wusongqing 已提交
2398 2399 2400 2401
| 名称      | 参数类型 | 必填 | 说明             |
| --------- | -------- | ---- | ---------------- |
| latitude  | number   | 是   | 地理位置的纬度。 |
| longitude | number   | 是   | 地理位置的经度。 |