js-apis-avsession.md 170.8 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.multimedia.avsession (媒体会话管理)
L
leiiyb 已提交
2 3 4 5

媒体会话管理提供媒体播控相关功能的接口,目的是让应用接入播控中心。

该模块提供以下媒体会话相关的常用功能:
Z
zengyawen 已提交
6 7
- [AVSession](#avsession) : 会话,可用于设置元数据、播放状态信息等操作。
- [AVSessionController](#avsessioncontroller): 会话控制器,可用于查看会话ID,完成对会话发送命令及事件,获取会话元数据、播放状态信息等操作。
L
leiiyb 已提交
8 9 10 11 12

> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

13 14
> 本模块的所有接口均为系统接口。

L
leiiyb 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
## 导入模块

```js
import avSession from '@ohos.multimedia.avsession';
```

## avSession.createAVSession

createAVSession(context: Context, tag: string, type: AVSessionType): Promise\<AVSession>

创建会话对象,一个Ability只能存在一个会话,重复创建会失败,结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
29 30
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
31 32 33 34
**参数:**

| 参数名 | 类型                            | 必填 | 说明                           |
| ------ | ------------------------------- | ---- | ------------------------------ |
Z
zengyawen 已提交
35
| context| [Context](js-apis-inner-app-context.md) | 是| 应用上下文,提供获取应用程序环境信息的能力。 |
L
leiiyb 已提交
36 37 38 39 40 41 42 43 44 45 46 47
| tag    | string                          | 是   | 会话的自定义名称。             |
| type   | [AVSessionType](#avsessiontype) | 是   | 会话类型,当前支持音频和视频。 |


**返回值:**

| 类型                              | 说明                                                         |
| --------------------------------- | ------------------------------------------------------------ |
| Promise<[AVSession](#avsession)\> | Promise对象。回调返回会话实例对象,可用于获取会话ID,以及设置元数据、播放状态,发送按键事件等操作。|


**错误码:**
48
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
49 50 51

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
52
| 6600101  | Session service exception. |
L
leiiyb 已提交
53 54 55 56 57 58 59 60 61 62

**示例:**

```js
import featureAbility from '@ohos.ability.featureAbility';

let session;
let tag = "createNewSession";
let context = featureAbility.getContext();

63
await avSession.createAVSession(context, tag, "audio").then((data) => {
64
    session = data;
L
leiiyb 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78
    console.info(`CreateAVSession : SUCCESS : sessionId = ${session.sessionId}`);
}).catch((err) => {
    console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

## avSession.createAVSession

createAVSession(context: Context, tag: string, type: AVSessionType, callback: AsyncCallback\<AVSession>): void

创建会话对象,一个Ability只能存在一个会话,重复创建会失败,结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
79 80
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
81 82 83 84
**参数:**

| 参数名   | 类型                                    | 必填 | 说明                                                         |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
Z
zengyawen 已提交
85
| context| [Context](js-apis-inner-app-context.md) | 是| 应用上下文,提供获取应用程序环境信息的能力。     |
L
leiiyb 已提交
86 87 88 89 90
| tag      | string                                  | 是   | 会话的自定义名称。                                           |
| type     | [AVSessionType](#avsessiontype)         | 是   | 会话类型,当前支持音频和视频。                               |
| callback | AsyncCallback<[AVSession](#avsession)\> | 是   | 回调函数。回调返回会话实例对象,可用于获取会话ID,以及设置元数据、播放状态,发送按键事件等操作。 |

**错误码:**
91
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
92 93 94

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
95
| 6600101  | Session service exception. |
L
leiiyb 已提交
96 97 98 99 100 101 102 103 104 105

**示例:**

```js
import featureAbility from '@ohos.ability.featureAbility';

let session;
let tag = "createNewSession";
let context = featureAbility.getContext();

106
avSession.createAVSession(context, tag, "audio", function (err, data) {
L
leiiyb 已提交
107 108 109
    if (err) {
        console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
110
        session = data;
L
leiiyb 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        console.info(`CreateAVSession : SUCCESS : sessionId = ${session.sessionId}`);
    }
});
```

## avSession.getAllSessionDescriptors

getAllSessionDescriptors(): Promise\<Array\<Readonly\<AVSessionDescriptor>>>

获取所有会话的相关描述。结果通过Promise异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**返回值:**

| 类型                                                         | 说明                                          |
| ------------------------------------------------------------ | --------------------------------------------- |
| Promise\<Array\<Readonly\<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | Promise对象。返回所有会话描述的只读对象。 |

**错误码:**
135
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
136 137 138

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
139
| 6600101  | Session service exception. |
L
leiiyb 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

**示例:**

```js
avSession.getAllSessionDescriptors().then((descriptors) => {
    console.info(`getAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`);
    if(descriptors.length > 0 ){
        console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`);
        console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`);
        console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`);
    }
}).catch((err) => {
    console.info(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

## avSession.getAllSessionDescriptors

getAllSessionDescriptors(callback: AsyncCallback\<Array\<Readonly\<AVSessionDescriptor>>>): void

获取所有会话的相关描述。结果通过callback异步回调方式返回。

162
**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES
L
leiiyb 已提交
163 164 165 166 167 168 169 170 171 172 173 174

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                       |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ |
| callback | AsyncCallback<Array<Readonly<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | 是   | 回调函数。返回所有会话描述的只读对象。 |

**错误码:**
175
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
176 177 178

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
179
| 6600101  |Session service exception. |
L
leiiyb 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

**示例:**

```js
avSession.getAllSessionDescriptors(function (err, descriptors) {
    if (err) {
        console.info(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`);
        if(descriptors.length > 0 ){
            console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`);
            console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`);
            console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`);
        }
    }
});
```

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
## avSession.getHistoricalSessionDescriptors<sup>10+</sup>

getHistoricalSessionDescriptors(maxSize?: number): Promise\<Array\<Readonly\<AVSessionDescriptor>>>

获取所有会话的相关描述。结果通过Promise异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名   | 类型    | 必填 | 说明                                                             |
| -------- | ------ | ---- | -----------------------------------------------------------------|
| maxSize  | number | 否   | 指定获取描述符数量的最大值,可选范围是0-10,不填则取默认值,默认值为3。|

**返回值:**

| 类型                                                                        | 说明                                   |
| --------------------------------------------------------------------------- | -------------------------------------- |
| Promise\<Array\<Readonly\<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | Promise对象。返回所有会话描述的只读对象。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |

**示例:**

```js
avSession.getHistoricalSessionDescriptors().then((descriptors) => {
    console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`);
    if(descriptors.length > 0 ){
        console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`);
        console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`);
        console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`);
        console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`);
        console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`);
    }
}).catch((err) => {
    console.info(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

## avSession.getHistoricalSessionDescriptors<sup>10+</sup>

getHistoricalSessionDescriptors(maxSize: number, callback: AsyncCallback\<Array\<Readonly\<AVSessionDescriptor>>>): void

获取所有会话的相关描述。结果通过callback异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名   | 类型                                                                            | 必填 | 说明                                                             |
| -------- | ------------------------------------------------------------------------------ | ---- | -----------------------------------------------------------------|
| maxSize  | number                                                                         | 是   | 指定获取描述符数量的最大值,可选范围是0-10,不填则取默认值,默认值为3。|
| callback | AsyncCallback<Array<Readonly<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | 是   | 回调函数。返回所有会话描述的只读对象。                              |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  |Session service exception. |

**示例:**

```js
avSession.getHistoricalSessionDescriptors(1, function (err, descriptors) {
    if (err) {
        console.info(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`);
        if(descriptors.length > 0 ){
            console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`);
            console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`);
            console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`);
            console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`);
            console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`);
        }
    }
});
```

L
leiiyb 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
## avSession.createController

createController(sessionId: string): Promise\<AVSessionController>

根据会话ID创建会话控制器,可以创建多个会话控制器。结果通过Promise异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名    | 类型   | 必填 | 说明     |
| --------- | ------ | ---- | -------- |
| sessionId | string | 是   | 会话ID。 |

**返回值:**

| 类型                                                  | 说明                                                         |
| ----------------------------------------------------- | ------------------------------------------------------------ |
| Promise<[AVSessionController](#avsessioncontroller)\> | Promise对象。返回会话控制器实例,可查看会话ID,<br>并完成对会话发送命令及事件,获取元数据、播放状态信息等操作。|

**错误码:**
316
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
317 318 319

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
320 321
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
322 323 324 325

**示例:**

```js
D
dingdongdong 已提交
326 327 328 329 330 331
import featureAbility from '@ohos.ability.featureAbility';

let session;
let tag = "createNewSession";
let context = featureAbility.getContext();

332
await avSession.createAVSession(context, tag, "audio").then((data) => {
D
dingdongdong 已提交
333 334 335 336 337 338
    session = data;
    console.info(`CreateAVSession : SUCCESS : sessionId = ${session.sessionId}`);
}).catch((err) => {
    console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
});

L
leiiyb 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
let controller;
await avSession.createController(session.sessionId).then((avcontroller) => {
    controller = avcontroller;
    console.info(`CreateController : SUCCESS : ${controller.sessionId}`);
}).catch((err) => {
    console.info(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

## avSession.createController

createController(sessionId: string, callback: AsyncCallback\<AVSessionController>): void

根据会话ID创建会话控制器,可以创建多个会话控制器。结果通过callback异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名    | 类型                                                        | 必填 | 说明                                                         |
| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| sessionId | string                                                      | 是   | 会话ID。                                                     |
| callback  | AsyncCallback<[AVSessionController](#avsessioncontroller)\> | 是   | 回调函数。返回会话控制器实例,可查看会话ID,<br>并完成对会话发送命令及事件,获取元数据、播放状态信息等操作。 |

**错误码:**
368
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
369 370 371

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
372 373
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
374 375 376 377

**示例:**

```js
D
dingdongdong 已提交
378 379 380 381 382 383
import featureAbility from '@ohos.ability.featureAbility';

let session;
let tag = "createNewSession";
let context = featureAbility.getContext();

384
await avSession.createAVSession(context, tag, "audio").then((data) => {
D
dingdongdong 已提交
385 386 387 388 389 390
    session = data;
    console.info(`CreateAVSession : SUCCESS : sessionId = ${session.sessionId}`);
}).catch((err) => {
    console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
});

L
leiiyb 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
let controller;
avSession.createController(session.sessionId, function (err, avcontroller) {
    if (err) {
        console.info(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        controller = avcontroller;
        console.info(`CreateController : SUCCESS : ${controller.sessionId}`);
    }
});
```

## avSession.castAudio

castAudio(session: SessionToken | 'all', audioDevices: Array<audio.AudioDeviceDescriptor>): Promise\<void>

投播会话到指定设备列表。结果通过Promise异步回调方式返回。

调用此接口之前,需要导入`ohos.multimedia.audio`模块获取AudioDeviceDescriptor的相关描述。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名       | 类型                                                                                                                                                                 | 必填 | 说明                                                         |
| ------------ |--------------------------------------------------------------------------------------------------------------------------------------------------------------------| ---- | ------------------------------------------------------------ |
| session      | [SessionToken](#sessiontoken) &#124; 'all'                                                                                                                         | 是   | 会话令牌。SessionToken表示单个token;字符串`'all'`指所有token。 |
| audioDevices | Array\<[audio.AudioDeviceDescriptor](js-apis-audio.md#audiodevicedescriptor)\> | 是   | 媒体设备列表。                          |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当投播成功,无返回结果,否则返回错误对象。 |

**错误码:**
430
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
431 432 433

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
434 435 436
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600104  | The remote session  connection failed. |
L
leiiyb 已提交
437 438 439 440 441 442 443

**示例:**

```js
import audio from '@ohos.multimedia.audio';

let audioManager = audio.getAudioManager();
444
let audioRoutingManager = audioManager.getRoutingManager();
L
leiiyb 已提交
445
let audioDevices;
446
await audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
L
leiiyb 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    audioDevices = data;
    console.info('Promise returned to indicate that the device list is obtained.');
}).catch((err) => {
    console.info(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`);
});

avSession.castAudio('all', audioDevices).then(() => {
    console.info('CreateController : SUCCESS');
}).catch((err) => {
    console.info(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

## avSession.castAudio

castAudio(session: SessionToken | 'all', audioDevices: Array<audio.AudioDeviceDescriptor>, callback: AsyncCallback\<void>): void

投播会话到指定设备列表。结果通过callback异步回调方式返回。

需要导入`ohos.multimedia.audio`模块获取AudioDeviceDescriptor的相关描述。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名       | 类型                                         | 必填 | 说明                                                         |
| ------------ |--------------------------------------------| ---- | ------------------------------------------------------------ |
| session      | [SessionToken](#sessiontoken) &#124; 'all' | 是   | 会话令牌。SessionToken表示单个token;字符串`'all'`指所有token。 |
| audioDevices | Array\<[audio.AudioDeviceDescriptor](js-apis-audio.md#audiodevicedescriptor)\>   | 是   | 媒体设备列表。                       |
| callback     | AsyncCallback<void\>                       | 是   | 回调函数。当投播成功,err为undefined,否则返回错误对象。                        |

**错误码:**
483
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
484 485 486

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
487 488 489
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600104  | The remote session  connection failed. |
L
leiiyb 已提交
490 491 492 493 494 495 496

**示例:**

```js
import audio from '@ohos.multimedia.audio';

let audioManager = audio.getAudioManager();
497
let audioRoutingManager = audioManager.getRoutingManager();
L
leiiyb 已提交
498
let audioDevices;
499
await audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
L
leiiyb 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    audioDevices = data;
    console.info('Promise returned to indicate that the device list is obtained.');
}).catch((err) => {
    console.info(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`);
});

avSession.castAudio('all', audioDevices, function (err) {
    if (err) {
        console.info(`CastAudio BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('CastAudio : SUCCESS ');
    }
});
```

## avSession.on('sessionCreate' | 'sessionDestroy' | 'topSessionChange')

on(type: 'sessionCreate' | 'sessionDestroy' | 'topSessionChange', callback: (session: AVSessionDescriptor) => void): void

会话的创建、销毁以及最新会话变更的监听事件。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 事件回调类型,支持的事件包括:<br/>- `'sessionCreate'`:会话创建事件,检测到会话创建时触发。<br/>- `'sessionDestroy'`:会话销毁事件,检测到会话销毁时触发。 <br/>- `'topSessionChange'`:最新会话的变化事件,检测到最新的会话改变时触发。|
| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | 是   | 回调函数。参数为会话相关描述。                               |

**错误码:**
535
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
536 537 538

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
539
| 6600101  | Session service exception. |
L
leiiyb 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582

**示例:**

```js
avSession.on('sessionCreate', (descriptor) => {
    console.info(`on sessionCreate : isActive : ${descriptor.isActive}`);
    console.info(`on sessionCreate : type : ${descriptor.type}`);
    console.info(`on sessionCreate : sessionTag : ${descriptor.sessionTag}`);
});

avSession.on('sessionDestroy', (descriptor) => {
    console.info(`on sessionDestroy : isActive : ${descriptor.isActive}`);
    console.info(`on sessionDestroy : type : ${descriptor.type}`);
    console.info(`on sessionDestroy : sessionTag : ${descriptor.sessionTag}`);
});

avSession.on('topSessionChange', (descriptor) => {
    console.info(`on topSessionChange : isActive : ${descriptor.isActive}`);
    console.info(`on topSessionChange : type : ${descriptor.type}`);
    console.info(`on topSessionChange : sessionTag : ${descriptor.sessionTag}`);
});
```

## avSession.off('sessionCreate' | 'sessionDestroy' | 'topSessionChange')

off(type: 'sessionCreate' | 'sessionDestroy' | 'topSessionChange', callback?: (session: AVSessionDescriptor) => void): void

取消会话相关事件监听,取消后,不再进行相关事件的监听。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 事件回调类型,支持的事件为:<br/>- `'sessionCreate'`:会话创建事件,检测到会话创建时触发。<br/>- `'sessionDestroy'`:会话销毁事件,检测到会话销毁时触发。 <br/>- `'topSessionChange'`:最新会话的变化事件,检测到最新的会话改变时触发。|
| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | 否   | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为会话相关描述,为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                               |

**错误码:**
583
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
584 585 586

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
587
| 6600101  | Session service exception. |
L
leiiyb 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604

**示例:**

```js
avSession.off('sessionCreate');
avSession.off('sessionDestroy');
avSession.off('topSessionChange');
```

## avSession.on('sessionServiceDie')

on(type: 'sessionServiceDie', callback: () => void): void

监听会话的服务死亡事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
605 606
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
607 608 609 610 611 612 613 614
**参数:**

| 参数名   | 类型                 | 必填 | 说明                                                         |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
| type     | string               | 是   | 事件回调类型,支持事件`'sessionServiceDie'`:会话服务死亡事件,检测到会话的服务死亡时触发。 |
| callback | callback: () => void | 是   | 回调函数。当监听事件注册成功,err为undefined,否则返回错误对象。                                |

**错误码:**
615
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
616 617 618

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
619
| 6600101  | Session service exception. |
L
leiiyb 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

**示例:**

```js
avSession.on('sessionServiceDie', () => {
    console.info('on sessionServiceDie  : session is  Died ');
});
```

## avSession.off('sessionServiceDie')

off(type: 'sessionServiceDie', callback?: () => void): void

取消会话服务死亡监听,取消后,不再进行服务死亡监听。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
637 638
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
639 640 641 642 643 644 645 646
**参数:**

| 参数名    | 类型                    | 必填  |      说明                                               |
| ------   | ---------------------- | ---- | ------------------------------------------------------- |
| type     | string                 | 是    | 事件回调类型,支持事件`'sessionServiceDie'`:会话服务死亡事件。|
| callback | callback: () => void   | 否    | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的服务死亡监听。            |

**错误码:**
647
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
648 649 650

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
651
| 6600101  | Session service exception. |
L
leiiyb 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

**示例:**

```js
avSession.off('sessionServiceDie');
```

## avSession.sendSystemAVKeyEvent

sendSystemAVKeyEvent(event: KeyEvent): Promise\<void>

发送按键事件给置顶会话。结果通过Promise异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名 | 类型                            | 必填 | 说明       |
| ------ | ------------------------------- | ---- | ---------- |
| event  | [KeyEvent](js-apis-keyevent.md) | 是   | 按键事件。 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当事件发送成功,无返回结果,否则返回错误对象。 |

**错误码:**
684
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
685 686 687

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
688 689
| 6600101  | Session service exception. |
| 6600105  | Invalid session command. |
L
leiiyb 已提交
690 691 692 693 694 695

**示例:**

```js

let keyItem = {code:0x49, pressedTime:2, deviceId:0};
D
dingdongdong 已提交
696
let event = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; 
L
leiiyb 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

avSession.sendSystemAVKeyEvent(event).then(() => {
    console.info('SendSystemAVKeyEvent Successfully');
}).catch((err) => {
    console.info(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`);
});

```

## avSession.sendSystemAVKeyEvent

sendSystemAVKeyEvent(event: KeyEvent, callback: AsyncCallback\<void>): void

发送按键事件给置顶会话。结果通过callback异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                  |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------- |
| event    | [KeyEvent](js-apis-keyevent.md) | 是   | 按键事件。                            |
| callback | AsyncCallback<void\>                                         | 是   | 回调函数。当事件发送成功,err为undefined,否则返回错误对象。 |

**错误码:**
726
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
727 728 729

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
730 731
| 6600101  | Session service exception. |
| 6600105  | Invalid session command. |
L
leiiyb 已提交
732 733 734 735 736

**示例:**

```js
let keyItem = {code:0x49, pressedTime:2, deviceId:0};
D
dingdongdong 已提交
737
let event = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; 
L
leiiyb 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

avSession.sendSystemAVKeyEvent(event, function (err) {
    if (err) {
        console.info(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('SendSystemAVKeyEvent : SUCCESS ');
    }
});
```

## avSession.sendSystemControlCommand

sendSystemControlCommand(command: AVControlCommand): Promise\<void>

发送控制命令给置顶会话。结果通过Promise异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名  | 类型                                  | 必填 | 说明                                |
| ------- | ------------------------------------- | ---- | ----------------------------------- |
| command | [AVControlCommand](#avcontrolcommand) | 是   | AVSession的相关命令和命令相关参数。 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 |

**错误码:**
773
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
774 775 776

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
777 778 779
| 6600101  | Session service exception. |
| 6600105  | Invalid session command. |
| 6600107  | Too many commands or events. |
L
leiiyb 已提交
780 781 782 783

**示例:**

```js
D
dingdongdong 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
let cmd : avSession.AVControlCommandType = 'play';
// let cmd : avSession.AVControlCommandType = 'pause';
// let cmd : avSession.AVControlCommandType = 'stop';
// let cmd : avSession.AVControlCommandType = 'playNext';
// let cmd : avSession.AVControlCommandType = 'playPrevious';
// let cmd : avSession.AVControlCommandType = 'fastForward';
// let cmd : avSession.AVControlCommandType = 'rewind';
let avcommand = {command:cmd};
// let cmd : avSession.AVControlCommandType = 'seek';
// let avcommand = {command:cmd, parameter:10};
// let cmd : avSession.AVControlCommandType = 'setSpeed';
// let avcommand = {command:cmd, parameter:2.6};
// let cmd : avSession.AVControlCommandType = 'setLoopMode';
// let avcommand = {command:cmd, parameter:avSession.LoopMode.LOOP_MODE_SINGLE};
// let cmd : avSession.AVControlCommandType = 'toggleFavorite';
// let avcommand = {command:cmd, parameter:"false"};
L
leiiyb 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
avSession.sendSystemControlCommand(avcommand).then(() => {
    console.info('SendSystemControlCommand successfully');
}).catch((err) => {
    console.info(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

## avSession.sendSystemControlCommand

sendSystemControlCommand(command: AVControlCommand, callback: AsyncCallback\<void>): void

发送控制命令给置顶会话。结果通过callback异步回调方式返回。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

**参数:**

| 参数名   | 类型                                  | 必填 | 说明                                  |
| -------- | ------------------------------------- | ---- | ------------------------------------- |
| command  | [AVControlCommand](#avcontrolcommand) | 是   | AVSession的相关命令和命令相关参数。   |
| callback | AsyncCallback<void\>                  | 是   | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 |

**错误码:**
827
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
828 829 830

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
831 832 833
| 6600101  | Session service exception. |
| 6600105  | Invalid session command. |
| 6600107  | Too many commands or events. |
L
leiiyb 已提交
834 835 836 837

**示例:**

```js
D
dingdongdong 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
let cmd : avSession.AVControlCommandType = 'play';
// let cmd : avSession.AVControlCommandType = 'pause';
// let cmd : avSession.AVControlCommandType = 'stop';
// let cmd : avSession.AVControlCommandType = 'playNext';
// let cmd : avSession.AVControlCommandType = 'playPrevious';
// let cmd : avSession.AVControlCommandType = 'fastForward';
// let cmd : avSession.AVControlCommandType = 'rewind';
let avcommand = {command:cmd};
// let cmd : avSession.AVControlCommandType = 'seek';
// let avcommand = {command:cmd, parameter:10};
// let cmd : avSession.AVControlCommandType = 'setSpeed';
// let avcommand = {command:cmd, parameter:2.6};
// let cmd : avSession.AVControlCommandType = 'setLoopMode';
// let avcommand = {command:cmd, parameter:avSession.LoopMode.LOOP_MODE_SINGLE};
// let cmd : avSession.AVControlCommandType = 'toggleFavorite';
// let avcommand = {command:cmd, parameter:"false"};
L
leiiyb 已提交
854 855 856 857 858 859 860 861 862
avSession.sendSystemControlCommand(avcommand, function (err) {
    if (err) {
        console.info(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('sendSystemControlCommand successfully');
    }
});
```

Z
zengyawen 已提交
863
## AVSession
L
leiiyb 已提交
864 865 866 867 868 869 870

调用[avSession.createAVSession](#avsessioncreateavsession)后,返回会话的实例,可以获得会话ID,完成设置元数据,播放状态信息等操作。

### 属性

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
871 872
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891

| 名称      | 类型   | 可读 | 可写 | 说明                          |
| :-------- | :----- | :--- | :--- | :---------------------------- |
| sessionId | string | 是   | 否   | AVSession对象唯一的会话标识。 |


**示例:**
```js
let sessionId = session.sessionId;
```

### setAVMetadata

setAVMetadata(data: AVMetadata): Promise\<void>

设置会话元数据。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
892 893
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906
**参数:**

| 参数名 | 类型                      | 必填 | 说明         |
| ------ | ------------------------- | ---- | ------------ |
| data   | [AVMetadata](#avmetadata) | 是   | 会话元数据。 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当元数据设置成功,无返回结果,否则返回错误对象。 |

**错误码:**
907
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
908 909 910

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
911 912
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

**示例:**

```js
let metadata  = {
    assetId: "121278",
    title: "lose yourself",
    artist: "Eminem",
    author: "ST",
    album: "Slim shady",
    writer: "ST",
    composer: "ST",
    duration: 2222,
    mediaImage: "https://www.example.com/example.jpg",
    subtitle: "8 Mile",
    description: "Rap",
    lyric: "https://www.example.com/example.lrc",
    previousAssetId: "121277",
    nextAssetId: "121279",
};
session.setAVMetadata(metadata).then(() => {
    console.info('SetAVMetadata successfully');
}).catch((err) => {
    console.info(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### setAVMetadata

setAVMetadata(data: AVMetadata, callback: AsyncCallback\<void>): void

设置会话元数据。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
948 949
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
950 951 952 953 954 955 956 957
**参数:**

| 参数名   | 类型                      | 必填 | 说明                                  |
| -------- | ------------------------- | ---- | ------------------------------------- |
| data     | [AVMetadata](#avmetadata) | 是   | 会话元数据。                          |
| callback | AsyncCallback<void\>      | 是   | 回调函数。当元数据设置成功,err为undefined,否则返回错误对象。 |

**错误码:**
958
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
959 960 961

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
962 963
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000

**示例:**

```js
let metadata  = {
    assetId: "121278",
    title: "lose yourself",
    artist: "Eminem",
    author: "ST",
    album: "Slim shady",
    writer: "ST",
    composer: "ST",
    duration: 2222,
    mediaImage: "https://www.example.com/example.jpg",
    subtitle: "8 Mile",
    description: "Rap",
    lyric: "https://www.example.com/example.lrc",
    previousAssetId: "121277",
    nextAssetId: "121279",
};
session.setAVMetadata(metadata, function (err) {
    if (err) {
        console.info(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('SetAVMetadata successfully');
    }
});
```

### setAVPlaybackState

setAVPlaybackState(state: AVPlaybackState): Promise\<void>

设置会话播放状态。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1001 1002
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
**参数:**

| 参数名 | 类型                                | 必填 | 说明                                           |
| ------ | ----------------------------------- | ---- | ---------------------------------------------- |
| data   | [AVPlaybackState](#avplaybackstate) | 是   | 会话播放状态,包括状态、倍数、循环模式等信息。 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当播放状态设置成功,无返回结果,否则返回错误对象。 |

**错误码:**
1016
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1017 1018 1019

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1020 1021
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1022 1023 1024 1025

**示例:**

```js
D
dingdongdong 已提交
1026
let playbackState = {
L
leiiyb 已提交
1027 1028 1029 1030 1031 1032 1033
    state:avSession.PlaybackState.PLAYBACK_STATE_PLAY,
    speed: 1.0,
    position:{elapsedTime:10, updateTime:(new Date()).getTime()},
    bufferedTime:1000,
    loopMode:avSession.LoopMode.LOOP_MODE_SINGLE,
    isFavorite:true,
};
D
dingdongdong 已提交
1034
session.setAVPlaybackState(playbackState).then(() => {
L
leiiyb 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    console.info('SetAVPlaybackState successfully');
}).catch((err) => {
    console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### setAVPlaybackState

setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\<void>): void

设置会话播放状态。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1049 1050
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1051 1052 1053 1054 1055 1056 1057 1058
**参数:**

| 参数名   | 类型                                | 必填 | 说明                                           |
| -------- | ----------------------------------- | ---- | ---------------------------------------------- |
| data     | [AVPlaybackState](#avplaybackstate) | 是   | 会话播放状态,包括状态、倍数、循环模式等信息。 |
| callback | AsyncCallback<void\>                | 是   | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。          |

**错误码:**
1059
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1060 1061 1062

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1063 1064
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

**示例:**

```js
let PlaybackState = {
    state:avSession.PlaybackState.PLAYBACK_STATE_PLAY,
    speed: 1.0,
    position:{elapsedTime:10, updateTime:(new Date()).getTime()},
    bufferedTime:1000,
    loopMode:avSession.LoopMode.LOOP_MODE_SINGLE,
    isFavorite:true,
};
session.setAVPlaybackState(PlaybackState, function (err) {
    if (err) {
        console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('SetAVPlaybackState successfully');
    }
});
```

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 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 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
### setAVQueueItems<sup>10+</sup>

setAVQueueItems(items: Array\<AVQueueItem>): Promise<void\>

设置媒体播放列表。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名  | 类型                                 | 必填 | 说明                               |
| ------ | ------------------------------------ | ---- | ---------------------------------- |
| items  | Array<[AVQueueItem](#avqueueitem10)\> | 是   | 播放列表单项的队列,用以表示播放列表。 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当播放列表设置成功,无返回结果,否则返回错误对象。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
let queueItemDescription_1 = {
    mediaId: '001',
    title: 'music_name',
    subtitle: 'music_sub_name',
    description: 'music_description',
    icon: PIXELMAP_OBJECT,
    iconUri: 'http://www.icon.uri.com',
    extras: {'extras':'any'}
};
let queueItem_1 = {
    itemId: 1,
    description: queueItemDescription_1
};
let queueItemDescription_2 = {
    mediaId: '002',
    title: 'music_name',
    subtitle: 'music_sub_name',
    description: 'music_description',
    icon: PIXELMAP_OBJECT,
    iconUri: 'http://www.icon.uri.com',
    extras: {'extras':'any'}
};
let queueItem_2 = {
    itemId: 2,
    description: queueItemDescription_2
};
let queueItemsArray = [queueItem_1, queueItem_2];
session.setAVQueueItems(queueItemsArray).then(() => {
    console.info('SetAVQueueItems successfully');
}).catch((err) => {
    console.info(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### setAVQueueItems<sup>10+</sup>

setAVQueueItems(items: Array\<AVQueueItem>, callback: AsyncCallback<void\>): void

设置媒体播放列表。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                                  | 必填 | 说明                                                         |
| -------- | ------------------------------------ | ---- | ----------------------------------------------------------- |
| items    | Array<[AVQueueItem](#avqueueitem10)\> | 是   | 播放列表单项的队列,用以表示播放列表。                          |
| callback | AsyncCallback<void\>                 | 是   | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
let queueItemDescription_1 = {
    mediaId: '001',
    title: 'music_name',
    subtitle: 'music_sub_name',
    description: 'music_description',
    icon: PIXELMAP_OBJECT,
    iconUri: 'http://www.icon.uri.com',
    extras: {'extras':'any'}
};
let queueItem_1 = {
    itemId: 1,
    description: queueItemDescription_1
};
let queueItemDescription_2 = {
    mediaId: '002',
    title: 'music_name',
    subtitle: 'music_sub_name',
    description: 'music_description',
    icon: PIXELMAP_OBJECT,
    iconUri: 'http://www.icon.uri.com',
    extras: {'extras':'any'}
};
let queueItem_2 = {
    itemId: 2,
    description: queueItemDescription_2
};
let queueItemsArray = [queueItem_1, queueItem_2];
session.setAVQueueItems(queueItemsArray, function (err) {
    if (err) {
        console.info(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('SetAVQueueItems successfully');
    }
});
```

### setAVQueueTitle<sup>10+</sup>

setAVQueueTitle(title: string): Promise\<void>

设置媒体播放列表名称。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名  | 类型   | 必填 | 说明           |
| ------ | ------ | ---- | -------------- |
| title  | string | 是   | 播放列表的名称。 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当播放列表设置成功,无返回结果,否则返回错误对象。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
let queueTitle = 'QUEUE_TITLE';
session.setAVQueueTitle(queueTitle).then(() => {
    console.info('SetAVQueueTitle successfully');
}).catch((err) => {
    console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### setAVQueueTitle<sup>10+</sup>

setAVQueueTitle(title: string, callback: AsyncCallback\<void\>): void

设置媒体播放列表名称。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                                  | 必填 | 说明                                                         |
| -------- | --------------------- | ---- | ----------------------------------------------------------- |
| title    | string                | 是   | 播放列表名称字段。                          |
| callback | AsyncCallback<void\>  | 是   | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
let queueTitle = 'QUEUE_TITLE';
session.setAVQueueTitle(queueTitle, function (err) {
    if (err) {
        console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('SetAVQueueTitle successfully');
    }
});
```

L
leiiyb 已提交
1296 1297 1298 1299 1300 1301 1302 1303
### setLaunchAbility

setLaunchAbility(ability: WantAgent): Promise\<void>

设置一个WantAgent用于拉起会话的Ability。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1304 1305
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1306 1307
**参数:**

1308 1309 1310
| 参数名  | 类型                                          | 必填 | 说明                                                        |
| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- |
| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | 是   | 应用的相关属性信息,如bundleName,abilityName,deviceId等。 |
L
leiiyb 已提交
1311 1312 1313 1314 1315 1316 1317 1318

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当Ability设置成功,无返回结果,否则返回错误对象。 |

**错误码:**
1319
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1320 1321 1322

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1323 1324
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1325 1326 1327 1328

**示例:**

```js
1329
import wantAgent from '@ohos.app.ability.wantAgent';
L
leiiyb 已提交
1330 1331 1332 1333 1334 1335

//WantAgentInfo对象
let wantAgentInfo = {
    wants: [
        {
            deviceId: "deviceId",
1336 1337
            bundleName: "com.example.myapplication",
            abilityName: "EntryAbility",
L
leiiyb 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
            action: "action1",
            entities: ["entity1"],
            type: "MIMETYPE",
            uri: "key={true,true,false}",
            parameters:
                {
                    mykey0: 2222,
                    mykey1: [1, 2, 3],
                    mykey2: "[1, 2, 3]",
                    mykey3: "ssssssssssssssssssssssssss",
                    mykey4: [false, true, false],
                    mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"],
                    mykey6: true,
                }
        }
    ],
    operationType: wantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
}

wantAgent.getWantAgent(wantAgentInfo).then((agent) => {
    session.setLaunchAbility(agent).then(() => {
        console.info('SetLaunchAbility successfully');
    }).catch((err) => {
        console.info(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`);
    });
});
```

### setLaunchAbility

setLaunchAbility(ability: WantAgent, callback: AsyncCallback\<void>): void

设置一个WantAgent用于拉起会话的Ability。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1376 1377
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1378 1379
**参数:**

1380 1381 1382 1383
| 参数名   | 类型                                          | 必填 | 说明                                                         |
| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| ability  | [WantAgent](js-apis-app-ability-wantAgent.md) | 是   | 应用的相关属性信息,如bundleName,abilityName,deviceId等。  |
| callback | AsyncCallback<void\>                          | 是   | 回调函数。当Ability设置成功,err为undefined,否则返回错误对象。 |
L
leiiyb 已提交
1384 1385

**错误码:**
1386
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1387 1388 1389

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1390 1391
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1392 1393 1394 1395

**示例:**

```js
1396
import wantAgent from '@ohos.app.ability.wantAgent';
L
leiiyb 已提交
1397 1398 1399 1400 1401 1402

//WantAgentInfo对象
let wantAgentInfo = {
    wants: [
        {
            deviceId: "deviceId",
1403 1404
            bundleName: "com.example.myapplication",
            abilityName: "EntryAbility",
L
leiiyb 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
            action: "action1",
            entities: ["entity1"],
            type: "MIMETYPE",
            uri: "key={true,true,false}",
            parameters:
                {
                    mykey0: 2222,
                    mykey1: [1, 2, 3],
                    mykey2: "[1, 2, 3]",
                    mykey3: "ssssssssssssssssssssssssss",
                    mykey4: [false, true, false],
                    mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"],
                    mykey6: true,
                }
        }
    ],
    operationType: wantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
}

wantAgent.getWantAgent(wantAgentInfo).then((agent) => {
    session.setLaunchAbility(agent, function (err) {
        if (err) {
            console.info(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`);
        } else {
            console.info('SetLaunchAbility successfully');
        }
    });
});
```

1437
### dispatchSessionEvent<sup>10+</sup>
1438

1439
dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\<void>
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

媒体提供方设置一个会话内自定义事件,包括事件名和键值对形式的事件内容, 结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名  | 类型                                          | 必填 | 说明                                                        |
| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- |
| event | string | 是   | 需要设置的会话事件的名称 |
| args | {[key: string]: any} | 是   | 需要传递的会话事件键值对 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当事件设置成功,无返回结果,否则返回错误对象。 |

**错误码:**
1461
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
let eventName = "dynamic_lyric";
let args = {
    lyric : "This is lyric"
}
1475 1476
await session.dispatchSessionEvent(eventName, args).catch((err) => {
    console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`);
1477 1478 1479
})
```

1480
### dispatchSessionEvent<sup>10+</sup>
1481

1482
dispatchSessionEvent(event: string, args: {[key: string]: Object}, callback: AsyncCallback<void>): void
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498

媒体提供方设置一个会话内自定义事件,包括事件名和键值对形式的事件内容, 结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名  | 类型                                          | 必填 | 说明                                                        |
| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- |
| event | string | 是   | 需要设置的会话事件的名称 |
| args | {[key: string]: any} | 是   | 需要传递的会话事件键值对 |
| callback | AsyncCallback<void\>                          | 是   | 回调函数。当会话事件设置成功,err为undefined,否则返回错误对象。 |

**错误码:**
1499
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
let eventName = "dynamic_lyric";
let args = {
    lyric : "This is lyric"
}
1513
await session.dispatchSessionEvent(eventName, args, (err) => {
1514
    if(err) {
1515
        console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`);
1516 1517 1518 1519
    }
})
```

L
leiiyb 已提交
1520 1521 1522 1523 1524 1525 1526 1527
### getController

getController(): Promise\<AVSessionController>

获取本会话对应的控制器。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1528 1529
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1530 1531 1532 1533 1534 1535 1536
**返回值:**

| 类型                                                 | 说明                          |
| ---------------------------------------------------- | ----------------------------- |
| Promise<[AVSessionController](#avsessioncontroller)> | Promise对象。返回会话控制器。 |

**错误码:**
1537
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1538 1539 1540

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1541 1542
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563

**示例:**

```js
let controller;
session.getController().then((avcontroller) => {
    controller = avcontroller;
    console.info(`GetController : SUCCESS : sessionid : ${controller.sessionId}`);
}).catch((err) => {
    console.info(`GetController BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getController

getController(callback: AsyncCallback\<AVSessionController>): void

获取本会话相应的控制器。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1564 1565
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1566 1567 1568 1569 1570 1571 1572
**参数:**

| 参数名   | 类型                                                        | 必填 | 说明                       |
| -------- | ----------------------------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback<[AVSessionController](#avsessioncontroller)\> | 是   | 回调函数。返回会话控制器。 |

**错误码:**
1573
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1574 1575 1576

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1577 1578
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601

**示例:**

```js
let controller;
session.getController(function (err, avcontroller) {
    if (err) {
        console.info(`GetController BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        controller = avcontroller;
        console.info(`GetController : SUCCESS : sessionid : ${controller.sessionId}`);
    }
});
```

### getOutputDevice

getOutputDevice(): Promise\<OutputDeviceInfo>

通过会话获取播放设备信息。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1602 1603
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1604 1605 1606 1607 1608 1609 1610
**返回值:**

| 类型                                           | 说明                              |
| ---------------------------------------------- | --------------------------------- |
| Promise<[OutputDeviceInfo](#outputdeviceinfo)> | Promise对象。返回播放设备信息。 |

**错误码:**
1611
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1612 1613 1614

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1615 1616
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635

**示例:**

```js
session.getOutputDevice().then((outputDeviceInfo) => {
    console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`);
}).catch((err) => {
    console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getOutputDevice

getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void

通过会话获取播放设备相关信息。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1636 1637
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1638 1639 1640 1641 1642 1643 1644
**参数:**

| 参数名   | 类型                                                  | 必填 | 说明                           |
| -------- | ----------------------------------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo)\> | 是   | 回调函数,返回播放设备信息。 |

**错误码:**
1645
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1646 1647 1648

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1649 1650
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671

**示例:**

```js
session.getOutputDevice(function (err, outputDeviceInfo) {
    if (err) {
        console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`);
    }
});
```

### activate

activate(): Promise\<void>

激活会话,激活后可正常使用会话。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1672 1673
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1674 1675 1676 1677 1678 1679 1680
**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当会话激活成功,无返回结果,否则返回错误对象。 |

**错误码:**
1681
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1682 1683 1684

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1685 1686
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705

**示例:**

```js
session.activate().then(() => {
    console.info('Activate : SUCCESS ');
}).catch((err) => {
    console.info(`Activate BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### activate

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

激活会话,激活后可正常使用会话。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1706 1707
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1708 1709 1710 1711 1712 1713 1714
**参数:**

| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback<void\> | 是   | 回调函数。当会话激活成功,err为undefined,否则返回错误对象。 |

**错误码:**
1715
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1716 1717 1718

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1719 1720
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741

**示例:**

```js
session.activate(function (err) {
    if (err) {
        console.info(`Activate BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('Activate : SUCCESS ');
    }
});
```

### deactivate

deactivate(): Promise\<void>

禁用当前会话的功能,可通过[activate](#activate)恢复。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1742 1743
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1744 1745 1746 1747 1748 1749 1750
**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当禁用会话成功,无返回结果,否则返回错误对象。|

**错误码:**
1751
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1752 1753 1754

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1755 1756
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777

**示例:**

```js
session.deactivate().then(() => {
    console.info('Deactivate : SUCCESS ');
}).catch((err) => {
    console.info(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### deactivate

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

禁用当前会话。结果通过callback异步回调方式返回。

禁用当前会话的功能,可通过[activate](#activate)恢复。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1778 1779
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1780 1781 1782 1783 1784 1785 1786
**参数:**

| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback<void\> | 是   | 回调函数。当禁用会话成功,err为undefined,否则返回错误对象。|

**错误码:**
1787
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1788 1789 1790

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1791 1792
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813

**示例:**

```js
session.deactivate(function (err) {
    if (err) {
        console.info(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('Deactivate : SUCCESS ');
    }
});
```

### destroy

destroy(): Promise\<void>

销毁当前会话,使当前会话完全失效。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1814 1815
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1816 1817 1818 1819 1820 1821 1822
**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当会话销毁成功,无返回结果,否则返回错误对象。 |

**错误码:**
1823
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1824 1825 1826

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1827 1828
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847

**示例:**

```js
session.destroy().then(() => {
    console.info('Destroy : SUCCESS ');
}).catch((err) => {
    console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### destroy

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

销毁当前会话,使当前会话完全失效。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1848 1849
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1850 1851 1852 1853 1854 1855 1856
**参数:**

| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback<void\> | 是   | 回调函数。当会话销毁成功,err为undefined,否则返回错误对象。 |

**错误码:**
1857
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1858 1859 1860

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1861 1862
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883

**示例:**

```js
session.destroy(function (err) {
    if (err) {
        console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('Destroy : SUCCESS ');
    }
});
```

### on('play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind')

on(type: 'play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind', callback: () => void): void

设置播放命令监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1884 1885
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1886 1887 1888 1889 1890 1891 1892 1893
**参数:**

| 参数名   | 类型                 | 必填 | 说明                                                         |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
| type     | string               | 是   | 事件回调类型,支持的事件包括:`'play'``'pause'``'stop'`` 'playNext'`` 'playPrevious'``'fastForward'`` 'rewind'`<br/>当对应的播放命令被发送到会话时,触发该事件回调。 |
| callback | callback: () => void | 是   | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。                                        |

**错误码:**
1894
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1895 1896 1897

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1898 1899
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934

**示例:**

```js
session.on('play', () => {
    console.info('on play entry');
});
session.on('pause', () => {
    console.info('on pause entry');
});
session.on('stop', () => {
    console.info('on stop entry');
});
session.on('playNext', () => {
    console.info('on playNext entry');
});
session.on('playPrevious', () => {
    console.info('on playPrevious entry');
});
session.on('fastForward', () => {
    console.info('on fastForward entry');
});
session.on('rewind', () => {
    console.info('on rewind entry');
});
```

### on('seek')

on(type: 'seek', callback: (time: number) => void): void

设置跳转节点监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1935 1936
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1937 1938 1939 1940 1941 1942 1943 1944
**参数:**

| 参数名   | 类型                   | 必填 | 说明                                                         |
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
| type     | string                 | 是   | 事件回调类型,支持事件`'seek'`:当跳转节点命令被发送到会话时,触发该事件。 |
| callback | (time: number) => void | 是   | 回调函数。参数time是时间节点,单位为毫秒。                   |

**错误码:**
1945
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1946 1947 1948

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1949 1950
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967

**示例:**
The session does not exist
```js
session.on('seek', (time) => {
    console.info(`on seek entry time : ${time}`);
});
```

### on('setSpeed')

on(type: 'setSpeed', callback: (speed: number) => void): void

设置播放速率的监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
1968 1969
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1970 1971 1972 1973 1974 1975 1976 1977
**参数:**

| 参数名   | 类型                    | 必填 | 说明                                                         |
| -------- | ----------------------- | ---- | ------------------------------------------------------------ |
| type     | string                  | 是   | 事件回调类型,支持事件`'setSpeed'`:当设置播放速率的命令被发送到会话时,触发该事件。 |
| callback | (speed: number) => void | 是   | 回调函数。参数speed是播放倍速。                              |

**错误码:**
1978
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
1979 1980 1981

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
1982 1983
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000

**示例:**

```js
session.on('setSpeed', (speed) => {
    console.info(`on setSpeed speed : ${speed}`);
});
```

### on('setLoopMode')

on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void

设置循环模式的监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2001 2002
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2003 2004 2005 2006 2007 2008 2009 2010
**参数:**

| 参数名    | 类型                                   | 必填 | 说明  |
| -------- | ------------------------------------- | ---- | ---- |
| type     | string                                | 是   | 事件回调类型,支持事件`'setLoopMode'`:当设置循环模式的命令被发送到会话时,触发该事件。 |
| callback | (mode: [LoopMode](#loopmode)) => void | 是   | 回调函数。参数mode是循环模式。                               |

**错误码:**
2011
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2012 2013 2014

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2015 2016
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033

**示例:**

```js
session.on('setLoopMode', (mode) => {
    console.info(`on setLoopMode mode : ${mode}`);
});
```

### on('toggleFavorite')

on(type: 'toggleFavorite', callback: (assetId: string) => void): void

设置是否收藏的监听事件

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2034 2035
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2036 2037 2038 2039 2040 2041 2042 2043
**参数:**

| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                    | 是   | 事件回调类型,支持事件`'toggleFavorite'`:当是否收藏的命令被发送到会话时,触发该事件。 |
| callback | (assetId: string) => void | 是   | 回调函数。参数assetId是媒体ID。                              |

**错误码:**
2044
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2045 2046 2047

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2048 2049
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2050 2051 2052 2053 2054 2055 2056 2057 2058

**示例:**

```js
session.on('toggleFavorite', (assetId) => {
    console.info(`on toggleFavorite mode : ${assetId}`);
});
```

2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
### on('skipToQueueItem')<sup>10+</sup>

on(type: 'skipToQueueItem', callback: (itemId: number) => void): void

设置播放列表其中某项被选中的监听事件,session端可以选择对这个单项歌曲进行播放。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                      | 必填 | 说明                                                                                      |
| -------- | ------------------------ | ---- | ---------------------------------------------------------------------------------------- |
| type     | string                   | 是   | 事件回调类型,支持事件`'skipToQueueItem'`:当播放列表选中单项的命令被发送到会话时,触发该事件。 |
| callback | (itemId: number) => void | 是   | 回调函数。参数itemId是选中的播放列表项的ID。                                                |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
session.on('skipToQueueItem', (itemId) => {
    console.info(`on skipToQueueItem id : ${itemId}`);
});
```

L
leiiyb 已提交
2092 2093 2094 2095 2096 2097 2098 2099
### on('handleKeyEvent')

on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void

设置按键事件的监听

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2100 2101
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2102 2103 2104 2105 2106 2107 2108 2109
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 事件回调类型,支持事件`'handleKeyEvent'`:当按键事件被发送到会话时,触发该事件。 |
| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | 是   | 回调函数。参数event是按键事件。                              |

**错误码:**
2110
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2111 2112 2113

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2114 2115
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

**示例:**

```js
session.on('handleKeyEvent', (event) => {
    console.info(`on handleKeyEvent event : ${event}`);
});
```

### on('outputDeviceChange')

on(type: 'outputDeviceChange', callback: (device: OutputDeviceInfo) => void): void

设置播放设备变化的监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2133 2134
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2135 2136 2137 2138 2139 2140 2141 2142
**参数:**

| 参数名   | 类型                                                    | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                                  | 是   | 事件回调类型,支持事件`'outputDeviceChange'`:当播放设备变化时,触发该事件。 |
| callback | (device: [OutputDeviceInfo](#outputdeviceinfo)) => void | 是   | 回调函数。参数device是设备相关信息。                         |

**错误码:**
2143
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2144 2145 2146

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2147 2148
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2149 2150 2151 2152 2153 2154 2155 2156 2157

**示例:**

```js
session.on('outputDeviceChange', (device) => {
    console.info(`on outputDeviceChange device isRemote : ${device.isRemote}`);
});
```

2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
### on('commonCommand')<sup>10+</sup>

on(type: 'commonCommand', callback: (command: string, args: {[key: string]: Object}) => void): void

设置自定义控制命令变化的监听器。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 事件回调类型,支持事件`'commonCommand'`:当自定义控制命令变化时,触发该事件。 |
| callback | (commonCommand: string, args: {[key:string]: Object}) => void         | 是   | 回调函数,commonCommand为变化的自定义控制命令名,args为自定义控制命令的参数。          |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |

**示例:**

```js
session.on('commonCommand', (commonCommand, args) => {
    console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`);
});
```

L
leiiyb 已提交
2191 2192 2193 2194 2195 2196 2197 2198
### off('play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind')

off(type: 'play' | 'pause' | 'stop' | 'playNext' | 'playPrevious' | 'fastForward' | 'rewind', callback?: () => void): void

取消会话相关事件监听,关闭后,不再进行相关事件回调。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2199 2200
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2201 2202 2203 2204 2205 2206 2207 2208
**参数:**

| 参数名    | 类型                  | 必填 | 说明                                                                                                                         |
| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- |
| type     | string               | 是   | 关闭对应的监听事件,支持的事件包括:`'play'`` 'pause'``'stop'``'playNext'`` 'playPrevious'`` 'fastForward'`` 'rewind'`。 |
| callback | callback: () => void | 否   | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                            |

**错误码:**
2209
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2210 2211 2212

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2213 2214
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235

**示例:**

```js
session.off('play');
session.off('pause');
session.off('stop');
session.off('playNext');
session.off('playPrevious');
session.off('fastForward');
session.off('rewind');
```

### off('seek')

off(type: 'seek', callback?: (time: number) => void): void

取消监听跳转节点事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2236 2237
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2238 2239 2240 2241 2242 2243 2244 2245
**参数:**

| 参数名   | 类型                   | 必填 | 说明                                          |
| -------- | ---------------------- | ---- | ----------------------------------------- |
| type     | string                 | 是   | 关闭对应的监听事件,支持关闭事件`'seek'`。       |
| callback | (time: number) => void | 否   | 回调函数,参数time是时间节点,单位为毫秒。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。        |

**错误码:**
2246
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2247 2248 2249

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2250 2251
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266

**示例:**

```js
session.off('seek');
```

### off('setSpeed')

off(type: 'setSpeed', callback?: (speed: number) => void): void

取消监听播放速率变化事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2267 2268
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2269 2270 2271 2272 2273 2274 2275 2276
**参数:**

| 参数名   | 类型                    | 必填 | 说明                                           |
| -------- | ----------------------- | ---- | -------------------------------------------|
| type     | string                  | 是   | 关闭对应的监听事件,支持关闭事件`'setSpeed'`。    |
| callback | (speed: number) => void | 否   | 回调函数,参数speed是播放倍速。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                 |

**错误码:**
2277
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2278 2279 2280

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2281 2282
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297

**示例:**

```js
session.off('setSpeed');
```

### off('setLoopMode')

off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void

取消监听循环模式变化事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2298 2299
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2300 2301 2302 2303 2304 2305 2306 2307
**参数:**

| 参数名   | 类型                                  | 必填 | 说明     |
| -------- | ------------------------------------- | ---- | ----- |
| type     | string | 是   | 关闭对应的监听事件,支持关闭事件`'setLoopMode'`。|
| callback | (mode: [LoopMode](#loopmode)) => void | 否   | 回调函数,参数mode是循环模式。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 |

**错误码:**
2308
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2309 2310 2311

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2312 2313
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328

**示例:**

```js
session.off('setLoopMode');
```

### off('toggleFavorite')

off(type: 'toggleFavorite', callback?: (assetId: string) => void): void

取消监听是否收藏的事件

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2329 2330
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2331 2332 2333 2334 2335 2336 2337 2338
**参数:**

| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | -------------------------------------------------------- |
| type     | string                    | 是   | 关闭对应的监听事件,支持关闭事件`'toggleFavorite'`。            |
| callback | (assetId: string) => void | 否   | 回调函数,参数assetId是媒体ID。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                               |

**错误码:**
2339
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2340 2341 2342

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2343 2344
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2345 2346 2347 2348 2349 2350 2351

**示例:**

```js
session.off('toggleFavorite');
```

2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
### off('skipToQueueItem')<sup>10+</sup>

off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void

取消监听播放列表单项选中的事件

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                      | 必填 | 说明                                                                                                                                                        |
| -------- | ------------------------ | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| type     | string                   | 是   | 关闭对应的监听事件,支持关闭事件`'skipToQueueItem'`。                                                                                                          |
| callback | (itemId: number) => void | 否   | 回调函数,参数itemId是播放列表单项ID。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
session.off('skipToQueueItem');
```

L
leiiyb 已提交
2383 2384 2385 2386 2387 2388 2389 2390
### off('handleKeyEvent')

off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void

取消监听按键事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2391 2392
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2393 2394 2395 2396 2397 2398 2399 2400
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 关闭对应的监听事件,支持关闭事件`'handleKeyEvent'`。             |
| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | 否   | 回调函数,参数event是按键事件。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                              |

**错误码:**
2401
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2402 2403 2404

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2405 2406
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421

**示例:**

```js
session.off('handleKeyEvent');
```

### off('outputDeviceChange')

off(type: 'outputDeviceChange', callback?: (device: OutputDeviceInfo) => void): void

取消监听播放设备变化的事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2422 2423
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2424 2425 2426 2427 2428 2429 2430 2431
**参数:**

| 参数名   | 类型                                                    | 必填 | 说明                                                      |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ |
| type     | string                                                  | 是   | 关闭对应的监听事件,支持关闭事件`'outputDeviceChange'`。     |
| callback | (device: [OutputDeviceInfo](#outputdeviceinfo)) => void | 否   | 回调函数,参数device是设备相关信息。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                        |

**错误码:**
2432
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2433 2434 2435

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2436 2437
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
2438 2439 2440 2441 2442 2443 2444 2445

**示例:**

```js
session.off('outputDeviceChange');
```


2446
### off('commonCommand')<sup>10+</sup>
L
leiiyb 已提交
2447

2448
off(type: 'commonCommand', callback?: (commonCommand: string, args: {[key:string]: Object}) => void): void
L
leiiyb 已提交
2449

2450
取消监听自定义控制命令的变化。
L
leiiyb 已提交
2451 2452 2453

**系统能力:** SystemCapability.Multimedia.AVSession.Core

2454
**系统接口:** 该接口为系统接口
L
leiiyb 已提交
2455

2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                     |
| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- |
| type     | string                                                       | 是   | 取消对应的监听事件,支持事件`'commonCommand'`。    |
| callback | (commonCommand: string, args: {[key:string]: Object}) => void         | 否   | 回调函数,参数commonCommand是变化的自定义控制命令名,args为自定义控制命令的参数。<br>该参数为可选参数,若不填写该参数,则认为取消所有对commonCommand事件的监听。                      |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------- |
| 6600101  | Session service exception. |

**示例:**

```js
session.off('commonCommand');
```



## AVSessionController

调用[avSession.createController](#avsessioncreatecontroller)后,返回会话控制器实例。控制器可查看会话ID,并可完成对会话发送命令及事件,获取会话元数据,播放状态信息等操作。

### 属性

**系统能力:** SystemCapability.Multimedia.AVSession.Core


| 名称      | 类型   | 可读 | 可写 | 说明                                    |
| :-------- | :----- | :--- | :--- | :-------------------------------------- |
L
leiiyb 已提交
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
| sessionId | string | 是   | 否   | AVSessionController对象唯一的会话标识。 |


**示例:**
```js
let sessionId;
await avSession.createController(session.sessionId).then((controller) => {
    sessionId = controller.sessionId;
}).catch((err) => {
    console.info(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getAVPlaybackState

getAVPlaybackState(): Promise\<AVPlaybackState>

获取当前会话播放状态相关信息。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2510 2511
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2512 2513 2514 2515 2516 2517 2518
**返回值:**

| 类型                                          | 说明                        |
| --------------------------------------------- | --------------------------- |
| Promise<[AVPlaybackState](#avplaybackstate)\> | Promise对象。返回播放状态对象。 |

**错误码:**
2519
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2520 2521 2522

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2523 2524 2525
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543

**示例:**
```js
controller.getAVPlaybackState().then((playbackState) => {
    console.info(`GetAVPlaybackState : SUCCESS : state : ${playbackState.state}`);
}).catch((err) => {
    console.info(`GetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getAVPlaybackState

getAVPlaybackState(callback: AsyncCallback\<AVPlaybackState>): void

获取当前播放状态相关信息。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2544 2545
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2546 2547 2548 2549 2550 2551 2552
**参数:**

| 参数名   | 类型                                                | 必填 | 说明                         |
| -------- | --------------------------------------------------- | ---- | ---------------------------- |
| callback | AsyncCallback<[AVPlaybackState](#avplaybackstate)\> | 是   | 回调函数,返回当前播放状态对象。 |

**错误码:**
2553
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2554 2555 2556

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2557 2558 2559
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571

**示例:**
```js
controller.getAVPlaybackState(function (err, playbackState) {
    if (err) {
        console.info(`GetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetAVPlaybackState : SUCCESS : state : ${playbackState.state}`);
    }
});
```

2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790
### getAVQueueItems<sup>10+</sup>

getAVQueueItems(): Promise\<Array\<AVQueueItem>>

获取当前会话播放列表相关信息。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**返回值:**

| 类型                                          | 说明                           |
| --------------------------------------------- | ----------------------------- |
| Promise<Array<[AVQueueItem](#avqueueitem10)\>\> | Promise对象。返回播放列表队列。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |

**示例:**
```js
controller.getAVQueueItems().then((items) => {
    console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`);
}).catch((err) => {
    console.info(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getAVQueueItems<sup>10+</sup>

getAVQueueItems(callback: AsyncCallback\<Array\<AVQueueItem>>): void

获取当前播放列表相关信息。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                                                 | 必填 | 说明                      |
| -------- | --------------------------------------------------- | ---- | ------------------------- |
| callback | AsyncCallback<Array<[AVQueueItem](#avqueueitem10)\>\> | 是   | 回调函数,返回播放列表队列。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |

**示例:**
```js
controller.getAVQueueItems(function (err, items) {
    if (err) {
        console.info(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`);
    }
});
```

### getAVQueueTitle<sup>10+</sup>

getAVQueueTitle(): Promise\<string>

获取当前会话播放列表的名称。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**返回值:**

| 类型             | 说明                           |
| ---------------- | ----------------------------- |
| Promise<string\> | Promise对象。返回播放列表名称。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |

**示例:**
```js
controller.getAVQueueTitle().then((title) => {
    console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`);
}).catch((err) => {
    console.info(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getAVQueueTitle<sup>10+</sup>

getAVQueueTitle(callback: AsyncCallback\<string>): void

获取当前播放列表的名称。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                    | 必填 | 说明                      |
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback<string\> | 是   | 回调函数,返回播放列表名称。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |

**示例:**
```js
controller.getAVQueueTitle(function (err, title) {
    if (err) {
        console.info(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`);
    }
});
```

### skipToQueueItem<sup>10+</sup>

skipToQueueItem(itemId: number): Promise\<void>

设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名  | 类型    | 必填 | 说明                                        |
| ------ | ------- | ---- | ------------------------------------------- |
| itemId | number  | 是   | 播放列表单项的ID值,用以表示选中的播放列表单项。 |

**返回值:**

| 类型           | 说明                                                             |
| -------------- | --------------------------------------------------------------- |
| Promise<void\> | Promise对象。当播放列表单项ID设置成功,无返回结果,否则返回错误对象。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
let queueItemId = 0;
controller.skipToQueueItem(queueItemId).then(() => {
    console.info('SkipToQueueItem successfully');
}).catch((err) => {
    console.info(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### skipToQueueItem<sup>10+</sup>

skipToQueueItem(itemId: number, callback: AsyncCallback\<void>): void

设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名    | 类型                  | 必填 | 说明                                                        |
| -------- | --------------------- | ---- | ----------------------------------------------------------- |
| itemId   | number                | 是   | 播放列表单项的ID值,用以表示选中的播放列表单项。                |
| callback | AsyncCallback<void\>  | 是   | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |

**示例:**

```js
let queueItemId = 0;
controller.skipToQueueItem(queueItemId, function (err) {
    if (err) {
        console.info(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('SkipToQueueItem successfully');
    }
});
```

L
leiiyb 已提交
2791 2792 2793 2794 2795 2796 2797 2798
### getAVMetadata

getAVMetadata(): Promise\<AVMetadata>

获取会话元数据。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2799 2800
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2801 2802 2803 2804 2805 2806 2807
**返回值:**

| 类型                                | 说明                          |
| ----------------------------------- | ----------------------------- |
| Promise<[AVMetadata](#avmetadata)\> | Promise对象,返回会话元数据。 |

**错误码:**
2808
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2809 2810 2811

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2812 2813 2814
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832

**示例:**
```js
controller.getAVMetadata().then((metadata) => {
    console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`);
}).catch((err) => {
    console.info(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getAVMetadata

getAVMetadata(callback: AsyncCallback\<AVMetadata>): void

获取会话元数据。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2833 2834
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2835 2836 2837 2838 2839 2840 2841
**参数:**

| 参数名   | 类型                                      | 必填 | 说明                       |
| -------- | ----------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback<[AVMetadata](#avmetadata)\> | 是   | 回调函数,返回会话元数据。 |

**错误码:**
2842
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2843 2844 2845

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2846 2847 2848
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868

**示例:**
```js
controller.getAVMetadata(function (err, metadata) {
    if (err) {
        console.info(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`);
    }
});
```

### getOutputDevice

getOutputDevice(): Promise\<OutputDeviceInfo>

获取播放设备信息。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2869 2870
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2871 2872 2873 2874 2875 2876 2877
**返回值:**

| 类型                                            | 说明                              |
| ----------------------------------------------- | --------------------------------- |
| Promise<[OutputDeviceInfo](#outputdeviceinfo)\> | Promise对象,返回播放设备信息。 |

**错误码:**
2878
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2879 2880 2881

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2882 2883
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901

**示例:**
```js
controller.getOutputDevice().then((deviceInfo) => {
    console.info(`GetOutputDevice : SUCCESS : isRemote : ${deviceInfo.isRemote}`);
}).catch((err) => {
    console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getOutputDevice

getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void

获取播放设备信息。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2902 2903
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2904 2905 2906 2907 2908 2909 2910
**参数:**

| 参数名   | 类型                                                  | 必填 | 说明                           |
| -------- | ----------------------------------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo)\> | 是   | 回调函数,返回播放设备信息。 |

**错误码:**
2911
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2912 2913 2914

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2915 2916
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937

**示例:**

```js
controller.getOutputDevice(function (err, deviceInfo) {
    if (err) {
        console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetOutputDevice : SUCCESS : isRemote : ${deviceInfo.isRemote}`);
    }
});
```

### sendAVKeyEvent

sendAVKeyEvent(event: KeyEvent): Promise\<void>

发送按键事件到控制器对应的会话。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2938 2939
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2940 2941 2942 2943 2944 2945 2946
**参数:**

| 参数名 | 类型                                                         | 必填 | 说明       |
| ------ | ------------------------------------------------------------ | ---- | ---------- |
| event  | [KeyEvent](js-apis-keyevent.md) | 是   | 按键事件。 |

**错误码:**
2947
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2948 2949 2950

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2951 2952 2953 2954 2955
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
| 6600105  | Invalid session command. |
| 6600106  | The session is not activated. |
L
leiiyb 已提交
2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当事件发送成功,无返回结果,否则返回错误对象。 |

**示例:**

```js
let keyItem = {code:0x49, pressedTime:2, deviceId:0};
let event = {action:2, key:keyItem, keys:[keyItem]};

controller.sendAVKeyEvent(event).then(() => {
    console.info('SendAVKeyEvent Successfully');
}).catch((err) => {
    console.info(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### sendAVKeyEvent

sendAVKeyEvent(event: KeyEvent, callback: AsyncCallback\<void>): void

发送按键事件到会话。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
2984 2985
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2986 2987 2988 2989 2990 2991 2992 2993
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明       |
| -------- | ------------------------------------------------------------ | ---- | ---------- |
| event    | [KeyEvent](js-apis-keyevent.md) | 是   | 按键事件。 |
| callback | AsyncCallback<void\>                                         | 是   | 回调函数。当事件发送成功,err为undefined,否则返回错误对象。 |

**错误码:**
2994
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
2995 2996 2997

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2998 2999 3000 3001 3002
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
| 6600105  | Invalid session command. |
| 6600106  | The session is not activated. |
L
leiiyb 已提交
3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026

**示例:**

```js
let keyItem = {code:0x49, pressedTime:2, deviceId:0};
let event = {action:2, key:keyItem, keys:[keyItem]};

controller.sendAVKeyEvent(event, function (err) {
    if (err) {
        console.info(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('SendAVKeyEvent Successfully');
    }
});
```

### getLaunchAbility

getLaunchAbility(): Promise\<WantAgent>

获取应用在会话中保存的WantAgent对象。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3027 3028
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3029 3030
**返回值:**

3031 3032 3033
| 类型                                                    | 说明                                                         |
| ------------------------------------------------------- | ------------------------------------------------------------ |
| Promise<[WantAgent](js-apis-app-ability-wantAgent.md)\> | Promise对象,返回在[setLaunchAbility](#setlaunchability)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 |
L
leiiyb 已提交
3034 3035

**错误码:**
3036
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3037 3038 3039

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3040 3041 3042
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3043 3044 3045 3046

**示例:**

```js
3047
import wantAgent from '@ohos.app.ability.wantAgent';
L
leiiyb 已提交
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063

controller.getLaunchAbility().then((agent) => {
    console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`);
}).catch((err) => {
    console.info(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getLaunchAbility

getLaunchAbility(callback: AsyncCallback\<WantAgent>): void

获取应用在会话中保存的WantAgent对象。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3064 3065
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3066 3067
**参数:**

3068 3069 3070
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback<[WantAgent](js-apis-app-ability-wantAgent.md)\> | 是   | 回调函数。返回在[setLaunchAbility](#setlaunchability)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 |
L
leiiyb 已提交
3071 3072

**错误码:**
3073
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3074 3075 3076

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3077 3078 3079
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3080 3081 3082 3083

**示例:**

```js
3084
import wantAgent from '@ohos.app.ability.wantAgent';
L
leiiyb 已提交
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102

controller.getLaunchAbility(function (err, agent) {
    if (err) {
        console.info(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`);
    }
});
```

### getRealPlaybackPositionSync

getRealPlaybackPositionSync(): number

获取当前播放位置。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3103 3104
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3105 3106 3107 3108 3109 3110 3111
**返回值:**

| 类型   | 说明               |
| ------ | ------------------ |
| number | 时间节点,毫秒数。 |

**错误码:**
3112
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3113 3114 3115

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3116 3117
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132

**示例:**

```js
let time = controller.getRealPlaybackPositionSync();
```

### isActive

isActive(): Promise\<boolean>

获取会话是否被激活。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3133 3134
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3135 3136 3137 3138 3139 3140 3141
**返回值:**

| 类型              | 说明                                                         |
| ----------------- | ------------------------------------------------------------ |
| Promise<boolean\> | Promise对象,返回会话是否为激活状态,true表示被激活,false表示禁用。 |

**错误码:**
3142
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3143 3144 3145

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3146 3147 3148
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167

**示例:**

```js
controller.isActive().then((isActive) => {
    console.info(`IsActive : SUCCESS : isactive : ${isActive}`);
}).catch((err) => {
    console.info(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### isActive

isActive(callback: AsyncCallback\<boolean>): void

判断会话是否被激活。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3168 3169
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3170 3171 3172 3173 3174 3175 3176
**参数:**

| 参数名   | 类型                    | 必填 | 说明                                                         |
| -------- | ----------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback<boolean\> | 是   | 回调函数,返回会话是否为激活状态,true表示被激活,false表示禁用。 |

**错误码:**
3177
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3178 3179 3180

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3181 3182 3183
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204

**示例:**

```js
controller.isActive(function (err, isActive) {
    if (err) {
        console.info(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`IsActive : SUCCESS : isactive : ${isActive}`);
    }
});
```

### destroy

destroy(): Promise\<void>

销毁当前控制器,销毁后当前控制器不可再用。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3205 3206
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3207 3208 3209 3210 3211 3212 3213
**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当控制器销毁成功,无返回结果,否则返回错误对象。 |

**错误码:**
3214
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3215 3216 3217

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3218 3219
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238

**示例:**

```js
controller.destroy().then(() => {
    console.info('Destroy : SUCCESS ');
}).catch((err) => {
    console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### destroy

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

销毁当前控制器,销毁后当前控制器不可再用。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3239 3240
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3241 3242 3243 3244 3245 3246 3247
**参数:**

| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback<void\> | 是   | 回调函数。当控制器销毁成功,err为undefined,否则返回错误对象。 |

**错误码:**
3248
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3249 3250 3251

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3252 3253
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274

**示例:**

```js
controller.destroy(function (err) {
    if (err) {
        console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('Destroy : SUCCESS ');
    }
});
```

### getValidCommands

getValidCommands(): Promise\<Array\<AVControlCommandType>>

获取会话支持的有效命令。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3275 3276
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3277 3278 3279 3280 3281 3282 3283
**返回值:**

| 类型                                                         | 说明                              |
| ------------------------------------------------------------ | --------------------------------- |
| Promise<Array<[AVControlCommandType](#avcontrolcommandtype)\>\> | Promise对象。返回有效命令的集合。 |

**错误码:**
3284
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3285 3286 3287

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3288 3289 3290
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309

**示例:**

```js
controller.getValidCommands.then((validCommands) => {
    console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`);
}).catch((err) => {
    console.info(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### getValidCommands

getValidCommands(callback: AsyncCallback\<Array\<AVControlCommandType>>): void

获取会话支持的有效命令。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3310 3311
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3312 3313 3314 3315 3316 3317 3318
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                           |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------ |
| callback | AsyncCallback\<Array\<[AVControlCommandType](#avcontrolcommandtype)\>\> | 是   | 回调函数,返回有效命令的集合。 |

**错误码:**
3319
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3320 3321 3322

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3323 3324 3325
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346

**示例:**

```js
controller.getValidCommands(function (err, validCommands) {
    if (err) {
        console.info(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`);
    }
});
```

### sendControlCommand

sendControlCommand(command: AVControlCommand): Promise\<void>

通过控制器发送命令到其对应的会话。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3347 3348
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3349 3350
**参数:**

3351
| 参数名    | 类型                                  | 必填 | 说明                           |
L
leiiyb 已提交
3352 3353 3354 3355 3356 3357 3358 3359 3360 3361
| ------- | ------------------------------------- | ---- | ------------------------------ |
| command | [AVControlCommand](#avcontrolcommand) | 是   | 会话的相关命令和命令相关参数。 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 |

**错误码:**
3362
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3363 3364 3365

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3366 3367 3368 3369 3370 3371
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
| 6600105  | Invalid session command. |
| 6600106  | The session is not activated. |
| 6600107  | Too many commands or events. |
L
leiiyb 已提交
3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401

**示例:**

```js
let avCommand = {command:'play'};
// let avCommand = {command:'pause'};
// let avCommand = {command:'stop'};
// let avCommand = {command:'playNext'};
// let avCommand = {command:'playPrevious'};
// let avCommand = {command:'fastForward'};
// let avCommand = {command:'rewind'};
// let avCommand = {command:'seek', parameter:10};
// let avCommand = {command:'setSpeed', parameter:2.6};
// let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE};
// let avCommand = {command:'toggleFavorite', parameter:"false"};
controller.sendControlCommand(avCommand).then(() => {
    console.info('SendControlCommand successfully');
}).catch((err) => {
    console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`);
});
```

### sendControlCommand

sendControlCommand(command: AVControlCommand, callback: AsyncCallback\<void>): void

通过会话控制器发送命令到其对应的会话。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3402 3403
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3404 3405 3406 3407 3408 3409 3410 3411
**参数:**

| 参数名   | 类型                                  | 必填 | 说明                           |
| -------- | ------------------------------------- | ---- | ------------------------------ |
| command  | [AVControlCommand](#avcontrolcommand) | 是   | 会话的相关命令和命令相关参数。 |
| callback | AsyncCallback<void\>                  | 是   | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。                     |

**错误码:**
3412
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3413 3414 3415

| 错误码ID | 错误信息 |
| -------- | ------------------------------- |
L
leiiyb 已提交
3416 3417 3418 3419 3420 3421
| 6600101  | Session service exception.                |
| 6600102  | The session does not exist.     |
| 6600103  | The session controller does not exist.   |
| 6600105  | Invalid session command.           |
| 6600106  | The session is not activated.                |
| 6600107  | Too many commands or events.      |
L
leiiyb 已提交
3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445

**示例:**

```js
let avCommand = {command:'play'};
// let avCommand = {command:'pause'};
// let avCommand = {command:'stop'};
// let avCommand = {command:'playNext'};
// let avCommand = {command:'playPrevious'};
// let avCommand = {command:'fastForward'};
// let avCommand = {command:'rewind'};
// let avCommand = {command:'seek', parameter:10};
// let avCommand = {command:'setSpeed', parameter:2.6};
// let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE};
// let avCommand = {command:'toggleFavorite', parameter:"false"};
controller.sendControlCommand(avCommand, function (err) {
    if (err) {
        console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('SendControlCommand successfully');
    }
});
```

3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
### sendCommonCommand<sup>10+</sup>

sendCommonCommand(command: string, args: {[key: string]: Object}): Promise\<void>

通过会话控制器发送自定义控制命令到其对应的会话。结果通过Promise异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**参数:**

| 参数名    | 类型                                  | 必填 | 说明                           |
| ------- | ------------------------------------- | ---- | ------------------------------ |
| command | string | 是   | 需要设置的自定义控制命令的名称 |
| args | {[key: string]: any} | 是   | 需要传递的控制命令键值对 |

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| Promise<void\> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600103  | The session controller does not exist. |
| 6600105  | Invalid session command. |
| 6600106  | The session is not activated. |
| 6600107  | Too many commands or events. |

**示例:**

```js
let commandName = "my_command";
let args = {
    command : "This is my command"
}
await controller.sendCommonCommand(commandName, args).catch((err) => {
    console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`);
})
```

### sendCommonCommand<sup>10+</sup>

sendCommonCommand(command: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void

通过会话控制器发送自定义命令到其对应的会话。结果通过callback异步回调方式返回。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**参数:**

| 参数名    | 类型                                  | 必填 | 说明                           |
| ------- | ------------------------------------- | ---- | ------------------------------ |
| command | string | 是   | 需要设置的自定义控制命令的名称 |
| args | {[key: string]: any} | 是   | 需要传递的控制命令键值对 |
| callback | AsyncCallback<void\>                  | 是   | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。                     |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ------------------------------- |
| 6600101  | Session service exception.                |
| 6600102  | The session does not exist.     |
| 6600103  | The session controller does not exist.   |
| 6600105  | Invalid session command.           |
| 6600106  | The session is not activated.                |
| 6600107  | Too many commands or events.      |

**示例:**

```js
let commandName = "my_command";
let args = {
    command : "This is my command"
}
controller.sendCommonCommand(commandName, args, (err) => {
    if(err) {
        console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`);
    }
})
```

L
leiiyb 已提交
3533 3534 3535 3536 3537 3538 3539 3540
### on('metadataChange')

on(type: 'metadataChange', filter: Array\<keyof AVMetadata> | 'all', callback: (data: AVMetadata) => void)

设置元数据变化的监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3541 3542
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3543 3544 3545 3546 3547 3548 3549 3550 3551
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 事件回调类型,支持事件`'metadataChange'`:当元数据变化时,触发该事件。 |
| filter   | Array\<keyof&nbsp;[AVMetadata](#avmetadata)\>&nbsp;&#124;&nbsp;'all' | 是   | 'all' 表示关注元数据所有字段变化;Array<keyof&nbsp;[AVMetadata](#avmetadata)\> 表示关注Array中的字段变化。 |
| callback | (data: [AVMetadata](#avmetadata)) => void                    | 是   | 回调函数,参数data是变化后的元数据。                         |

**错误码:**
3552
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3553 3554 3555

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
L
leiiyb 已提交
3556 3557
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579

**示例:**

```js
controller.on('metadataChange', 'all', (metadata) => {
    console.info(`on metadataChange assetId : ${metadata.assetId}`);
});

let metaFilter = ['assetId', 'title', 'description'];
controller.on('metadataChange', metaFilter, (metadata) => {
    console.info(`on metadataChange assetId : ${metadata.assetId}`);
});
```

### on('playbackStateChange')

on(type: 'playbackStateChange', filter: Array\<keyof AVPlaybackState> | 'all', callback: (state: AVPlaybackState) => void)

设置播放状态变化的监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3580 3581
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3582 3583 3584 3585 3586 3587 3588 3589 3590
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 事件回调类型,支持事件`'playbackStateChange'`:当播放状态变化时,触发该事件。 |
| filter   | Array\<keyof&nbsp;[AVPlaybackState](#avplaybackstate)\>&nbsp;&#124;&nbsp;'all' | 是   | 'all' 表示关注播放状态所有字段变化;Array<keyof&nbsp;[AVPlaybackState](#avplaybackstate)\> 表示关注Array中的字段变化。 |
| callback | (state: [AVPlaybackState](#avplaybackstate)) => void         | 是   | 回调函数,参数state是变化后的播放状态。                      |

**错误码:**
3591
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3592 3593 3594

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
L
leiiyb 已提交
3595 3596
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610

**示例:**

```js
controller.on('playbackStateChange', 'all', (playbackState) => {
    console.info(`on playbackStateChange state : ${playbackState.state}`);
});

let playbackFilter = ['state', 'speed', 'loopMode'];
controller.on('playbackStateChange', playbackFilter, (playbackState) => {
    console.info(`on playbackStateChange state : ${playbackState.state}`);
});
```

3611
### on('sessionEvent')<sup>10+</sup>
3612

3613
on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key:string]: Object}) => void): void
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624

媒体控制器设置会话自定义事件变化的监听器。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647
| type     | string                                                       | 是   | 事件回调类型,支持事件`'sessionEvent'`:当会话事件变化时,触发该事件。 |
| callback | (sessionEvent: string, args: {[key:string]: object}) => void         | 是   | 回调函数,sessionEvent为变化的会话事件名,args为事件的参数。          |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |

**示例:**

```js
controller.on('sessionEvent', (sessionEvent, args) => {
    console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`);
});
```

### on('queueItemsChange')<sup>10+</sup>

on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void

3648
媒体控制器设置会话自定义播放列表变化的监听器。
3649 3650 3651 3652 3653 3654 3655 3656 3657 3658

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                                                   | 必填 | 说明                                                                         |
| -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------------------------------- |
| type     | string                                                | 是   | 事件回调类型,支持事件`'queueItemsChange'`:当session修改播放列表时,触发该事件。 |
3659
| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void  | 是   | 回调函数,items为变化的播放列表。                            |
3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |

**示例:**

```js
controller.on('queueItemsChange', (items) => {
    console.info(`OnQueueItemsChange, items length is ${items.length}`);
});
```

### on('queueTitleChange')<sup>10+</sup>

on(type: 'queueTitleChange', callback: (title: string) => void): void

3681
媒体控制器设置会话自定义播放列表的名称变化的监听器。
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                     | 必填 | 说明                                                                             |
| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------- |
| type     | string                  | 是   | 事件回调类型,支持事件`'queueTitleChange'`:当session修改播放列表名称时,触发该事件。 |
3692
| callback | (title: string) => void | 是   | 回调函数,title为变化的播放列表名称。                                |
3693 3694

**错误码:**
3695
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
3696 3697 3698 3699 3700 3701 3702 3703 3704

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |

**示例:**

```js
3705 3706
controller.on('queueTitleChange', (title) => {
    console.info(`queueTitleChange, title is ${title}`);
3707 3708 3709
});
```

L
leiiyb 已提交
3710 3711 3712 3713 3714 3715 3716 3717
### on('sessionDestroy')

on(type: 'sessionDestroy', callback: () => void)

会话销毁的监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3718 3719
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3720 3721 3722 3723 3724 3725 3726 3727
**参数:**

| 参数名   | 类型       | 必填 | 说明                                                         |
| -------- | ---------- | ---- | ------------------------------------------------------------ |
| type     | string     | 是   | 事件回调类型,支持事件`'sessionDestroy'`:当检测到会话销毁时,触发该事件)。 |
| callback | () => void | 是   | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。                  |

**错误码:**
3728
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3729 3730 3731

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
L
leiiyb 已提交
3732 3733
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750

**示例:**

```js
controller.on('sessionDestroy', () => {
    console.info('on sessionDestroy : SUCCESS ');
});
```

### on('activeStateChange')

on(type: 'activeStateChange', callback: (isActive: boolean) => void)

会话的激活状态的监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3751 3752
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3753 3754 3755 3756 3757 3758 3759 3760
**参数:**

| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                      | 是   | 事件回调类型,支持事件`'activeStateChange'`:当检测到会话的激活状态发生改变时,触发该事件。 |
| callback | (isActive: boolean) => void | 是   | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。                   |

**错误码:**
3761
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3762 3763 3764

| 错误码ID | 错误信息 |
| -------- | ----------------------------- |
L
leiiyb 已提交
3765 3766
| 6600101  | Session service exception. |
| 6600103  |The session controller does not exist. |
L
leiiyb 已提交
3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783

**示例:**

```js
controller.on('activeStateChange', (isActive) => {
    console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`);
});
```

### on('validCommandChange')

on(type: 'validCommandChange', callback: (commands: Array\<AVControlCommandType>) => void)

会话支持的有效命令变化监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3784 3785
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3786 3787 3788 3789 3790 3791 3792 3793
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 事件回调类型,支持事件`'validCommandChange'`:当检测到会话的合法命令发生改变时,触发该事件。 |
| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype)\>) => void | 是   | 回调函数。参数commands是有效命令的集合。                     |

**错误码:**
3794
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3795 3796 3797

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
L
leiiyb 已提交
3798 3799
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817

**示例:**

```js
controller.on('validCommandChange', (validCommands) => {
    console.info(`validCommandChange : SUCCESS : size : ${validCommands.size}`);
    console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`);
});
```

### on('outputDeviceChange')

on(type: 'outputDeviceChange', callback: (device: OutputDeviceInfo) => void): void

设置播放设备变化的监听事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3818 3819
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3820 3821 3822 3823 3824 3825 3826 3827
**参数:**

| 参数名   | 类型                                                    | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                                  | 是   | 事件回调类型,支持事件为`'outputDeviceChange'`:当播放设备变化时,触发该事件)。 |
| callback | (device: [OutputDeviceInfo](#outputdeviceinfo)) => void | 是   | 回调函数,参数device是设备相关信息。                         |

**错误码:**
3828
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3829 3830 3831

| 错误码ID | 错误信息 |
| -------- | ----------------------- |
L
leiiyb 已提交
3832 3833
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850

**示例:**

```js
controller.on('outputDeviceChange', (device) => {
    console.info(`on outputDeviceChange device isRemote : ${device.isRemote}`);
});
```

### off('metadataChange')

off(type: 'metadataChange', callback?: (data: AVMetadata) => void)

控制器取消监听元数据变化的事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3851 3852
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3853 3854 3855 3856 3857 3858 3859 3860
**参数:**

| 参数名   | 类型                                               | 必填 | 说明                                                    |
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------ |
| type     | string                                           | 是   | 取消对应的监听事件,支持事件`'metadataChange'`。         |
| callback | (data: [AVMetadata](#avmetadata)) => void        | 否   | 回调函数,参数data是变化后的元数据。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                         |

**错误码:**
3861
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3862 3863 3864

| 错误码ID | 错误信息 |
| -------- | ---------------- |
L
leiiyb 已提交
3865
| 6600101  | Session service exception. |
L
leiiyb 已提交
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880

**示例:**

```js
controller.off('metadataChange');
```

### off('playbackStateChange')

off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void)

控制器取消监听播放状态变化的事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
3881 3882
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3883 3884 3885 3886 3887 3888 3889 3890
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                     |
| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- |
| type     | string                                                       | 是   | 取消对应的监听事件,支持事件`'playbackStateChange'`。    |
| callback | (state: [AVPlaybackState](#avplaybackstate)) => void         | 否   | 回调函数,参数state是变化后的播放状态。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                      |

**错误码:**
3891
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
3892 3893 3894

| 错误码ID | 错误信息 |
| -------- | ---------------- |
L
leiiyb 已提交
3895
| 6600101  | Session service exception. |
L
leiiyb 已提交
3896 3897 3898 3899 3900 3901 3902

**示例:**

```js
controller.off('playbackStateChange');
```

3903
### off('sessionEvent')<sup>10+</sup>
3904

3905
off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key:string]: Obejct}) => void): void
3906

3907
控制器取消监听会话事件的变化通知。
3908 3909 3910 3911 3912 3913 3914 3915 3916

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                     |
| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- |
3917 3918
| type     | string                                                       | 是   | 取消对应的监听事件,支持事件`'sessionEvent'`。    |
| callback | (sessionEvent: string, args: {[key:string]: object}) => void         | 否   | 回调函数,参数sessionEvent是变化的事件名,args为事件的参数。<br>该参数为可选参数,若不填写该参数,则认为取消所有对sessionEvent事件的监听。                      |
3919 3920

**错误码:**
3921
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
3922 3923 3924 3925 3926 3927 3928 3929

| 错误码ID | 错误信息 |
| -------- | ---------------- |
| 6600101  | Session service exception. |

**示例:**

```js
3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946
controller.off('sessionEvent');
```

### off('queueItemsChange')<sup>10+</sup>

off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void

控制器取消监听播放列表变化的事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名    | 类型                                                 | 必填 | 说明                                                                                                |
| -------- | ---------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------- |
3947
| type     | string                                               | 是   | 取消对应的监听事件,支持事件`'queueItemsChange'`。                                                     |
3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990
| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | 否   | 回调函数,参数items是变化的播放列表。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------- |
| 6600101  | Session service exception. |

**示例:**

```js
controller.off('queueItemsChange');
```

### off('queueTitleChange')<sup>10+</sup>

off(type: 'queueTitleChange', callback?: (title: string) => void): void

控制器取消监听播放列表名称变化的事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

**系统接口:** 该接口为系统接口

**参数:**

| 参数名    | 类型                    | 必填 | 说明                                                                                                    |
| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- |
| type     | string                  | 是   | 取消对应的监听事件,支持事件`'queueTitleChange'`。                                                         |
| callback | (title: string) => void | 否   | 回调函数,参数items是变化的播放列表名称。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 |

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)

| 错误码ID | 错误信息 |
| -------- | ---------------- |
| 6600101  | Session service exception. |

**示例:**

```js
controller.off('queueTitleChange');
3991 3992
```

L
leiiyb 已提交
3993 3994 3995 3996 3997 3998 3999 4000
### off('sessionDestroy')

off(type: 'sessionDestroy', callback?: () => void)

控制器取消监听会话的销毁事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4001 4002
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4003 4004 4005 4006 4007 4008 4009 4010
**参数:**

| 参数名   | 类型       | 必填 | 说明                                                      |
| -------- | ---------- | ---- | ----------------------------------------------------- |
| type     | string     | 是   | 取消对应的监听事件,支持事件`'sessionDestroy'`。         |
| callback | () => void | 否   | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                                               |

**错误码:**
4011
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
4012 4013 4014

| 错误码ID | 错误信息 |
| -------- | ---------------- |
L
leiiyb 已提交
4015
| 6600101  | Session service exception. |
L
leiiyb 已提交
4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030

**示例:**

```js
controller.off('sessionDestroy');
```

### off('activeStateChange')

off(type: 'activeStateChange', callback?: (isActive: boolean) => void)

控制器取消监听会话激活状态变化的事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4031 4032
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4033 4034 4035 4036 4037 4038 4039 4040
**参数:**

| 参数名   | 类型                        | 必填 | 说明                                                      |
| -------- | --------------------------- | ---- | ----------------------------------------------------- |
| type     | string                      | 是   | 取消对应的监听事件,支持事件`'activeStateChange'`。      |
| callback | (isActive: boolean) => void | 否   | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                   |

**错误码:**
4041
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
4042 4043 4044

| 错误码ID | 错误信息 |
| -------- | ---------------- |
L
leiiyb 已提交
4045
| 6600101  | Session service exception. |
L
leiiyb 已提交
4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060

**示例:**

```js
controller.off('activeStateChange');
```

### off('validCommandChange')

off(type: 'validCommandChange', callback?: (commands: Array\<AVControlCommandType>) => void)

控制器取消监听会话有效命令变化的事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4061 4062
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4063 4064 4065 4066 4067 4068 4069 4070
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                        |
| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- |
| type     | string                                                       | 是   | 取消对应的监听事件,支持事件`'validCommandChange'`。         |
| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype)\>) => void | 否   | 回调函数。参数commands是有效命令的集合。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。          |

**错误码:**
4071
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
4072 4073 4074

| 错误码ID | 错误信息           |
| -------- | ---------------- |
L
leiiyb 已提交
4075
| 6600101  | Session service exception. |
L
leiiyb 已提交
4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090

**示例:**

```js
controller.off('validCommandChange');
```

### off('outputDeviceChange')

off(type: 'outputDeviceChange', callback?: (device: OutputDeviceInfo) => void): void

控制器取消监听分布式设备变化的事件。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4091 4092
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4093 4094 4095 4096 4097 4098 4099 4100
**参数:**

| 参数名   | 类型                                                    | 必填 | 说明                                                      |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ |
| type     | string                                                  | 是   | 取消对应的监听事件,支持事件`'outputDeviceChange'`。      |
| callback | (device: [OutputDeviceInfo](#outputdeviceinfo)) => void | 否   | 回调函数,参数device是设备相关信息。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                         |

**错误码:**
4101
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
L
leiiyb 已提交
4102 4103 4104

| 错误码ID  | 错误信息          |
| -------- | ---------------- |
L
leiiyb 已提交
4105
| 6600101  | Session service exception. |
L
leiiyb 已提交
4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133

**示例:**

```js
controller.off('outputDeviceChange');
```

## SessionToken

会话令牌的信息。

**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

| 名称      | 类型   | 必填 | 说明         |
| :-------- | :----- | :--- | :----------- |
| sessionId | string | 是   | 会话ID       |
| pid       | number | 是   | 会话的进程ID |
| uid       | number | 是   | 用户ID       |

## AVSessionType
当前会话支持的会话类型。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4134 4135
**系统接口:** 该接口为系统接口。

L
leiiyb 已提交
4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164
| 名称  | 类型   | 说明 |
| ----- | ------ | ---- |
| audio | string | 音频 |
| video | string | 视频 |

## AVSessionDescriptor

会话的相关描述信息。

**系统能力:** SystemCapability.Multimedia.AVSession.Manager

**系统接口:** 该接口为系统接口。

| 名称         | 类型                                                         | 可读 | 可写 | 说明                                                |
| ------------ | ------------------------------------------------------------ | ---- | --------------------------------------------------- | --------------------------------------------------- |
| sessionId    | string                                                       | 是  | 否 | 会话ID                                              |
| type         | [AVSessionType](#avsessiontype)                              | 是   | 否  | 会话类型                                            |
| sessionTag   | string                                                       | 是   | 否  | 会话的自定义名称                                    |
| elementName  | [ElementName](js-apis-bundle-ElementName.md)                 | 是   | 否  | 会话所属应用的信息(包含bundleName、abilityName等) |
| isActive     | boolean                                                      | 是   | 否  | 会话是否被激活                                      |
| isTopSession | boolean                                                      | 是   | 否  | 会话是否为最新的会话                                |
| outputDevice | [OutputDeviceInfo](#outputdeviceinfo)                        | 是   | 否  | 分布式设备相关信息                                  |

## AVControlCommandType

会话可传递的命令。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4165 4166
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
| 名称           | 类型   | 说明         |
| -------------- | ------ | ------------ |
| play           | string | 播放         |
| pause          | string | 暂停         |
| stop           | string | 停止         |
| playNext       | string | 下一首       |
| playPrevious   | string | 上一首       |
| fastForward    | string | 快进         |
| rewind         | string | 快退         |
| seek           | string | 跳转某一节点 |
| setSpeed       | string | 设置播放倍速 |
| setLoopMode    | string | 设置循环模式 |
| toggleFavorite | string | 是否收藏     |

## AVControlCommand

会话接受的命令的对象描述。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4187 4188
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199
| 名称      | 类型                                              | 必填 | 说明           |
| --------- | ------------------------------------------------- | ---- | -------------- |
| command   | [AVControlCommandType](#avcontrolcommandtype)     | 是   | 命令           |
| parameter | [LoopMode](#loopmode) &#124; string &#124; number | 否   | 命令对应的参数 |

## AVMetadata

媒体元数据的相关属性。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4200 4201
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4202 4203 4204 4205 4206 4207 4208 4209 4210
| 名称            | 类型                      | 必填 | 说明                                                                  |
| --------------- |-------------------------| ---- |---------------------------------------------------------------------|
| assetId         | string                  | 是   | 媒体ID。                                                               |
| title           | string                  | 否   | 标题。                                                                 |
| artist          | string                  | 否   | 艺术家。                                                                |
| author          | string                  | 否   | 专辑作者。                                                               |
| album           | string                  | 否   | 专辑名称。                                                               |
| writer          | string                  | 否   | 词作者。                                                                |
| composer        | string                  | 否   | 作曲者。                                                                |
4211
| duration        | number                  | 否   | 媒体时长,单位毫秒(ms)。                                                  |
L
leiiyb 已提交
4212 4213 4214 4215 4216 4217 4218 4219
| mediaImage      | image.PixelMap &#124; string | 否   | 图片的像素数据或者图片路径地址(本地路径或网络路径)。                             |
| publishDate     | Date                    | 否   | 发行日期。                                                               |
| subtitle        | string                  | 否   | 子标题。                                                                |
| description     | string                  | 否   | 媒体描述。                                                               |
| lyric           | string                  | 否   | 歌词文件路径地址(本地路径或网络路径) |
| previousAssetId | string                  | 否   | 上一首媒体ID。                                                            |
| nextAssetId     | string                  | 否   | 下一首媒体ID。                                                            |

4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247
## AVMediaDescription<sup>10+</sup>

播放列表媒体元数据的相关属性。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

| 名称         | 类型                    | 必填  | 说明                     |
| ------------ | ----------------------- | ---- | ----------------------- |
| mediaId      | string                  | 是   | 播放列表媒体ID。          |
| title        | string                  | 否   | 播放列表媒体标题。        |
| subtitle     | string                  | 否   | 播放列表媒体子标题。      |
| description  | string                  | 否   | 播放列表媒体描述的文本。   |
| icon         | image.PixelMap          | 否   | 播放列表媒体图片像素数据。 |
| iconUri      | string                  | 否   | 播放列表媒体图片路径地址。 |
| extras       | {[key: string]: any}    | 否   | 播放列表媒体额外字段。     |
| mediaUri     | string                  | 否   | 播放列表媒体URI。         |

## AVQueueItem<sup>10+</sup>

播放列表中单项的相关属性。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

| 名称         | 类型                                        | 必填 | 说明                        |
| ------------ | ------------------------------------------ | ---- | --------------------------- |
| itemId       | number                                     | 是   | 播放列表中单项的ID。          |
| description  | [AVMediaDescription](#avmediadescription10)  | 是   | 播放列表中单项的媒体元数据。   |

L
leiiyb 已提交
4248 4249 4250 4251 4252 4253
## AVPlaybackState

媒体播放状态的相关属性。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4254 4255
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
| 名称         | 类型                                  | 必填 | 说明     |
| ------------ | ------------------------------------- | ---- | ------- |
| state        | [PlaybackState](#playbackstate)       | 否   | 播放状态 |
| speed        | number                                | 否   | 播放倍速 |
| position     | [PlaybackPosition](#playbackposition) | 否   | 播放位置 |
| bufferedTime | number                                | 否   | 缓冲时间 |
| loopMode     | [LoopMode](#loopmode)                 | 否   | 循环模式 |
| isFavorite   | boolean                               | 否   | 是否收藏 |

## PlaybackPosition

媒体播放位置的相关属性。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4271 4272
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283
| 名称        | 类型   | 必填 | 说明               |
| ----------- | ------ | ---- | ------------------ |
| elapsedTime | number | 是   | 已用时间,单位毫秒(ms)。 |
| updateTime  | number | 是   | 更新时间,单位毫秒(ms)。 |

## OutputDeviceInfo

播放设备的相关信息。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4284 4285
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297
| 名称       | 类型           | 必填 | 说明                   |
| ---------- | -------------- | ---- | ---------------------- |
| isRemote   | boolean        | 是   | 设备是否连接。         |
| audioDeviceId   | Array<number\> | 是   | 播放设备的ID集合。  |
| deviceName | Array<string\> | 是   | 播放设备的名称集合。    |

## PlaybackState

表示媒体播放状态的枚举。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4298 4299
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316
| 名称                        | 值   | 说明         |
| --------------------------- | ---- | ----------- |
| PLAYBACK_STATE_INITIAL      | 0    | 初始状态     |
| PLAYBACK_STATE_PREPARE      | 1    | 播放准备状态  |
| PLAYBACK_STATE_PLAY         | 2    | 正在播放     |
| PLAYBACK_STATE_PAUSE        | 3    | 暂停         |
| PLAYBACK_STATE_FAST_FORWARD | 4    | 快进         |
| PLAYBACK_STATE_REWIND       | 5    | 快退         |
| PLAYBACK_STATE_STOP         | 6    | 停止         |


## LoopMode

表示媒体播放循环模式的枚举。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4317 4318
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331
| 名称               | 值   | 说明     |
| ------------------ | ---- | -------- |
| LOOP_MODE_SEQUENCE | 0    | 顺序播放 |
| LOOP_MODE_SINGLE   | 1    | 单曲循环 |
| LOOP_MODE_LIST     | 2    | 表单循环 |
| LOOP_MODE_SHUFFLE  | 3    | 随机播放 |

## AVSessionErrorCode

会话发生错误时的错误码。

**系统能力:** SystemCapability.Multimedia.AVSession.Core

L
li-yifan2 已提交
4332 4333
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4334 4335
| 名称                           | 值      | 说明                             |
| ------------------------------ | ------- | ------------------------------- |
L
leiiyb 已提交
4336 4337 4338 4339 4340 4341 4342
| ERR_CODE_SERVICE_EXCEPTION     | 6600101 | Session service exception.               |
| ERR_CODE_SESSION_NOT_EXIST     | 6600102 | The session does not exist.      |
| ERR_CODE_CONTROLLER_NOT_EXIST  | 6600103 | The session controller does not exist.   |
| ERR_CODE_REMOTE_CONNECTION_ERR | 6600104 | The remote session  connection failed.         |
| ERR_CODE_COMMAND_INVALID       | 6600105 | Invalid session command.           |
| ERR_CODE_SESSION_INACTIVE      | 6600106 | The session is not activated.                |
| ERR_CODE_MESSAGE_OVERLOAD      | 6600107 | Too many commands or events.       |