js-apis-avsession.md 170.7 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 444 445 446 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

**示例:**

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

let audioManager = audio.getAudioManager();
let audioDevices;
await audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
    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,否则返回错误对象。                        |

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

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
486 487 488
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
| 6600104  | The remote session  connection failed. |
L
leiiyb 已提交
489 490 491 492 493 494 495 496 497 498 499 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

**示例:**

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

let audioManager = audio.getAudioManager();
let audioDevices;
await audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
    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 | 是   | 回调函数。参数为会话相关描述。                               |

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

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

**示例:**

```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>该参数为会话相关描述,为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                               |

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

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

**示例:**

```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 已提交
603 604
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
605 606 607 608 609 610 611 612
**参数:**

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

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

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

**示例:**

```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 已提交
635 636
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
637 638 639 640 641 642 643 644
**参数:**

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

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

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
649
| 6600101  | Session service exception. |
L
leiiyb 已提交
650 651 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

**示例:**

```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对象。当事件发送成功,无返回结果,否则返回错误对象。 |

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

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

**示例:**

```js

let keyItem = {code:0x49, pressedTime:2, deviceId:0};
D
dingdongdong 已提交
694
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 已提交
695 696 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

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,否则返回错误对象。 |

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

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

**示例:**

```js
let keyItem = {code:0x49, pressedTime:2, deviceId:0};
D
dingdongdong 已提交
735
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 已提交
736 737 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

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对象。当命令发送成功,无返回结果,否则返回错误对象。 |

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

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

**示例:**

```js
D
dingdongdong 已提交
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
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 已提交
798 799 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
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,否则返回错误对象。 |

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

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

**示例:**

```js
D
dingdongdong 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
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 已提交
852 853 854 855 856 857 858 859 860
avSession.sendSystemControlCommand(avcommand, function (err) {
    if (err) {
        console.info(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
        console.info('sendSystemControlCommand successfully');
    }
});
```

Z
zengyawen 已提交
861
## AVSession
L
leiiyb 已提交
862 863 864 865 866 867 868

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

### 属性

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

L
li-yifan2 已提交
869 870
**系统接口:** 该接口为系统接口

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

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


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

### setAVMetadata

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

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

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

L
li-yifan2 已提交
890 891
**系统接口:** 该接口为系统接口

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

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

**返回值:**

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

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

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
909 910
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
911 912 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

**示例:**

```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 已提交
946 947
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
948 949 950 951 952 953 954 955
**参数:**

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

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

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
960 961
| 6600101  | Session service exception. |
| 6600102  | The session does not exist. |
L
leiiyb 已提交
962 963 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

**示例:**

```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 已提交
999 1000
**系统接口:** 该接口为系统接口

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

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

**返回值:**

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

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

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

**示例:**

```js
D
dingdongdong 已提交
1024
let playbackState = {
L
leiiyb 已提交
1025 1026 1027 1028 1029 1030 1031
    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 已提交
1032
session.setAVPlaybackState(playbackState).then(() => {
L
leiiyb 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    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 已提交
1047 1048
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1049 1050 1051 1052 1053 1054 1055 1056
**参数:**

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

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

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

**示例:**

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

1084 1085 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
### 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 已提交
1294 1295 1296 1297 1298 1299 1300 1301
### setLaunchAbility

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

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

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

L
li-yifan2 已提交
1302 1303
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1304 1305
**参数:**

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

**返回值:**

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

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

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

**示例:**

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

//WantAgentInfo对象
let wantAgentInfo = {
    wants: [
        {
            deviceId: "deviceId",
1334 1335
            bundleName: "com.example.myapplication",
            abilityName: "EntryAbility",
L
leiiyb 已提交
1336 1337 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
            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 已提交
1374 1375
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1376 1377
**参数:**

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

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

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

**示例:**

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

//WantAgentInfo对象
let wantAgentInfo = {
    wants: [
        {
            deviceId: "deviceId",
1401 1402
            bundleName: "com.example.myapplication",
            abilityName: "EntryAbility",
L
leiiyb 已提交
1403 1404 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
            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');
        }
    });
});
```

1435
### dispatchSessionEvent<sup>10+</sup>
1436

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

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

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

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

**参数:**

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

**返回值:**

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

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

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

**示例:**

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

1478
### dispatchSessionEvent<sup>10+</sup>
1479

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

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

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

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

**参数:**

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

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

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

**示例:**

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

L
leiiyb 已提交
1518 1519 1520 1521 1522 1523 1524 1525
### getController

getController(): Promise\<AVSessionController>

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

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

L
li-yifan2 已提交
1526 1527
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1528 1529 1530 1531 1532 1533 1534
**返回值:**

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

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

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

**示例:**

```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 已提交
1562 1563
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1564 1565 1566 1567 1568 1569 1570
**参数:**

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

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

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

**示例:**

```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 已提交
1600 1601
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1602 1603 1604 1605 1606 1607 1608
**返回值:**

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

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

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

**示例:**

```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 已提交
1634 1635
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1636 1637 1638 1639 1640 1641 1642
**参数:**

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

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

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

**示例:**

```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 已提交
1670 1671
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1672 1673 1674 1675 1676 1677 1678
**返回值:**

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

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

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

**示例:**

```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 已提交
1704 1705
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1706 1707 1708 1709 1710 1711 1712
**参数:**

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

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

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

**示例:**

```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 已提交
1740 1741
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1742 1743 1744 1745 1746 1747 1748
**返回值:**

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

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

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

**示例:**

```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 已提交
1776 1777
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1778 1779 1780 1781 1782 1783 1784
**参数:**

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

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

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

**示例:**

```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 已提交
1812 1813
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1814 1815 1816 1817 1818 1819 1820
**返回值:**

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

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

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

**示例:**

```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 已提交
1846 1847
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1848 1849 1850 1851 1852 1853 1854
**参数:**

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

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

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

**示例:**

```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 已提交
1882 1883
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1884 1885 1886 1887 1888 1889 1890 1891
**参数:**

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

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

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

**示例:**

```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 已提交
1933 1934
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1935 1936 1937 1938 1939 1940 1941 1942
**参数:**

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

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

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

**示例:**
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 已提交
1966 1967
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
1968 1969 1970 1971 1972 1973 1974 1975
**参数:**

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

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

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

**示例:**

```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 已提交
1999 2000
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2001 2002 2003 2004 2005 2006 2007 2008
**参数:**

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

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

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

**示例:**

```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 已提交
2032 2033
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2034 2035 2036 2037 2038 2039 2040 2041
**参数:**

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

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

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

**示例:**

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

2057 2058 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
### 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 已提交
2090 2091 2092 2093 2094 2095 2096 2097
### on('handleKeyEvent')

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

设置按键事件的监听

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

L
li-yifan2 已提交
2098 2099
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2100 2101 2102 2103 2104 2105 2106 2107
**参数:**

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

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

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

**示例:**

```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 已提交
2131 2132
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2133 2134 2135 2136 2137 2138 2139 2140
**参数:**

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

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

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

**示例:**

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

2156 2157 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
### 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 已提交
2189 2190 2191 2192 2193 2194 2195 2196
### 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 已提交
2197 2198
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2199 2200 2201 2202 2203 2204 2205 2206
**参数:**

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

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

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

**示例:**

```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 已提交
2234 2235
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2236 2237 2238 2239 2240 2241 2242 2243
**参数:**

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

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

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

**示例:**

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

### off('setSpeed')

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

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

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

L
li-yifan2 已提交
2265 2266
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2267 2268 2269 2270 2271 2272 2273 2274
**参数:**

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

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

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

**示例:**

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

### off('setLoopMode')

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

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

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

L
li-yifan2 已提交
2296 2297
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2298 2299 2300 2301 2302 2303 2304 2305
**参数:**

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

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

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

**示例:**

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

### off('toggleFavorite')

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

取消监听是否收藏的事件

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

L
li-yifan2 已提交
2327 2328
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2329 2330 2331 2332 2333 2334 2335 2336
**参数:**

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

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

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

**示例:**

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

2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
### 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 已提交
2381 2382 2383 2384 2385 2386 2387 2388
### off('handleKeyEvent')

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

取消监听按键事件。

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

L
li-yifan2 已提交
2389 2390
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2391 2392 2393 2394 2395 2396 2397 2398
**参数:**

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

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

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

**示例:**

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

### off('outputDeviceChange')

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

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

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

L
li-yifan2 已提交
2420 2421
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2422 2423 2424 2425 2426 2427 2428 2429
**参数:**

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

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

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

**示例:**

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


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

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

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

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

2452
**系统接口:** 该接口为系统接口
L
leiiyb 已提交
2453

2454 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
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                     |
| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- |
| 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 已提交
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
| 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 已提交
2508 2509
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2510 2511 2512 2513 2514 2515 2516
**返回值:**

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

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

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

**示例:**
```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 已提交
2542 2543
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2544 2545 2546 2547 2548 2549 2550
**参数:**

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

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

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

**示例:**
```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}`);
    }
});
```

2570 2571 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
### 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 已提交
2789 2790 2791 2792 2793 2794 2795 2796
### getAVMetadata

getAVMetadata(): Promise\<AVMetadata>

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

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

L
li-yifan2 已提交
2797 2798
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2799 2800 2801 2802 2803 2804 2805
**返回值:**

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

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

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

**示例:**
```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 已提交
2831 2832
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2833 2834 2835 2836 2837 2838 2839
**参数:**

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

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

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

**示例:**
```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 已提交
2867 2868
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2869 2870 2871 2872 2873 2874 2875
**返回值:**

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

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

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

**示例:**
```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 已提交
2900 2901
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2902 2903 2904 2905 2906 2907 2908
**参数:**

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

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

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

**示例:**

```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 已提交
2936 2937
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2938 2939 2940 2941 2942 2943 2944
**参数:**

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

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

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2949 2950 2951 2952 2953
| 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 已提交
2954 2955 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

**返回值:**

| 类型           | 说明                          |
| -------------- | ----------------------------- |
| 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 已提交
2982 2983
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
2984 2985 2986 2987 2988 2989 2990 2991
**参数:**

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

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

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
2996 2997 2998 2999 3000
| 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 已提交
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024

**示例:**

```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 已提交
3025 3026
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3027 3028
**返回值:**

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

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

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

**示例:**

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

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 已提交
3062 3063
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3064 3065
**参数:**

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

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

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

**示例:**

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

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 已提交
3101 3102
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3103 3104 3105 3106 3107 3108 3109
**返回值:**

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

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

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

**示例:**

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

### isActive

isActive(): Promise\<boolean>

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

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

L
li-yifan2 已提交
3131 3132
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3133 3134 3135 3136 3137 3138 3139
**返回值:**

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

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

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

**示例:**

```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 已提交
3166 3167
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3168 3169 3170 3171 3172 3173 3174
**参数:**

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

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

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

**示例:**

```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 已提交
3203 3204
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3205 3206 3207 3208 3209 3210 3211
**返回值:**

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

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

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

**示例:**

```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 已提交
3237 3238
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3239 3240 3241 3242 3243 3244 3245
**参数:**

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

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

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

**示例:**

```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 已提交
3273 3274
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3275 3276 3277 3278 3279 3280 3281
**返回值:**

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

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

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

**示例:**

```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 已提交
3308 3309
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3310 3311 3312 3313 3314 3315 3316
**参数:**

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

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

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

**示例:**

```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 已提交
3345 3346
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3347 3348
**参数:**

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

**返回值:**

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

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

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
L
leiiyb 已提交
3364 3365 3366 3367 3368 3369
| 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 已提交
3370 3371 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

**示例:**

```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 已提交
3400 3401
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3402 3403 3404 3405 3406 3407 3408 3409
**参数:**

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

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

| 错误码ID | 错误信息 |
| -------- | ------------------------------- |
L
leiiyb 已提交
3414 3415 3416 3417 3418 3419
| 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 已提交
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443

**示例:**

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

3444 3445 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
### 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 已提交
3531 3532 3533 3534 3535 3536 3537 3538
### on('metadataChange')

on(type: 'metadataChange', filter: Array\<keyof AVMetadata> | 'all', callback: (data: AVMetadata) => void)

设置元数据变化的监听事件。

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

L
li-yifan2 已提交
3539 3540
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3541 3542 3543 3544 3545 3546 3547 3548 3549
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| 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是变化后的元数据。                         |

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

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
L
leiiyb 已提交
3554 3555
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577

**示例:**

```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 已提交
3578 3579
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3580 3581 3582 3583 3584 3585 3586 3587 3588
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| 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是变化后的播放状态。                      |

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

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
L
leiiyb 已提交
3593 3594
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608

**示例:**

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

3609
### on('sessionEvent')<sup>10+</sup>
3610

3611
on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key:string]: Object}) => void): void
3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622

媒体控制器设置会话自定义事件变化的监听器。

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

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

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645
| 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

3646
媒体控制器设置会话自定义播放列表变化的监听器。
3647 3648 3649 3650 3651 3652 3653 3654 3655 3656

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

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

**参数:**

| 参数名   | 类型                                                   | 必填 | 说明                                                                         |
| -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------------------------------- |
| type     | string                                                | 是   | 事件回调类型,支持事件`'queueItemsChange'`:当session修改播放列表时,触发该事件。 |
3657
| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void  | 是   | 回调函数,items为变化的播放列表。                            |
3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678

**错误码:**
以下错误码的详细介绍请参见[媒体会话管理错误码](../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

3679
媒体控制器设置会话自定义播放列表的名称变化的监听器。
3680 3681 3682 3683 3684 3685 3686 3687 3688 3689

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

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                                                                             |
| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------- |
| type     | string                  | 是   | 事件回调类型,支持事件`'queueTitleChange'`:当session修改播放列表名称时,触发该事件。 |
3690
| callback | (title: string) => void | 是   | 回调函数,title为变化的播放列表名称。                                |
3691 3692

**错误码:**
3693
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
3694 3695 3696 3697 3698 3699 3700 3701 3702

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

**示例:**

```js
3703 3704
controller.on('queueTitleChange', (title) => {
    console.info(`queueTitleChange, title is ${title}`);
3705 3706 3707
});
```

L
leiiyb 已提交
3708 3709 3710 3711 3712 3713 3714 3715
### on('sessionDestroy')

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

会话销毁的监听事件。

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

L
li-yifan2 已提交
3716 3717
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3718 3719 3720 3721 3722 3723 3724 3725
**参数:**

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

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

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
L
leiiyb 已提交
3730 3731
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748

**示例:**

```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 已提交
3749 3750
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3751 3752 3753 3754 3755 3756 3757 3758
**参数:**

| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                      | 是   | 事件回调类型,支持事件`'activeStateChange'`:当检测到会话的激活状态发生改变时,触发该事件。 |
| callback | (isActive: boolean) => void | 是   | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。                   |

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

| 错误码ID | 错误信息 |
| -------- | ----------------------------- |
L
leiiyb 已提交
3763 3764
| 6600101  | Session service exception. |
| 6600103  |The session controller does not exist. |
L
leiiyb 已提交
3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781

**示例:**

```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 已提交
3782 3783
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3784 3785 3786 3787 3788 3789 3790 3791
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 事件回调类型,支持事件`'validCommandChange'`:当检测到会话的合法命令发生改变时,触发该事件。 |
| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype)\>) => void | 是   | 回调函数。参数commands是有效命令的集合。                     |

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

| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
L
leiiyb 已提交
3796 3797
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815

**示例:**

```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 已提交
3816 3817
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3818 3819 3820 3821 3822 3823 3824 3825
**参数:**

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

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

| 错误码ID | 错误信息 |
| -------- | ----------------------- |
L
leiiyb 已提交
3830 3831
| 6600101  | Session service exception. |
| 6600103  | The session controller does not exist. |
L
leiiyb 已提交
3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848

**示例:**

```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 已提交
3849 3850
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3851 3852 3853 3854 3855 3856 3857 3858
**参数:**

| 参数名   | 类型                                               | 必填 | 说明                                                    |
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------ |
| type     | string                                           | 是   | 取消对应的监听事件,支持事件`'metadataChange'`。         |
| callback | (data: [AVMetadata](#avmetadata)) => void        | 否   | 回调函数,参数data是变化后的元数据。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                         |

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

| 错误码ID | 错误信息 |
| -------- | ---------------- |
L
leiiyb 已提交
3863
| 6600101  | Session service exception. |
L
leiiyb 已提交
3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878

**示例:**

```js
controller.off('metadataChange');
```

### off('playbackStateChange')

off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void)

控制器取消监听播放状态变化的事件。

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

L
li-yifan2 已提交
3879 3880
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
3881 3882 3883 3884 3885 3886 3887 3888
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                     |
| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- |
| type     | string                                                       | 是   | 取消对应的监听事件,支持事件`'playbackStateChange'`。    |
| callback | (state: [AVPlaybackState](#avplaybackstate)) => void         | 否   | 回调函数,参数state是变化后的播放状态。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                      |

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

| 错误码ID | 错误信息 |
| -------- | ---------------- |
L
leiiyb 已提交
3893
| 6600101  | Session service exception. |
L
leiiyb 已提交
3894 3895 3896 3897 3898 3899 3900

**示例:**

```js
controller.off('playbackStateChange');
```

3901
### off('sessionEvent')<sup>10+</sup>
3902

3903
off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key:string]: Obejct}) => void): void
3904

3905
控制器取消监听会话事件的变化通知。
3906 3907 3908 3909 3910 3911 3912 3913 3914

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

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

**参数:**

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

**错误码:**
3919
以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)
3920 3921 3922 3923 3924 3925 3926 3927

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

**示例:**

```js
3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
controller.off('sessionEvent');
```

### off('queueItemsChange')<sup>10+</sup>

off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void

控制器取消监听播放列表变化的事件。

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

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

**参数:**

| 参数名    | 类型                                                 | 必填 | 说明                                                                                                |
| -------- | ---------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------- |
3945
| type     | string                                               | 是   | 取消对应的监听事件,支持事件`'queueItemsChange'`。                                                     |
3946 3947 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
| 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');
3989 3990
```

L
leiiyb 已提交
3991 3992 3993 3994 3995 3996 3997 3998
### off('sessionDestroy')

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

控制器取消监听会话的销毁事件。

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

L
li-yifan2 已提交
3999 4000
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4001 4002 4003 4004 4005 4006 4007 4008
**参数:**

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

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

| 错误码ID | 错误信息 |
| -------- | ---------------- |
L
leiiyb 已提交
4013
| 6600101  | Session service exception. |
L
leiiyb 已提交
4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028

**示例:**

```js
controller.off('sessionDestroy');
```

### off('activeStateChange')

off(type: 'activeStateChange', callback?: (isActive: boolean) => void)

控制器取消监听会话激活状态变化的事件。

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

L
li-yifan2 已提交
4029 4030
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4031 4032 4033 4034 4035 4036 4037 4038
**参数:**

| 参数名   | 类型                        | 必填 | 说明                                                      |
| -------- | --------------------------- | ---- | ----------------------------------------------------- |
| type     | string                      | 是   | 取消对应的监听事件,支持事件`'activeStateChange'`。      |
| callback | (isActive: boolean) => void | 否   | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。                   |

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

| 错误码ID | 错误信息 |
| -------- | ---------------- |
L
leiiyb 已提交
4043
| 6600101  | Session service exception. |
L
leiiyb 已提交
4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058

**示例:**

```js
controller.off('activeStateChange');
```

### off('validCommandChange')

off(type: 'validCommandChange', callback?: (commands: Array\<AVControlCommandType>) => void)

控制器取消监听会话有效命令变化的事件。

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

L
li-yifan2 已提交
4059 4060
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4061 4062 4063 4064 4065 4066 4067 4068
**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                        |
| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- |
| type     | string                                                       | 是   | 取消对应的监听事件,支持事件`'validCommandChange'`。         |
| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype)\>) => void | 否   | 回调函数。参数commands是有效命令的集合。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。          |

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

| 错误码ID | 错误信息           |
| -------- | ---------------- |
L
leiiyb 已提交
4073
| 6600101  | Session service exception. |
L
leiiyb 已提交
4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088

**示例:**

```js
controller.off('validCommandChange');
```

### off('outputDeviceChange')

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

控制器取消监听分布式设备变化的事件。

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

L
li-yifan2 已提交
4089 4090
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4091 4092 4093 4094 4095 4096 4097 4098
**参数:**

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

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

| 错误码ID  | 错误信息          |
| -------- | ---------------- |
L
leiiyb 已提交
4103
| 6600101  | Session service exception. |
L
leiiyb 已提交
4104 4105 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

**示例:**

```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 已提交
4132 4133
**系统接口:** 该接口为系统接口。

L
leiiyb 已提交
4134 4135 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
| 名称  | 类型   | 说明 |
| ----- | ------ | ---- |
| 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 已提交
4163 4164
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184
| 名称           | 类型   | 说明         |
| -------------- | ------ | ------------ |
| 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 已提交
4185 4186
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197
| 名称      | 类型                                              | 必填 | 说明           |
| --------- | ------------------------------------------------- | ---- | -------------- |
| command   | [AVControlCommandType](#avcontrolcommandtype)     | 是   | 命令           |
| parameter | [LoopMode](#loopmode) &#124; string &#124; number | 否   | 命令对应的参数 |

## AVMetadata

媒体元数据的相关属性。

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

L
li-yifan2 已提交
4198 4199
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4200 4201 4202 4203 4204 4205 4206 4207 4208
| 名称            | 类型                      | 必填 | 说明                                                                  |
| --------------- |-------------------------| ---- |---------------------------------------------------------------------|
| assetId         | string                  | 是   | 媒体ID。                                                               |
| title           | string                  | 否   | 标题。                                                                 |
| artist          | string                  | 否   | 艺术家。                                                                |
| author          | string                  | 否   | 专辑作者。                                                               |
| album           | string                  | 否   | 专辑名称。                                                               |
| writer          | string                  | 否   | 词作者。                                                                |
| composer        | string                  | 否   | 作曲者。                                                                |
4209
| duration        | number                  | 否   | 媒体时长,单位毫秒(ms)。                                                  |
L
leiiyb 已提交
4210 4211 4212 4213 4214 4215 4216 4217
| mediaImage      | image.PixelMap &#124; string | 否   | 图片的像素数据或者图片路径地址(本地路径或网络路径)。                             |
| publishDate     | Date                    | 否   | 发行日期。                                                               |
| subtitle        | string                  | 否   | 子标题。                                                                |
| description     | string                  | 否   | 媒体描述。                                                               |
| lyric           | string                  | 否   | 歌词文件路径地址(本地路径或网络路径) |
| previousAssetId | string                  | 否   | 上一首媒体ID。                                                            |
| nextAssetId     | string                  | 否   | 下一首媒体ID。                                                            |

4218 4219 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
## 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 已提交
4246 4247 4248 4249 4250 4251
## AVPlaybackState

媒体播放状态的相关属性。

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

L
li-yifan2 已提交
4252 4253
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268
| 名称         | 类型                                  | 必填 | 说明     |
| ------------ | ------------------------------------- | ---- | ------- |
| state        | [PlaybackState](#playbackstate)       | 否   | 播放状态 |
| speed        | number                                | 否   | 播放倍速 |
| position     | [PlaybackPosition](#playbackposition) | 否   | 播放位置 |
| bufferedTime | number                                | 否   | 缓冲时间 |
| loopMode     | [LoopMode](#loopmode)                 | 否   | 循环模式 |
| isFavorite   | boolean                               | 否   | 是否收藏 |

## PlaybackPosition

媒体播放位置的相关属性。

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

L
li-yifan2 已提交
4269 4270
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281
| 名称        | 类型   | 必填 | 说明               |
| ----------- | ------ | ---- | ------------------ |
| elapsedTime | number | 是   | 已用时间,单位毫秒(ms)。 |
| updateTime  | number | 是   | 更新时间,单位毫秒(ms)。 |

## OutputDeviceInfo

播放设备的相关信息。

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

L
li-yifan2 已提交
4282 4283
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295
| 名称       | 类型           | 必填 | 说明                   |
| ---------- | -------------- | ---- | ---------------------- |
| isRemote   | boolean        | 是   | 设备是否连接。         |
| audioDeviceId   | Array<number\> | 是   | 播放设备的ID集合。  |
| deviceName | Array<string\> | 是   | 播放设备的名称集合。    |

## PlaybackState

表示媒体播放状态的枚举。

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

L
li-yifan2 已提交
4296 4297
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314
| 名称                        | 值   | 说明         |
| --------------------------- | ---- | ----------- |
| 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 已提交
4315 4316
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329
| 名称               | 值   | 说明     |
| ------------------ | ---- | -------- |
| LOOP_MODE_SEQUENCE | 0    | 顺序播放 |
| LOOP_MODE_SINGLE   | 1    | 单曲循环 |
| LOOP_MODE_LIST     | 2    | 表单循环 |
| LOOP_MODE_SHUFFLE  | 3    | 随机播放 |

## AVSessionErrorCode

会话发生错误时的错误码。

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

L
li-yifan2 已提交
4330 4331
**系统接口:** 该接口为系统接口

L
leiiyb 已提交
4332 4333
| 名称                           | 值      | 说明                             |
| ------------------------------ | ------- | ------------------------------- |
L
leiiyb 已提交
4334 4335 4336 4337 4338 4339 4340
| 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.       |