1# Interface (AVSessionController) 2<!--Kit: AVSession Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @ccfriend; @liao_qian--> 5<!--Designer: @ccfriend--> 6<!--Tester: @chenmingxi1_huawei--> 7<!--Adviser: @zengyawen--> 8 9> **说明:** 10> 11> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12> - 本Interface首批接口从API version 10开始支持。 13 14AVSessionController控制器可查看会话ID,并可完成对会话发送命令及事件,获取会话元数据,播放状态信息等操作。 15 16## 导入模块 17 18```ts 19import { avSession } from '@kit.AVSessionKit'; 20``` 21 22## 属性 23 24**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 25 26**系统能力:** SystemCapability.Multimedia.AVSession.Core 27 28| 名称 | 类型 | 只读 | 可选 | 说明 | 29| :-------- | :----- | :--- | :--- | :-------------------------------------- | 30| sessionId<sup>10+</sup> | string | 是 | 否 | AVSessionController对象唯一的会话标识。 | 31 32 33**示例:** 34 35```ts 36import { BusinessError } from '@kit.BasicServicesKit'; 37import { avSession } from '@kit.AVSessionKit'; 38 39let tag: string = "createNewSession"; 40let sessionId: string = ""; 41let AVSessionController: avSession.AVSessionController; 42avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession)=> { 43 currentAVSession = data; 44 sessionId = currentAVSession.sessionId; 45 AVSessionController = await currentAVSession.getController(); 46 console.info(`CreateAVSession : SUCCESS :sessionId = ${sessionId}`); 47}).catch((err: BusinessError) => { 48 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 49}); 50``` 51 52## getAVPlaybackState<sup>10+</sup> 53 54getAVPlaybackState(callback: AsyncCallback\<AVPlaybackState>): void 55 56获取当前的远端播放状态。结果通过callback异步回调方式返回。 57 58**系统能力:** SystemCapability.Multimedia.AVSession.Core 59 60**参数:** 61 62| 参数名 | 类型 | 必填 | 说明 | 63| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 64| callback | AsyncCallback<[AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10)\> | 是 | 回调函数,返回远端播放状态。 | 65 66**错误码:** 67 68以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 69 70| 错误码ID | 错误信息 | 71| -------- | ---------------------------------------- | 72| 6600101 | Session service exception. | 73| 6600102 | The session does not exist. | 74| 6600103 | The session controller does not exist. | 75 76**示例:** 77 78```ts 79import { BusinessError } from '@kit.BasicServicesKit'; 80 81avsessionController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { 82 if (err) { 83 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 84 } else { 85 console.info('getAVPlaybackState : SUCCESS'); 86 } 87}); 88``` 89 90## getAVPlaybackState<sup>10+</sup> 91 92getAVPlaybackState(): Promise\<AVPlaybackState> 93 94获取当前的远端播放状态。结果通过Promise异步回调方式返回。 95 96**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 97 98**系统能力:** SystemCapability.Multimedia.AVSession.Core 99 100**返回值:** 101 102| 类型 | 说明 | 103| --------- | ------------------------------------------------------------ | 104| Promise<[AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10)\> | Promise对象。返回远端播放状态。 | 105 106**错误码:** 107 108以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 109 110| 错误码ID | 错误信息 | 111| -------- | ---------------------------------------- | 112| 6600101 | Session service exception. | 113| 6600102 | The session does not exist. | 114| 6600103 | The session controller does not exist. | 115 116**示例:** 117 118```ts 119import { BusinessError } from '@kit.BasicServicesKit'; 120 121avsessionController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { 122 console.info('getAVPlaybackState : SUCCESS'); 123}).catch((err: BusinessError) => { 124 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 125}); 126``` 127 128## getAVMetadata<sup>10+</sup> 129 130getAVMetadata(): Promise\<AVMetadata> 131 132获取会话元数据。结果通过Promise异步回调方式返回。 133 134**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 135 136**系统能力:** SystemCapability.Multimedia.AVSession.Core 137 138**返回值:** 139 140| 类型 | 说明 | 141| ----------------------------------- | ----------------------------- | 142| Promise<[AVMetadata](arkts-apis-avsession-i.md#avmetadata10)\> | Promise对象,返回会话元数据。 | 143 144**错误码:** 145 146以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 147 148| 错误码ID | 错误信息 | 149| -------- | ---------------------------------------- | 150| 6600101 | Session service exception. | 151| 6600102 | The session does not exist. | 152| 6600103 | The session controller does not exist. | 153 154**示例:** 155 156```ts 157import { BusinessError } from '@kit.BasicServicesKit'; 158 159avsessionController.getAVMetadata().then((metadata: avSession.AVMetadata) => { 160 console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); 161}).catch((err: BusinessError) => { 162 console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 163}); 164``` 165 166## getAVMetadata<sup>10+</sup> 167 168getAVMetadata(callback: AsyncCallback\<AVMetadata>): void 169 170获取会话元数据。结果通过callback异步回调方式返回。 171 172**系统能力:** SystemCapability.Multimedia.AVSession.Core 173 174**参数:** 175 176| 参数名 | 类型 | 必填 | 说明 | 177| -------- | ----------------------------------------- | ---- | -------------------------- | 178| callback | AsyncCallback<[AVMetadata](arkts-apis-avsession-i.md#avmetadata10)\> | 是 | 回调函数,返回会话元数据。 | 179 180**错误码:** 181 182以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 183 184| 错误码ID | 错误信息 | 185| -------- | ---------------------------------------- | 186| 6600101 | Session service exception. | 187| 6600102 | The session does not exist. | 188| 6600103 | The session controller does not exist. | 189 190**示例:** 191 192```ts 193import { BusinessError } from '@kit.BasicServicesKit'; 194 195avsessionController.getAVMetadata((err: BusinessError, metadata: avSession.AVMetadata) => { 196 if (err) { 197 console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 198 } else { 199 console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); 200 } 201}); 202``` 203 204## getAVQueueTitle<sup>10+</sup> 205 206getAVQueueTitle(): Promise\<string> 207 208获取当前会话播放列表的名称。结果通过Promise异步回调方式返回。 209 210**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 211 212**系统能力:** SystemCapability.Multimedia.AVSession.Core 213 214**返回值:** 215 216| 类型 | 说明 | 217| ---------------- | ----------------------------- | 218| Promise<string\> | Promise对象。返回播放列表名称。 | 219 220**错误码:** 221 222以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 223 224| 错误码ID | 错误信息 | 225| -------- | ---------------------------------------- | 226| 6600101 | Session service exception. | 227| 6600102 | The session does not exist. | 228| 6600103 | The session controller does not exist. | 229 230**示例:** 231 232```ts 233import { BusinessError } from '@kit.BasicServicesKit'; 234 235avsessionController.getAVQueueTitle().then((title: string) => { 236 console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); 237}).catch((err: BusinessError) => { 238 console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 239}); 240``` 241 242## getAVQueueTitle<sup>10+</sup> 243 244getAVQueueTitle(callback: AsyncCallback\<string>): void 245 246获取当前播放列表的名称。结果通过callback异步回调方式返回。 247 248**系统能力:** SystemCapability.Multimedia.AVSession.Core 249 250**参数:** 251 252| 参数名 | 类型 | 必填 | 说明 | 253| -------- | ---------------------- | ---- | ------------------------- | 254| callback | AsyncCallback<string\> | 是 | 回调函数,返回播放列表名称。 | 255 256**错误码:** 257 258以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 259 260| 错误码ID | 错误信息 | 261| -------- | ---------------------------------------- | 262| 6600101 | Session service exception. | 263| 6600102 | The session does not exist. | 264| 6600103 | The session controller does not exist. | 265 266**示例:** 267 268```ts 269import { BusinessError } from '@kit.BasicServicesKit'; 270 271avsessionController.getAVQueueTitle((err: BusinessError, title: string) => { 272 if (err) { 273 console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 274 } else { 275 console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); 276 } 277}); 278``` 279 280## getAVQueueItems<sup>10+</sup> 281 282getAVQueueItems(): Promise\<Array\<AVQueueItem>> 283 284获取当前会话播放列表相关信息。结果通过Promise异步回调方式返回。 285 286**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 287 288**系统能力:** SystemCapability.Multimedia.AVSession.Core 289 290**返回值:** 291 292| 类型 | 说明 | 293| --------------------------------------------- | ----------------------------- | 294| Promise<Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\>\> | Promise对象。返回播放列表队列。 | 295 296**错误码:** 297 298以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 299 300| 错误码ID | 错误信息 | 301| -------- | ---------------------------------------- | 302| 6600101 | Session service exception. | 303| 6600102 | The session does not exist. | 304| 6600103 | The session controller does not exist. | 305 306**示例:** 307 308```ts 309import { BusinessError } from '@kit.BasicServicesKit'; 310 311avsessionController.getAVQueueItems().then((items: avSession.AVQueueItem[]) => { 312 console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); 313}).catch((err: BusinessError) => { 314 console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 315}); 316``` 317 318## getAVQueueItems<sup>10+</sup> 319 320getAVQueueItems(callback: AsyncCallback\<Array\<AVQueueItem>>): void 321 322获取当前播放列表相关信息。结果通过callback异步回调方式返回。 323 324**系统能力:** SystemCapability.Multimedia.AVSession.Core 325 326**参数:** 327 328| 参数名 | 类型 | 必填 | 说明 | 329| -------- | --------------------------------------------------- | ---- | ------------------------- | 330| callback | AsyncCallback<Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\>\> | 是 | 回调函数,返回播放列表队列。 | 331 332**错误码:** 333 334以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 335 336| 错误码ID | 错误信息 | 337| -------- | ---------------------------------------- | 338| 6600101 | Session service exception. | 339| 6600102 | The session does not exist. | 340| 6600103 | The session controller does not exist. | 341 342**示例:** 343 344```ts 345import { BusinessError } from '@kit.BasicServicesKit'; 346 347avsessionController.getAVQueueItems((err: BusinessError, items: avSession.AVQueueItem[]) => { 348 if (err) { 349 console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 350 } else { 351 console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); 352 } 353}); 354``` 355 356## skipToQueueItem<sup>10+</sup> 357 358skipToQueueItem(itemId: number): Promise\<void> 359 360设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过Promise异步回调方式返回。 361 362**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 363 364**系统能力:** SystemCapability.Multimedia.AVSession.Core 365 366**参数:** 367 368| 参数名 | 类型 | 必填 | 说明 | 369| ------ | ------- | ---- | ------------------------------------------- | 370| itemId | number | 是 | 播放列表单项的ID值,用以表示选中的播放列表单项。 | 371 372**返回值:** 373 374| 类型 | 说明 | 375| -------------- | --------------------------------------------------------------- | 376| Promise\<void> | Promise对象。当播放列表单项ID设置成功,无返回结果,否则返回错误对象。 | 377 378**错误码:** 379 380以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 381 382| 错误码ID | 错误信息 | 383| -------- | ---------------------------------------- | 384| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 385| 6600101 | Session service exception. | 386| 6600102 | The session does not exist. | 387| 6600103 | The session controller does not exist. | 388 389**示例:** 390 391```ts 392import { BusinessError } from '@kit.BasicServicesKit'; 393 394let queueItemId = 0; 395avsessionController.skipToQueueItem(queueItemId).then(() => { 396 console.info('SkipToQueueItem successfully'); 397}).catch((err: BusinessError) => { 398 console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); 399}); 400``` 401 402## skipToQueueItem<sup>10+</sup> 403 404skipToQueueItem(itemId: number, callback: AsyncCallback\<void>): void 405 406设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过callback异步回调方式返回。 407 408**系统能力:** SystemCapability.Multimedia.AVSession.Core 409 410**参数:** 411 412| 参数名 | 类型 | 必填 | 说明 | 413| -------- | --------------------- | ---- | ----------------------------------------------------------- | 414| itemId | number | 是 | 播放列表单项的ID值,用以表示选中的播放列表单项。 | 415| callback | AsyncCallback\<void> | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | 416 417**错误码:** 418 419以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 420 421| 错误码ID | 错误信息 | 422| -------- | ---------------------------------------- | 423| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 424| 6600101 | Session service exception. | 425| 6600102 | The session does not exist. | 426| 6600103 | The session controller does not exist. | 427 428**示例:** 429 430```ts 431import { BusinessError } from '@kit.BasicServicesKit'; 432 433let queueItemId = 0; 434avsessionController.skipToQueueItem(queueItemId, (err: BusinessError) => { 435 if (err) { 436 console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); 437 } else { 438 console.info('SkipToQueueItem successfully'); 439 } 440}); 441``` 442 443## getOutputDevice<sup>10+</sup> 444 445getOutputDevice(): Promise\<OutputDeviceInfo> 446 447获取播放设备信息。结果通过Promise异步回调方式返回。 448 449**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 450 451**系统能力:** SystemCapability.Multimedia.AVSession.Core 452 453**返回值:** 454 455| 类型 | 说明 | 456| ----------------------------------------------- | --------------------------------- | 457| Promise<[OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10)\> | Promise对象,返回播放设备信息。 | 458 459**错误码:** 460 461以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 462 463| 错误码ID | 错误信息 | 464| -------- | ---------------------------------------- | 465| 600101 | Session service exception. | 466| 600103 | The session controller does not exist. | 467 468**示例:** 469 470```ts 471import { BusinessError } from '@kit.BasicServicesKit'; 472 473avsessionController.getOutputDevice().then((deviceInfo: avSession.OutputDeviceInfo) => { 474 console.info('GetOutputDevice : SUCCESS'); 475}).catch((err: BusinessError) => { 476 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 477}); 478``` 479 480## getOutputDevice<sup>10+</sup> 481 482getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void 483 484获取播放设备信息。结果通过callback异步回调方式返回。 485 486**系统能力:** SystemCapability.Multimedia.AVSession.Core 487 488**参数:** 489 490| 参数名 | 类型 | 必填 | 说明 | 491| -------- | ----------------------------------------------------- | ---- | ------------------------------ | 492| callback | AsyncCallback<[OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10)\> | 是 | 回调函数,返回播放设备信息。 | 493 494**错误码:** 495 496以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 497 498| 错误码ID | 错误信息 | 499| -------- | ---------------------------------------- | 500| 600101 | Session service exception. | 501| 600103 | The session controller does not exist. | 502 503**示例:** 504 505```ts 506import { BusinessError } from '@kit.BasicServicesKit'; 507 508avsessionController.getOutputDevice((err: BusinessError, deviceInfo: avSession.OutputDeviceInfo) => { 509 if (err) { 510 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 511 } else { 512 console.info('GetOutputDevice : SUCCESS'); 513 } 514}); 515``` 516 517## sendAVKeyEvent<sup>10+</sup> 518 519sendAVKeyEvent(event: KeyEvent): Promise\<void> 520 521发送按键事件到控制器对应的会话。结果通过Promise异步回调方式返回。 522 523**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 524 525**系统能力:** SystemCapability.Multimedia.AVSession.Core 526 527**参数:** 528 529| 参数名 | 类型 | 必填 | 说明 | 530| ------ | ------------------------------------------------------------ | ---- | ---------- | 531| event | [KeyEvent](../apis-input-kit/js-apis-keyevent.md) | 是 | 按键事件。 | 532 533**错误码:** 534 535以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 536 537| 错误码ID | 错误信息 | 538| -------- | ---------------------------------------- | 539| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 540| 600101 | Session service exception. | 541| 600102 | The session does not exist. | 542| 600103 | The session controller does not exist. | 543| 600105 | Invalid session command. | 544| 600106 | The session is not activated. | 545 546**返回值:** 547 548| 类型 | 说明 | 549| -------------- | ----------------------------- | 550| Promise\<void> | Promise对象。当事件发送成功,无返回结果,否则返回错误对象。 | 551 552**示例:** 553 554```ts 555import { Key, KeyEvent } from '@kit.InputKit'; 556import { BusinessError } from '@kit.BasicServicesKit'; 557 558let keyItem: Key = {code:0x49, pressedTime:2, deviceId:0}; 559let event:KeyEvent = {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}; 560 561 562avsessionController.sendAVKeyEvent(event).then(() => { 563 console.info('SendAVKeyEvent Successfully'); 564}).catch((err: BusinessError) => { 565 console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 566}); 567``` 568 569## sendAVKeyEvent<sup>10+</sup> 570 571sendAVKeyEvent(event: KeyEvent, callback: AsyncCallback\<void>): void 572 573发送按键事件到会话。结果通过callback异步回调方式返回。 574 575**系统能力:** SystemCapability.Multimedia.AVSession.Core 576 577**参数:** 578 579| 参数名 | 类型 | 必填 | 说明 | 580| -------- | ------------------------------------------------------------ | ---- | ---------- | 581| event | [KeyEvent](../apis-input-kit/js-apis-keyevent.md) | 是 | 按键事件。 | 582| callback | AsyncCallback\<void> | 是 | 回调函数。当事件发送成功,err为undefined,否则返回错误对象。 | 583 584**错误码:** 585 586以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 587 588| 错误码ID | 错误信息 | 589| -------- | ---------------------------------------- | 590| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 591| 600101 | Session service exception. | 592| 600102 | The session does not exist. | 593| 600103 | The session controller does not exist. | 594| 600105 | Invalid session command. | 595| 600106 | The session is not activated. | 596 597**示例:** 598 599```ts 600import { Key, KeyEvent } from '@kit.InputKit'; 601import { BusinessError } from '@kit.BasicServicesKit'; 602 603let keyItem: Key = {code:0x49, pressedTime:2, deviceId:0}; 604let event:KeyEvent = {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}; 605avsessionController.sendAVKeyEvent(event, (err: BusinessError) => { 606 if (err) { 607 console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 608 } else { 609 console.info('SendAVKeyEvent Successfully'); 610 } 611}); 612``` 613 614## getLaunchAbility<sup>10+</sup> 615 616getLaunchAbility(): Promise\<WantAgent> 617 618获取应用在会话中保存的WantAgent对象。结果通过Promise异步回调方式返回。 619 620**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 621 622**系统能力:** SystemCapability.Multimedia.AVSession.Core 623 624**返回值:** 625 626| 类型 | 说明 | 627| ------------------------------------------------------- | ------------------------------------------------------------ | 628| Promise<[WantAgent](../apis-ability-kit/js-apis-app-ability-wantAgent.md)\> | Promise对象,返回在[setLaunchAbility](arkts-apis-avsession-AVSession.md#setlaunchability10)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 | 629 630**错误码:** 631 632以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 633 634| 错误码ID | 错误信息 | 635| -------- | ---------------------------------------- | 636| 6600101 | Session service exception. | 637| 6600102 | The session does not exist. | 638| 6600103 | The session controller does not exist. | 639 640**示例:** 641 642```ts 643import { BusinessError } from '@kit.BasicServicesKit'; 644 645avsessionController.getLaunchAbility().then((agent: object) => { 646 console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); 647}).catch((err: BusinessError) => { 648 console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 649}); 650``` 651 652## getLaunchAbility<sup>10+</sup> 653 654getLaunchAbility(callback: AsyncCallback\<WantAgent>): void 655 656获取应用在会话中保存的WantAgent对象。结果通过callback异步回调方式返回。 657 658**系统能力:** SystemCapability.Multimedia.AVSession.Core 659 660**参数:** 661 662| 参数名 | 类型 | 必填 | 说明 | 663| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 664| callback | AsyncCallback<[WantAgent](../apis-ability-kit/js-apis-app-ability-wantAgent.md)\> | 是 | 回调函数。返回在[setLaunchAbility](arkts-apis-avsession-AVSession.md#setlaunchability10)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 | 665 666**错误码:** 667 668以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 669 670| 错误码ID | 错误信息 | 671| -------- | ---------------------------------------- | 672| 6600101 | Session service exception. | 673| 6600102 | The session does not exist. | 674| 6600103 | The session controller does not exist. | 675 676**示例:** 677 678```ts 679import { BusinessError } from '@kit.BasicServicesKit'; 680 681avsessionController.getLaunchAbility((err: BusinessError, agent: object) => { 682 if (err) { 683 console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 684 } else { 685 console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); 686 } 687}); 688``` 689 690## getRealPlaybackPositionSync<sup>10+</sup> 691 692getRealPlaybackPositionSync(): number 693 694获取当前播放位置。 695 696**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 697 698**系统能力:** SystemCapability.Multimedia.AVSession.Core 699 700**返回值:** 701 702| 类型 | 说明 | 703| ------ | ------------------ | 704| number | 时间节点,毫秒数。 | 705 706**错误码:** 707 708以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 709 710| 错误码ID | 错误信息 | 711| -------- | ---------------------------------------- | 712| 6600101 | Session service exception. | 713| 6600103 | The session controller does not exist. | 714 715**示例:** 716 717```ts 718let time: number = avsessionController.getRealPlaybackPositionSync(); 719``` 720 721## isActive<sup>10+</sup> 722 723isActive(): Promise\<boolean> 724 725获取会话是否被激活。结果通过Promise异步回调方式返回。 726 727**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 728 729**系统能力:** SystemCapability.Multimedia.AVSession.Core 730 731**返回值:** 732 733| 类型 | 说明 | 734| ----------------- | ------------------------------------------------------------ | 735| Promise<boolean\> | Promise对象,返回会话是否为激活状态,true表示被激活,false表示禁用。 | 736 737**错误码:** 738 739以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 740 741| 错误码ID | 错误信息 | 742| -------- | ---------------------------------------- | 743| 6600101 | Session service exception. | 744| 6600102 | The session does not exist. | 745| 6600103 | The session controller does not exist. | 746 747**示例:** 748 749```ts 750import { BusinessError } from '@kit.BasicServicesKit'; 751 752avsessionController.isActive().then((isActive: boolean) => { 753 console.info(`IsActive : SUCCESS : isactive : ${isActive}`); 754}).catch((err: BusinessError) => { 755 console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); 756}); 757``` 758 759## isActive<sup>10+</sup> 760 761isActive(callback: AsyncCallback\<boolean>): void 762 763判断会话是否被激活。结果通过callback异步回调方式返回。 764 765**系统能力:** SystemCapability.Multimedia.AVSession.Core 766 767**参数:** 768 769| 参数名 | 类型 | 必填 | 说明 | 770| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 771| callback | AsyncCallback<boolean\> | 是 | 回调函数。返回会话是否为激活状态,true表示被激活,false表示禁用。 | 772 773**错误码:** 774 775以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 776 777| 错误码ID | 错误信息 | 778| -------- | ---------------------------------------- | 779| 6600101 | Session service exception. | 780| 6600102 | The session does not exist. | 781| 6600103 | The session controller does not exist. | 782 783**示例:** 784 785```ts 786import { BusinessError } from '@kit.BasicServicesKit'; 787 788avsessionController.isActive((err: BusinessError, isActive: boolean) => { 789 if (err) { 790 console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); 791 } else { 792 console.info(`IsActive : SUCCESS : isactive : ${isActive}`); 793 } 794}); 795``` 796 797## destroy<sup>10+</sup> 798 799destroy(): Promise\<void> 800 801销毁当前控制器,销毁后当前控制器不可再用。结果通过Promise异步回调方式返回。 802 803**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 804 805**系统能力:** SystemCapability.Multimedia.AVSession.Core 806 807**返回值:** 808 809| 类型 | 说明 | 810| -------------- | ----------------------------- | 811| Promise\<void> | Promise对象。当控制器销毁成功,无返回结果,否则返回错误对象。 | 812 813**错误码:** 814 815以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 816 817| 错误码ID | 错误信息 | 818| -------- | ---------------------------------------- | 819| 6600101 | Session service exception. | 820| 6600103 | The session controller does not exist. | 821 822**示例:** 823 824```ts 825import { BusinessError } from '@kit.BasicServicesKit'; 826 827avsessionController.destroy().then(() => { 828 console.info('Destroy : SUCCESS '); 829}).catch((err: BusinessError) => { 830 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 831}); 832``` 833 834## destroy<sup>10+</sup> 835 836destroy(callback: AsyncCallback\<void>): void 837 838销毁当前控制器,销毁后当前控制器不可再用。结果通过callback异步回调方式返回。 839 840**系统能力:** SystemCapability.Multimedia.AVSession.Core 841 842**参数:** 843 844| 参数名 | 类型 | 必填 | 说明 | 845| -------- | -------------------- | ---- | ---------- | 846| callback | AsyncCallback\<void> | 是 | 回调函数。当控制器销毁成功,err为undefined,否则返回错误对象。 | 847 848**错误码:** 849 850以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 851 852| 错误码ID | 错误信息 | 853| -------- | ---------------------------------------- | 854| 6600101 | Session service exception. | 855| 6600103 | The session controller does not exist. | 856 857**示例:** 858 859```ts 860import { BusinessError } from '@kit.BasicServicesKit'; 861 862avsessionController.destroy((err: BusinessError) => { 863 if (err) { 864 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 865 } else { 866 console.info('Destroy : SUCCESS '); 867 } 868}); 869``` 870 871## getValidCommands<sup>10+</sup> 872 873getValidCommands(): Promise\<Array\<AVControlCommandType>> 874 875获取会话支持的有效命令。结果通过Promise异步回调方式返回。 876 877**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 878 879**系统能力:** SystemCapability.Multimedia.AVSession.Core 880 881**返回值:** 882 883| 类型 | 说明 | 884| ------------------------------------------------------------ | --------------------------------- | 885| Promise<Array<[AVControlCommandType](arkts-apis-avsession-t.md#avcontrolcommandtype10)\>\> | Promise对象。返回有效命令的集合。 | 886 887**错误码:** 888 889以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 890 891| 错误码ID | 错误信息 | 892| -------- | ---------------------------------------- | 893| 6600101 | Session service exception. | 894| 6600102 | The session does not exist. | 895| 6600103 | The session controller does not exist. | 896 897**示例:** 898 899```ts 900import { BusinessError } from '@kit.BasicServicesKit'; 901 902avsessionController.getValidCommands().then((validCommands: avSession.AVControlCommandType[]) => { 903 console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); 904}).catch((err: BusinessError) => { 905 console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 906}); 907``` 908 909## getValidCommands<sup>10+</sup> 910 911getValidCommands(callback: AsyncCallback\<Array\<AVControlCommandType>>): void 912 913获取会话支持的有效命令。结果通过callback异步回调方式返回。 914 915**系统能力:** SystemCapability.Multimedia.AVSession.Core 916 917**参数:** 918 919| 参数名 | 类型 | 必填 | 说明 | 920| -------- | ------------------------------------------------------------ | ---- | ------------------------------ | 921| callback | AsyncCallback\<Array\<[AVControlCommandType](arkts-apis-avsession-t.md#avcontrolcommandtype10)\>\> | 是 | 回调函数,返回有效命令的集合。 | 922 923**错误码:** 924 925以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 926 927| 错误码ID | 错误信息 | 928| -------- | ---------------------------------------- | 929| 6600101 | Session service exception. | 930| 6600102 | The session does not exist. | 931| 6600103 | The session controller does not exist. | 932 933**示例:** 934 935```ts 936import { BusinessError } from '@kit.BasicServicesKit'; 937 938avsessionController.getValidCommands((err: BusinessError, validCommands: avSession.AVControlCommandType[]) => { 939 if (err) { 940 console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 941 } else { 942 console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); 943 } 944}); 945``` 946 947## sendControlCommand<sup>10+</sup> 948 949sendControlCommand(command: AVControlCommand): Promise\<void> 950 951通过控制器发送命令到其对应的会话。结果通过Promise异步回调方式返回。 952 953> **说明:** 954> 955> 媒体控制方在使用sendControlCommand命令前,需要确保控制对应的媒体会话注册了对应的监听,注册媒体会话相关监听的方法请参见接口[on'play'](arkts-apis-avsession-AVSession.md#onplay10)、[on'pause'](arkts-apis-avsession-AVSession.md#onpause10)等。 956 957**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 958 959**系统能力:** SystemCapability.Multimedia.AVSession.Core 960 961**参数:** 962 963| 参数名 | 类型 | 必填 | 说明 | 964| ------- | ------------------------------------- | ---- | ------------------------------ | 965| command | [AVControlCommand](arkts-apis-avsession-i.md#avcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | 966 967**返回值:** 968 969| 类型 | 说明 | 970| -------------- | ----------------------------- | 971| Promise\<void> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | 972 973**错误码:** 974 975以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 976 977| 错误码ID | 错误信息 | 978| -------- | ---------------------------------------- | 979| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 980| 6600101 | Session service exception. | 981| 6600102 | The session does not exist. | 982| 6600103 | The session controller does not exist. | 983| 6600105 | Invalid session command. | 984| 6600106 | The session is not activated. | 985| 6600107 | Too many commands or events. | 986 987**示例:** 988 989```ts 990import { BusinessError } from '@kit.BasicServicesKit'; 991 992let avCommand: avSession.AVControlCommand = {command:'play'}; 993avsessionController.sendControlCommand(avCommand).then(() => { 994 console.info('SendControlCommand successfully'); 995}).catch((err: BusinessError) => { 996 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 997}); 998``` 999 1000## sendControlCommand<sup>10+</sup> 1001 1002sendControlCommand(command: AVControlCommand, callback: AsyncCallback\<void>): void 1003 1004通过会话控制器发送命令到其对应的会话。结果通过callback异步回调方式返回。 1005 1006> **说明:** 1007> 1008> 媒体控制方在使用sendControlCommand命令前,需要确保控制对应的媒体会话注册了对应的监听,注册媒体会话相关监听的方法请参见接口[on'play'](arkts-apis-avsession-AVSession.md#onplay10)、[on'pause'](arkts-apis-avsession-AVSession.md#onpause10)等。 1009 1010**系统能力:** SystemCapability.Multimedia.AVSession.Core 1011 1012**参数:** 1013 1014| 参数名 | 类型 | 必填 | 说明 | 1015| -------- | ------------------------------------- | ---- | ------------------------------ | 1016| command | [AVControlCommand](arkts-apis-avsession-i.md#avcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | 1017| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 1018 1019**错误码:** 1020 1021以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1022 1023| 错误码ID | 错误信息 | 1024| -------- | ------------------------------- | 1025| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 1026| 6600101 | Session service exception. | 1027| 6600102 | The session does not exist. | 1028| 6600103 | The session controller does not exist. | 1029| 6600105 | Invalid session command. | 1030| 6600106 | The session is not activated. | 1031| 6600107 | Too many commands or events. | 1032 1033**示例:** 1034 1035```ts 1036import { BusinessError } from '@kit.BasicServicesKit'; 1037 1038let avCommand: avSession.AVControlCommand = {command:'play'}; 1039avsessionController.sendControlCommand(avCommand, (err: BusinessError) => { 1040 if (err) { 1041 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 1042 } else { 1043 console.info('SendControlCommand successfully'); 1044 } 1045}); 1046``` 1047 1048## sendCommonCommand<sup>10+</sup> 1049 1050sendCommonCommand(command: string, args: {[key: string]: Object}): Promise\<void> 1051 1052通过会话控制器发送自定义控制命令到其对应的会话。结果通过Promise异步回调方式返回。 1053 1054**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1055 1056**系统能力:** SystemCapability.Multimedia.AVSession.Core 1057 1058**参数:** 1059 1060| 参数名 | 类型 | 必填 | 说明 | 1061| ------- | ------------------------------------- | ---- | ------------------------------ | 1062| command | string | 是 | 需要设置的自定义控制命令的名称。 | 1063| args | {[key: string]: Object} | 是 | 需要传递的控制命令键值对。 | 1064 1065> **说明:** 1066> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](../apis-ability-kit/js-apis-app-ability-want.md)。 1067 1068**返回值:** 1069 1070| 类型 | 说明 | 1071| -------------- | ----------------------------- | 1072| Promise\<void> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | 1073 1074**错误码:** 1075 1076以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1077 1078| 错误码ID | 错误信息 | 1079| -------- | ---------------------------------------- | 1080| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 1081| 6600101 | Session service exception. | 1082| 6600102 | The session does not exist. | 1083| 6600103 | The session controller does not exist. | 1084| 6600105 | Invalid session command. | 1085| 6600106 | The session is not activated. | 1086| 6600107 | Too many commands or events. | 1087 1088**示例:** 1089 1090```ts 1091import { BusinessError } from '@kit.BasicServicesKit'; 1092import { avSession } from '@kit.AVSessionKit'; 1093 1094let tag: string = "createNewSession"; 1095let sessionId: string = ""; 1096let controller:avSession.AVSessionController | undefined = undefined; 1097avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession)=> { 1098 currentAVSession = data; 1099 sessionId = currentAVSession.sessionId; 1100 controller = await currentAVSession.getController(); 1101 console.info(`CreateAVSession : SUCCESS :sessionId = ${sessionId}`); 1102}).catch((err: BusinessError) => { 1103 console.error(`CreateAVSession BusinessError:code: ${err.code}, message: ${err.message}`) 1104}); 1105let commandName = "my_command"; 1106if (controller !== undefined) { 1107 (controller as avSession.AVSessionController).sendCommonCommand(commandName, {command : "This is my command"}).then(() => { 1108 console.info('SendCommonCommand successfully'); 1109 }).catch((err: BusinessError) => { 1110 console.error(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); 1111 }) 1112} 1113``` 1114 1115## sendCommonCommand<sup>10+</sup> 1116 1117sendCommonCommand(command: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void 1118 1119通过会话控制器发送自定义命令到其对应的会话。结果通过callback异步回调方式返回。 1120 1121**系统能力:** SystemCapability.Multimedia.AVSession.Core 1122 1123**参数:** 1124 1125| 参数名 | 类型 | 必填 | 说明 | 1126| ------- | ------------------------------------- | ---- | ------------------------------ | 1127| command | string | 是 | 需要设置的自定义控制命令的名称。 | 1128| args | {[key: string]: Object} | 是 | 需要传递的控制命令键值对。 | 1129| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 1130 1131> **说明:** 1132> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](../apis-ability-kit/js-apis-app-ability-want.md)。 1133 1134**错误码:** 1135 1136以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1137 1138| 错误码ID | 错误信息 | 1139| -------- | ------------------------------- | 1140| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.| 1141| 6600101 | Session service exception. | 1142| 6600102 | The session does not exist. | 1143| 6600103 | The session controller does not exist. | 1144| 6600105 | Invalid session command. | 1145| 6600106 | The session is not activated. | 1146| 6600107 | Too many commands or events. | 1147 1148**示例:** 1149 1150```ts 1151import { BusinessError } from '@kit.BasicServicesKit'; 1152import { avSession } from '@kit.AVSessionKit'; 1153 1154let tag: string = "createNewSession"; 1155let sessionId: string = ""; 1156let controller:avSession.AVSessionController | undefined = undefined; 1157avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession)=> { 1158 currentAVSession = data; 1159 sessionId = currentAVSession.sessionId; 1160 controller = await currentAVSession.getController(); 1161 console.info(`CreateAVSession : SUCCESS :sessionId = ${sessionId}`); 1162}).catch((err: BusinessError) => { 1163 console.error(`CreateAVSession BusinessError:code: ${err.code}, message: ${err.message}`) 1164}); 1165let commandName = "my_command"; 1166if (controller !== undefined) { 1167 (controller as avSession.AVSessionController).sendCommonCommand(commandName, {command : "This is my command"}, (err: BusinessError) => { 1168 if (err) { 1169 console.error(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); 1170 } 1171 }) 1172} 1173``` 1174 1175## sendCustomData<sup>20+</sup> 1176 1177sendCustomData(data: Record\<string, Object>): Promise\<void> 1178 1179发送私有数据到远端设备。使用Promise异步回调。 1180 1181**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 1182 1183**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1184 1185**参数:** 1186 1187| 参数名 | 类型 | 必填 | 说明 | 1188| ------ | ---------------------- | ---- | ------------------------------------------------------------ | 1189| data | Record\<string, Object> | 是 | 应用程序填充的自定义数据。服务端仅解析key为'customData',且Object为string类型的对象。 | 1190 1191**返回值:** 1192 1193| 类型 | 说明 | 1194| -------------- | ----------------------------- | 1195| Promise\<void> | Promise对象,无返回结果。 | 1196 1197**错误码:** 1198 1199以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1200 1201| 错误码ID | 错误信息 | 1202| -------- | ------------------------------------------------------------ | 1203| 6600101 | Session service exception. | 1204| 6600102 | The session does not exist. | 1205| 6600103 | The session controller does not exist. | 1206 1207**示例:** 1208 1209```ts 1210import { BusinessError } from '@kit.BasicServicesKit'; 1211import { avSession } from '@kit.AVSessionKit'; 1212 1213let tag: string = "createNewSession"; 1214let sessionId: string = ""; 1215let controller:avSession.AVSessionController | undefined = undefined; 1216avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession) => { 1217 currentAVSession = data; 1218 sessionId = currentAVSession.sessionId; 1219 controller = await currentAVSession.getController(); 1220 console.info(`CreateAVSession : SUCCESS :sessionId = ${sessionId}`); 1221}).catch((err: BusinessError) => { 1222 console.error(`CreateAVSession BusinessError:code: ${err.code}, message: ${err.message}`) 1223}); 1224 1225if (controller !== undefined) { 1226 (controller as avSession.AVSessionController).sendCustomData({customData : "This is my data"}) 1227} 1228``` 1229 1230## getExtras<sup>10+</sup> 1231 1232getExtras(): Promise\<{[key: string]: Object}> 1233 1234获取媒体提供方设置的自定义媒体数据包。结果通过Promise异步回调方式返回。 1235 1236**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1237 1238**系统能力:** SystemCapability.Multimedia.AVSession.Core 1239 1240**返回值:** 1241 1242| 类型 | 说明 | 1243| ----------------------------------- | ----------------------------- | 1244| Promise<{[key: string]: Object}\> | Promise对象,返回媒体提供方设置的自定义媒体数据包,数据包的内容与setExtras设置的内容完全一致。 | 1245 1246**错误码:** 1247 1248以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1249 1250| 错误码ID | 错误信息 | 1251| -------- | ---------------------------------------- | 1252| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 1253| 6600101 | Session service exception. | 1254| 6600102 | The session does not exist. | 1255| 6600103 | The session controller does not exist. | 1256| 6600105 | Invalid session command. | 1257| 6600107 | Too many commands or events. | 1258 1259**示例:** 1260 1261```ts 1262import { BusinessError } from '@kit.BasicServicesKit'; 1263import { avSession } from '@kit.AVSessionKit'; 1264 1265let tag: string = "createNewSession"; 1266let sessionId: string = ""; 1267let controller:avSession.AVSessionController | undefined = undefined; 1268avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession)=> { 1269 currentAVSession = data; 1270 sessionId = currentAVSession.sessionId; 1271 controller = await currentAVSession.getController(); 1272 console.info(`CreateAVSession : SUCCESS :sessionId = ${sessionId}`); 1273}).catch((err: BusinessError) => { 1274 console.error(`CreateAVSession BusinessError:code: ${err.code}, message: ${err.message}`) 1275}); 1276if (controller !== undefined) { 1277 (controller as avSession.AVSessionController).getExtras().then((extras) => { 1278 console.info(`getExtras : SUCCESS : ${extras}`); 1279 }).catch((err: BusinessError) => { 1280 console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); 1281 }); 1282} 1283``` 1284 1285## getExtras<sup>10+</sup> 1286 1287getExtras(callback: AsyncCallback\<{[key: string]: Object}>): void 1288 1289获取媒体提供方设置的自定义媒体数据包,结果通过callback异步回调方式返回。 1290 1291**系统能力:** SystemCapability.Multimedia.AVSession.Core 1292 1293**参数:** 1294 1295| 参数名 | 类型 | 必填 | 说明 | 1296| -------- | ----------------------------------------- | ---- | -------------------------- | 1297| callback | AsyncCallback<{[key: string]: Object}\> | 是 | 回调函数,返回媒体提供方设置的自定义媒体数据包,数据包的内容与setExtras设置的内容完全一致。 | 1298 1299**错误码:** 1300 1301以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1302 1303| 错误码ID | 错误信息 | 1304| -------- | ---------------------------------------- | 1305| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 1306| 6600101 | Session service exception. | 1307| 6600102 | The session does not exist. | 1308| 6600103 | The session controller does not exist. | 1309| 6600105 | Invalid session command. | 1310| 6600107 |Too many commands or events. | 1311 1312**示例:** 1313 1314```ts 1315import { BusinessError } from '@kit.BasicServicesKit'; 1316import { avSession } from '@kit.AVSessionKit'; 1317 1318let tag: string = "createNewSession"; 1319let sessionId: string = ""; 1320let controller:avSession.AVSessionController | undefined = undefined; 1321avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession)=> { 1322 currentAVSession = data; 1323 sessionId = currentAVSession.sessionId; 1324 controller = await currentAVSession.getController(); 1325 console.info(`CreateAVSession : SUCCESS :sessionId = ${sessionId}`); 1326}).catch((err: BusinessError) => { 1327 console.error(`CreateAVSession BusinessError:code: ${err.code}, message: ${err.message}`) 1328}); 1329if (controller !== undefined) { 1330 (controller as avSession.AVSessionController).getExtras((err, extras) => { 1331 if (err) { 1332 console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); 1333 } else { 1334 console.info(`getExtras : SUCCESS : ${extras}`); 1335 } 1336 }); 1337} 1338``` 1339 1340## getExtrasWithEvent<sup>18+</sup> 1341 1342getExtrasWithEvent(extraEvent: string): Promise\<ExtraInfo> 1343 1344根据远端分布式事件类型,获取远端分布式媒体提供方设置的自定义媒体数据包。结果通过Promise异步回调方式返回。 1345 1346**系统能力:** SystemCapability.Multimedia.AVSession.Core 1347 1348**参数:** 1349 1350| 参数名 | 类型 | 必填 | 说明 | 1351| -------- | ----------------------------------------- | ---- | -------------------------- | 1352| extraEvent | string | 是 | 远端分布式事件类型。<br>当前支持的事件类型包括:<br>'AUDIO_GET_VOLUME':获取远端设备音量,<br>'AUDIO_GET_AVAILABLE_DEVICES':获取远端所有可连接设备,<br>'AUDIO_GET_PREFERRED_OUTPUT_DEVICE_FOR_RENDERER_INFO':获取远端实际发声设备。 | 1353 1354**返回值:** 1355 1356| 类型 | 说明 | 1357| ----------------------------------- | ----------------------------- | 1358| Promise<[ExtraInfo](arkts-apis-avsession-t.md#extrainfo18)\> | Promise对象,返回远端分布式媒体提供方设置的自定义媒体数据包。<br>参数ExtraInfo支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](../apis-ability-kit/js-apis-app-ability-want.md)。 | 1359 1360**错误码:** 1361 1362以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1363 1364| 错误码ID | 错误信息 | 1365| -------- | ---------------------------------------- | 1366| 6600101 | Session service exception. | 1367| 6600102 | The session does not exist. | 1368| 6600103 | The session controller does not exist. | 1369| 6600105 | Invalid session command. | 1370 1371**示例:** 1372 1373```ts 1374import { BusinessError } from '@kit.BasicServicesKit'; 1375 1376let controller: avSession.AVSessionController | ESObject; 1377const COMMON_COMMAND_STRING_1 = 'AUDIO_GET_VOLUME'; 1378const COMMON_COMMAND_STRING_2 = 'AUDIO_GET_AVAILABLE_DEVICES'; 1379const COMMON_COMMAND_STRING_3 = 'AUDIO_GET_PREFERRED_OUTPUT_DEVICE_FOR_RENDERER_INFO'; 1380if (controller !== undefined) { 1381 controller.getExtrasWithEvent(COMMON_COMMAND_STRING_1).then(() => { 1382 console.info(`${[COMMON_COMMAND_STRING_1]}`); 1383 }).catch((err: BusinessError) => { 1384 console.error(`getExtrasWithEvent failed with err: ${err.code}, ${err.message}`); 1385 }) 1386 1387 controller.getExtrasWithEvent(COMMON_COMMAND_STRING_2).then(() => { 1388 console.info(`${[COMMON_COMMAND_STRING_2]}`); 1389 }).catch((err: BusinessError) => { 1390 console.error(`getExtrasWithEvent failed with err: ${err.code}, ${err.message}`); 1391 }) 1392 1393 controller.getExtrasWithEvent(COMMON_COMMAND_STRING_3).then(() => { 1394 console.info(`${[COMMON_COMMAND_STRING_3]}`); 1395 }).catch((err: BusinessError) => { 1396 console.error(`getExtrasWithEvent failed with err: ${err.code}, ${err.message}`); 1397 }) 1398} 1399``` 1400 1401## on('metadataChange')<sup>10+</sup> 1402 1403on(type: 'metadataChange', filter: Array\<keyof AVMetadata> | 'all', callback: (data: AVMetadata) => void) 1404 1405设置元数据变化的监听事件。 1406 1407每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1408 1409**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1410 1411**系统能力:** SystemCapability.Multimedia.AVSession.Core 1412 1413**参数:** 1414 1415| 参数名 | 类型 | 必填 | 说明 | 1416| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1417| type | string | 是 | 事件回调类型,支持事件`'metadataChange'`:当元数据需要更新时,触发该事件。<br>需要更新表示对应属性值被重新设置过,不论新值与旧值是否相同。 | 1418| filter | Array\<keyof [AVMetadata](arkts-apis-avsession-i.md#avmetadata10)\> | 'all' | 是 | 'all'表示关注元数据所有字段更新。<br>Array<keyof [AVMetadata](arkts-apis-avsession-i.md#avmetadata10)\> 表示关注Array中的字段更新。 | 1419| callback | (data: [AVMetadata](arkts-apis-avsession-i.md#avmetadata10)) => void | 是 | 回调函数,参数data是需要更新的元数据。只包含需要更新的元数据属性,不代表当前全量的元数据。 | 1420 1421**错误码:** 1422 1423以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1424 1425| 错误码ID | 错误信息 | 1426| -------- | ------------------------------ | 1427| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1428| 6600101 | Session service exception. | 1429| 6600103 | The session controller does not exist. | 1430 1431**示例:** 1432 1433```ts 1434avsessionController.on('metadataChange', 'all', (metadata: avSession.AVMetadata) => { 1435 console.info(`on metadataChange assetId : ${metadata.assetId}`); 1436}); 1437 1438avsessionController.on('metadataChange', ['assetId', 'title', 'description'], (metadata: avSession.AVMetadata) => { 1439 console.info(`on metadataChange assetId : ${metadata.assetId}`); 1440}); 1441 1442``` 1443 1444## off('metadataChange')<sup>10+</sup> 1445 1446off(type: 'metadataChange', callback?: (data: AVMetadata) => void) 1447 1448取消元数据变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 1449 1450**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1451 1452**系统能力:** SystemCapability.Multimedia.AVSession.Core 1453 1454**参数:** 1455 1456| 参数名 | 类型 | 必填 | 说明 | 1457| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------ | 1458| type | string | 是 | 取消对应的监听事件,支持事件`'metadataChange'`。 | 1459| callback | (data: [AVMetadata](arkts-apis-avsession-i.md#avmetadata10)) => void | 否 | 回调函数,参数data是需要更新的元数据。只包含需要更新的元数据属性,并不代表当前全量的元数据。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 1460 1461**错误码:** 1462 1463以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1464 1465| 错误码ID | 错误信息 | 1466| -------- | ---------------- | 1467| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1468| 6600101 | Session service exception. | 1469| 6600103 | The session controller does not exist. | 1470 1471**示例:** 1472 1473```ts 1474avsessionController.off('metadataChange'); 1475``` 1476 1477## on('playbackStateChange')<sup>10+</sup> 1478 1479on(type: 'playbackStateChange', filter: Array\<keyof AVPlaybackState> | 'all', callback: (state: AVPlaybackState) => void) 1480 1481设置播放状态变化的监听事件。 1482 1483每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1484 1485**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1486 1487**系统能力:** SystemCapability.Multimedia.AVSession.Core 1488 1489**参数:** 1490 1491| 参数名 | 类型 | 必填 | 说明 | 1492| --------| -----------|-----|------------| 1493| type | string | 是 | 事件回调类型,支持事件`'playbackStateChange'`,当播放状态需要更新时,触发该事件。<br>需要更新表示对应属性值被重新设置过,不论新值与旧值是否相同。 | 1494| filter | Array\<keyof [AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10)\> | 'all' | 是 | 'all'表示关注播放状态所有字段更新。<br>Array<keyof [AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10)\> 表示关注Array中的字段更新。 | 1495| callback | (state: [AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10)) => void | 是 | 回调函数,参数state是需要更新的播放状态。只包含需要更新的播放状态属性,并不代表当前全量的播放状态。| 1496 1497**错误码:** 1498 1499以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1500 1501| 错误码ID | 错误信息 | 1502| -------- | ------------------------------ | 1503| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1504| 6600101 | Session service exception. | 1505| 6600103 | The session controller does not exist. | 1506 1507**示例:** 1508 1509```ts 1510avsessionController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { 1511 console.info(`on playbackStateChange state : ${playbackState.state}`); 1512}); 1513 1514avsessionController.on('playbackStateChange', ['state', 'speed', 'loopMode'], (playbackState: avSession.AVPlaybackState) => { 1515 console.info(`on playbackStateChange state : ${playbackState.state}`); 1516}); 1517``` 1518 1519## off('playbackStateChange')<sup>10+</sup> 1520 1521off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void) 1522 1523取消播放状态变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 1524 1525**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1526 1527**系统能力:** SystemCapability.Multimedia.AVSession.Core 1528 1529**参数:** 1530 1531| 参数名 | 类型 | 必填 | 说明 | 1532| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 1533| type | string | 是 | 取消对应的监听事件,支持事件`'playbackStateChange'`。 | 1534| callback | (state: [AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10)) => void | 否 | 回调函数,参数state是需要更新的播放状态。只包含需要更新的播放状态属性,并不代表当前全量的播放状态。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 1535 1536**错误码:** 1537 1538以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1539 1540| 错误码ID | 错误信息 | 1541| -------- | ---------------- | 1542| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1543| 6600101 | Session service exception. | 1544| 6600103 | The session controller does not exist. | 1545 1546**示例:** 1547 1548```ts 1549avsessionController.off('playbackStateChange'); 1550``` 1551 1552## on('callMetadataChange')<sup>11+</sup> 1553 1554on(type: 'callMetadataChange', filter: Array\<keyof CallMetadata> | 'all', callback: Callback\<CallMetadata>): void 1555 1556设置通话元数据变化的监听事件。 1557 1558每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1559 1560**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1561 1562**系统能力:** SystemCapability.Multimedia.AVSession.Core 1563 1564**参数:** 1565 1566| 参数名 | 类型 | 必填 | 说明 | 1567| --------| -----------|-----|------------| 1568| type | string | 是 | 事件回调类型,支持事件`'callMetadataChange'`:当通话元数据变化时,触发该事件。 | 1569| filter | Array\<keyof [CallMetadata](arkts-apis-avsession-i.md#callmetadata11)\> | 'all' | 是 | 'all' 表示关注通话元数据所有字段变化;Array<keyof [CallMetadata](arkts-apis-avsession-i.md#callmetadata11)\> 表示关注Array中的字段变化。 | 1570| callback | Callback<[CallMetadata](arkts-apis-avsession-i.md#callmetadata11)\> | 是 | 回调函数,参数callmetadata是变化后的通话元数据。| 1571 1572**错误码:** 1573 1574以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1575 1576| 错误码ID | 错误信息 | 1577| -------- | ------------------------------ | 1578| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1579| 6600101 | Session service exception. | 1580| 6600103 | The session controller does not exist. | 1581 1582**示例:** 1583 1584```ts 1585avsessionController.on('callMetadataChange', 'all', (callmetadata: avSession.CallMetadata) => { 1586 console.info(`on callMetadataChange state : ${callmetadata.name}`); 1587}); 1588 1589avsessionController.on('callMetadataChange', ['name'], (callmetadata: avSession.CallMetadata) => { 1590 console.info(`on callMetadataChange state : ${callmetadata.name}`); 1591}); 1592``` 1593 1594## off('callMetadataChange')<sup>11+</sup> 1595 1596off(type: 'callMetadataChange', callback?: Callback\<CallMetadata>): void 1597 1598取消设置通话元数据变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 1599 1600**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1601 1602**系统能力:** SystemCapability.Multimedia.AVSession.Core 1603 1604**参数:** 1605 1606| 参数名 | 类型 | 必填 | 说明 | 1607| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 1608| type | string | 是 | 取消对应的监听事件,支持事件`'callMetadataChange'`。 | 1609| callback | Callback<[CallMetadata](arkts-apis-avsession-i.md#callmetadata11)\> | 否 | 回调函数,参数calldata是变化后的通话原数据。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 1610 1611**错误码:** 1612 1613以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1614 1615| 错误码ID | 错误信息 | 1616| -------- | ---------------- | 1617| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1618| 6600101 | Session service exception. | 1619| 6600103 | The session controller does not exist. | 1620 1621**示例:** 1622 1623```ts 1624avsessionController.off('callMetadataChange'); 1625``` 1626 1627## on('callStateChange')<sup>11+</sup> 1628 1629on(type: 'callStateChange', filter: Array\<keyof AVCallState> | 'all', callback: Callback\<AVCallState>): void 1630 1631设置通话状态变化的监听事件。 1632 1633每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1634 1635**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1636 1637**系统能力:** SystemCapability.Multimedia.AVSession.Core 1638 1639**参数:** 1640 1641| 参数名 | 类型 | 必填 | 说明 | 1642| --------| -----------|-----|------------| 1643| type | string | 是 | 事件回调类型,支持事件`'callStateChange'`:当通话状态变化时,触发该事件。 | 1644| filter | Array<keyof [AVCallState](arkts-apis-avsession-i.md#avcallstate11)\> | 'all' | 是 | 'all' 表示关注通话状态所有字段变化;Array<keyof [AVCallState](arkts-apis-avsession-i.md#avcallstate11)\> 表示关注Array中的字段变化。 | 1645| callback | Callback<[AVCallState](arkts-apis-avsession-i.md#avcallstate11)\> | 是 | 回调函数,参数callstate是变化后的通话状态。| 1646 1647**错误码:** 1648 1649以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1650 1651| 错误码ID | 错误信息 | 1652| -------- | ------------------------------ | 1653| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1654| 6600101 | Session service exception. | 1655| 6600103 | The session controller does not exist. | 1656 1657**示例:** 1658 1659```ts 1660avsessionController.on('callStateChange', 'all', (callstate: avSession.AVCallState) => { 1661 console.info(`on callStateChange state : ${callstate.state}`); 1662}); 1663 1664avsessionController.on('callStateChange', ['state'], (callstate: avSession.AVCallState) => { 1665 console.info(`on callStateChange state : ${callstate.state}`); 1666}); 1667``` 1668 1669## off('callStateChange')<sup>11+</sup> 1670 1671off(type: 'callStateChange', callback?: Callback\<AVCallState>): void 1672 1673取消设置通话状态变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 1674 1675**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1676 1677**系统能力:** SystemCapability.Multimedia.AVSession.Core 1678 1679**参数:** 1680 1681| 参数名 | 类型 | 必填 | 说明 | 1682| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 1683| type | string | 是 | 取消对应的监听事件,支持事件`'callStateChange'`。 | 1684| callback | Callback<[AVCallState](arkts-apis-avsession-i.md#avcallstate11)\> | 否 | 回调函数,参数callstate是变化后的通话状态。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 1685 1686**错误码:** 1687 1688以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1689 1690| 错误码ID | 错误信息 | 1691| -------- | ---------------- | 1692| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1693| 6600101 | Session service exception. | 1694| 6600103 | The session controller does not exist. | 1695 1696**示例:** 1697 1698```ts 1699avsessionController.off('callMetadataChange'); 1700``` 1701 1702## on('sessionDestroy')<sup>10+</sup> 1703 1704on(type: 'sessionDestroy', callback: () => void) 1705 1706会话销毁的监听事件。 1707 1708每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1709 1710**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1711 1712**系统能力:** SystemCapability.Multimedia.AVSession.Core 1713 1714**参数:** 1715 1716| 参数名 | 类型 | 必填 | 说明 | 1717| -------- | ---------- | ---- | ------------------------------------------------------------ | 1718| type | string | 是 | 事件回调类型,支持事件`'sessionDestroy'`:当检测到会话销毁时,触发该事件)。 | 1719| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | 1720 1721**错误码:** 1722 1723以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1724 1725| 错误码ID | 错误信息 | 1726| -------- | ------------------------------ | 1727| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1728| 6600101 | Session service exception. | 1729| 6600103 | The session controller does not exist. | 1730 1731**示例:** 1732 1733```ts 1734avsessionController.on('sessionDestroy', () => { 1735 console.info('on sessionDestroy : SUCCESS '); 1736}); 1737``` 1738 1739## off('sessionDestroy')<sup>10+</sup> 1740 1741off(type: 'sessionDestroy', callback?: () => void) 1742 1743取消监听会话的销毁事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 1744 1745**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1746 1747**系统能力:** SystemCapability.Multimedia.AVSession.Core 1748 1749**参数:** 1750 1751| 参数名 | 类型 | 必填 | 说明 | 1752| -------- | ---------- | ---- | ----------------------------------------------------- | 1753| type | string | 是 | 取消对应的监听事件,支持事件`'sessionDestroy'`。 | 1754| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 1755 1756**错误码:** 1757 1758以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1759 1760| 错误码ID | 错误信息 | 1761| -------- | ---------------- | 1762| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1763| 6600101 | Session service exception. | 1764| 6600103 | The session controller does not exist. | 1765 1766**示例:** 1767 1768```ts 1769avsessionController.off('sessionDestroy'); 1770``` 1771 1772## on('activeStateChange')<sup>10+</sup> 1773 1774on(type: 'activeStateChange', callback: (isActive: boolean) => void) 1775 1776会话的激活状态的监听事件。 1777 1778每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1779 1780**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1781 1782**系统能力:** SystemCapability.Multimedia.AVSession.Core 1783 1784**参数:** 1785 1786| 参数名 | 类型 | 必填 | 说明 | 1787| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 1788| type | string | 是 | 事件回调类型,支持事件`'activeStateChange'`:当检测到会话的激活状态发生改变时,触发该事件。 | 1789| callback | (isActive: boolean) => void | 是 | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。 | 1790 1791**错误码:** 1792 1793以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1794 1795| 错误码ID | 错误信息 | 1796| -------- | ----------------------------- | 1797| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1798| 6600101 | Session service exception. | 1799| 6600103 |The session controller does not exist. | 1800 1801**示例:** 1802 1803```ts 1804avsessionController.on('activeStateChange', (isActive: boolean) => { 1805 console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`); 1806}); 1807``` 1808 1809## off('activeStateChange')<sup>10+</sup> 1810 1811off(type: 'activeStateChange', callback?: (isActive: boolean) => void) 1812 1813取消监听会话激活状态变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 1814 1815**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1816 1817**系统能力:** SystemCapability.Multimedia.AVSession.Core 1818 1819**参数:** 1820 1821| 参数名 | 类型 | 必填 | 说明 | 1822| -------- | --------------------------- | ---- | ----------------------------------------------------- | 1823| type | string | 是 | 取消对应的监听事件,支持事件`'activeStateChange'`。 | 1824| callback | (isActive: boolean) => void | 否 | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 1825 1826**错误码:** 1827 1828以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1829 1830| 错误码ID | 错误信息 | 1831| -------- | ---------------- | 1832| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1833| 6600101 | Session service exception. | 1834| 6600103 | The session controller does not exist. | 1835 1836**示例:** 1837 1838```ts 1839avsessionController.off('activeStateChange'); 1840``` 1841 1842## on('validCommandChange')<sup>10+</sup> 1843 1844on(type: 'validCommandChange', callback: (commands: Array\<AVControlCommandType>) => void) 1845 1846会话支持的有效命令变化监听事件。 1847 1848每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1849 1850**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1851 1852**系统能力:** SystemCapability.Multimedia.AVSession.Core 1853 1854**参数:** 1855 1856| 参数名 | 类型 | 必填 | 说明 | 1857| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1858| type | string | 是 | 事件回调类型,支持事件`'validCommandChange'`:当检测到会话的合法命令发生改变时,触发该事件。 | 1859| callback | (commands: Array<[AVControlCommandType](arkts-apis-avsession-t.md#avcontrolcommandtype10)\>) => void | 是 | 回调函数。参数commands是有效命令的集合。 | 1860 1861**错误码:** 1862 1863以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1864 1865| 错误码ID | 错误信息 | 1866| -------- | ------------------------------ | 1867| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1868| 6600101 | Session service exception. | 1869| 6600103 | The session controller does not exist. | 1870 1871**示例:** 1872 1873```ts 1874avsessionController.on('validCommandChange', (validCommands: avSession.AVControlCommandType[]) => { 1875 console.info(`validCommandChange : SUCCESS : size : ${validCommands.length}`); 1876 console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); 1877}); 1878``` 1879 1880## off('validCommandChange')<sup>10+</sup> 1881 1882off(type: 'validCommandChange', callback?: (commands: Array\<AVControlCommandType>) => void) 1883 1884取消监听会话有效命令变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 1885 1886**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1887 1888**系统能力:** SystemCapability.Multimedia.AVSession.Core 1889 1890**参数:** 1891 1892| 参数名 | 类型 | 必填 | 说明 | 1893| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | 1894| type | string | 是 | 取消对应的监听事件,支持事件`'validCommandChange'`。 | 1895| callback | (commands: Array<[AVControlCommandType](arkts-apis-avsession-t.md#avcontrolcommandtype10)\>) => void | 否 | 回调函数。参数commands是有效命令的集合。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 1896 1897**错误码:** 1898 1899以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1900 1901| 错误码ID | 错误信息 | 1902| -------- | ---------------- | 1903| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1904| 6600101 | Session service exception. | 1905| 6600103 | The session controller does not exist. | 1906 1907**示例:** 1908 1909```ts 1910avsessionController.off('validCommandChange'); 1911``` 1912 1913## on('outputDeviceChange')<sup>10+</sup> 1914 1915on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void 1916 1917设置播放设备变化的监听事件。 1918 1919每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1920 1921**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1922 1923**系统能力:** SystemCapability.Multimedia.AVSession.Core 1924 1925**参数:** 1926 1927| 参数名 | 类型 | 必填 | 说明 | 1928| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 1929| type | string | 是 | 事件回调类型,支持事件为`'outputDeviceChange'`:当播放设备变化时,触发该事件)。 | 1930| callback | (state: [ConnectionState](arkts-apis-avsession-e.md#connectionstate10), device: [OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10)) => void | 是 | 回调函数,参数device是设备相关信息。 | 1931 1932**错误码:** 1933 1934以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1935 1936| 错误码ID | 错误信息 | 1937| -------- | ----------------------- | 1938| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1939| 6600101 | Session service exception. | 1940| 6600103 | The session controller does not exist. | 1941 1942**示例:** 1943 1944```ts 1945avsessionController.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { 1946 console.info(`on outputDeviceChange state: ${state}, device : ${device}`); 1947}); 1948``` 1949 1950## off('outputDeviceChange')<sup>10+</sup> 1951 1952off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void 1953 1954取消监听分布式设备变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 1955 1956**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1957 1958**系统能力:** SystemCapability.Multimedia.AVSession.Core 1959 1960**参数:** 1961 1962| 参数名 | 类型 | 必填 | 说明 | 1963| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | 1964| type | string | 是 | 取消对应的监听事件,支持事件`'outputDeviceChange'`。 | 1965| callback | (state: [ConnectionState](arkts-apis-avsession-e.md#connectionstate10), device: [OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10)) => void | 否 | 回调函数,参数device是设备相关信息。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 1966 1967**错误码:** 1968 1969以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 1970 1971| 错误码ID | 错误信息 | 1972| -------- | ---------------- | 1973| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1974| 6600101 | Session service exception. | 1975| 6600103 | The session controller does not exist. | 1976 1977**示例:** 1978 1979```ts 1980avsessionController.off('outputDeviceChange'); 1981``` 1982 1983## on('sessionEvent')<sup>10+</sup> 1984 1985on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key: string]: Object}) => void): void 1986 1987媒体控制器设置会话自定义事件变化的监听器。 1988 1989每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 1990 1991**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1992 1993**系统能力:** SystemCapability.Multimedia.AVSession.Core 1994 1995**参数:** 1996 1997| 参数名 | 类型 | 必填 | 说明 | 1998| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1999| type | string | 是 | 事件回调类型,支持事件`'sessionEvent'`:当会话事件变化时,触发该事件。 | 2000| callback | (sessionEvent: string, args: {[key: string]: Object}) => void | 是 | 回调函数,sessionEvent为变化的会话事件名,args为事件的参数。 | 2001 2002**错误码:** 2003 2004以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2005 2006| 错误码ID | 错误信息 | 2007| -------- | ------------------------------ | 2008| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2009| 6600101 | Session service exception. | 2010| 6600103 | The session controller does not exist. | 2011 2012**示例:** 2013 2014```ts 2015import { BusinessError } from '@kit.BasicServicesKit'; 2016import { avSession } from '@kit.AVSessionKit'; 2017 2018let tag: string = "createNewSession"; 2019let sessionId: string = ""; 2020let controller:avSession.AVSessionController | undefined = undefined; 2021avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession)=> { 2022 currentAVSession = data; 2023 sessionId = currentAVSession.sessionId; 2024 controller = await currentAVSession.getController(); 2025 console.info('CreateAVSession : SUCCESS :sessionid = ${sessionid}'); 2026}).catch((err: BusinessError) => { 2027 console.error('CreateAVSession BusinessError:code: ${err.code}, message: ${err.message}') 2028}); 2029if (controller !== undefined) { 2030 (controller as avSession.AVSessionController).on('sessionEvent', (sessionEvent, args) => { 2031 console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`); 2032 }); 2033} 2034``` 2035 2036## off('sessionEvent')<sup>10+</sup> 2037 2038off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key: string]: Object}) => void): void 2039 2040取消会话事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 2041 2042**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2043 2044**系统能力:** SystemCapability.Multimedia.AVSession.Core 2045 2046**参数:** 2047 2048| 参数名 | 类型 | 必填 | 说明 | 2049| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 2050| type | string | 是 | 取消对应的监听事件,支持事件`'sessionEvent'`。 | 2051| callback | (sessionEvent: string, args: {[key: string]: Object}) => void | 否 | 回调函数,参数sessionEvent是变化的事件名,args为事件的参数。<br>该参数为可选参数,若不填写该参数,则认为取消所有对sessionEvent事件的监听。 | 2052 2053**错误码:** 2054 2055以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2056 2057| 错误码ID | 错误信息 | 2058| -------- | ---------------- | 2059| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2060| 6600101 | Session service exception. | 2061| 6600103 | The session controller does not exist. | 2062 2063**示例:** 2064 2065```ts 2066avsessionController.off('sessionEvent'); 2067``` 2068 2069## on('queueItemsChange')<sup>10+</sup> 2070 2071on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\>) => void): void 2072 2073媒体控制器设置会话自定义播放列表变化的监听器。 2074 2075每个指令支持注册多个回调,如果需要只执行最新监听,需要先注销旧的监听,否则新旧监听都会触发回调。 2076 2077**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2078 2079**系统能力:** SystemCapability.Multimedia.AVSession.Core 2080 2081**参数:** 2082 2083| 参数名 | 类型 | 必填 | 说明 | 2084| -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------------------------------- | 2085| type | string | 是 | 事件回调类型,支持事件`'queueItemsChange'`:当session修改播放列表时,触发该事件。 | 2086| callback | (items: Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\>) => void | 是 | 回调函数,items为变化的播放列表。 | 2087 2088**错误码:** 2089 2090以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2091 2092| 错误码ID | 错误信息 | 2093| -------- | ------------------------------ | 2094| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2095| 6600101 | Session service exception. | 2096| 6600103 | The session controller does not exist. | 2097 2098**示例:** 2099 2100```ts 2101avsessionController.on('queueItemsChange', (items: avSession.AVQueueItem[]) => { 2102 console.info(`OnQueueItemsChange, items length is ${items.length}`); 2103}); 2104``` 2105 2106## off('queueItemsChange')<sup>10+</sup> 2107 2108off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\>) => void): void 2109 2110取消播放列表变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 2111 2112**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2113 2114**系统能力:** SystemCapability.Multimedia.AVSession.Core 2115 2116**参数:** 2117 2118| 参数名 | 类型 | 必填 | 说明 | 2119| -------- | ---------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------- | 2120| type | string | 是 | 取消对应的监听事件,支持事件`'queueItemsChange'`。 | 2121| callback | (items: Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\>) => void | 否 | 回调函数,参数items是变化的播放列表。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 2122 2123**错误码:** 2124 2125以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2126 2127| 错误码ID | 错误信息 | 2128| -------- | ---------------- | 2129| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2130| 6600101 | Session service exception. | 2131| 6600103 | The session controller does not exist. | 2132 2133**示例:** 2134 2135```ts 2136avsessionController.off('queueItemsChange'); 2137``` 2138 2139## on('queueTitleChange')<sup>10+</sup> 2140 2141on(type: 'queueTitleChange', callback: (title: string) => void): void 2142 2143媒体控制器设置会话自定义播放列表的名称变化的监听器。 2144 2145**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2146 2147**系统能力:** SystemCapability.Multimedia.AVSession.Core 2148 2149**参数:** 2150 2151| 参数名 | 类型 | 必填 | 说明 | 2152| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------- | 2153| type | string | 是 | 事件回调类型,支持事件`'queueTitleChange'`:当session修改播放列表名称时,触发该事件。 | 2154| callback | (title: string) => void | 是 | 回调函数,title为变化的播放列表名称。 | 2155 2156**错误码:** 2157 2158以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2159 2160| 错误码ID | 错误信息 | 2161| -------- | ------------------------------ | 2162| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2163| 6600101 | Session service exception. | 2164| 6600103 | The session controller does not exist. | 2165 2166**示例:** 2167 2168```ts 2169avsessionController.on('queueTitleChange', (title: string) => { 2170 console.info(`queueTitleChange, title is ${title}`); 2171}); 2172``` 2173 2174## off('queueTitleChange')<sup>10+</sup> 2175 2176off(type: 'queueTitleChange', callback?: (title: string) => void): void 2177 2178取消播放列表名称变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 2179 2180**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2181 2182**系统能力:** SystemCapability.Multimedia.AVSession.Core 2183 2184**参数:** 2185 2186| 参数名 | 类型 | 必填 | 说明 | 2187| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | 2188| type | string | 是 | 取消对应的监听事件,支持事件`'queueTitleChange'`。 | 2189| callback | (title: string) => void | 否 | 回调函数,参数items是变化的播放列表名称。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 2190 2191**错误码:** 2192 2193以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2194 2195| 错误码ID | 错误信息 | 2196| -------- | ---------------- | 2197| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2198| 6600101 | Session service exception. | 2199| 6600103 | The session controller does not exist. | 2200 2201**示例:** 2202 2203```ts 2204avsessionController.off('queueTitleChange'); 2205``` 2206 2207## on('extrasChange')<sup>10+</sup> 2208 2209on(type: 'extrasChange', callback: (extras: {[key: string]: Object}) => void): void 2210 2211媒体控制器设置自定义媒体数据包事件变化的监听器。 2212 2213**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2214 2215**系统能力:** SystemCapability.Multimedia.AVSession.Core 2216 2217**参数:** 2218 2219| 参数名 | 类型 | 必填 | 说明 | 2220| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2221| type | string | 是 | 事件回调类型,支持事件`'extrasChange'`:当媒体提供方设置自定义媒体数据包时,触发该事件。 | 2222| callback | (extras: {[key: string]: Object}) => void | 是 | 回调函数,extras为媒体提供方新设置的自定义媒体数据包,该自定义媒体数据包与dispatchSessionEvent方法设置的数据包完全一致。 | 2223 2224**错误码:** 2225 2226以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2227 2228| 错误码ID | 错误信息 | 2229| -------- | ------------------------------ | 2230| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2231| 6600101 | Session service exception. | 2232| 6600103 | The session controller does not exist. | 2233 2234**示例:** 2235 2236```ts 2237import { BusinessError } from '@kit.BasicServicesKit'; 2238import { avSession } from '@kit.AVSessionKit'; 2239 2240let tag: string = "createNewSession"; 2241let sessionId: string = ""; 2242let controller:avSession.AVSessionController | undefined = undefined; 2243avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession)=> { 2244 currentAVSession = data; 2245 sessionId = currentAVSession.sessionId; 2246 controller = await currentAVSession.getController(); 2247 console.info(`CreateAVSession : SUCCESS :sessionId = ${sessionId}`); 2248}).catch((err: BusinessError) => { 2249 console.error(`CreateAVSession BusinessError:code: ${err.code}, message: ${err.message}`) 2250}); 2251if (controller !== undefined) { 2252 (controller as avSession.AVSessionController).on('extrasChange', (extras) => { 2253 console.info(`Caught extrasChange event,the new extra is: ${JSON.stringify(extras)}`); 2254 }); 2255} 2256``` 2257 2258## off('extrasChange')<sup>10+</sup> 2259 2260off(type: 'extrasChange', callback?: (extras: {[key: string]: Object}) => void): void 2261 2262取消自定义媒体数据包变化事件监听。指定callback,可取消对应监听;未指定callback,取消所有事件监听。 2263 2264**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2265 2266**系统能力:** SystemCapability.Multimedia.AVSession.Core 2267 2268**参数:** 2269 2270| 参数名 | 类型 | 必填 | 说明 | 2271| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | 2272| type | string | 是 | 取消对应的监听事件,支持事件`'extrasChange'`。 | 2273| callback | (extras: {[key: string]: Object}) => void | 否 | 注册监听事件时的回调函数。<br>该参数为可选参数,若不填写该参数,则认为取消会话所有与此事件相关的监听。 | 2274 2275**错误码:** 2276 2277以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2278 2279| 错误码ID | 错误信息 | 2280| -------- | ---------------- | 2281| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2282| 6600101 | Session service exception. | 2283| 6600103 | The session controller does not exist. | 2284 2285**示例:** 2286 2287```ts 2288avsessionController.off('extrasChange'); 2289``` 2290 2291## on('customDataChange')<sup>20+</sup> 2292 2293on(type: 'customDataChange', callback: Callback\<Record\<string, Object>>): void 2294 2295注册从远程设备发送的自定义数据的监听器。 2296 2297**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 2298 2299**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 2300 2301**参数:** 2302 2303| 参数名 | 类型 | 必填 | 说明 | 2304| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 2305| type | string | 是 | 事件回调类型,支持事件'customDataChange',当媒体提供方发送自定义数据时,触发该事件。 | 2306| callback | Callback\<Record\<string, Object>> | 是 | 回调函数,用于接收自定义数据。 | 2307 2308**错误码:** 2309 2310以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2311 2312| 错误码ID | 错误信息 | 2313| -------- | ------------------------------------------------------------ | 2314| 6600101 | Session service exception. | 2315| 6600103 | The session controller does not exist. | 2316 2317**示例:** 2318 2319```ts 2320import { BusinessError } from '@kit.BasicServicesKit'; 2321import { avSession } from '@kit.AVSessionKit'; 2322 2323let tag: string = "createNewSession"; 2324let sessionId: string = ""; 2325let controller:avSession.AVSessionController | undefined = undefined; 2326avSession.createAVSession(context, tag, "audio").then(async (data:avSession.AVSession)=> { 2327 currentAVSession = data; 2328 sessionId = currentAVSession.sessionId; 2329 controller = await currentAVSession.getController(); 2330 console.info('CreateAVSession : SUCCESS :sessionid = ${sessionid}'); 2331}).catch((err: BusinessError) => { 2332 console.error('CreateAVSession BusinessError:code: ${err.code}, message: ${err.message}') 2333}); 2334if (controller !== undefined) { 2335 (controller as avSession.AVSessionController).on('customDataChange', (callback) => { 2336 console.info(`Caught customDataChange event,the new callback is: ${JSON.stringify(callback)}`); 2337 }); 2338} 2339``` 2340 2341## off('customDataChange')<sup>20+</sup> 2342 2343off(type: 'customDataChange', callback?: Callback\<Record\<string, Object>>): void 2344 2345取消自定义数据监听。 2346 2347**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 2348 2349**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 2350 2351**参数:** 2352 2353| 参数名 | 类型 | 必填 | 说明 | 2354| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 2355| type | string | 是 | 取消对应的监听事件,支持的事件是'customDataChange'。 | 2356| callback | Callback\<Record\<string, Object>> | 否 | 注册监听事件时的回调函数。该参数为可选参数,若不填写该参数,则认为取消会话所有与此事件相关的监听。 | 2357 2358**错误码:** 2359 2360以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2361 2362| 错误码ID | 错误信息 | 2363| -------- | ------------------------------------------------------------ | 2364| 6600101 | Session service exception. | 2365| 6600103 | The session controller does not exist. | 2366 2367**示例:** 2368 2369```ts 2370avsessionController.off('customDataChange'); 2371``` 2372 2373## getAVPlaybackStateSync<sup>10+</sup> 2374 2375getAVPlaybackStateSync(): AVPlaybackState; 2376 2377使用同步方法获取当前会话的播放状态。 2378 2379**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2380 2381**系统能力:** SystemCapability.Multimedia.AVSession.Core 2382 2383**返回值:** 2384 2385| 类型 | 说明 | 2386| --------- | ------------------------------------------------------------ | 2387| [AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10) | 当前会话的播放状态。 | 2388 2389**错误码:** 2390 2391以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2392 2393| 错误码ID | 错误信息 | 2394| -------- | ---------------------------------------- | 2395| 6600101 | Session service exception. | 2396| 6600102 | The session does not exist. | 2397| 6600103 | The session controller does not exist. | 2398 2399**示例:** 2400 2401```ts 2402import { BusinessError } from '@kit.BasicServicesKit'; 2403 2404try { 2405 let playbackState: avSession.AVPlaybackState = avsessionController.getAVPlaybackStateSync(); 2406} catch (err) { 2407 let error = err as BusinessError; 2408 console.error(`getAVPlaybackStateSync error, error code: ${error.code}, error message: ${error.message}`); 2409} 2410``` 2411 2412## getAVMetadataSync<sup>10+</sup> 2413 2414getAVMetadataSync(): AVMetadata 2415 2416使用同步方法获取会话元数据。 2417 2418**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2419 2420**系统能力:** SystemCapability.Multimedia.AVSession.Core 2421 2422**返回值:** 2423 2424| 类型 | 说明 | 2425| ----------------------------------- | ----------------------------- | 2426| [AVMetadata](arkts-apis-avsession-i.md#avmetadata10) | 会话元数据。 | 2427 2428**错误码:** 2429 2430以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2431 2432| 错误码ID | 错误信息 | 2433| -------- | ---------------------------------------- | 2434| 6600101 | Session service exception. | 2435| 6600102 | The session does not exist. | 2436| 6600103 | The session controller does not exist. | 2437 2438**示例:** 2439```ts 2440import { BusinessError } from '@kit.BasicServicesKit'; 2441 2442try { 2443 let metaData: avSession.AVMetadata = avsessionController.getAVMetadataSync(); 2444} catch (err) { 2445 let error = err as BusinessError; 2446 console.error(`getAVMetadataSync error, error code: ${error.code}, error message: ${error.message}`); 2447} 2448``` 2449 2450## getAVCallState<sup>11+</sup> 2451 2452getAVCallState(): Promise\<AVCallState> 2453 2454获取通话状态数据。结果通过Promise异步回调方式返回。 2455 2456**系统能力:** SystemCapability.Multimedia.AVSession.Core 2457 2458**返回值:** 2459 2460| 类型 | 说明 | 2461| ----------------------------------- | ----------------------------- | 2462| Promise<[AVCallState](arkts-apis-avsession-i.md#avcallstate11)\> | Promise对象,返回通话状态。 | 2463 2464**错误码:** 2465 2466以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2467 2468| 错误码ID | 错误信息 | 2469| -------- | ---------------------------------------- | 2470| 6600101 | Session service exception. | 2471| 6600102 | The session does not exist. | 2472| 6600103 | The session controller does not exist. | 2473 2474**示例:** 2475 2476```ts 2477import { BusinessError } from '@kit.BasicServicesKit'; 2478 2479avsessionController.getAVCallState().then((callstate: avSession.AVCallState) => { 2480 console.info(`getAVCallState : SUCCESS : state : ${callstate.state}`); 2481}).catch((err: BusinessError) => { 2482 console.error(`getAVCallState BusinessError: code: ${err.code}, message: ${err.message}`); 2483}); 2484``` 2485 2486## getAVCallState<sup>11+</sup> 2487 2488getAVCallState(callback: AsyncCallback\<AVCallState>): void 2489 2490获取通话状态数据。结果通过callback异步回调方式返回。 2491 2492**系统能力:** SystemCapability.Multimedia.AVSession.Core 2493 2494**参数:** 2495 2496| 参数名 | 类型 | 必填 | 说明 | 2497| -------- | ----------------------------------------- | ---- | -------------------------- | 2498| callback | AsyncCallback<[AVCallState](arkts-apis-avsession-i.md#avcallstate11)\> | 是 | 回调函数,返回通话状态。 | 2499 2500**错误码:** 2501 2502以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2503 2504| 错误码ID | 错误信息 | 2505| -------- | ---------------------------------------- | 2506| 6600101 | Session service exception. | 2507| 6600102 | The session does not exist. | 2508| 6600103 | The session controller does not exist. | 2509 2510**示例:** 2511 2512```ts 2513import { BusinessError } from '@kit.BasicServicesKit'; 2514 2515avsessionController.getAVCallState((err: BusinessError, callstate: avSession.AVCallState) => { 2516 if (err) { 2517 console.error(`getAVCallState BusinessError: code: ${err.code}, message: ${err.message}`); 2518 } else { 2519 console.info(`getAVCallState : SUCCESS : state : ${callstate.state}`); 2520 } 2521}); 2522``` 2523 2524## getCallMetadata<sup>11+</sup> 2525 2526getCallMetadata(): Promise\<CallMetadata> 2527 2528获取通话会话的元数据。结果通过Promise异步回调方式返回。 2529 2530**系统能力:** SystemCapability.Multimedia.AVSession.Core 2531 2532**返回值:** 2533 2534| 类型 | 说明 | 2535| ----------------------------------- | ----------------------------- | 2536| Promise<[CallMetadata](arkts-apis-avsession-i.md#callmetadata11)\> | Promise对象,返回会话元数据。 | 2537 2538**错误码:** 2539 2540以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2541 2542| 错误码ID | 错误信息 | 2543| -------- | ---------------------------------------- | 2544| 6600101 | Session service exception. | 2545| 6600102 | The session does not exist. | 2546| 6600103 | The session controller does not exist. | 2547 2548**示例:** 2549 2550```ts 2551import { BusinessError } from '@kit.BasicServicesKit'; 2552 2553avsessionController.getCallMetadata().then((calldata: avSession.CallMetadata) => { 2554 console.info(`getCallMetadata : SUCCESS : name : ${calldata.name}`); 2555}).catch((err: BusinessError) => { 2556 console.error(`getCallMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 2557}); 2558``` 2559 2560## getCallMetadata<sup>11+</sup> 2561 2562getCallMetadata(callback: AsyncCallback\<CallMetadata>): void 2563 2564获取通话会话的元数据。结果通过callback异步回调方式返回。 2565 2566**系统能力:** SystemCapability.Multimedia.AVSession.Core 2567 2568**参数:** 2569 2570| 参数名 | 类型 | 必填 | 说明 | 2571| -------- | ----------------------------------------- | ---- | -------------------------- | 2572| callback | AsyncCallback<[CallMetadata](arkts-apis-avsession-i.md#callmetadata11)\> | 是 | 回调函数,返回会话元数据。 | 2573 2574**错误码:** 2575 2576以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2577 2578| 错误码ID | 错误信息 | 2579| -------- | ---------------------------------------- | 2580| 6600101 | Session service exception. | 2581| 6600102 | The session does not exist. | 2582| 6600103 | The session controller does not exist. | 2583 2584**示例:** 2585 2586```ts 2587import { BusinessError } from '@kit.BasicServicesKit'; 2588 2589avsessionController.getCallMetadata((err: BusinessError, calldata: avSession.CallMetadata) => { 2590 if (err) { 2591 console.error(`getCallMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 2592 } else { 2593 console.info(`getCallMetadata : SUCCESS : name : ${calldata.name}`); 2594 } 2595}); 2596``` 2597 2598## getAVQueueTitleSync<sup>10+</sup> 2599 2600getAVQueueTitleSync(): string 2601 2602使用同步方法获取当前会话播放列表的名称。 2603 2604**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2605 2606**系统能力:** SystemCapability.Multimedia.AVSession.Core 2607 2608**返回值:** 2609 2610| 类型 | 说明 | 2611| ---------------- | ----------------------------- | 2612| string | 当前会话播放列表名称。 | 2613 2614**错误码:** 2615 2616以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2617 2618| 错误码ID | 错误信息 | 2619| -------- | ---------------------------------------- | 2620| 6600101 | Session service exception. | 2621| 6600102 | The session does not exist. | 2622| 6600103 | The session controller does not exist. | 2623 2624**示例:** 2625 2626```ts 2627import { BusinessError } from '@kit.BasicServicesKit'; 2628 2629try { 2630 let currentQueueTitle: string = avsessionController.getAVQueueTitleSync(); 2631} catch (err) { 2632 let error = err as BusinessError; 2633 console.error(`getAVQueueTitleSync error, error code: ${error.code}, error message: ${error.message}`); 2634} 2635``` 2636 2637## getAVQueueItemsSync<sup>10+</sup> 2638 2639getAVQueueItemsSync(): Array\<AVQueueItem\> 2640 2641使用同步方法获取当前会话播放列表相关信息。 2642 2643**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2644 2645**系统能力:** SystemCapability.Multimedia.AVSession.Core 2646 2647**返回值:** 2648 2649| 类型 | 说明 | 2650| --------------------------------------------- | ----------------------------- | 2651| Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\> | 当前会话播放列表队列。 | 2652 2653**错误码:** 2654 2655以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2656 2657| 错误码ID | 错误信息 | 2658| -------- | ---------------------------------------- | 2659| 6600101 | Session service exception. | 2660| 6600102 | The session does not exist. | 2661| 6600103 | The session controller does not exist. | 2662 2663**示例:** 2664 2665```ts 2666import { BusinessError } from '@kit.BasicServicesKit'; 2667 2668try { 2669 let currentQueueItems: Array<avSession.AVQueueItem> = avsessionController.getAVQueueItemsSync(); 2670} catch (err) { 2671 let error = err as BusinessError; 2672 console.error(`getAVQueueItemsSync error, error code: ${error.code}, error message: ${error.message}`); 2673} 2674``` 2675 2676## getOutputDeviceSync<sup>10+</sup> 2677 2678getOutputDeviceSync(): OutputDeviceInfo 2679 2680使用同步方法获取当前输出设备信息。 2681 2682**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2683 2684**系统能力:** SystemCapability.Multimedia.AVSession.Core 2685 2686**返回值:** 2687 2688| 类型 | 说明 | 2689| ----------------------------------------------- | --------------------------------- | 2690| [OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10) | 当前输出设备信息。 | 2691 2692**错误码:** 2693 2694以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2695 2696| 错误码ID | 错误信息 | 2697| -------- | ---------------------------------------- | 2698| 6600101 | Session service exception. | 2699| 6600103 | The session controller does not exist. | 2700 2701**示例:** 2702 2703```ts 2704import { BusinessError } from '@kit.BasicServicesKit'; 2705 2706try { 2707 let currentOutputDevice: avSession.OutputDeviceInfo = avsessionController.getOutputDeviceSync(); 2708} catch (err) { 2709 let error = err as BusinessError; 2710 console.error(`getOutputDeviceSync error, error code: ${error.code}, error message: ${error.message}`); 2711} 2712``` 2713 2714## isActiveSync<sup>10+</sup> 2715 2716isActiveSync(): boolean 2717 2718使用同步方法判断会话是否被激活。 2719 2720**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2721 2722**系统能力:** SystemCapability.Multimedia.AVSession.Core 2723 2724**返回值:** 2725 2726| 类型 | 说明 | 2727| ----------------- | ------------------------------------------------------------ | 2728| boolean | 会话是否为激活状态,true表示被激活,false表示禁用。 | 2729 2730**错误码:** 2731 2732以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2733 2734| 错误码ID | 错误信息 | 2735| -------- | ---------------------------------------- | 2736| 6600101 | Session service exception. | 2737| 6600102 | The session does not exist. | 2738| 6600103 | The session controller does not exist. | 2739 2740**示例:** 2741 2742```ts 2743import { BusinessError } from '@kit.BasicServicesKit'; 2744 2745try { 2746 let isActive: boolean = avsessionController.isActiveSync(); 2747} catch (err) { 2748 let error = err as BusinessError; 2749 console.error(`isActiveSync error, error code: ${error.code}, error message: ${error.message}`); 2750} 2751``` 2752 2753## getValidCommandsSync<sup>10+</sup> 2754 2755getValidCommandsSync(): Array\<AVControlCommandType\> 2756 2757使用同步方法获取会话支持的有效命令。 2758 2759**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2760 2761**系统能力:** SystemCapability.Multimedia.AVSession.Core 2762 2763**返回值:** 2764 2765| 类型 | 说明 | 2766| ------------------------------------------------------------ | --------------------------------- | 2767| Array<[AVControlCommandType](arkts-apis-avsession-t.md#avcontrolcommandtype10)\> | 会话支持的有效命令的集合。 | 2768 2769**错误码:** 2770 2771以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 2772 2773| 错误码ID | 错误信息 | 2774| -------- | ---------------------------------------- | 2775| 6600101 | Session service exception. | 2776| 6600102 | The session does not exist. | 2777| 6600103 | The session controller does not exist. | 2778 2779**示例:** 2780 2781```ts 2782import { BusinessError } from '@kit.BasicServicesKit'; 2783 2784try { 2785 let validCommands: Array<avSession.AVControlCommandType> = avsessionController.getValidCommandsSync(); 2786} catch (err) { 2787 let error = err as BusinessError; 2788 console.error(`getValidCommandsSync error, error code: ${error.code}, error message: ${error.message}`); 2789} 2790``` 2791