1# Interface (AVSession) 2<!--Kit: AVSession Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @ccfriend; @liao_qian--> 5<!--SE: @ccfriend--> 6<!--TSE: @chenmingxi1_huawei--> 7 8> **NOTE** 9> 10> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 11> - The initial APIs of this interface are supported since API version 10. 12 13An AVSession object is created by calling [avSession.createAVSession](arkts-apis-avsession-f.md#avsessioncreateavsession10). The object enables you to obtain the session ID and set the metadata and playback state. 14 15## Modules to Import 16 17```ts 18import { avSession } from '@kit.AVSessionKit'; 19``` 20 21## Properties 22 23**Atomic service API**: This API can be used in atomic services since API version 12. 24 25**System capability**: SystemCapability.Multimedia.AVSession.Core 26 27| Name | Type | Read-Only| Optional| Description | 28| :-------- | :----- | :--- | :--- | :---------------------------- | 29| sessionId<sup>10+</sup> | string | Yes | No | Unique session ID of the AVSession object.| 30| sessionType<sup>10+</sup> | [AVSessionType](arkts-apis-avsession-t.md#avsessiontype10) | Yes | No | AVSession type.| 31 32**Example** 33 34```ts 35let sessionId: string = currentAVSession.sessionId; 36let sessionType: avSession.AVSessionType = currentAVSession.sessionType; 37``` 38 39## setAVMetadata<sup>10+</sup> 40 41setAVMetadata(data: AVMetadata): Promise\<void> 42 43Sets session metadata. This API uses a promise to return the result. 44 45**Atomic service API**: This API can be used in atomic services since API version 12. 46 47**System capability**: SystemCapability.Multimedia.AVSession.Core 48 49**Parameters** 50 51| Name| Type | Mandatory| Description | 52| ------ | ------------------------- | ---- | ------------ | 53| data | [AVMetadata](arkts-apis-avsession-i.md#avmetadata10) | Yes | Session metadata.| 54 55**Return value** 56 57| Type | Description | 58| -------------- | ----------------------------- | 59| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 60 61**Error codes** 62 63For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 64 65| ID| Error Message| 66| -------- | ---------| 67| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 68| 6600101 | Session service exception. | 69| 6600102 | The session does not exist. | 70 71**Example** 72 73```ts 74import { BusinessError } from '@kit.BasicServicesKit'; 75 76let metadata: avSession.AVMetadata = { 77 assetId: "121278", 78 title: "lose yourself", 79 artist: "Eminem", 80 author: "ST", 81 album: "Slim shady", 82 writer: "ST", 83 composer: "ST", 84 duration: 2222, 85 mediaImage: "https://www.example.com/example.jpg", 86 subtitle: "8 Mile", 87 description: "Rap", 88 // The LRC contains two types of elements: time tag + lyrics, and ID tag. 89 // Example: [00:25.44]xxx\r\n[00:26.44]xxx\r\n 90 lyric: "Lyrics in LRC format", 91 // The singleLyricText field stores a single line of lyric text without timestamps. 92 // Example: "Content of a single lyric line" 93 singleLyricText: "Content of a single lyric line", 94 previousAssetId: "121277", 95 nextAssetId: "121279" 96}; 97currentAVSession.setAVMetadata(metadata).then(() => { 98 console.info('SetAVMetadata successfully'); 99}).catch((err: BusinessError) => { 100 console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 101}); 102``` 103 104## setAVMetadata<sup>10+</sup> 105 106setAVMetadata(data: AVMetadata, callback: AsyncCallback\<void>): void 107 108Sets session metadata. This API uses an asynchronous callback to return the result. 109 110**System capability**: SystemCapability.Multimedia.AVSession.Core 111 112**Parameters** 113 114| Name | Type | Mandatory| Description | 115| -------- | ------------------------- | ---- | ------------------------------------- | 116| data | [AVMetadata](arkts-apis-avsession-i.md#avmetadata10) | Yes | Session metadata. | 117| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 118 119**Error codes** 120 121For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 122 123| ID| Error Message| 124| -------- | ---------| 125| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 126| 6600101 | Session service exception. | 127| 6600102 | The session does not exist. | 128 129**Example** 130 131```ts 132import { BusinessError } from '@kit.BasicServicesKit'; 133 134let metadata: avSession.AVMetadata = { 135 assetId: "121278", 136 title: "lose yourself", 137 artist: "Eminem", 138 author: "ST", 139 album: "Slim shady", 140 writer: "ST", 141 composer: "ST", 142 duration: 2222, 143 mediaImage: "https://www.example.com/example.jpg", 144 subtitle: "8 Mile", 145 description: "Rap", 146 // The LRC contains two types of elements: time tag + lyrics, and ID tag. 147 // Example: [00:25.44]xxx\r\n[00:26.44]xxx\r\n 148 lyric: "Lyrics in LRC format", 149 // The singleLyricText field stores a single line of lyric text without timestamps. 150 // Example: "Content of a single lyric line" 151 singleLyricText: "Content of a single lyric line", 152 previousAssetId: "121277", 153 nextAssetId: "121279" 154}; 155currentAVSession.setAVMetadata(metadata, (err: BusinessError) => { 156 if (err) { 157 console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 158 } else { 159 console.info('SetAVMetadata successfully'); 160 } 161}); 162``` 163 164## setCallMetadata<sup>11+</sup> 165 166setCallMetadata(data: CallMetadata): Promise\<void> 167 168Sets call metadata. This API uses a promise to return the result. 169 170**System capability**: SystemCapability.Multimedia.AVSession.Core 171 172**Parameters** 173 174| Name| Type | Mandatory| Description | 175| ------ | ------------------------- | ---- | ------------ | 176| data | [CallMetadata](arkts-apis-avsession-i.md#callmetadata11) | Yes | Call metadata.| 177 178**Return value** 179 180| Type | Description | 181| -------------- | ----------------------------- | 182| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 183 184**Error codes** 185 186For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 187 188| ID| Error Message| 189| -------- | ---------| 190| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 191| 6600101 | Session service exception. | 192| 6600102 | The session does not exist. | 193 194**Example** 195 196```ts 197import { image } from '@kit.ImageKit'; 198import { resourceManager } from '@kit.LocalizationKit'; 199import { BusinessError } from '@kit.BasicServicesKit'; 200 201let value = await resourceManager.getSysResourceManager().getRawFileContent('IMAGE_URI'); 202 let imageSource = await image.createImageSource(value.buffer); 203 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 204 let calldata: avSession.CallMetadata = { 205 name: "xiaoming", 206 phoneNumber: "111xxxxxxxx", 207 avatar: imagePixel 208 }; 209currentAVSession.setCallMetadata(calldata).then(() => { 210 console.info('setCallMetadata successfully'); 211}).catch((err: BusinessError) => { 212 console.error(`setCallMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 213}); 214``` 215 216## setCallMetadata<sup>11+</sup> 217 218setCallMetadata(data: CallMetadata, callback: AsyncCallback\<void>): void 219 220Sets call metadata. This API uses an asynchronous callback to return the result. 221 222**System capability**: SystemCapability.Multimedia.AVSession.Core 223 224**Parameters** 225 226| Name | Type | Mandatory| Description | 227| -------- | ------------------------- | ---- | ------------------------------------- | 228| data | [CallMetadata](arkts-apis-avsession-i.md#callmetadata11) | Yes | Call metadata. | 229| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 230 231**Error codes** 232 233For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 234 235| ID| Error Message| 236| -------- | ---------| 237| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 238| 6600101 | Session service exception. | 239| 6600102 | The session does not exist. | 240 241**Example** 242 243```ts 244import { image } from '@kit.ImageKit'; 245import { resourceManager } from '@kit.LocalizationKit'; 246import { BusinessError } from '@kit.BasicServicesKit'; 247 248async function setCallMetadata() { 249 let value = await resourceManager.getSysResourceManager().getRawFileContent('IMAGE_URI'); 250 let imageSource = await image.createImageSource(value.buffer); 251 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 252 let calldata: avSession.CallMetadata = { 253 name: "xiaoming", 254 phoneNumber: "111xxxxxxxx", 255 avatar: imagePixel 256 }; 257 currentAVSession.setCallMetadata(calldata, (err: BusinessError) => { 258 if (err) { 259 console.error(`setCallMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 260 } else { 261 console.info('setCallMetadata successfully'); 262 } 263 }); 264} 265``` 266 267## setAVCallState<sup>11+</sup> 268 269setAVCallState(state: AVCallState): Promise\<void> 270 271Sets the call state. This API uses a promise to return the result. 272 273**System capability**: SystemCapability.Multimedia.AVSession.Core 274 275**Parameters** 276 277| Name| Type | Mandatory| Description | 278| ------ | ------------------------- | ---- | ------------ | 279| state | [AVCallState](arkts-apis-avsession-i.md#avcallstate11) | Yes | Call state.| 280 281**Return value** 282 283| Type | Description | 284| -------------- | ----------------------------- | 285| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 286 287**Error codes** 288 289For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 290 291| ID| Error Message| 292| -------- | ---------| 293| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 294| 6600101 | Session service exception. | 295| 6600102 | The session does not exist. | 296 297**Example** 298 299```ts 300import { BusinessError } from '@kit.BasicServicesKit'; 301 302let calldata: avSession.AVCallState = { 303 state: avSession.CallState.CALL_STATE_ACTIVE, 304 muted: false 305}; 306currentAVSession.setAVCallState(calldata).then(() => { 307 console.info('setAVCallState successfully'); 308}).catch((err: BusinessError) => { 309 console.error(`setAVCallState BusinessError: code: ${err.code}, message: ${err.message}`); 310}); 311``` 312 313## setAVCallState<sup>11+</sup> 314 315setAVCallState(state: AVCallState, callback: AsyncCallback\<void>): void 316 317Sets the call state. This API uses an asynchronous callback to return the result. 318 319**System capability**: SystemCapability.Multimedia.AVSession.Core 320 321**Parameters** 322 323| Name | Type | Mandatory| Description | 324| -------- | ------------------------- | ---- | ------------------------------------- | 325| state | [AVCallState](arkts-apis-avsession-i.md#avcallstate11) | Yes | Call state. | 326| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 327 328**Error codes** 329 330For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 331 332| ID| Error Message| 333| -------- | ---------| 334| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 335| 6600101 | Session service exception. | 336| 6600102 | The session does not exist. | 337 338**Example** 339 340```ts 341import { BusinessError } from '@kit.BasicServicesKit'; 342 343let avcalldata: avSession.AVCallState = { 344 state: avSession.CallState.CALL_STATE_ACTIVE, 345 muted: false 346}; 347currentAVSession.setAVCallState(avcalldata, (err: BusinessError) => { 348 if (err) { 349 console.error(`setAVCallState BusinessError: code: ${err.code}, message: ${err.message}`); 350 } else { 351 console.info('setAVCallState successfully'); 352 } 353}); 354``` 355 356## setAVPlaybackState<sup>10+</sup> 357 358setAVPlaybackState(state: AVPlaybackState): Promise\<void> 359 360Sets information related to the session playback state. This API uses a promise to return the result. 361 362**Atomic service API**: This API can be used in atomic services since API version 12. 363 364**System capability**: SystemCapability.Multimedia.AVSession.Core 365 366**Parameters** 367 368| Name| Type | Mandatory| Description | 369| ------ | ----------------------------------- | ---- | ---------------| 370| state | [AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10) | Yes | Information related to the session playback state.| 371 372**Return value** 373 374| Type | Description | 375| -------------- | ----------------------------- | 376| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 377 378**Error codes** 379 380For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 381 382| ID| Error Message| 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 388**Example** 389 390```ts 391import { BusinessError } from '@kit.BasicServicesKit'; 392 393let playbackState: avSession.AVPlaybackState = { 394 state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, 395 speed: 1.0, 396 position:{elapsedTime:10, updateTime:(new Date()).getTime()}, 397 bufferedTime:1000, 398 loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, 399 isFavorite:true 400}; 401currentAVSession.setAVPlaybackState(playbackState).then(() => { 402 console.info('SetAVPlaybackState successfully'); 403}).catch((err: BusinessError) => { 404 console.error(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 405}); 406``` 407 408## setAVPlaybackState<sup>10+</sup> 409 410setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\<void>): void 411 412Sets information related to the session playback state. This API uses an asynchronous callback to return the result. 413 414**System capability**: SystemCapability.Multimedia.AVSession.Core 415 416**Parameters** 417 418| Name | Type | Mandatory| Description | 419| -------- | ----------------------------------- | ---- | ---------------| 420| state | [AVPlaybackState](arkts-apis-avsession-i.md#avplaybackstate10) | Yes | Information related to the session playback state.| 421| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object. | 422 423**Error codes** 424 425For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 426 427| ID| Error Message| 428| -------- | ---------| 429| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 430| 6600101 | Session service exception. | 431| 6600102 | The session does not exist. | 432 433**Example** 434 435```ts 436import { BusinessError } from '@kit.BasicServicesKit'; 437 438let PlaybackState: avSession.AVPlaybackState = { 439 state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, 440 speed: 1.0, 441 position:{elapsedTime:10, updateTime:(new Date()).getTime()}, 442 bufferedTime:1000, 443 loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, 444 isFavorite:true 445}; 446currentAVSession.setAVPlaybackState(PlaybackState, (err: BusinessError) => { 447 if (err) { 448 console.error(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 449 } else { 450 console.info('SetAVPlaybackState successfully'); 451 } 452}); 453``` 454 455## setLaunchAbility<sup>10+</sup> 456 457setLaunchAbility(ability: WantAgent): Promise\<void> 458 459Sets a launcher ability. This API uses a promise to return the result. 460 461**Atomic service API**: This API can be used in atomic services since API version 12. 462 463**System capability**: SystemCapability.Multimedia.AVSession.Core 464 465**Parameters** 466 467| Name | Type | Mandatory| Description | 468| ------- | --------------| ---- | ----------------------------| 469| ability | [WantAgent](../apis-ability-kit/js-apis-app-ability-wantAgent.md) | Yes | Application properties, such as the bundle name, ability name, and deviceID.| 470 471**Return value** 472 473| Type | Description | 474| -------------- | ----------------------------- | 475| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 476 477**Error codes** 478 479For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 480 481| ID| Error Message| 482| -------- | ---------| 483| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 484| 6600101 | Session service exception. | 485| 6600102 | The session does not exist. | 486 487**Example** 488 489```ts 490import { wantAgent } from '@kit.AbilityKit'; 491import { BusinessError } from '@kit.BasicServicesKit'; 492 493// WantAgentInfo object. 494let wantAgentInfo: wantAgent.WantAgentInfo = { 495 wants: [ 496 { 497 deviceId: "deviceId", 498 bundleName: "com.example.myapplication", 499 abilityName: "EntryAbility", 500 action: "action1", 501 entities: ["entity1"], 502 type: "MIMETYPE", 503 uri: "key = {true,true,false}", 504 parameters: 505 { 506 mykey0: 2222, 507 mykey1: [1, 2, 3], 508 mykey2: "[1, 2, 3]", 509 mykey3: "ssssssssssssssssssssssssss", 510 mykey4: [false, true, false], 511 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 512 mykey6: true 513 } 514 } 515 ], 516 operationType: wantAgent.OperationType.START_ABILITIES, 517 requestCode: 0, 518 wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 519} 520 521wantAgent.getWantAgent(wantAgentInfo).then((agent) => { 522 currentAVSession.setLaunchAbility(agent).then(() => { 523 console.info('SetLaunchAbility successfully'); 524 }).catch((err: BusinessError) => { 525 console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 526 }); 527}); 528``` 529 530## setLaunchAbility<sup>10+</sup> 531 532setLaunchAbility(ability: WantAgent, callback: AsyncCallback\<void>): void 533 534Sets a launcher ability. This API uses an asynchronous callback to return the result. 535 536**System capability**: SystemCapability.Multimedia.AVSession.Core 537 538**Parameters** 539 540| Name | Type | Mandatory| Description | 541| -------- | --------------| ---- | --------- | 542| ability | [WantAgent](../apis-ability-kit/js-apis-app-ability-wantAgent.md) | Yes | Application properties, such as the bundle name, ability name, and deviceID. | 543| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 544 545**Error codes** 546 547For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 548 549| ID| Error Message| 550| -------- | ---------| 551| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 552| 6600101 | Session service exception. | 553| 6600102 | The session does not exist. | 554 555**Example** 556 557```ts 558import { wantAgent } from '@kit.AbilityKit'; 559import { BusinessError } from '@kit.BasicServicesKit'; 560 561// WantAgentInfo object. 562let wantAgentInfo: wantAgent.WantAgentInfo = { 563 wants: [ 564 { 565 deviceId: "deviceId", 566 bundleName: "com.example.myapplication", 567 abilityName: "EntryAbility", 568 action: "action1", 569 entities: ["entity1"], 570 type: "MIMETYPE", 571 uri: "key = {true,true,false}", 572 parameters: 573 { 574 mykey0: 2222, 575 mykey1: [1, 2, 3], 576 mykey2: "[1, 2, 3]", 577 mykey3: "ssssssssssssssssssssssssss", 578 mykey4: [false, true, false], 579 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 580 mykey6: true 581 } 582 } 583 ], 584 operationType: wantAgent.OperationType.START_ABILITIES, 585 requestCode: 0, 586 wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 587} 588 589wantAgent.getWantAgent(wantAgentInfo).then((agent) => { 590 currentAVSession.setLaunchAbility(agent, (err: BusinessError) => { 591 if (err) { 592 console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 593 } else { 594 console.info('SetLaunchAbility successfully'); 595 } 596 }); 597}); 598``` 599 600## dispatchSessionEvent<sup>10+</sup> 601 602dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\<void> 603 604Dispatches a custom event in the session, including the event name and event content in key-value pair format. This API uses a promise to return the result. It is called by the provider. 605 606**Atomic service API**: This API can be used in atomic services since API version 12. 607 608**System capability**: SystemCapability.Multimedia.AVSession.Core 609 610**Parameters** 611 612| Name | Type | Mandatory| Description | 613| ------- | --------------| ---- | ----------------------------| 614| event | string | Yes | Name of the session event.| 615| args | {[key: string]: Object} | Yes | Content of the session event.| 616 617> **NOTE** 618> The **args** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want(Want)](../apis-ability-kit/js-apis-app-ability-want.md). 619 620**Return value** 621 622| Type | Description | 623| -------------- | ----------------------------- | 624| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 625 626**Error codes** 627 628For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 629 630| ID| Error Message| 631| -------- | ---------| 632| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 633| 6600101 | Session service exception. | 634| 6600102 | The session does not exist. | 635 636**Example** 637 638```ts 639import { BusinessError } from '@kit.BasicServicesKit'; 640import { avSession } from '@kit.AVSessionKit'; 641@Entry 642@Component 643struct Index { 644 @State message: string = 'hello world'; 645 646 build() { 647 Column() { 648 Text(this.message) 649 .onClick(()=>{ 650 let currentAVSession: avSession.AVSession | undefined = undefined; 651 let tag = "createNewSession"; 652 let context: Context = this.getUIContext().getHostContext() as Context; 653 654 avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 655 if (err) { 656 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 657 } else { 658 currentAVSession = data; 659 } 660 }); 661 let eventName = "dynamic_lyric"; 662 if (currentAVSession !== undefined) { 663 (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}).then(() => { 664 console.info('dispatchSessionEvent successfully'); 665 }).catch((err: BusinessError) => { 666 console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); 667 }) 668 } 669 }) 670 } 671 .width('100%') 672 .height('100%') 673 } 674} 675``` 676 677## dispatchSessionEvent<sup>10+</sup> 678 679dispatchSessionEvent(event: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void 680 681Dispatches a custom event in the session, including the event name and event content in key-value pair format. This API uses an asynchronous callback to return the result. It is called by the provider. 682 683**System capability**: SystemCapability.Multimedia.AVSession.Core 684 685**Parameters** 686 687| Name | Type | Mandatory| Description | 688| ------- | --------------| ---- | ----------------------------| 689| event | string | Yes | Name of the session event.| 690| args | {[key: string]: Object} | Yes | Content of the session event.| 691| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 692 693> **NOTE** 694 695> The **args** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want(Want)](../apis-ability-kit/js-apis-app-ability-want.md). 696 697**Error codes** 698 699For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 700 701| ID| Error Message| 702| -------- | ---------| 703| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 704| 6600101 | Session service exception. | 705| 6600102 | The session does not exist. | 706 707**Example** 708 709```ts 710import { BusinessError } from '@kit.BasicServicesKit'; 711import { avSession } from '@kit.AVSessionKit'; 712@Entry 713@Component 714struct Index { 715 @State message: string = 'hello world'; 716 717 build() { 718 Column() { 719 Text(this.message) 720 .onClick(()=>{ 721 let currentAVSession: avSession.AVSession | undefined = undefined; 722 let tag = "createNewSession"; 723 let context: Context = this.getUIContext().getHostContext() as Context; 724 725 avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 726 if (err) { 727 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 728 } else { 729 currentAVSession = data; 730 } 731 }); 732 let eventName: string = "dynamic_lyric"; 733 if (currentAVSession !== undefined) { 734 (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}, (err: BusinessError) => { 735 if (err) { 736 console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); 737 } 738 }) 739 } 740 }) 741 } 742 .width('100%') 743 .height('100%') 744 } 745} 746``` 747 748## setAVQueueItems<sup>10+</sup> 749 750setAVQueueItems(items: Array\<AVQueueItem>): Promise\<void> 751 752Sets a playlist. This API uses a promise to return the result. 753 754**Atomic service API**: This API can be used in atomic services since API version 12. 755 756**System capability**: SystemCapability.Multimedia.AVSession.Core 757 758**Parameters** 759 760| Name | Type | Mandatory| Description | 761| ------ | ------------------------------------ | ---- | ---------------------------------- | 762| items | Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\> | Yes | Playlist to set.| 763 764**Return value** 765 766| Type | Description | 767| -------------- | ----------------------------- | 768| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 769 770**Error codes** 771 772For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 773 774| ID| Error Message| 775| -------- | ---------| 776| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 777| 6600101 | Session service exception. | 778| 6600102 | The session does not exist. | 779 780**Example** 781 782```ts 783import { image } from '@kit.ImageKit'; 784import { resourceManager } from '@kit.LocalizationKit'; 785import { BusinessError } from '@kit.BasicServicesKit'; 786 787async function setAVQueueItems() { 788 let value = await resourceManager.getSysResourceManager().getRawFileContent('IMAGE_URI'); 789 let imageSource = await image.createImageSource(value.buffer); 790 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 791 let queueItemDescription_1: avSession.AVMediaDescription = { 792 assetId: '001', 793 title: 'music_name', 794 subtitle: 'music_sub_name', 795 description: 'music_description', 796 mediaImage : imagePixel, 797 extras: {extras:'any'} 798 }; 799 let queueItem_1: avSession.AVQueueItem = { 800 itemId: 1, 801 description: queueItemDescription_1 802 }; 803 let queueItemDescription_2: avSession.AVMediaDescription = { 804 assetId: '002', 805 title: 'music_name', 806 subtitle: 'music_sub_name', 807 description: 'music_description', 808 mediaImage: imagePixel, 809 extras: {extras:'any'} 810 }; 811 let queueItem_2: avSession.AVQueueItem = { 812 itemId: 2, 813 description: queueItemDescription_2 814 }; 815 let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; 816 currentAVSession.setAVQueueItems(queueItemsArray).then(() => { 817 console.info('SetAVQueueItems successfully'); 818 }).catch((err: BusinessError) => { 819 console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 820 }); 821} 822``` 823 824## setAVQueueItems<sup>10+</sup> 825 826setAVQueueItems(items: Array\<AVQueueItem>, callback: AsyncCallback\<void>): void 827 828Sets a playlist. This API uses an asynchronous callback to return the result. 829 830**System capability**: SystemCapability.Multimedia.AVSession.Core 831 832**Parameters** 833 834| Name | Type | Mandatory| Description | 835| -------- | ------------------------------------ | ---- | ----------------------------| 836| items | Array<[AVQueueItem](arkts-apis-avsession-i.md#avqueueitem10)\> | Yes | Playlist to set. | 837| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 838 839**Error codes** 840 841For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 842 843| ID| Error Message| 844| -------- | ---------| 845| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 846| 6600101 | Session service exception. | 847| 6600102 | The session does not exist. | 848 849**Example** 850 851```ts 852import { image } from '@kit.ImageKit'; 853import { resourceManager } from '@kit.LocalizationKit'; 854import { BusinessError } from '@kit.BasicServicesKit'; 855 856async function setAVQueueItems() { 857 let value = await resourceManager.getSysResourceManager().getRawFileContent('IMAGE_URI'); 858 let imageSource = await image.createImageSource(value.buffer); 859 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 860 let queueItemDescription_1: avSession.AVMediaDescription = { 861 assetId: '001', 862 title: 'music_name', 863 subtitle: 'music_sub_name', 864 description: 'music_description', 865 mediaImage : imagePixel, 866 extras: {extras:'any'} 867 }; 868 let queueItem_1: avSession.AVQueueItem = { 869 itemId: 1, 870 description: queueItemDescription_1 871 }; 872 let queueItemDescription_2: avSession.AVMediaDescription = { 873 assetId: '002', 874 title: 'music_name', 875 subtitle: 'music_sub_name', 876 description: 'music_description', 877 mediaImage: imagePixel, 878 extras: {extras:'any'} 879 }; 880 let queueItem_2: avSession.AVQueueItem = { 881 itemId: 2, 882 description: queueItemDescription_2 883 }; 884 let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; 885 currentAVSession.setAVQueueItems(queueItemsArray, (err: BusinessError) => { 886 if (err) { 887 console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 888 } else { 889 console.info('SetAVQueueItems successfully'); 890 } 891 }); 892} 893``` 894 895## setAVQueueTitle<sup>10+</sup> 896 897setAVQueueTitle(title: string): Promise\<void> 898 899Sets a name for the playlist. This API uses a promise to return the result. 900 901**Atomic service API**: This API can be used in atomic services since API version 12. 902 903**System capability**: SystemCapability.Multimedia.AVSession.Core 904 905**Parameters** 906 907| Name | Type | Mandatory| Description | 908| ------ | ------ | ---- | -------------- | 909| title | string | Yes | Name of the playlist.| 910 911**Return value** 912 913| Type | Description | 914| -------------- | ----------------------------- | 915| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 916 917**Error codes** 918 919For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 920 921| ID| Error Message| 922| -------- | ---------| 923| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 924| 6600101 | Session service exception. | 925| 6600102 | The session does not exist. | 926 927**Example** 928 929```ts 930import { BusinessError } from '@kit.BasicServicesKit'; 931 932let queueTitle = 'QUEUE_TITLE'; 933currentAVSession.setAVQueueTitle(queueTitle).then(() => { 934 console.info('SetAVQueueTitle successfully'); 935}).catch((err: BusinessError) => { 936 console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 937}); 938``` 939 940## setAVQueueTitle<sup>10+</sup> 941 942setAVQueueTitle(title: string, callback: AsyncCallback\<void>): void 943 944Sets a name for the playlist. This API uses an asynchronous callback to return the result. 945 946**System capability**: SystemCapability.Multimedia.AVSession.Core 947 948**Parameters** 949 950| Name | Type | Mandatory| Description | 951| -------- | --------------------- | ---- | ----------------------------| 952| title | string | Yes | Name of the playlist. | 953| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 954 955**Error codes** 956 957For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 958 959| ID| Error Message| 960| -------- | ---------| 961| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 962| 6600101 | Session service exception. | 963| 6600102 | The session does not exist. | 964 965**Example** 966 967```ts 968import { BusinessError } from '@kit.BasicServicesKit'; 969 970let queueTitle = 'QUEUE_TITLE'; 971currentAVSession.setAVQueueTitle(queueTitle, (err: BusinessError) => { 972 if (err) { 973 console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 974 } else { 975 console.info('SetAVQueueTitle successfully'); 976 } 977}); 978``` 979 980## setExtras<sup>10+</sup> 981 982setExtras(extras: {[key: string]: Object}): Promise\<void> 983 984Sets a custom media packet in the form of key-value pairs. This API uses a promise to return the result. It is called by the provider. 985 986**Atomic service API**: This API can be used in atomic services since API version 12. 987 988**System capability**: SystemCapability.Multimedia.AVSession.Core 989 990**Parameters** 991 992| Name | Type | Mandatory| Description | 993| ------- | --------------| ---- | ----------------------------| 994| extras | {[key: string]: Object} | Yes | Key-value pairs of the custom media packet.| 995 996> **NOTE** 997 998> The **extras** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want(Want)](../apis-ability-kit/js-apis-app-ability-want.md). 999 1000**Return value** 1001 1002| Type | Description | 1003| -------------- | ----------------------------- | 1004| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 1005 1006**Error codes** 1007 1008For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1009 1010| ID| Error Message| 1011| -------- | ---------| 1012| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 1013| 6600101 | Session service exception. | 1014| 6600102 | The session does not exist. | 1015 1016**Example** 1017 1018```ts 1019import { BusinessError } from '@kit.BasicServicesKit'; 1020import { avSession } from '@kit.AVSessionKit'; 1021@Entry 1022@Component 1023struct Index { 1024 @State message: string = 'hello world'; 1025 1026 build() { 1027 Column() { 1028 Text(this.message) 1029 .onClick(()=>{ 1030 let currentAVSession: avSession.AVSession | undefined = undefined; 1031 let tag = "createNewSession"; 1032 let context: Context = this.getUIContext().getHostContext() as Context; 1033 1034 avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1035 if (err) { 1036 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1037 } else { 1038 currentAVSession = data; 1039 } 1040 }); 1041 if (currentAVSession !== undefined) { 1042 (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}).then(() => { 1043 console.info('setExtras successfully'); 1044 }).catch((err: BusinessError) => { 1045 console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); 1046 }) 1047 } 1048 }) 1049 } 1050 .width('100%') 1051 .height('100%') 1052 } 1053} 1054``` 1055 1056## setExtras<sup>10+</sup> 1057 1058setExtras(extras: {[key: string]: Object}, callback: AsyncCallback\<void>): void 1059 1060Sets a custom media packet in the form of key-value pairs. This API uses an asynchronous callback to return the result. It is called by the provider. 1061 1062**System capability**: SystemCapability.Multimedia.AVSession.Core 1063 1064**Parameters** 1065 1066| Name | Type | Mandatory| Description | 1067| ------- | --------------| ---- | ----------------------------| 1068| extras | {[key: string]: Object} | Yes | Key-value pairs of the custom media packet.| 1069| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1070 1071> **NOTE** 1072 1073> The **extras** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want(Want)](../apis-ability-kit/js-apis-app-ability-want.md). 1074 1075**Error codes** 1076 1077For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1078 1079| ID| Error Message| 1080| -------- | ---------| 1081| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 1082| 6600101 | Session service exception. | 1083| 6600102 | The session does not exist. | 1084 1085**Example** 1086 1087```ts 1088import { BusinessError } from '@kit.BasicServicesKit'; 1089import { avSession } from '@kit.AVSessionKit'; 1090@Entry 1091@Component 1092struct Index { 1093 @State message: string = 'hello world'; 1094 1095 build() { 1096 Column() { 1097 Text(this.message) 1098 .onClick(()=>{ 1099 let currentAVSession: avSession.AVSession | undefined = undefined; 1100 let tag = "createNewSession"; 1101 let context: Context = this.getUIContext().getHostContext() as Context; 1102 1103 avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1104 if (err) { 1105 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1106 } else { 1107 currentAVSession = data; 1108 } 1109 }); 1110 if (currentAVSession !== undefined) { 1111 (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}, (err: BusinessError) => { 1112 if (err) { 1113 console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); 1114 } 1115 }) 1116 } 1117 }) 1118 } 1119 .width('100%') 1120 .height('100%') 1121 } 1122} 1123``` 1124 1125## sendCustomData<sup>20+</sup> 1126 1127sendCustomData(data: Record\<string, Object>): Promise\<void> 1128 1129Sends custom data to the remote device. This API uses a promise to return the result. 1130 1131**Atomic service API**: This API can be used in atomic services since API version 20. 1132 1133**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1134 1135**Parameters** 1136 1137| Name| Type | Mandatory| Description | 1138| ------ | ---------------------- | ---- | ------------------------------------------------------------ | 1139| data | Record\<string, Object> | Yes | Custom data filled by the application. Only objects with the key **'customData'** and of the type string are parsed on the server.| 1140 1141**Return value** 1142 1143| Type | Description | 1144| -------------- | ----------------------------- | 1145| Promise\<void> | Promise that returns no value.| 1146 1147**Error codes** 1148 1149For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1150 1151| ID| Error Message | 1152| -------- | ------------------------------------------------------------ | 1153| 6600101 | Session service exception. | 1154| 6600102 | The session does not exist. | 1155 1156**Example** 1157 1158```ts 1159import { BusinessError } from '@kit.BasicServicesKit'; 1160import { avSession } from '@kit.AVSessionKit'; 1161@Entry 1162@Component 1163struct Index { 1164 @State message: string = 'hello world'; 1165 1166 build() { 1167 Column() { 1168 Text(this.message) 1169 .onClick(()=>{ 1170 let currentAVSession: avSession.AVSession | undefined = undefined; 1171 let tag = "createNewSession"; 1172 let context: Context = this.getUIContext().getHostContext() as Context; 1173 1174 avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1175 if (err) { 1176 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1177 } else { 1178 currentAVSession = data; 1179 } 1180 }); 1181 if (currentAVSession !== undefined) { 1182 (currentAVSession as avSession.AVSession).sendCustomData({customData : "This is custom data"}).then(() => { 1183 console.info('sendCustomData successfully'); 1184 }).catch((err: BusinessError) => { 1185 console.error(`sendCustomData BusinessError: code: ${err.code}, message: ${err.message}`); 1186 }) 1187 } 1188 }) 1189 } 1190 .width('100%') 1191 .height('100%') 1192 } 1193} 1194``` 1195 1196## getController<sup>10+</sup> 1197 1198getController(): Promise\<AVSessionController> 1199 1200Obtains the controller corresponding to this session. This API uses a promise to return the result. 1201 1202**Atomic service API**: This API can be used in atomic services since API version 12. 1203 1204**System capability**: SystemCapability.Multimedia.AVSession.Core 1205 1206**Return value** 1207 1208| Type | Description | 1209| ---------------------| ----------------------------- | 1210| Promise<[AVSessionController](arkts-apis-avsession-AVSessionController.md)> | Promise used to return the session controller.| 1211 1212**Error codes** 1213 1214For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1215 1216| ID| Error Message| 1217| -------- | ---------| 1218| 6600101 | Session service exception. | 1219| 6600102 | The session does not exist. | 1220 1221**Example** 1222 1223```ts 1224import { BusinessError } from '@kit.BasicServicesKit'; 1225import { avSession } from '@kit.AVSessionKit'; 1226 1227@Entry 1228@Component 1229struct Index { 1230 @State message: string = 'hello world'; 1231 build() { 1232 Column() { 1233 Text(this.message) 1234 .onClick(async ()=>{ 1235 let context: Context = this.getUIContext().getHostContext() as Context; 1236 let currentAVSession: avSession.AVSession = await avSession.createAVSession(context, 'SESSION_NAME', 'audio'); 1237 let avsessionController: avSession.AVSessionController; 1238 currentAVSession.getController().then(async (avcontroller: avSession.AVSessionController) => { 1239 avsessionController = avcontroller; 1240 console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); 1241 }).catch((err: BusinessError) => { 1242 console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); 1243 }); 1244 }) 1245 } 1246 .width('100%') 1247 .height('100%') 1248 } 1249} 1250``` 1251 1252## getController<sup>10+</sup> 1253 1254getController(callback: AsyncCallback\<AVSessionController>): void 1255 1256Obtains the controller corresponding to this session. This API uses an asynchronous callback to return the result. 1257 1258**System capability**: SystemCapability.Multimedia.AVSession.Core 1259 1260**Parameters** 1261 1262| Name | Type | Mandatory| Description | 1263| -------- | ----------------------------| ---- | -------------------------- | 1264| callback | AsyncCallback<[AVSessionController](arkts-apis-avsession-AVSessionController.md)\> | Yes | Callback used to return the session controller.| 1265 1266**Error codes** 1267 1268For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1269 1270| ID| Error Message| 1271| -------- | ---------| 1272| 6600101 | Session service exception. | 1273| 6600102 | The session does not exist. | 1274 1275**Example** 1276 1277```ts 1278import { avSession } from '@kit.AVSessionKit'; 1279import { BusinessError } from '@kit.BasicServicesKit'; 1280 1281@Entry 1282@Component 1283struct Index { 1284 @State message: string = 'hello world'; 1285 1286 build() { 1287 Column() { 1288 Text(this.message) 1289 .onClick(async () => { 1290 let context: Context = this.getUIContext().getHostContext() as Context; 1291 let currentAVSession: avSession.AVSession = await avSession.createAVSession(context, 'SESSION_NAME', 'audio'); 1292 let avsessionController: avSession.AVSessionController; 1293 currentAVSession.getController((err: BusinessError, avcontroller: avSession.AVSessionController) => { 1294 if (err) { 1295 console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); 1296 } else { 1297 avsessionController = avcontroller; 1298 console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); 1299 } 1300 }); 1301 }) 1302 } 1303 .width('100%') 1304 .height('100%') 1305 } 1306} 1307``` 1308 1309## getAVCastController<sup>10+</sup> 1310 1311getAVCastController(): Promise\<AVCastController> 1312 1313Obtains the cast controller when a casting connection is set up. This API uses a promise to return the result. If the session is not in the cast state, the controller returns **null**. 1314 1315**Atomic service API**: This API can be used in atomic services since API version 12. 1316 1317**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1318 1319**Return value** 1320 1321| Type | Description | 1322| --------- | --------- | 1323| Promise<[AVCastController](arkts-apis-avsession-AVCastController.md)\> | Promise used to return the cast controller.| 1324 1325**Error codes** 1326 1327For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1328 1329| ID| Error Message| 1330| -------- | --------------------------------------- | 1331| 6600102| The session does not exist. | 1332| 6600109| The remote connection is not established. | 1333 1334**Example** 1335 1336```ts 1337import { BusinessError } from '@kit.BasicServicesKit'; 1338 1339let aVCastController: avSession.AVCastController; 1340currentAVSession.getAVCastController().then((avcontroller: avSession.AVCastController) => { 1341 aVCastController = avcontroller; 1342 console.info('getAVCastController : SUCCESS'); 1343}).catch((err: BusinessError) => { 1344 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 1345}); 1346``` 1347 1348## getAVCastController<sup>10+</sup> 1349 1350getAVCastController(callback: AsyncCallback\<AVCastController>): void 1351 1352Obtains the cast controller when a casting connection is set up. This API uses an asynchronous callback to return the result. If the session is not in the cast state, the controller returns **null**. 1353 1354**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1355 1356**Parameters** 1357 1358| Name | Type | Mandatory| Description | 1359| --------- | ----------------------------| ---- | --------- | 1360| callback | AsyncCallback<[AVCastController](arkts-apis-avsession-AVCastController.md)\> | Yes | Callback used to return the cast controller.| 1361 1362**Error codes** 1363 1364For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1365 1366| ID| Error Message | 1367| -------- |---------------------------------------| 1368| 6600102| The session does not exist. | 1369| 6600109| The remote connection is not established. | 1370 1371**Example** 1372 1373```ts 1374import { BusinessError } from '@kit.BasicServicesKit'; 1375 1376let aVCastController: avSession.AVCastController; 1377currentAVSession.getAVCastController((err: BusinessError, avcontroller: avSession.AVCastController) => { 1378 if (err) { 1379 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 1380 } else { 1381 aVCastController = avcontroller; 1382 console.info('getAVCastController : SUCCESS'); 1383 } 1384}); 1385``` 1386 1387## getOutputDevice<sup>10+</sup> 1388 1389getOutputDevice(): Promise\<OutputDeviceInfo> 1390 1391Obtains information about the output device for this session. This API uses a promise to return the result. 1392 1393**Atomic service API**: This API can be used in atomic services since API version 12. 1394 1395**System capability**: SystemCapability.Multimedia.AVSession.Core 1396 1397**Return value** 1398 1399| Type | Description | 1400| ---------------| --------------------------------- | 1401| Promise<[OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10)> | Promise used to return the output device information.| 1402 1403**Error codes** 1404 1405For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1406 1407| ID| Error Message| 1408| -------- | ---------| 1409| 6600101 | Session service exception. | 1410| 6600102 | The session does not exist. | 1411 1412**Example** 1413 1414```ts 1415import { BusinessError } from '@kit.BasicServicesKit'; 1416 1417currentAVSession.getOutputDevice().then((outputDeviceInfo: avSession.OutputDeviceInfo) => { 1418 console.info(`GetOutputDevice : SUCCESS : devices length : ${outputDeviceInfo.devices.length}`); 1419}).catch((err: BusinessError) => { 1420 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 1421}) 1422``` 1423 1424## getOutputDevice<sup>10+</sup> 1425 1426getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void 1427 1428Obtains information about the output device for this session. This API uses an asynchronous callback to return the result. 1429 1430**System capability**: SystemCapability.Multimedia.AVSession.Core 1431 1432**Parameters** 1433 1434| Name | Type | Mandatory| Description | 1435| -------- | ----------------------| ---- | ------------------------------ | 1436| callback | AsyncCallback<[OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10)\> | Yes | Callback used to return the information obtained.| 1437 1438**Error codes** 1439 1440For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1441 1442| ID| Error Message| 1443| -------- | ---------| 1444| 6600101 | Session service exception. | 1445| 6600102 | The session does not exist. | 1446 1447**Example** 1448 1449```ts 1450import { BusinessError } from '@kit.BasicServicesKit'; 1451 1452currentAVSession.getOutputDevice((err: BusinessError, outputDeviceInfo: avSession.OutputDeviceInfo) => { 1453 if (err) { 1454 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 1455 } else { 1456 console.info(`GetOutputDevice : SUCCESS : devices length : ${outputDeviceInfo.devices.length}`); 1457 } 1458}); 1459``` 1460 1461## activate<sup>10+</sup> 1462 1463activate(): Promise\<void> 1464 1465Activates this session. A session can be used only after being activated. This API uses a promise to return the result. 1466 1467**Atomic service API**: This API can be used in atomic services since API version 12. 1468 1469**System capability**: SystemCapability.Multimedia.AVSession.Core 1470 1471**Return value** 1472 1473| Type | Description | 1474| -------------- | ----------------------------- | 1475| Promise\<void> | Promise used to return the result. If the session is activated, no value is returned; otherwise, an error object is returned.| 1476 1477**Error codes** 1478 1479For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1480 1481| ID| Error Message| 1482| -------- | ---------| 1483| 6600101 | Session service exception. | 1484| 6600102 | The session does not exist. | 1485 1486**Example** 1487 1488```ts 1489import { BusinessError } from '@kit.BasicServicesKit'; 1490 1491currentAVSession.activate().then(() => { 1492 console.info('Activate : SUCCESS '); 1493}).catch((err: BusinessError) => { 1494 console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); 1495}); 1496``` 1497 1498## activate<sup>10+</sup> 1499 1500activate(callback: AsyncCallback\<void>): void 1501 1502Activates this session. A session can be used only after being activated. This API uses an asynchronous callback to return the result. 1503 1504**System capability**: SystemCapability.Multimedia.AVSession.Core 1505 1506**Parameters** 1507 1508| Name | Type | Mandatory| Description | 1509| -------- | -------------------- | ---- | ---------- | 1510| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is activated, **err** is **undefined**; otherwise, **err** is an error object.| 1511 1512**Error codes** 1513 1514For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1515 1516| ID| Error Message| 1517| -------- | ---------| 1518| 6600101 | Session service exception. | 1519| 6600102 | The session does not exist. | 1520 1521**Example** 1522 1523```ts 1524import { BusinessError } from '@kit.BasicServicesKit'; 1525 1526currentAVSession.activate((err: BusinessError) => { 1527 if (err) { 1528 console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); 1529 } else { 1530 console.info('Activate : SUCCESS '); 1531 } 1532}); 1533``` 1534 1535## deactivate<sup>10+</sup> 1536 1537deactivate(): Promise\<void> 1538 1539Deactivates this session. You can use [activate](#activate10) to activate the session again. This API uses a promise to return the result. 1540 1541**Atomic service API**: This API can be used in atomic services since API version 12. 1542 1543**System capability**: SystemCapability.Multimedia.AVSession.Core 1544 1545**Return value** 1546 1547| Type | Description | 1548| -------------- | ----------------------------- | 1549| Promise\<void> | Promise used to return the result. If the session is deactivated, no value is returned; otherwise, an error object is returned.| 1550 1551**Error codes** 1552 1553For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1554 1555| ID| Error Message| 1556| -------- | ---------| 1557| 6600101 | Session service exception. | 1558| 6600102 | The session does not exist. | 1559 1560**Example** 1561 1562```ts 1563import { BusinessError } from '@kit.BasicServicesKit'; 1564 1565currentAVSession.deactivate().then(() => { 1566 console.info('Deactivate : SUCCESS '); 1567}).catch((err: BusinessError) => { 1568 console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); 1569}); 1570``` 1571 1572## deactivate<sup>10+</sup> 1573 1574deactivate(callback: AsyncCallback\<void>): void 1575 1576Deactivates this session. This API uses an asynchronous callback to return the result. 1577 1578Deactivates this session. You can use [activate](#activate10) to activate the session again. 1579 1580**System capability**: SystemCapability.Multimedia.AVSession.Core 1581 1582**Parameters** 1583 1584| Name | Type | Mandatory| Description | 1585| -------- | -------------------- | ---- | ---------- | 1586| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is deactivated, **err** is **undefined**; otherwise, **err** is an error object.| 1587 1588**Error codes** 1589 1590For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1591 1592| ID| Error Message| 1593| -------- | ---------| 1594| 6600101 | Session service exception. | 1595| 6600102 | The session does not exist. | 1596 1597**Example** 1598 1599```ts 1600import { BusinessError } from '@kit.BasicServicesKit'; 1601 1602currentAVSession.deactivate((err: BusinessError) => { 1603 if (err) { 1604 console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); 1605 } else { 1606 console.info('Deactivate : SUCCESS '); 1607 } 1608}); 1609``` 1610 1611## destroy<sup>10+</sup> 1612 1613destroy(): Promise\<void> 1614 1615Destroys this session. This API uses a promise to return the result. 1616 1617**Atomic service API**: This API can be used in atomic services since API version 12. 1618 1619**System capability**: SystemCapability.Multimedia.AVSession.Core 1620 1621**Return value** 1622 1623| Type | Description | 1624| -------------- | ----------------------------- | 1625| Promise\<void> | Promise used to return the result. If the session is destroyed, no value is returned; otherwise, an error object is returned.| 1626 1627**Error codes** 1628 1629For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1630 1631| ID| Error Message| 1632| -------- | ---------| 1633| 6600101 | Session service exception. | 1634| 6600102 | The session does not exist. | 1635 1636**Example** 1637 1638```ts 1639import { BusinessError } from '@kit.BasicServicesKit'; 1640 1641currentAVSession.destroy().then(() => { 1642 console.info('Destroy : SUCCESS '); 1643}).catch((err: BusinessError) => { 1644 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 1645}); 1646``` 1647 1648## destroy<sup>10+</sup> 1649 1650destroy(callback: AsyncCallback\<void>): void 1651 1652Destroys this session. This API uses an asynchronous callback to return the result. 1653 1654**System capability**: SystemCapability.Multimedia.AVSession.Core 1655 1656**Parameters** 1657 1658| Name | Type | Mandatory| Description | 1659| -------- | -------------------- | ---- | ---------- | 1660| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is destroyed, **err** is **undefined**; otherwise, **err** is an error object.| 1661 1662**Error codes** 1663 1664For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1665 1666| ID| Error Message| 1667| -------- | ---------| 1668| 6600101 | Session service exception. | 1669| 6600102 | The session does not exist. | 1670 1671**Example** 1672 1673```ts 1674import { BusinessError } from '@kit.BasicServicesKit'; 1675 1676currentAVSession.destroy((err: BusinessError) => { 1677 if (err) { 1678 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 1679 } else { 1680 console.info('Destroy : SUCCESS '); 1681 } 1682}); 1683``` 1684 1685## on('play')<sup>10+</sup> 1686 1687on(type: 'play', callback: () => void): void 1688 1689Subscribes to play command events. The subscription means that the application supports the play command. 1690 1691Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 1692 1693**Atomic service API**: This API can be used in atomic services since API version 12. 1694 1695**System capability**: SystemCapability.Multimedia.AVSession.Core 1696 1697**Parameters** 1698 1699| Name | Type | Mandatory| Description | 1700| -------- | -------------------- | ---- | --------- | 1701| type | string | Yes | Event type. The event **'play'** is triggered when the command for starting playback is sent to the session.| 1702| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1703 1704**Error codes** 1705 1706For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1707 1708| ID| Error Message| 1709| -------- | ---------| 1710| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1711| 6600101 | Session service exception. | 1712| 6600102 | The session does not exist. | 1713 1714**Example** 1715 1716```ts 1717currentAVSession.on('play', () => { 1718 console.info('on play entry'); 1719}); 1720``` 1721 1722## on('pause')<sup>10+</sup> 1723 1724on(type: 'pause', callback: () => void): void 1725 1726Subscribes to pause command events. The subscription means that the application supports the pause command. 1727 1728Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 1729 1730**Atomic service API**: This API can be used in atomic services since API version 12. 1731 1732**System capability**: SystemCapability.Multimedia.AVSession.Core 1733 1734**Parameters** 1735 1736| Name | Type | Mandatory| Description | 1737| -------- | -------------------- | ---- | --------- | 1738| type | string | Yes | Event type. The event **'pause'** is triggered when the command for pausing the playback is sent to the session.| 1739| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1740 1741**Error codes** 1742 1743For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1744 1745| ID| Error Message| 1746| -------- | ---------| 1747| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1748| 6600101 | Session service exception. | 1749| 6600102 | The session does not exist. | 1750 1751**Example** 1752 1753```ts 1754currentAVSession.on('pause', () => { 1755 console.info('on pause entry'); 1756}); 1757``` 1758 1759## on('stop')<sup>10+</sup> 1760 1761on(type:'stop', callback: () => void): void 1762 1763Subscribes to stop command events. The subscription means that the application supports the stop command. 1764 1765Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 1766 1767**Atomic service API**: This API can be used in atomic services since API version 12. 1768 1769**System capability**: SystemCapability.Multimedia.AVSession.Core 1770 1771**Parameters** 1772 1773| Name | Type | Mandatory| Description | 1774| -------- | -------------------- | ---- | --------- | 1775| type | string | Yes | Event type. The event **'stop'** is triggered when the command for stopping the playback is sent to the session.| 1776| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1777 1778**Error codes** 1779 1780For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1781 1782| ID| Error Message| 1783| -------- | ---------| 1784| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1785| 6600101 | Session service exception. | 1786| 6600102 | The session does not exist. | 1787 1788**Example** 1789 1790```ts 1791currentAVSession.on('stop', () => { 1792 console.info('on stop entry'); 1793}); 1794``` 1795 1796## on('playNext')<sup>10+</sup> 1797 1798on(type:'playNext', callback: () => void): void 1799 1800Subscribes to playNext command events. The subscription means that the application supports the playNext command. 1801 1802Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 1803 1804**Atomic service API**: This API can be used in atomic services since API version 12. 1805 1806**System capability**: SystemCapability.Multimedia.AVSession.Core 1807 1808**Parameters** 1809 1810| Name | Type | Mandatory| Description | 1811| -------- | -------------------- | ---- | --------- | 1812| type | string | Yes | Event type. The event **'playNext'** is triggered when the command for playing the next item is sent to the session.| 1813| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1814 1815**Error codes** 1816 1817For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1818 1819| ID| Error Message| 1820| -------- | ---------| 1821| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1822| 6600101 | Session service exception. | 1823| 6600102 | The session does not exist. | 1824 1825**Example** 1826 1827```ts 1828currentAVSession.on('playNext', () => { 1829 console.info('on playNext entry'); 1830}); 1831``` 1832 1833## on('playPrevious')<sup>10+</sup> 1834 1835on(type:'playPrevious', callback: () => void): void 1836 1837Subscribes to playPrevious command events. The subscription means that the application supports the playPrevious command. 1838 1839Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 1840 1841**Atomic service API**: This API can be used in atomic services since API version 12. 1842 1843**System capability**: SystemCapability.Multimedia.AVSession.Core 1844 1845**Parameters** 1846 1847| Name | Type | Mandatory| Description | 1848| -------- | -------------------- | ---- | --------- | 1849| type | string | Yes | Event type. The event **'playPrevious'** is triggered when the command for playing the previous item sent to the session.| 1850| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1851 1852**Error codes** 1853 1854For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1855 1856| ID| Error Message| 1857| -------- | ---------| 1858| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1859| 6600101 | Session service exception. | 1860| 6600102 | The session does not exist. | 1861 1862**Example** 1863 1864```ts 1865currentAVSession.on('playPrevious', () => { 1866 console.info('on playPrevious entry'); 1867}); 1868``` 1869 1870## on('fastForward')<sup>10+</sup> 1871 1872on(type: 'fastForward', callback: (time?: number) => void): void 1873 1874Subscribes to fastForward command events. The subscription means that the application supports the fastForward command. 1875 1876Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 1877 1878**Atomic service API**: This API can be used in atomic services since API version 12. 1879 1880**System capability**: SystemCapability.Multimedia.AVSession.Core 1881 1882**Parameters** 1883 1884| Name | Type | Mandatory| Description | 1885| -------- | -------------------- | ---- | --------- | 1886| type | string | Yes | Event type. The event **'fastForward'** is triggered when the command for fast forwarding is sent to the session.| 1887| callback | (time?: number) => void | Yes | Callback used for subscription. The **time** parameter in the callback indicates the time to seek to, in seconds. | 1888 1889**Error codes** 1890 1891For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1892 1893| ID| Error Message| 1894| -------- | ---------| 1895| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1896| 6600101 | Session service exception. | 1897| 6600102 | The session does not exist. | 1898 1899**Example** 1900 1901```ts 1902currentAVSession.on('fastForward', (time?: number) => { 1903 console.info('on fastForward entry'); 1904}); 1905``` 1906 1907## on('rewind')<sup>10+</sup> 1908 1909on(type:'rewind', callback: (time?: number) => void): void 1910 1911Subscribes to rewind command events. 1912 1913Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 1914 1915**Atomic service API**: This API can be used in atomic services since API version 12. 1916 1917**System capability**: SystemCapability.Multimedia.AVSession.Core 1918 1919**Parameters** 1920 1921| Name | Type | Mandatory| Description | 1922| -------- | -------------------- | ---- | --------- | 1923| type | string | Yes | Event type. The event **'rewind'** is triggered when the command for rewinding is sent to the session.| 1924| callback | (time?: number) => void | Yes | Callback used for subscription. The **time** parameter in the callback indicates the time to seek to, in seconds. | 1925 1926**Error codes** 1927 1928For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1929 1930| ID| Error Message| 1931| -------- | ---------| 1932| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1933| 6600101 | Session service exception. | 1934| 6600102 | The session does not exist. | 1935 1936**Example** 1937 1938```ts 1939currentAVSession.on('rewind', (time?: number) => { 1940 console.info('on rewind entry'); 1941}); 1942``` 1943 1944## on('playWithAssetId')<sup>20+</sup> 1945 1946on(type:'playWithAssetId', callback: Callback\<string>): void 1947 1948Subscribes to playback events with a given media asset ID. 1949 1950Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 1951 1952**Atomic service API**: This API can be used in atomic services since API version 20. 1953 1954**System capability**: SystemCapability.Multimedia.AVSession.Core 1955 1956**Parameters** 1957 1958| Name | Type | Mandatory| Description | 1959| -------- | -------------------- | ---- | --------- | 1960| type | string | Yes | Event type. The event **'playWithAssetId'** is triggered when the media asset ID is played.| 1961| callback | Callback\<string> | Yes | Callback The **assetId** parameter in the callback indicates the media asset ID. | 1962 1963**Error codes** 1964 1965For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1966 1967| ID| Error Message| 1968| -------- | ---------| 1969| 6600101 | Session service exception. | 1970| 6600102 | The session does not exist. | 1971 1972**Example** 1973 1974```ts 1975let playWithAssetIdCallback = (assetId: string) => { 1976 console.info(`on playWithAssetId entry, assetId = ${assetId}`); 1977} 1978currentAVSession.on('playWithAssetId', playWithAssetIdCallback); 1979``` 1980 1981## off('playWithAssetId')<sup>20+</sup> 1982 1983off(type: 'playWithAssetId', callback?: Callback\<string>): void 1984 1985Unsubscribes from playback events with a given media asset ID. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 1986 1987**Atomic service API**: This API can be used in atomic services since API version 20. 1988 1989**System capability**: SystemCapability.Multimedia.AVSession.Core 1990 1991**Parameters** 1992 1993| Name | Type | Mandatory| Description | 1994| -------- | -------------------- | ---- | ---------------------- | 1995| type | string | Yes | Event type, which is **'playWithAssetId'** in this case.| 1996| callback | Callback\<string> | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. The **assetId** parameter in the callback indicates the media asset ID. | 1997 1998**Error codes** 1999 2000For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2001 2002| ID| Error Message| 2003| -------- | ---------| 2004| 6600101 | Session service exception. | 2005| 6600102 | The session does not exist. | 2006 2007**Example** 2008 2009```ts 2010currentAVSession.off('playWithAssetId'); 2011``` 2012 2013## on('seek')<sup>10+</sup> 2014 2015on(type: 'seek', callback: (time: number) => void): void 2016 2017Subscribes to seek command events. 2018 2019Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2020 2021**Atomic service API**: This API can be used in atomic services since API version 12. 2022 2023**System capability**: SystemCapability.Multimedia.AVSession.Core 2024 2025**Parameters** 2026 2027| Name | Type | Mandatory| Description | 2028| -------- | ---------------------- | ---- | --------- | 2029| type | string | Yes | Event type. The event **'seek'** is triggered when the seek command is sent to the session.| 2030| callback | (time: number) => void | Yes | Callback used for subscription. The **time** parameter in the callback indicates the time to seek to, in milliseconds. | 2031 2032**Error codes** 2033 2034For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2035 2036| ID| Error Message| 2037| -------- | ---------| 2038| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2039| 6600101 | Session service exception. | 2040| 6600102 | The session does not exist. | 2041 2042**Example** 2043 2044```ts 2045currentAVSession.on('seek', (time: number) => { 2046 console.info(`on seek entry time : ${time}`); 2047}); 2048``` 2049 2050## on('setSpeed')<sup>10+</sup> 2051 2052on(type: 'setSpeed', callback: (speed: number) => void): void 2053 2054Subscribes to setSpeed command events. 2055 2056Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2057 2058**Atomic service API**: This API can be used in atomic services since API version 12. 2059 2060**System capability**: SystemCapability.Multimedia.AVSession.Core 2061 2062**Parameters** 2063 2064| Name | Type | Mandatory| Description | 2065| -------- | ----------------------- | ---- | --------- | 2066| type | string | Yes | Event type. The event **'setSpeed'** is triggered when the command for setting the playback speed is sent to the session.| 2067| callback | (speed: number) => void | Yes | Callback used for subscription. The **speed** parameter in the callback indicates the playback speed. | 2068 2069**Error codes** 2070 2071For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2072 2073| ID| Error Message| 2074| -------- | ---------| 2075| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2076| 6600101 | Session service exception. | 2077| 6600102 | The session does not exist. | 2078 2079**Example** 2080 2081```ts 2082currentAVSession.on('setSpeed', (speed: number) => { 2083 console.info(`on setSpeed speed : ${speed}`); 2084}); 2085``` 2086 2087## on('setLoopMode')<sup>10+</sup> 2088 2089on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void 2090 2091Subscribes to setLoopMode command events. 2092 2093Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2094 2095**Atomic service API**: This API can be used in atomic services since API version 12. 2096 2097**System capability**: SystemCapability.Multimedia.AVSession.Core 2098 2099**Parameters** 2100 2101| Name | Type | Mandatory| Description | 2102| -------- | ------------------------------------- | ---- | ---- | 2103| type | string | Yes | Event type. The event **'setLoopMode'** is triggered when the command for setting the loop mode is sent to the session.| 2104| callback | (mode: [LoopMode](arkts-apis-avsession-e.md#loopmode10)) => void | Yes | Callback used for subscription. The **mode** parameter in the callback indicates the loop mode. | 2105 2106**Error codes** 2107 2108For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2109 2110| ID| Error Message| 2111| -------- | ---------| 2112| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2113| 6600101 | Session service exception. | 2114| 6600102 | The session does not exist. | 2115 2116**Example** 2117 2118```ts 2119currentAVSession.on('setLoopMode', (mode: avSession.LoopMode) => { 2120 console.info(`on setLoopMode mode : ${mode}`); 2121}); 2122``` 2123 2124## on('setTargetLoopMode')<sup>18+</sup> 2125 2126on(type: 'setTargetLoopMode', callback: Callback\<LoopMode>): void 2127 2128Subscribes to setTargetLoopMode command events. 2129 2130Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2131 2132**Atomic service API**: This API can be used in atomic services since API version 18. 2133 2134**System capability**: SystemCapability.Multimedia.AVSession.Core 2135 2136**Parameters** 2137 2138| Name | Type | Mandatory| Description | 2139| -------- | ------------------------------------- | ---- | ---- | 2140| type | string | Yes | Event type. The event **'setTargetLoopMode'**<br>is triggered when the command for setting the target loop mode is sent to the session.| 2141| callback | Callback<[LoopMode](arkts-apis-avsession-e.md#loopmode10)> | Yes | Callback used for subscription. The **LoopMode** parameter in the callback indicates the target loop mode. | 2142 2143**Error codes** 2144 2145For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [AVSession Error Codes](errorcode-avsession.md). 2146 2147| ID| Error Message| 2148| -------- | ---------| 2149| 6600101 | Session service exception. | 2150| 6600102 | The session does not exist. | 2151 2152**Example** 2153 2154```ts 2155currentAVSession.on('setTargetLoopMode', (mode: avSession.LoopMode) => { 2156 console.info(`on setTargetLoopMode mode : ${mode}`); 2157}); 2158``` 2159 2160## on('toggleFavorite')<sup>10+</sup> 2161 2162on(type: 'toggleFavorite', callback: (assetId: string) => void): void 2163 2164Subscribes to toggleFavorite command events. 2165 2166Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2167 2168**Atomic service API**: This API can be used in atomic services since API version 12. 2169 2170**System capability**: SystemCapability.Multimedia.AVSession.Core 2171 2172**Parameters** 2173 2174| Name | Type | Mandatory| Description | 2175| -------- | ------------------------- | ---- | --------- | 2176| type | string | Yes | Event type. The event **'toggleFavorite'** is triggered when the command for favoriting the media asset is sent to the session.| 2177| callback | (assetId: string) => void | Yes | Callback used for subscription. The **assetId** parameter in the callback indicates the media asset ID. | 2178 2179**Error codes** 2180 2181For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2182 2183| ID| Error Message| 2184| -------- | ---------| 2185| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2186| 6600101 | Session service exception. | 2187| 6600102 | The session does not exist. | 2188 2189**Example** 2190 2191```ts 2192currentAVSession.on('toggleFavorite', (assetId: string) => { 2193 console.info(`on toggleFavorite mode : ${assetId}`); 2194}); 2195``` 2196 2197## on('skipToQueueItem')<sup>10+</sup> 2198 2199on(type: 'skipToQueueItem', callback: (itemId: number) => void): void 2200 2201Subscribes to the event that indicates an item in the playlist is selected. The session can play the selected item. 2202 2203Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2204 2205**Atomic service API**: This API can be used in atomic services since API version 12. 2206 2207**System capability**: SystemCapability.Multimedia.AVSession.Core 2208 2209**Parameters** 2210 2211| Name | Type | Mandatory| Description | 2212| -------- | ------------------------ | ---- | ------------------------------------- | 2213| type | string | Yes | Event type. The event **'skipToQueueItem'** is triggered when an item in the playlist is selected.| 2214| callback | (itemId: number) => void | Yes | Callback used for subscription. The **itemId** parameter in the callback indicates the ID of the selected item. | 2215 2216**Error codes** 2217 2218For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2219 2220| ID| Error Message| 2221| -------- | ---------| 2222| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2223| 6600101 | Session service exception. | 2224| 6600102 | The session does not exist. | 2225 2226**Example** 2227 2228```ts 2229currentAVSession.on('skipToQueueItem', (itemId: number) => { 2230 console.info(`on skipToQueueItem id : ${itemId}`); 2231}); 2232``` 2233 2234## on('handleKeyEvent')<sup>10+</sup> 2235 2236on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void 2237 2238Subscribes to key events of external devices such as Bluetooth and wired devices to listen for the play, pause, previous, next, fast-forward, and rewind commands in the key events. 2239 2240Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2241 2242**Atomic service API**: This API can be used in atomic services since API version 12. 2243 2244**System capability**: SystemCapability.Multimedia.AVSession.Core 2245 2246**Parameters** 2247 2248| Name | Type | Mandatory| Description | 2249| -------- | --------- | ---- | --------- | 2250| type | string | Yes | Event type. The event **'handleKeyEvent'** is triggered when a key event is sent to the session.| 2251| callback | (event: [KeyEvent](../apis-input-kit/js-apis-keyevent.md)) => void | Yes | Callback used for subscription. The **event** parameter in the callback indicates the key event. | 2252 2253**Error codes** 2254 2255For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2256 2257| ID| Error Message| 2258| -------- | --------- | 2259| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2260| 6600101 | Session service exception. | 2261| 6600102 | The session does not exist. | 2262 2263**Example** 2264 2265```ts 2266import { KeyEvent } from '@kit.InputKit'; 2267 2268currentAVSession.on('handleKeyEvent', (event: KeyEvent) => { 2269 console.info(`on handleKeyEvent event : ${event}`); 2270}); 2271 2272``` 2273 2274## on('outputDeviceChange')<sup>10+</sup> 2275 2276on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void 2277 2278Subscribes to output device change events. After the application integrates the [AVCastPicker component](ohos-multimedia-avcastpicker.md), the application receives the device change callback when the user switches the device through the component. 2279 2280Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2281 2282**Atomic service API**: This API can be used in atomic services since API version 12. 2283 2284**System capability**: SystemCapability.Multimedia.AVSession.Core 2285 2286**Parameters** 2287 2288| Name | Type| Mandatory| Description | 2289| -------- | ------------------------| ---- | --------- | 2290| type | string | Yes | Event type. The event **'outputDeviceChange'** is triggered when the output device changes.| 2291| callback | (state: [ConnectionState](arkts-apis-avsession-e.md#connectionstate10), device: [OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10)) => void | Yes | Callback function, where the **device** parameter specifies the output device information.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2292 2293**Error codes** 2294 2295For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2296 2297| ID| Error Message| 2298| -------- | ---------| 2299| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2300| 6600101 | Session service exception. | 2301| 6600102 | The session does not exist. | 2302 2303**Example** 2304 2305```ts 2306currentAVSession.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { 2307 console.info(`on outputDeviceChange device : ${device}`); 2308}); 2309``` 2310 2311## on('commonCommand')<sup>10+</sup> 2312 2313on(type: 'commonCommand', callback: (command: string, args: {[key: string]: Object}) => void): void 2314 2315Subscribes to custom control command change events. 2316 2317Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2318 2319**Atomic service API**: This API can be used in atomic services since API version 12. 2320 2321**System capability**: SystemCapability.Multimedia.AVSession.Core 2322 2323**Parameters** 2324 2325| Name | Type | Mandatory| Description | 2326| -------- | --------- | ---- | --------- | 2327| type | string | Yes | Event type. The event **'commonCommand'** is triggered when a custom control command changes.| 2328| callback | (command: string, args: {[key: string]: Object}) => void | Yes | Callback used for subscription. The **command** parameter in the callback indicates the name of the changed custom control command, and **args** indicates the parameters carried in the command. The parameters must be the same as those set in [sendCommonCommand](arkts-apis-avsession-AVSessionController.md#sendcommoncommand10). | 2329 2330**Error codes** 2331 2332For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2333 2334| ID| Error Message| 2335| -------- | ------------------------------ | 2336| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2337| 6600101 | Session service exception. | 2338| 6600102 | The session does not exist. | 2339 2340**Example** 2341 2342```ts 2343import { BusinessError } from '@kit.BasicServicesKit'; 2344import { avSession } from '@kit.AVSessionKit'; 2345@Entry 2346@Component 2347struct Index { 2348 @State message: string = 'hello world'; 2349 2350 build() { 2351 Column() { 2352 Text(this.message) 2353 .onClick(()=>{ 2354 let currentAVSession: avSession.AVSession | undefined = undefined; 2355 let tag = "createNewSession"; 2356 let context: Context = this.getUIContext().getHostContext() as Context; 2357 2358 avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2359 if (err) { 2360 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2361 } else { 2362 currentAVSession = data; 2363 } 2364 }); 2365 if (currentAVSession !== undefined) { 2366 (currentAVSession as avSession.AVSession).on('commonCommand', (commonCommand, args) => { 2367 console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`); 2368 }); 2369 } 2370 }) 2371 } 2372 .width('100%') 2373 .height('100%') 2374 } 2375} 2376``` 2377 2378## off('play')<sup>10+</sup> 2379 2380off(type: 'play', callback?: () => void): void 2381 2382Unsubscribes from play command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2383 2384**Atomic service API**: This API can be used in atomic services since API version 12. 2385 2386**System capability**: SystemCapability.Multimedia.AVSession.Core 2387 2388**Parameters** 2389 2390| Name | Type | Mandatory| Description | 2391| -------- | -------------------- | ---- | ---------------------- | 2392| type | string | Yes | Event type, which is **'play'** in this case.| 2393| callback | () => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2394 2395**Error codes** 2396 2397For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2398 2399| ID| Error Message| 2400| -------- | ---------| 2401| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2402| 6600101 | Session service exception. | 2403| 6600102 | The session does not exist. | 2404 2405**Example** 2406 2407```ts 2408currentAVSession.off('play'); 2409``` 2410 2411## off('pause')<sup>10+</sup> 2412 2413off(type: 'pause', callback?: () => void): void 2414 2415Unsubscribes from pause command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2416 2417**Atomic service API**: This API can be used in atomic services since API version 12. 2418 2419**System capability**: SystemCapability.Multimedia.AVSession.Core 2420 2421**Parameters** 2422 2423| Name | Type | Mandatory| Description | 2424| -------- | -------------------- | ---- | ---------------------- | 2425| type | string | Yes | Event type, which is **'pause'** in this case.| 2426| callback | () => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.| 2427 2428**Error codes** 2429 2430For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2431 2432| ID| Error Message| 2433| -------- | ---------| 2434| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2435| 6600101 | Session service exception. | 2436| 6600102 | The session does not exist. | 2437 2438**Example** 2439 2440```ts 2441currentAVSession.off('pause'); 2442``` 2443 2444## off('stop')<sup>10+</sup> 2445 2446off(type: 'stop', callback?: () => void): void 2447 2448Unsubscribes from stop command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2449 2450**Atomic service API**: This API can be used in atomic services since API version 12. 2451 2452**System capability**: SystemCapability.Multimedia.AVSession.Core 2453 2454**Parameters** 2455 2456| Name | Type | Mandatory| Description | 2457| -------- | -------------------- | ---- | ---------------------- | 2458| type | string | Yes | Event type, which is **'stop'** in this case.| 2459| callback | () => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2460 2461**Error codes** 2462 2463For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2464 2465| ID| Error Message| 2466| -------- | ---------| 2467| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2468| 6600101 | Session service exception. | 2469| 6600102 | The session does not exist. | 2470 2471**Example** 2472 2473```ts 2474currentAVSession.off('stop'); 2475``` 2476 2477## off('playNext')<sup>10+</sup> 2478 2479off(type: 'playNext', callback?: () => void): void 2480 2481Unsubscribes from playNext command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2482 2483**Atomic service API**: This API can be used in atomic services since API version 12. 2484 2485**System capability**: SystemCapability.Multimedia.AVSession.Core 2486 2487**Parameters** 2488 2489| Name | Type | Mandatory| Description | 2490| -------- | -------------------- | ---- | ---------------------- | 2491| type | string | Yes | Event type, which is **'playNext'** in this case.| 2492| callback | () => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2493 2494**Error codes** 2495 2496For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2497 2498| ID| Error Message| 2499| -------- | ---------| 2500| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2501| 6600101 | Session service exception. | 2502| 6600102 | The session does not exist. | 2503 2504**Example** 2505 2506```ts 2507currentAVSession.off('playNext'); 2508``` 2509 2510## off('playPrevious')<sup>10+</sup> 2511 2512off(type: 'playPrevious', callback?: () => void): void 2513 2514Unsubscribes from playPrevious command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2515 2516**Atomic service API**: This API can be used in atomic services since API version 12. 2517 2518**System capability**: SystemCapability.Multimedia.AVSession.Core 2519 2520**Parameters** 2521 2522| Name | Type | Mandatory| Description | 2523| -------- | -------------------- | ---- | ---------------------- | 2524| type | string | Yes | Event type, which is **'playPrevious'** in this case.| 2525| callback | () => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2526 2527**Error codes** 2528 2529For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2530 2531| ID| Error Message| 2532| -------- | ---------| 2533| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2534| 6600101 | Session service exception. | 2535| 6600102 | The session does not exist. | 2536 2537**Example** 2538 2539```ts 2540currentAVSession.off('playPrevious'); 2541``` 2542 2543## off('fastForward')<sup>10+</sup> 2544 2545off(type: 'fastForward', callback?: () => void): void 2546 2547Unsubscribes from fastForward command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2548 2549**Atomic service API**: This API can be used in atomic services since API version 12. 2550 2551**System capability**: SystemCapability.Multimedia.AVSession.Core 2552 2553**Parameters** 2554 2555| Name | Type | Mandatory| Description | 2556| -------- | -------------------- | ---- | ---------------------- | 2557| type | string | Yes | Event type, which is **'fastForward'** in this case.| 2558| callback | () => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2559 2560**Error codes** 2561 2562For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2563 2564| ID| Error Message| 2565| -------- | ---------| 2566| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2567| 6600101 | Session service exception. | 2568| 6600102 | The session does not exist. | 2569 2570**Example** 2571 2572```ts 2573currentAVSession.off('fastForward'); 2574``` 2575 2576## off('rewind')<sup>10+</sup> 2577 2578off(type: 'rewind', callback?: () => void): void 2579 2580Unsubscribes from rewind command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2581 2582**Atomic service API**: This API can be used in atomic services since API version 12. 2583 2584**System capability**: SystemCapability.Multimedia.AVSession.Core 2585 2586**Parameters** 2587 2588| Name | Type | Mandatory| Description | 2589| -------- | -------------------- | ---- | ---------------------- | 2590| type | string | Yes | Event type, which is **'rewind'** in this case.| 2591| callback | () => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2592 2593**Error codes** 2594 2595For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2596 2597| ID| Error Message| 2598| -------- | ---------| 2599| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2600| 6600101 | Session service exception. | 2601| 6600102 | The session does not exist. | 2602 2603**Example** 2604 2605```ts 2606currentAVSession.off('rewind'); 2607``` 2608 2609## off('seek')<sup>10+</sup> 2610 2611off(type: 'seek', callback?: (time: number) => void): void 2612 2613Unsubscribes from seek command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2614 2615**Atomic service API**: This API can be used in atomic services since API version 12. 2616 2617**System capability**: SystemCapability.Multimedia.AVSession.Core 2618 2619**Parameters** 2620 2621| Name | Type | Mandatory| Description | 2622| -------- | ---------------------- | ---- | ----------| 2623| type | string | Yes | Event type, which is **'seek'** in this case. | 2624| callback | (time: number) => void | No | Callback used for unsubscription. The **time** parameter in the callback indicates the time to seek to, in milliseconds.<br>If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2625 2626**Error codes** 2627 2628For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2629 2630| ID| Error Message| 2631| -------- | ---------| 2632| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2633| 6600101 | Session service exception. | 2634| 6600102 | The session does not exist. | 2635 2636**Example** 2637 2638```ts 2639currentAVSession.off('seek'); 2640``` 2641 2642## off('setSpeed')<sup>10+</sup> 2643 2644off(type: 'setSpeed', callback?: (speed: number) => void): void 2645 2646Unsubscribes from setSpeed command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2647 2648**Atomic service API**: This API can be used in atomic services since API version 12. 2649 2650**System capability**: SystemCapability.Multimedia.AVSession.Core 2651 2652**Parameters** 2653 2654| Name | Type | Mandatory| Description | 2655| -------- | ----------------------- | ---- | -------------------------------------------| 2656| type | string | Yes | Event type, which is **'setSpeed'** in this case. | 2657| callback | (speed: number) => void | No | Callback used for unsubscription. The **speed** parameter in the callback indicates the playback speed.<br>If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2658 2659**Error codes** 2660 2661For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2662 2663| ID| Error Message| 2664| -------- | ---------| 2665| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2666| 6600101 | Session service exception. | 2667| 6600102 | The session does not exist. | 2668 2669**Example** 2670 2671```ts 2672currentAVSession.off('setSpeed'); 2673``` 2674 2675## off('setLoopMode')<sup>10+</sup> 2676 2677off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void 2678 2679Unsubscribes from setLoopMode command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2680 2681**Atomic service API**: This API can be used in atomic services since API version 12. 2682 2683**System capability**: SystemCapability.Multimedia.AVSession.Core 2684 2685**Parameters** 2686 2687| Name | Type | Mandatory| Description | 2688| -------- | ------------------------------------- | ---- | ----- | 2689| type | string | Yes | Event type, which is **'setLoopMode'** in this case.| 2690| callback | (mode: [LoopMode](arkts-apis-avsession-e.md#loopmode10)) => void | No | Callback used for unsubscription. The **mode** parameter in the callback indicates the loop mode.<br>- If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>- The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.| 2691 2692**Error codes** 2693 2694For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2695 2696| ID| Error Message| 2697| -------- | ---------| 2698| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2699| 6600101 | Session service exception. | 2700| 6600102 | The session does not exist. | 2701 2702**Example** 2703 2704```ts 2705currentAVSession.off('setLoopMode'); 2706``` 2707 2708## off('setTargetLoopMode')<sup>18+</sup> 2709 2710off(type: 'setTargetLoopMode', callback?: Callback\<LoopMode>): void 2711 2712Unsubscribes from setTargetLoopMode command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2713 2714**Atomic service API**: This API can be used in atomic services since API version 18. 2715 2716**System capability**: SystemCapability.Multimedia.AVSession.Core 2717 2718**Parameters** 2719 2720| Name | Type | Mandatory| Description | 2721| -------- | ------------------------------------- | ---- | ----- | 2722| type | string | Yes | Event type, which is **'setTargetLoopMode'** in this case.| 2723| callback | Callback<[LoopMode](arkts-apis-avsession-e.md#loopmode10)> | No | Callback used for unsubscription. The **LoopMode** parameter in the callback indicates the target loop mode.<br>- If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>- The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.| 2724 2725**Error codes** 2726 2727For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [AVSession Error Codes](errorcode-avsession.md). 2728 2729| ID| Error Message| 2730| -------- | ---------| 2731| 6600101 | Session service exception. | 2732| 6600102 | The session does not exist. | 2733 2734**Example** 2735 2736```ts 2737currentAVSession.off('setTargetLoopMode'); 2738``` 2739 2740## off('toggleFavorite')<sup>10+</sup> 2741 2742off(type: 'toggleFavorite', callback?: (assetId: string) => void): void 2743 2744Unsubscribes from toggleFavorite command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2745 2746**Atomic service API**: This API can be used in atomic services since API version 12. 2747 2748**System capability**: SystemCapability.Multimedia.AVSession.Core 2749 2750**Parameters** 2751 2752| Name | Type | Mandatory| Description | 2753| -------- | ------------------------- | ---- | -------------------------| 2754| type | string | Yes | Event type, which is **'toggleFavorite'** in this case. | 2755| callback | (assetId: string) => void | No | Callback used for unsubscription. The **assetId** parameter in the callback indicates the media asset ID.<br>If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2756 2757**Error codes** 2758 2759For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2760 2761| ID| Error Message| 2762| -------- | ---------| 2763| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2764| 6600101 | Session service exception. | 2765| 6600102 | The session does not exist. | 2766 2767**Example** 2768 2769```ts 2770currentAVSession.off('toggleFavorite'); 2771``` 2772 2773## off('skipToQueueItem')<sup>10+</sup> 2774 2775off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void 2776 2777Unsubscribes from the event that indicates an item in the playlist is selected. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2778 2779**Atomic service API**: This API can be used in atomic services since API version 12. 2780 2781**System capability**: SystemCapability.Multimedia.AVSession.Core 2782 2783**Parameters** 2784 2785| Name | Type | Mandatory| Description | 2786| -------- | ------------------------ | ---- | ----------------------| 2787| type | string | Yes | Event type, which is **'skipToQueueItem'** in this case. | 2788| callback | (itemId: number) => void | No | Callback used for unsubscription. The **itemId** parameter in the callback indicates the ID of the item.<br>If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.| 2789 2790**Error codes** 2791 2792For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2793 2794| ID| Error Message| 2795| -------- | --------- | 2796| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2797| 6600101 | Session service exception. | 2798| 6600102 | The session does not exist. | 2799 2800**Example** 2801 2802```ts 2803currentAVSession.off('skipToQueueItem'); 2804``` 2805 2806## off('handleKeyEvent')<sup>10+</sup> 2807 2808off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void 2809 2810Unsubscribes from key events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2811 2812**Atomic service API**: This API can be used in atomic services since API version 12. 2813 2814**System capability**: SystemCapability.Multimedia.AVSession.Core 2815 2816**Parameters** 2817 2818| Name | Type | Mandatory| Description | 2819| -------- | --------- | ---- | --------- | 2820| type | string | Yes | Event type, which is **'handleKeyEvent'** in this case. | 2821| callback | (event: [KeyEvent](../apis-input-kit/js-apis-keyevent.md)) => void | No | Callback used for unsubscription. The **event** parameter in the callback indicates the key event.<br>If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2822 2823**Error codes** 2824 2825For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2826 2827| ID| Error Message| 2828| -------- | ---------| 2829| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2830| 6600101 | Session service exception. | 2831| 6600102 | The session does not exist. | 2832 2833**Example** 2834 2835```ts 2836currentAVSession.off('handleKeyEvent'); 2837``` 2838 2839## off('outputDeviceChange')<sup>10+</sup> 2840 2841off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void 2842 2843Unsubscribes from playback device change events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2844 2845**Atomic service API**: This API can be used in atomic services since API version 12. 2846 2847**System capability**: SystemCapability.Multimedia.AVSession.Core 2848 2849**Parameters** 2850 2851| Name | Type| Mandatory| Description | 2852| -------- | ------------------------| ---- | -----------------------| 2853| type | string | Yes | Event type, which is **'outputDeviceChange'** in this case. | 2854| callback | (state: [ConnectionState](arkts-apis-avsession-e.md#connectionstate10), device: [OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10)) => void | No | Callback function, where the **device** parameter specifies the output device information.<br>If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2855 2856**Error codes** 2857 2858For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2859 2860| ID| Error Message| 2861| -------- | ---------| 2862| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2863| 6600101 | Session service exception. | 2864| 6600102 | The session does not exist. | 2865 2866**Example** 2867 2868```ts 2869currentAVSession.off('outputDeviceChange'); 2870``` 2871 2872## off('commonCommand')<sup>10+</sup> 2873 2874off(type: 'commonCommand', callback?: (command: string, args: {[key: string]: Object}) => void): void 2875 2876Unsubscribes from custom control command change events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2877 2878**Atomic service API**: This API can be used in atomic services since API version 12. 2879 2880**System capability**: SystemCapability.Multimedia.AVSession.Core 2881 2882**Parameters** 2883 2884| Name | Type | Mandatory| Description | 2885| -------- | --------- | ---- | ----------------------| 2886| type | string | Yes | Event type, which is **'commonCommand'** in this case. | 2887| callback | (command: string, args: {[key: string]: Object}) => void | No | Callback used for unsubscription. The **command** parameter in the callback indicates the name of the changed custom control command, and **args** indicates the parameters carried in the command.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2888 2889**Error codes** 2890 2891For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2892 2893| ID| Error Message| 2894| -------- | ---------------- | 2895| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2896| 6600101 | Session service exception. | 2897| 6600102 | The session does not exist. | 2898 2899**Example** 2900 2901```ts 2902currentAVSession.off('commonCommand'); 2903``` 2904 2905## on('answer')<sup>11+</sup> 2906 2907on(type: 'answer', callback: Callback\<void>): void 2908 2909Subscribes to call answer events. 2910 2911Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2912 2913**Atomic service API**: This API can be used in atomic services since API version 12. 2914 2915**System capability**: SystemCapability.Multimedia.AVSession.Core 2916 2917**Parameters** 2918 2919| Name | Type | Mandatory| Description | 2920| -------- | --------- | ---- | --------- | 2921| type | string | Yes | Event type. The event **'answer'** is triggered when a call is answered.| 2922| callback | Callback\<void> | Yes | Callback used to return the result. | 2923 2924**Error codes** 2925 2926For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2927 2928| ID| Error Message| 2929| -------- | ------------------------------ | 2930| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2931| 6600101 | Session service exception. | 2932| 6600102 | The session does not exist. | 2933 2934**Example** 2935 2936```ts 2937currentAVSession.on('answer', () => { 2938 console.info('on call answer'); 2939}); 2940``` 2941 2942## off('answer')<sup>11+</sup> 2943 2944off(type: 'answer', callback?: Callback\<void>): void 2945 2946Unsubscribes from call answer events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 2947 2948**Atomic service API**: This API can be used in atomic services since API version 12. 2949 2950**System capability**: SystemCapability.Multimedia.AVSession.Core 2951 2952**Parameters** 2953 2954| Name | Type | Mandatory| Description | 2955| -------- | -------------------- | ---- | ---------------------- | 2956| type | string | Yes | Event type, which is **'answer'** in this case.| 2957| callback | Callback\<void> | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 2958 2959**Error codes** 2960 2961For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2962 2963| ID| Error Message| 2964| -------- | ---------| 2965| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2966| 6600101 | Session service exception. | 2967| 6600102 | The session does not exist. | 2968 2969**Example** 2970 2971```ts 2972currentAVSession.off('answer'); 2973``` 2974 2975## on('hangUp')<sup>11+</sup> 2976 2977on(type: 'hangUp', callback: Callback\<void>): void 2978 2979Subscribes to call hangup events. 2980 2981Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 2982 2983**Atomic service API**: This API can be used in atomic services since API version 12. 2984 2985**System capability**: SystemCapability.Multimedia.AVSession.Core 2986 2987**Parameters** 2988 2989| Name | Type | Mandatory| Description | 2990| -------- | --------- | ---- | --------- | 2991| type | string | Yes | Event type. The event **'hangUp'** is triggered when a call is hung up.| 2992| callback | Callback\<void> | Yes | Callback used to return the result. | 2993 2994**Error codes** 2995 2996For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2997 2998| ID| Error Message| 2999| -------- | ------------------------------ | 3000| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3001| 6600101 | Session service exception. | 3002| 6600102 | The session does not exist. | 3003 3004**Example** 3005 3006```ts 3007currentAVSession.on('hangUp', () => { 3008 console.info('on call hangUp'); 3009}); 3010``` 3011 3012## off('hangUp')<sup>11+</sup> 3013 3014off(type: 'hangUp', callback?: Callback\<void>): void 3015 3016Unsubscribes from call answer events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 3017 3018**Atomic service API**: This API can be used in atomic services since API version 12. 3019 3020**System capability**: SystemCapability.Multimedia.AVSession.Core 3021 3022**Parameters** 3023 3024| Name | Type | Mandatory| Description | 3025| -------- | -------------------- | ---- | ---------------------- | 3026| type | string | Yes | Event type, which is **'hangUp'** in this case.| 3027| callback | Callback\<void> | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 3028 3029**Error codes** 3030 3031For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3032 3033| ID| Error Message| 3034| -------- | ---------| 3035| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3036| 6600101 | Session service exception. | 3037| 6600102 | The session does not exist. | 3038 3039**Example** 3040 3041```ts 3042currentAVSession.off('hangUp'); 3043``` 3044 3045## on('toggleCallMute')<sup>11+</sup> 3046 3047on(type: 'toggleCallMute', callback: Callback\<void>): void 3048 3049Subscribes to call mute events. 3050 3051Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 3052 3053**Atomic service API**: This API can be used in atomic services since API version 12. 3054 3055**System capability**: SystemCapability.Multimedia.AVSession.Core 3056 3057**Parameters** 3058 3059| Name | Type | Mandatory| Description | 3060| -------- | --------- | ---- | --------- | 3061| type | string | Yes | Event type. The event **'toggleCallMute'** is triggered when a call is muted or unmuted.| 3062| callback | Callback\<void> | Yes | Callback used to return the result. | 3063 3064**Error codes** 3065 3066For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3067 3068| ID| Error Message| 3069| -------- | ------------------------------ | 3070| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3071| 6600101 | Session service exception. | 3072| 6600102 | The session does not exist. | 3073 3074**Example** 3075 3076```ts 3077currentAVSession.on('toggleCallMute', () => { 3078 console.info('on call toggleCallMute'); 3079}); 3080``` 3081 3082## off('toggleCallMute')<sup>11+</sup> 3083 3084off(type: 'toggleCallMute', callback?: Callback\<void>): void 3085 3086Unsubscribes from call mute events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 3087 3088**Atomic service API**: This API can be used in atomic services since API version 12. 3089 3090**System capability**: SystemCapability.Multimedia.AVSession.Core 3091 3092**Parameters** 3093 3094| Name | Type | Mandatory| Description | 3095| -------- | -------------------- | ---- | ---------------------- | 3096| type | string | Yes | Event type, which is **'toggleCallMute'** in this case.| 3097| callback | Callback\<void> | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 3098 3099**Error codes** 3100 3101For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3102 3103| ID| Error Message| 3104| -------- | ---------| 3105| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3106| 6600101 | Session service exception. | 3107| 6600102 | The session does not exist. | 3108 3109**Example** 3110 3111```ts 3112currentAVSession.off('toggleCallMute'); 3113``` 3114 3115## on('castDisplayChange')<sup>12+</sup> 3116 3117on(type: 'castDisplayChange', callback: Callback\<CastDisplayInfo>): void 3118 3119Subscribes to cast display change events in the case of extended screens. 3120 3121Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 3122 3123**Atomic service API**: This API can be used in atomic services since API version 12. 3124 3125**System capability**: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast 3126 3127**Parameters** 3128 3129| Name | Type | Mandatory| Description | 3130| -------- | -------------------- | ---- | ---------------------- | 3131| type | string | Yes | Event type. The event **'castDisplayChange'** is triggered when the cast display in the case of extended screens changes.| 3132| callback | Callback<[CastDisplayInfo](arkts-apis-avsession-i.md#castdisplayinfo12)>> | Yes | Callback used to return the information about the cast display. | 3133 3134**Error codes** 3135 3136For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3137 3138| ID| Error Message| 3139| -------- | ---------| 3140| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3141| 6600101 | Session service exception. | 3142| 6600102 | The session does not exist. | 3143 3144**Example** 3145 3146```ts 3147let castDisplay: avSession.CastDisplayInfo; 3148currentAVSession.on('castDisplayChange', (display: avSession.CastDisplayInfo) => { 3149 if (display.state === avSession.CastDisplayState.STATE_ON) { 3150 castDisplay = display; 3151 console.info(`Succeeded in castDisplayChange display : ${display.id} ON`); 3152 } else if (display.state === avSession.CastDisplayState.STATE_OFF){ 3153 console.info(`Succeeded in castDisplayChange display : ${display.id} OFF`); 3154 } 3155}); 3156``` 3157 3158## off('castDisplayChange')<sup>12+</sup> 3159 3160 off(type: 'castDisplayChange', callback?: Callback\<CastDisplayInfo>): void 3161 3162Unsubscribes from cast display change events in the case of extended screens. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 3163 3164**Atomic service API**: This API can be used in atomic services since API version 12. 3165 3166**System capability**: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast 3167 3168**Parameters** 3169 3170| Name | Type | Mandatory| Description | 3171| -------- | -------------------- | ---- | ---------------------- | 3172| type | string | Yes | Event type, which is **'castDisplayChange'** in this case.| 3173| callback | Callback<[CastDisplayInfo](arkts-apis-avsession-i.md#castdisplayinfo12) | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object. The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 3174 3175**Error codes** 3176 3177For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3178 3179| ID| Error Message| 3180| -------- | ---------| 3181| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3182| 6600101 | Session service exception. | 3183| 6600102 | The session does not exist. | 3184 3185**Example** 3186 3187```ts 3188currentAVSession.off('castDisplayChange'); 3189``` 3190 3191## stopCasting<sup>10+</sup> 3192 3193stopCasting(callback: AsyncCallback\<void>): void 3194 3195Stops castings. This API uses an asynchronous callback to return the result. 3196 3197**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3198 3199**Parameters** 3200 3201| Name | Type | Mandatory| Description | 3202| -------- | ------------------------------------- | ---- | ------------------------------------- | 3203| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object.| 3204 3205**Error codes** 3206 3207For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3208 3209| ID| Error Message| 3210| -------- | ---------| 3211| 6600109 | The remote connection is not established. | 3212 3213**Example** 3214 3215```ts 3216import { BusinessError } from '@kit.BasicServicesKit'; 3217 3218currentAVSession.stopCasting((err: BusinessError) => { 3219 if (err) { 3220 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 3221 } else { 3222 console.info('stopCasting successfully'); 3223 } 3224}); 3225``` 3226 3227## stopCasting<sup>10+</sup> 3228 3229stopCasting(): Promise\<void> 3230 3231Stops castings. This API uses a promise to return the result. 3232 3233**Atomic service API**: This API can be used in atomic services since API version 12. 3234 3235**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3236 3237**Return value** 3238 3239| Type | Description | 3240| -------------- | ----------------------------- | 3241| Promise\<void> | Promise used to return the result. If casting stops, no value is returned; otherwise, an error object is returned.| 3242 3243**Error codes** 3244 3245For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3246 3247| ID| Error Message| 3248| -------- | ---------| 3249| 6600109 | The remote connection is not established. | 3250 3251**Example** 3252 3253```ts 3254import { BusinessError } from '@kit.BasicServicesKit'; 3255 3256currentAVSession.stopCasting().then(() => { 3257 console.info('stopCasting successfully'); 3258}).catch((err: BusinessError) => { 3259 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 3260}); 3261``` 3262 3263## getOutputDeviceSync<sup>10+</sup> 3264 3265getOutputDeviceSync(): OutputDeviceInfo 3266 3267Obtains the output device information. This API returns the result synchronously. 3268 3269**Atomic service API**: This API can be used in atomic services since API version 12. 3270 3271**System capability**: SystemCapability.Multimedia.AVSession.Core 3272 3273**Return value** 3274 3275| Type | Description | 3276| ----------------| --------------------------------- | 3277| [OutputDeviceInfo](arkts-apis-avsession-i.md#outputdeviceinfo10) | Information about the output device.| 3278 3279**Error codes** 3280 3281For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3282 3283| ID | Error Message| 3284|---------| --------------------------------------- | 3285| 6600101 | Session service exception. | 3286| 6600102 | The session does not exist. | 3287 3288**Example** 3289 3290```ts 3291import { BusinessError } from '@kit.BasicServicesKit'; 3292 3293try { 3294 let currentOutputDevice: avSession.OutputDeviceInfo = currentAVSession.getOutputDeviceSync(); 3295} catch (err) { 3296 let error = err as BusinessError; 3297 console.error(`getOutputDeviceSync error, error code: ${error.code}, error message: ${error.message}`); 3298} 3299``` 3300 3301## getAllCastDisplays<sup>12+</sup> 3302 3303getAllCastDisplays(): Promise<Array\<CastDisplayInfo>> 3304 3305Obtains all displays that support extended screen projection in the current system. This API uses a promise to return the result. 3306 3307**Atomic service API**: This API can be used in atomic services since API version 12. 3308 3309**System capability**: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast 3310 3311**Return value** 3312 3313| Type | Description | 3314| ----------------| --------------------------------- | 3315| Promise<Array<[CastDisplayInfo](arkts-apis-avsession-i.md#castdisplayinfo12)>>| Promise used to return the information about all the cast displays.| 3316 3317**Error codes** 3318 3319For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3320 3321| ID | Error Message| 3322|---------| --------------------------------------- | 3323| 6600101 | Session service exception. | 3324| 6600102 | The session does not exist. | 3325 3326**Example** 3327 3328```ts 3329import { BusinessError } from '@kit.BasicServicesKit'; 3330 3331let castDisplay: avSession.CastDisplayInfo; 3332currentAVSession.getAllCastDisplays().then((data: Array< avSession.CastDisplayInfo >) => { 3333 if (data.length >= 1) { 3334 castDisplay = data[0]; 3335 } 3336 }).catch((err: BusinessError) => { 3337 console.error(`Failed to getAllCastDisplay. Code: ${err.code}, message: ${err.message}`); 3338 }); 3339``` 3340 3341## on('playFromAssetId')<sup>(deprecated)</sup> 3342 3343on(type:'playFromAssetId', callback: (assetId: number) => void): void 3344 3345Subscribes to playback events with a given media asset ID. 3346 3347> **NOTE** 3348> 3349> This API is supported since API version 11 and deprecated since API version 20. You are advised to use [on('playWithAssetId')](#onplaywithassetid20) instead. 3350 3351**Atomic service API**: This API can be used in atomic services since API version 12. 3352 3353**System capability**: SystemCapability.Multimedia.AVSession.Core 3354 3355**Parameters** 3356 3357| Name | Type | Mandatory| Description | 3358| -------- | -------------------- | ---- | --------- | 3359| type | string | Yes | Event type. The event **'playFromAssetId'** is triggered when the media asset ID is played.| 3360| callback | (assetId: number) => void | Yes | Callback The **assetId** parameter in the callback indicates the media asset ID. | 3361 3362**Error codes** 3363 3364For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3365 3366| ID| Error Message| 3367| -------- | ---------| 3368| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3369| 6600101 | Session service exception. | 3370| 6600102 | The session does not exist. | 3371 3372**Example** 3373 3374```ts 3375currentAVSession.on('playFromAssetId', (assetId: number) => { 3376 console.info('on playFromAssetId entry'); 3377}); 3378``` 3379 3380## off('playFromAssetId')<sup>(deprecated)</sup> 3381 3382off(type: 'playFromAssetId', callback?: (assetId: number) => void): void 3383 3384Unsubscribes from playback events with a given media asset ID. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 3385 3386> **NOTE** 3387> 3388> This API is supported since API version 11 and deprecated since API version 20. You are advised to use [off('playWithAssetId')](#offplaywithassetid20) instead. 3389 3390**Atomic service API**: This API can be used in atomic services since API version 12. 3391 3392**System capability**: SystemCapability.Multimedia.AVSession.Core 3393 3394**Parameters** 3395 3396| Name | Type | Mandatory| Description | 3397| -------- | -------------------- | ---- | ---------------------- | 3398| type | string | Yes | Event type, which is **'playFromAssetId'** in this case.| 3399| callback | (assetId: number) => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. The **assetId** parameter in the callback indicates the media asset ID. | 3400 3401**Error codes** 3402 3403For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3404 3405| ID| Error Message| 3406| -------- | ---------| 3407| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3408| 6600101 | Session service exception. | 3409| 6600102 | The session does not exist. | 3410 3411**Example** 3412 3413```ts 3414currentAVSession.off('playFromAssetId'); 3415``` 3416 3417## on('customDataChange')<sup>20+</sup> 3418 3419on(type: 'customDataChange', callback: Callback\<Record\<string, Object>>): void 3420 3421Subscribes to events indicating that custom data is sent to a remote device. 3422 3423**Atomic service API**: This API can be used in atomic services since API version 20. 3424 3425**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3426 3427**Parameters** 3428 3429| Name | Type | Mandatory| Description | 3430| -------- | -------------------------------- | ---- | ------------------------------------------------------------ | 3431| type | string | Yes | Event type. The event **'customDataChange'** is triggered when the provider sends custom data.| 3432| callback | Callback\<Record\<string, Object>> | Yes | Callback used to receive the custom data. | 3433 3434**Error codes** 3435 3436For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3437 3438| ID| Error Message | 3439| -------- | ------------------------------------------------------------ | 3440| 6600101 | Session service exception. | 3441| 6600102 | The session does not exist. | 3442 3443**Example** 3444 3445```ts 3446currentAVSession.on('customDataChange', (callback) => { 3447 console.info(`Caught customDataChange event,the new callback is: ${JSON.stringify(callback)}`); 3448}); 3449``` 3450 3451## off('customDataChange')<sup>20+</sup> 3452 3453off(type: 'customDataChange', callback?: Callback\<Record\<string, Object>>): void 3454 3455Unsubscribes from events indicating that custom data is sent to a remote device. 3456 3457**Atomic service API**: This API can be used in atomic services since API version 20. 3458 3459**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3460 3461**Parameters** 3462 3463| Name | Type | Mandatory| Description | 3464| -------- | -------------------------------- | ---- | ------------------------------------------------------------ | 3465| type | string | Yes | Event type, which is **'customDataChange'** in this case. | 3466| callback | Callback\<Record\<string, Object>> | No | Callback used for unsubscription. The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.| 3467 3468**Error codes** 3469 3470For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3471 3472| ID| Error Message | 3473| -------- | ------------------------------------------------------------ | 3474| 6600101 | Session service exception. | 3475| 6600102 | The session does not exist. | 3476 3477**Example** 3478 3479```ts 3480currentAVSession.off('customDataChange'); 3481``` 3482