1# @ohos.multimedia.avsession (AVSession Management) 2 3The **avSession** module provides APIs for media playback control so that applications can access the system's Media Controller. 4 5This module provides the following typical features related to media sessions: 6 7- [AVSession](#avsession10): used to set session metadata, playback state information, and more. 8- [AVSessionController](#avsessioncontroller10): used to obtain session IDs, send commands and events to sessions, and obtain the session metadata and playback state information. 9- [AVCastController](#avcastcontroller10): used to control playback, listen for remote playback state changes, and obtain the remote playback state in casting scenarios. 10 11> **NOTE** 12> 13> 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. 14 15## Modules to Import 16 17```ts 18import { avSession } from '@kit.AVSessionKit'; 19``` 20 21## avSession.createAVSession<sup>10+</sup> 22 23createAVSession(context: Context, tag: string, type: AVSessionType): Promise\<AVSession> 24 25Creates a media session. This API uses a promise to return the result. An ability can have only one session, and repeated calling of this API fails. 26 27**Atomic service API**: This API can be used in atomic services since API version 12. 28 29**System capability**: SystemCapability.Multimedia.AVSession.Core 30 31**Parameters** 32 33| Name| Type | Mandatory| Description | 34| ------ | ------------------------------- | ---- | ------------------------------ | 35| context| [Context](../apis-ability-kit/js-apis-inner-app-context.md) | Yes| Context of the UIAbility, which is used to obtain information about the application component.| 36| tag | string | Yes | Custom session name. | 37| type | [AVSessionType](#avsessiontype10) | Yes | Session type.| 38 39**Return value** 40 41| Type | Description | 42| --------------------------------- | ------------------------------------------------------------ | 43| Promise<[AVSession](#avsession10)\> | Promise used to return the media session obtained, which can be used to obtain the session ID, set the metadata and playback state information, and send key events.| 44 45**Error codes** 46 47For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 48 49| ID| Error Message| 50| -------- | ---------------------------------------- | 51| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 52| 6600101 | Session service exception. | 53 54**Example** 55 56```ts 57import { BusinessError } from '@kit.BasicServicesKit'; 58 59let currentAVSession: avSession.AVSession; 60let tag = "createNewSession"; 61let context: Context = getContext(this); 62let sessionId: string; // Used as an input parameter of subsequent functions. 63 64avSession.createAVSession(context, tag, "audio").then((data: avSession.AVSession) => { 65 currentAVSession = data; 66 sessionId = currentAVSession.sessionId; 67 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 68}).catch((err: BusinessError) => { 69 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 70}); 71``` 72 73## avSession.createAVSession<sup>10+</sup> 74 75createAVSession(context: Context, tag: string, type: AVSessionType, callback: AsyncCallback\<AVSession>): void 76 77Creates a media session. This API uses an asynchronous callback to return the result. An ability can have only one session, and repeated calling of this API fails. 78 79**System capability**: SystemCapability.Multimedia.AVSession.Core 80 81**Parameters** 82 83| Name | Type | Mandatory| Description | 84| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 85| context| [Context](../apis-ability-kit/js-apis-inner-app-context.md) | Yes| Context of the UIAbility, which is used to obtain information about the application component. | 86| tag | string | Yes | Custom session name. | 87| type | [AVSessionType](#avsessiontype10) | Yes | Session type. | 88| callback | AsyncCallback<[AVSession](#avsession10)\> | Yes | Callback used to return the media session obtained, which can be used to obtain the session ID, set the metadata and playback state information, and send key events.| 89 90**Error codes** 91 92For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 93 94| ID| Error Message| 95| -------- | ---------------------------------------- | 96| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 97| 6600101 | Session service exception. | 98 99**Example** 100 101```ts 102import { BusinessError } from '@kit.BasicServicesKit'; 103 104let currentAVSession: avSession.AVSession; 105let tag = "createNewSession"; 106let context: Context = getContext(this); 107let sessionId: string; // Used as an input parameter of subsequent functions. 108 109avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 110 if (err) { 111 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 112 } else { 113 currentAVSession = data; 114 sessionId = currentAVSession.sessionId; 115 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 116 } 117}); 118``` 119 120## ProtocolType<sup>11+</sup> 121 122Enumerates the protocol types supported by the remote device. 123 124**Atomic service API**: This API can be used in atomic services since API version 12. 125 126**System capability**: SystemCapability.Multimedia.AVSession.AVCast 127 128| Name | Value | Description | 129| --------------------------- | ---- | ----------- | 130| TYPE_LOCAL<sup>11+</sup> | 0 | Local device, which can be the built-in speaker or audio jack of the device, or an A2DP device.| 131| TYPE_CAST_PLUS_STREAM<sup>11+</sup> | 2 | Cast+ stream mode, indicating that the media asset is being displayed on another device.| 132| TYPE_DLNA<sup>12+</sup> | 4 | DLNA protocol, indicating that the media asset is being displayed on another device.| 133 134## AVSessionType<sup>10+<sup> 135 136type AVSessionType = 'audio' | 'video' | 'voice_call' | 'video_call' 137 138Enumerates the session types supported by the session. 139 140You can use the strings listed in the following table. 141 142**Atomic service API**: This API can be used in atomic services since API version 12. 143 144**System capability**: SystemCapability.Multimedia.AVSession.Core 145 146| Type | Description| 147| ----- | ---- | 148| 'audio' | Audio session.| 149| 'video' | Video session.| 150| 'voice_call'<sup>11+<sup> | Voice call.| 151| 'video_call'<sup>12+<sup> | Video call.| 152 153## AVSession<sup>10+</sup> 154 155An **AVSession** object is created by calling [avSession.createAVSession](#avsessioncreateavsession10). The object enables you to obtain the session ID and set the metadata and playback state. 156 157### Attributes 158 159**Atomic service API**: This API can be used in atomic services since API version 12. 160 161**System capability**: SystemCapability.Multimedia.AVSession.Core 162 163| Name | Type | Readable| Writable| Description | 164| :-------- | :----- | :--- | :--- | :---------------------------- | 165| sessionId | string | Yes | No | Unique session ID of the **AVSession** object.| 166| sessionType| [AVSessionType](#avsessiontype10) | Yes | No | AVSession type.| 167 168**Example** 169 170```ts 171let sessionId: string = currentAVSession.sessionId; 172let sessionType: avSession.AVSessionType = currentAVSession.sessionType; 173``` 174 175### setAVMetadata<sup>10+</sup> 176 177setAVMetadata(data: AVMetadata): Promise\<void> 178 179Sets session metadata. This API uses a promise to return the result. 180 181**Atomic service API**: This API can be used in atomic services since API version 12. 182 183**System capability**: SystemCapability.Multimedia.AVSession.Core 184 185**Parameters** 186 187| Name| Type | Mandatory| Description | 188| ------ | ------------------------- | ---- | ------------ | 189| data | [AVMetadata](#avmetadata10) | Yes | Session metadata.| 190 191**Return value** 192 193| Type | Description | 194| -------------- | ----------------------------- | 195| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 196 197**Error codes** 198 199For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 200 201| ID| Error Message| 202| -------- | ---------------------------------------- | 203| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 204| 6600101 | Session service exception. | 205| 6600102 | The session does not exist. | 206 207**Example** 208 209```ts 210import { BusinessError } from '@kit.BasicServicesKit'; 211 212let metadata: avSession.AVMetadata = { 213 assetId: "121278", 214 title: "lose yourself", 215 artist: "Eminem", 216 author: "ST", 217 album: "Slim shady", 218 writer: "ST", 219 composer: "ST", 220 duration: 2222, 221 mediaImage: "https://www.example.com/example.jpg", 222 subtitle: "8 Mile", 223 description: "Rap", 224 // The LRC contains two types of elements: time tag + lyrics, and ID tag. 225 // Example: [00:25.44]xxx\r\n[00:26.44]xxx\r\n 226 lyric: "Lyrics in LRC format", 227 previousAssetId: "121277", 228 nextAssetId: "121279" 229}; 230currentAVSession.setAVMetadata(metadata).then(() => { 231 console.info('SetAVMetadata successfully'); 232}).catch((err: BusinessError) => { 233 console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 234}); 235``` 236 237### setAVMetadata<sup>10+</sup> 238 239setAVMetadata(data: AVMetadata, callback: AsyncCallback\<void>): void 240 241Sets session metadata. This API uses an asynchronous callback to return the result. 242 243**System capability**: SystemCapability.Multimedia.AVSession.Core 244 245**Parameters** 246 247| Name | Type | Mandatory| Description | 248| -------- | ------------------------- | ---- | ------------------------------------- | 249| data | [AVMetadata](#avmetadata10) | Yes | Session metadata. | 250| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 251 252**Error codes** 253 254For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 255 256| ID| Error Message| 257| -------- | ---------------------------------------- | 258| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 259| 6600101 | Session service exception. | 260| 6600102 | The session does not exist. | 261 262**Example** 263 264```ts 265import { BusinessError } from '@kit.BasicServicesKit'; 266 267let metadata: avSession.AVMetadata = { 268 assetId: "121278", 269 title: "lose yourself", 270 artist: "Eminem", 271 author: "ST", 272 album: "Slim shady", 273 writer: "ST", 274 composer: "ST", 275 duration: 2222, 276 mediaImage: "https://www.example.com/example.jpg", 277 subtitle: "8 Mile", 278 description: "Rap", 279 // The LRC contains two types of elements: time tag + lyrics, and ID tag. 280 // Example: [00:25.44]xxx\r\n[00:26.44]xxx\r\n 281 lyric: "Lyrics in LRC format", 282 previousAssetId: "121277", 283 nextAssetId: "121279" 284}; 285currentAVSession.setAVMetadata(metadata, (err: BusinessError) => { 286 if (err) { 287 console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 288 } else { 289 console.info('SetAVMetadata successfully'); 290 } 291}); 292``` 293 294### setCallMetadata<sup>11+</sup> 295 296setCallMetadata(data: CallMetadata): Promise\<void> 297 298Sets call metadata. This API uses a promise to return the result. 299 300**System capability**: SystemCapability.Multimedia.AVSession.Core 301 302**Parameters** 303 304| Name| Type | Mandatory| Description | 305| ------ | ------------------------- | ---- | ------------ | 306| data | [CallMetadata](#callmetadata11) | Yes | Call metadata.| 307 308**Return value** 309 310| Type | Description | 311| -------------- | ----------------------------- | 312| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 313 314**Error codes** 315 316For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 317 318| ID| Error Message| 319| -------- | ---------------------------------------- | 320| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 321| 6600101 | Session service exception. | 322| 6600102 | The session does not exist. | 323 324**Example** 325 326```ts 327import { image } from '@kit.ImageKit'; 328import { resourceManager } from '@kit.LocalizationKit'; 329import { BusinessError } from '@kit.BasicServicesKit'; 330 331let value = await resourceManager.getSystemResourceManager().getRawFileContent('IMAGE_URI'); 332 let imageSource = await image.createImageSource(value.buffer); 333 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 334 let calldata: avSession.CallMetadata = { 335 name: "xiaoming", 336 phoneNumber: "111xxxxxxxx", 337 avatar: imagePixel 338 }; 339currentAVSession.setCallMetadata(calldata).then(() => { 340 console.info('setCallMetadata successfully'); 341}).catch((err: BusinessError) => { 342 console.error(`setCallMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 343}); 344``` 345 346### setCallMetadata<sup>11+</sup> 347 348setCallMetadata(data: CallMetadata, callback: AsyncCallback\<void>): void 349 350Sets call metadata. This API uses an asynchronous callback to return the result. 351 352**System capability**: SystemCapability.Multimedia.AVSession.Core 353 354**Parameters** 355 356| Name | Type | Mandatory| Description | 357| -------- | ------------------------- | ---- | ------------------------------------- | 358| data | [CallMetadata](#callmetadata11) | Yes | Call metadata. | 359| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 360 361**Error codes** 362 363For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 364 365| ID| Error Message| 366| -------- | ---------------------------------------- | 367| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 368| 6600101 | Session service exception. | 369| 6600102 | The session does not exist. | 370 371**Example** 372 373```ts 374import { image } from '@kit.ImageKit'; 375import { resourceManager } from '@kit.LocalizationKit'; 376import { BusinessError } from '@kit.BasicServicesKit'; 377 378async function setCallMetadata() { 379 let value = await resourceManager.getSystemResourceManager().getRawFileContent('IMAGE_URI'); 380 let imageSource = await image.createImageSource(value.buffer); 381 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 382 let calldata: avSession.CallMetadata = { 383 name: "xiaoming", 384 phoneNumber: "111xxxxxxxx", 385 avatar: imagePixel 386 }; 387 currentAVSession.setCallMetadata(calldata, (err: BusinessError) => { 388 if (err) { 389 console.error(`setCallMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 390 } else { 391 console.info('setCallMetadata successfully'); 392 } 393 }); 394} 395``` 396 397### setAVCallState<sup>11+</sup> 398 399setAVCallState(state: AVCallState): Promise\<void> 400 401Sets the call state. This API uses a promise to return the result. 402 403**System capability**: SystemCapability.Multimedia.AVSession.Core 404 405**Parameters** 406 407| Name| Type | Mandatory| Description | 408| ------ | ------------------------- | ---- | ------------ | 409| state | [AVCallState](#avcallstate11) | Yes | Call state.| 410 411**Return value** 412 413| Type | Description | 414| -------------- | ----------------------------- | 415| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 416 417**Error codes** 418 419For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 420 421| ID| Error Message| 422| -------- | ---------------------------------------- | 423| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 424| 6600101 | Session service exception. | 425| 6600102 | The session does not exist. | 426 427**Example** 428 429```ts 430import { BusinessError } from '@kit.BasicServicesKit'; 431 432let calldata: avSession.AVCallState = { 433 state: avSession.CallState.CALL_STATE_ACTIVE, 434 muted: false 435}; 436currentAVSession.setAVCallState(calldata).then(() => { 437 console.info('setAVCallState successfully'); 438}).catch((err: BusinessError) => { 439 console.error(`setAVCallState BusinessError: code: ${err.code}, message: ${err.message}`); 440}); 441``` 442 443### setAVCallState<sup>11+</sup> 444 445setAVCallState(state: AVCallState, callback: AsyncCallback\<void>): void 446 447Sets the call state. This API uses an asynchronous callback to return the result. 448 449**System capability**: SystemCapability.Multimedia.AVSession.Core 450 451**Parameters** 452 453| Name | Type | Mandatory| Description | 454| -------- | ------------------------- | ---- | ------------------------------------- | 455| state | [AVCallState](#avcallstate11) | Yes | Call state. | 456| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 457 458**Error codes** 459 460For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 461 462| ID| Error Message| 463| -------- | ---------------------------------------- | 464| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 465| 6600101 | Session service exception. | 466| 6600102 | The session does not exist. | 467 468**Example** 469 470```ts 471import { BusinessError } from '@kit.BasicServicesKit'; 472 473let avcalldata: avSession.AVCallState = { 474 state: avSession.CallState.CALL_STATE_ACTIVE, 475 muted: false 476}; 477currentAVSession.setAVCallState(avcalldata, (err: BusinessError) => { 478 if (err) { 479 console.error(`setAVCallState BusinessError: code: ${err.code}, message: ${err.message}`); 480 } else { 481 console.info('setAVCallState successfully'); 482 } 483}); 484``` 485 486### setAVPlaybackState<sup>10+</sup> 487 488setAVPlaybackState(state: AVPlaybackState): Promise\<void> 489 490Sets information related to the session playback state. This API uses a promise to return the result. 491 492**Atomic service API**: This API can be used in atomic services since API version 12. 493 494**System capability**: SystemCapability.Multimedia.AVSession.Core 495 496**Parameters** 497 498| Name| Type | Mandatory| Description | 499| ------ | ----------------------------------- | ---- | ---------------------------------------------- | 500| state | [AVPlaybackState](#avplaybackstate10) | Yes | Information related to the session playback state.| 501 502**Return value** 503 504| Type | Description | 505| -------------- | ----------------------------- | 506| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 507 508**Error codes** 509 510For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 511 512| ID| Error Message| 513| -------- | ---------------------------------------- | 514| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 515| 6600101 | Session service exception. | 516| 6600102 | The session does not exist. | 517 518**Example** 519 520```ts 521import { BusinessError } from '@kit.BasicServicesKit'; 522 523let playbackState: avSession.AVPlaybackState = { 524 state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, 525 speed: 1.0, 526 position:{elapsedTime:10, updateTime:(new Date()).getTime()}, 527 bufferedTime:1000, 528 loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, 529 isFavorite:true 530}; 531currentAVSession.setAVPlaybackState(playbackState).then(() => { 532 console.info('SetAVPlaybackState successfully'); 533}).catch((err: BusinessError) => { 534 console.error(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 535}); 536``` 537 538### setAVPlaybackState<sup>10+</sup> 539 540setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\<void>): void 541 542Sets information related to the session playback state. This API uses an asynchronous callback to return the result. 543 544**System capability**: SystemCapability.Multimedia.AVSession.Core 545 546**Parameters** 547 548| Name | Type | Mandatory| Description | 549| -------- | ----------------------------------- | ---- | ---------------------------------------------- | 550| state | [AVPlaybackState](#avplaybackstate10) | Yes | Information related to the session playback state.| 551| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object. | 552 553**Error codes** 554 555For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 556 557| ID| Error Message| 558| -------- | ---------------------------------------- | 559| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 560| 6600101 | Session service exception. | 561| 6600102 | The session does not exist. | 562 563**Example** 564 565```ts 566import { BusinessError } from '@kit.BasicServicesKit'; 567 568let PlaybackState: avSession.AVPlaybackState = { 569 state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, 570 speed: 1.0, 571 position:{elapsedTime:10, updateTime:(new Date()).getTime()}, 572 bufferedTime:1000, 573 loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, 574 isFavorite:true 575}; 576currentAVSession.setAVPlaybackState(PlaybackState, (err: BusinessError) => { 577 if (err) { 578 console.error(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 579 } else { 580 console.info('SetAVPlaybackState successfully'); 581 } 582}); 583``` 584 585### setLaunchAbility<sup>10+</sup> 586 587setLaunchAbility(ability: WantAgent): Promise\<void> 588 589Sets a launcher ability. This API uses a promise to return the result. 590 591**Atomic service API**: This API can be used in atomic services since API version 12. 592 593**System capability**: SystemCapability.Multimedia.AVSession.Core 594 595**Parameters** 596 597| Name | Type | Mandatory| Description | 598| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 599| ability | [WantAgent](../apis-ability-kit/js-apis-app-ability-wantAgent.md) | Yes | Application attributes, such as the bundle name, ability name, and deviceID.| 600 601**Return value** 602 603| Type | Description | 604| -------------- | ----------------------------- | 605| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 606 607**Error codes** 608 609For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 610 611| ID| Error Message| 612| -------- | ---------------------------------------- | 613| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 614| 6600101 | Session service exception. | 615| 6600102 | The session does not exist. | 616 617**Example** 618 619```ts 620import { wantAgent } from '@kit.AbilityKit'; 621import { BusinessError } from '@kit.BasicServicesKit'; 622 623// WantAgentInfo object. 624let wantAgentInfo: wantAgent.WantAgentInfo = { 625 wants: [ 626 { 627 deviceId: "deviceId", 628 bundleName: "com.example.myapplication", 629 abilityName: "EntryAbility", 630 action: "action1", 631 entities: ["entity1"], 632 type: "MIMETYPE", 633 uri: "key = {true,true,false}", 634 parameters: 635 { 636 mykey0: 2222, 637 mykey1: [1, 2, 3], 638 mykey2: "[1, 2, 3]", 639 mykey3: "ssssssssssssssssssssssssss", 640 mykey4: [false, true, false], 641 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 642 mykey6: true 643 } 644 } 645 ], 646 operationType: wantAgent.OperationType.START_ABILITIES, 647 requestCode: 0, 648 wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 649} 650 651wantAgent.getWantAgent(wantAgentInfo).then((agent) => { 652 currentAVSession.setLaunchAbility(agent).then(() => { 653 console.info('SetLaunchAbility successfully'); 654 }).catch((err: BusinessError) => { 655 console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 656 }); 657}); 658``` 659 660### setLaunchAbility<sup>10+</sup> 661 662setLaunchAbility(ability: WantAgent, callback: AsyncCallback\<void>): void 663 664Sets a launcher ability. This API uses an asynchronous callback to return the result. 665 666**System capability**: SystemCapability.Multimedia.AVSession.Core 667 668**Parameters** 669 670| Name | Type | Mandatory| Description | 671| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 672| ability | [WantAgent](../apis-ability-kit/js-apis-app-ability-wantAgent.md) | Yes | Application attributes, such as the bundle name, ability name, and deviceID. | 673| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 674 675**Error codes** 676 677For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 678 679| ID| Error Message| 680| -------- | ---------------------------------------- | 681| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 682| 6600101 | Session service exception. | 683| 6600102 | The session does not exist. | 684 685**Example** 686 687```ts 688import { wantAgent } from '@kit.AbilityKit'; 689import { BusinessError } from '@kit.BasicServicesKit'; 690 691// WantAgentInfo object. 692let wantAgentInfo: wantAgent.WantAgentInfo = { 693 wants: [ 694 { 695 deviceId: "deviceId", 696 bundleName: "com.example.myapplication", 697 abilityName: "EntryAbility", 698 action: "action1", 699 entities: ["entity1"], 700 type: "MIMETYPE", 701 uri: "key = {true,true,false}", 702 parameters: 703 { 704 mykey0: 2222, 705 mykey1: [1, 2, 3], 706 mykey2: "[1, 2, 3]", 707 mykey3: "ssssssssssssssssssssssssss", 708 mykey4: [false, true, false], 709 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 710 mykey6: true 711 } 712 } 713 ], 714 operationType: wantAgent.OperationType.START_ABILITIES, 715 requestCode: 0, 716 wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 717} 718 719wantAgent.getWantAgent(wantAgentInfo).then((agent) => { 720 currentAVSession.setLaunchAbility(agent, (err: BusinessError) => { 721 if (err) { 722 console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 723 } else { 724 console.info('SetLaunchAbility successfully'); 725 } 726 }); 727}); 728``` 729 730### dispatchSessionEvent<sup>10+</sup> 731 732dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\<void> 733 734Dispatches 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. 735 736**Atomic service API**: This API can be used in atomic services since API version 12. 737 738**System capability**: SystemCapability.Multimedia.AVSession.Core 739 740**Parameters** 741 742| Name | Type | Mandatory| Description | 743| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 744| event | string | Yes | Name of the session event.| 745| args | {[key: string]: Object} | Yes | Content of the session event.| 746 747> **NOTE** 748> 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). 749 750**Return value** 751 752| Type | Description | 753| -------------- | ----------------------------- | 754| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 755 756**Error codes** 757 758For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 759 760| ID| Error Message| 761| -------- | ---------------------------------------- | 762| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 763| 6600101 | Session service exception. | 764| 6600102 | The session does not exist. | 765 766**Example** 767 768```ts 769import { BusinessError } from '@kit.BasicServicesKit'; 770 771let currentAVSession: avSession.AVSession | undefined = undefined; 772let tag = "createNewSession"; 773let context: Context = getContext(this); 774 775avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 776 if (err) { 777 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 778 } else { 779 currentAVSession = data; 780 } 781}); 782let eventName = "dynamic_lyric"; 783if (currentAVSession !== undefined) { 784 (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}).then(() => { 785 console.info('dispatchSessionEvent successfully'); 786 }).catch((err: BusinessError) => { 787 console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); 788 }) 789} 790``` 791 792### dispatchSessionEvent<sup>10+</sup> 793 794dispatchSessionEvent(event: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void 795 796Dispatches 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. 797 798**System capability**: SystemCapability.Multimedia.AVSession.Core 799 800**Parameters** 801 802| Name | Type | Mandatory| Description | 803| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 804| event | string | Yes | Name of the session event.| 805| args | {[key: string]: Object} | Yes | Content of the session event.| 806| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 807 808> **NOTE** 809 810> 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). 811 812**Error codes** 813 814For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 815 816| ID| Error Message| 817| -------- | ---------------------------------------- | 818| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 819| 6600101 | Session service exception. | 820| 6600102 | The session does not exist. | 821 822**Example** 823 824```ts 825import { BusinessError } from '@kit.BasicServicesKit'; 826 827let currentAVSession: avSession.AVSession | undefined = undefined; 828let tag = "createNewSession"; 829let context: Context = getContext(this); 830 831avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 832 if (err) { 833 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 834 } else { 835 currentAVSession = data; 836 } 837}); 838let eventName: string = "dynamic_lyric"; 839if (currentAVSession !== undefined) { 840 (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}, (err: BusinessError) => { 841 if (err) { 842 console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); 843 } 844 }) 845} 846``` 847 848### setAVQueueItems<sup>10+</sup> 849 850setAVQueueItems(items: Array\<AVQueueItem>): Promise\<void> 851 852Sets a playlist. This API uses a promise to return the result. 853 854**Atomic service API**: This API can be used in atomic services since API version 12. 855 856**System capability**: SystemCapability.Multimedia.AVSession.Core 857 858**Parameters** 859 860| Name | Type | Mandatory| Description | 861| ------ | ------------------------------------ | ---- | ---------------------------------- | 862| items | Array<[AVQueueItem](#avqueueitem10)\> | Yes | Playlist to set.| 863 864**Return value** 865 866| Type | Description | 867| -------------- | ----------------------------- | 868| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 869 870**Error codes** 871 872For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 873 874| ID| Error Message| 875| -------- | ---------------------------------------- | 876| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 877| 6600101 | Session service exception. | 878| 6600102 | The session does not exist. | 879 880**Example** 881 882```ts 883import { image } from '@kit.ImageKit'; 884import { resourceManager } from '@kit.LocalizationKit'; 885import { BusinessError } from '@kit.BasicServicesKit'; 886 887async function setAVQueueItems() { 888 let value = await resourceManager.getSystemResourceManager().getRawFileContent('IMAGE_URI'); 889 let imageSource = await image.createImageSource(value.buffer); 890 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 891 let queueItemDescription_1: avSession.AVMediaDescription = { 892 assetId: '001', 893 title: 'music_name', 894 subtitle: 'music_sub_name', 895 description: 'music_description', 896 mediaImage : imagePixel, 897 extras: {extras:'any'} 898 }; 899 let queueItem_1: avSession.AVQueueItem = { 900 itemId: 1, 901 description: queueItemDescription_1 902 }; 903 let queueItemDescription_2: avSession.AVMediaDescription = { 904 assetId: '002', 905 title: 'music_name', 906 subtitle: 'music_sub_name', 907 description: 'music_description', 908 mediaImage: imagePixel, 909 extras: {extras:'any'} 910 }; 911 let queueItem_2: avSession.AVQueueItem = { 912 itemId: 2, 913 description: queueItemDescription_2 914 }; 915 let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; 916 currentAVSession.setAVQueueItems(queueItemsArray).then(() => { 917 console.info('SetAVQueueItems successfully'); 918 }).catch((err: BusinessError) => { 919 console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 920 }); 921} 922``` 923 924### setAVQueueItems<sup>10+</sup> 925 926setAVQueueItems(items: Array\<AVQueueItem>, callback: AsyncCallback\<void>): void 927 928Sets a playlist. This API uses an asynchronous callback to return the result. 929 930**System capability**: SystemCapability.Multimedia.AVSession.Core 931 932**Parameters** 933 934| Name | Type | Mandatory| Description | 935| -------- | ------------------------------------ | ---- | ----------------------------------------------------------- | 936| items | Array<[AVQueueItem](#avqueueitem10)\> | Yes | Playlist to set. | 937| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 938 939**Error codes** 940 941For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 942 943| ID| Error Message| 944| -------- | ---------------------------------------- | 945| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 946| 6600101 | Session service exception. | 947| 6600102 | The session does not exist. | 948 949**Example** 950 951```ts 952import { image } from '@kit.ImageKit'; 953import { resourceManager } from '@kit.LocalizationKit'; 954import { BusinessError } from '@kit.BasicServicesKit'; 955 956async function setAVQueueItems() { 957 let value = await resourceManager.getSystemResourceManager().getRawFileContent('IMAGE_URI'); 958 let imageSource = await image.createImageSource(value.buffer); 959 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 960 let queueItemDescription_1: avSession.AVMediaDescription = { 961 assetId: '001', 962 title: 'music_name', 963 subtitle: 'music_sub_name', 964 description: 'music_description', 965 mediaImage : imagePixel, 966 extras: {extras:'any'} 967 }; 968 let queueItem_1: avSession.AVQueueItem = { 969 itemId: 1, 970 description: queueItemDescription_1 971 }; 972 let queueItemDescription_2: avSession.AVMediaDescription = { 973 assetId: '002', 974 title: 'music_name', 975 subtitle: 'music_sub_name', 976 description: 'music_description', 977 mediaImage: imagePixel, 978 extras: {extras:'any'} 979 }; 980 let queueItem_2: avSession.AVQueueItem = { 981 itemId: 2, 982 description: queueItemDescription_2 983 }; 984 let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; 985 currentAVSession.setAVQueueItems(queueItemsArray, (err: BusinessError) => { 986 if (err) { 987 console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 988 } else { 989 console.info('SetAVQueueItems successfully'); 990 } 991 }); 992} 993``` 994 995### setAVQueueTitle<sup>10+</sup> 996 997setAVQueueTitle(title: string): Promise\<void> 998 999Sets a name for the playlist. This API uses a promise to return the result. 1000 1001**Atomic service API**: This API can be used in atomic services since API version 12. 1002 1003**System capability**: SystemCapability.Multimedia.AVSession.Core 1004 1005**Parameters** 1006 1007| Name | Type | Mandatory| Description | 1008| ------ | ------ | ---- | -------------- | 1009| title | string | Yes | Name of the playlist.| 1010 1011**Return value** 1012 1013| Type | Description | 1014| -------------- | ----------------------------- | 1015| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 1016 1017**Error codes** 1018 1019For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1020 1021| ID| Error Message| 1022| -------- | ---------------------------------------- | 1023| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 1024| 6600101 | Session service exception. | 1025| 6600102 | The session does not exist. | 1026 1027**Example** 1028 1029```ts 1030import { BusinessError } from '@kit.BasicServicesKit'; 1031 1032let queueTitle = 'QUEUE_TITLE'; 1033currentAVSession.setAVQueueTitle(queueTitle).then(() => { 1034 console.info('SetAVQueueTitle successfully'); 1035}).catch((err: BusinessError) => { 1036 console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 1037}); 1038``` 1039 1040### setAVQueueTitle<sup>10+</sup> 1041 1042setAVQueueTitle(title: string, callback: AsyncCallback\<void>): void 1043 1044Sets a name for the playlist. This API uses an asynchronous callback to return the result. 1045 1046**System capability**: SystemCapability.Multimedia.AVSession.Core 1047 1048**Parameters** 1049 1050| Name | Type | Mandatory| Description | 1051| -------- | --------------------- | ---- | ----------------------------------------------------------- | 1052| title | string | Yes | Name of the playlist. | 1053| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1054 1055**Error codes** 1056 1057For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1058 1059| ID| Error Message| 1060| -------- | ---------------------------------------- | 1061| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 1062| 6600101 | Session service exception. | 1063| 6600102 | The session does not exist. | 1064 1065**Example** 1066 1067```ts 1068import { BusinessError } from '@kit.BasicServicesKit'; 1069 1070let queueTitle = 'QUEUE_TITLE'; 1071currentAVSession.setAVQueueTitle(queueTitle, (err: BusinessError) => { 1072 if (err) { 1073 console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 1074 } else { 1075 console.info('SetAVQueueTitle successfully'); 1076 } 1077}); 1078``` 1079 1080### setExtras<sup>10+</sup> 1081 1082setExtras(extras: {[key: string]: Object}): Promise\<void> 1083 1084Sets 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. 1085 1086**Atomic service API**: This API can be used in atomic services since API version 12. 1087 1088**System capability**: SystemCapability.Multimedia.AVSession.Core 1089 1090**Parameters** 1091 1092| Name | Type | Mandatory| Description | 1093| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 1094| extras | {[key: string]: Object} | Yes | Key-value pairs of the custom media packet.| 1095 1096> **NOTE** 1097 1098> 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). 1099 1100**Return value** 1101 1102| Type | Description | 1103| -------------- | ----------------------------- | 1104| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 1105 1106**Error codes** 1107 1108For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1109 1110| ID| Error Message| 1111| -------- | ---------------------------------------- | 1112| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 1113| 6600101 | Session service exception. | 1114| 6600102 | The session does not exist. | 1115 1116**Example** 1117 1118```ts 1119import { BusinessError } from '@kit.BasicServicesKit'; 1120 1121let currentAVSession: avSession.AVSession | undefined = undefined; 1122let tag = "createNewSession"; 1123let context: Context = getContext(this); 1124 1125avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1126 if (err) { 1127 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1128 } else { 1129 currentAVSession = data; 1130 } 1131}); 1132if (currentAVSession !== undefined) { 1133 (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}).then(() => { 1134 console.info('setExtras successfully'); 1135 }).catch((err: BusinessError) => { 1136 console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); 1137 }) 1138} 1139``` 1140 1141### setExtras<sup>10+</sup> 1142 1143setExtras(extras: {[key: string]: Object}, callback: AsyncCallback\<void>): void 1144 1145Sets 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. 1146 1147**System capability**: SystemCapability.Multimedia.AVSession.Core 1148 1149**Parameters** 1150 1151| Name | Type | Mandatory| Description | 1152| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 1153| extras | {[key: string]: Object} | Yes | Key-value pairs of the custom media packet.| 1154| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1155 1156> **NOTE** 1157 1158> 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). 1159 1160**Error codes** 1161 1162For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1163 1164| ID| Error Message| 1165| -------- | ---------------------------------------- | 1166| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 1167| 6600101 | Session service exception. | 1168| 6600102 | The session does not exist. | 1169 1170**Example** 1171 1172```ts 1173import { BusinessError } from '@kit.BasicServicesKit'; 1174 1175let currentAVSession: avSession.AVSession | undefined = undefined; 1176let tag = "createNewSession"; 1177let context: Context = getContext(this); 1178 1179avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1180 if (err) { 1181 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1182 } else { 1183 currentAVSession = data; 1184 } 1185}); 1186if (currentAVSession !== undefined) { 1187 (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}, (err: BusinessError) => { 1188 if (err) { 1189 console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); 1190 } 1191 }) 1192} 1193``` 1194 1195### getController<sup>10+</sup> 1196 1197getController(): Promise\<AVSessionController> 1198 1199Obtains the controller corresponding to this session. This API uses a promise to return the result. 1200 1201**Atomic service API**: This API can be used in atomic services since API version 12. 1202 1203**System capability**: SystemCapability.Multimedia.AVSession.Core 1204 1205**Return value** 1206 1207| Type | Description | 1208| ---------------------------------------------------- | ----------------------------- | 1209| Promise<[AVSessionController](#avsessioncontroller10)> | Promise used to return the session controller.| 1210 1211**Error codes** 1212 1213For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1214 1215| ID| Error Message| 1216| -------- | ---------------------------------------- | 1217| 6600101 | Session service exception. | 1218| 6600102 | The session does not exist. | 1219 1220**Example** 1221 1222```ts 1223import { BusinessError } from '@kit.BasicServicesKit'; 1224 1225let avsessionController: avSession.AVSessionController; 1226currentAVSession.getController().then((avcontroller: avSession.AVSessionController) => { 1227 avsessionController = avcontroller; 1228 console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); 1229}).catch((err: BusinessError) => { 1230 console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); 1231}); 1232``` 1233 1234### getController<sup>10+</sup> 1235 1236getController(callback: AsyncCallback\<AVSessionController>): void 1237 1238Obtains the controller corresponding to this session. This API uses an asynchronous callback to return the result. 1239 1240**System capability**: SystemCapability.Multimedia.AVSession.Core 1241 1242**Parameters** 1243 1244| Name | Type | Mandatory| Description | 1245| -------- | ----------------------------------------------------------- | ---- | -------------------------- | 1246| callback | AsyncCallback<[AVSessionController](#avsessioncontroller10)\> | Yes | Callback used to return the session controller.| 1247 1248**Error codes** 1249 1250For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1251 1252| ID| Error Message| 1253| -------- | ---------------------------------------- | 1254| 6600101 | Session service exception. | 1255| 6600102 | The session does not exist. | 1256 1257**Example** 1258 1259```ts 1260import { BusinessError } from '@kit.BasicServicesKit'; 1261 1262let avsessionController: avSession.AVSessionController; 1263currentAVSession.getController((err: BusinessError, avcontroller: avSession.AVSessionController) => { 1264 if (err) { 1265 console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); 1266 } else { 1267 avsessionController = avcontroller; 1268 console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); 1269 } 1270}); 1271``` 1272 1273### getAVCastController<sup>10+</sup> 1274 1275getAVCastController(callback: AsyncCallback\<AVCastController>): void 1276 1277Obtains 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**. 1278 1279**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1280 1281**Parameters** 1282 1283| Name | Type | Mandatory| Description | 1284| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 1285| callback | AsyncCallback<[AVCastController](#avcastcontroller10)\> | Yes | Callback used to return the cast controller.| 1286 1287**Error codes** 1288 1289For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1290 1291| ID| Error Message | 1292| -------- |---------------------------------------| 1293| 6600102| The session does not exist. | 1294| 6600109| The remote connection is not established. | 1295 1296**Example** 1297 1298```ts 1299import { BusinessError } from '@kit.BasicServicesKit'; 1300 1301let aVCastController: avSession.AVCastController; 1302currentAVSession.getAVCastController().then((avcontroller: avSession.AVCastController) => { 1303 aVCastController = avcontroller; 1304 console.info('getAVCastController : SUCCESS'); 1305}).catch((err: BusinessError) => { 1306 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 1307}); 1308``` 1309 1310### getAVCastController<sup>10+</sup> 1311 1312getAVCastController(): Promise\<AVCastController> 1313 1314Obtains 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**. 1315 1316**Atomic service API**: This API can be used in atomic services since API version 12. 1317 1318**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1319 1320**Return value** 1321 1322| Type | Description | 1323| --------- | ------------------------------------------------------------ | 1324| Promise<[AVCastController](#avcastcontroller10)\> | Promise used to return the cast controller.| 1325 1326**Error codes** 1327 1328For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1329 1330| ID| Error Message| 1331| -------- | --------------------------------------- | 1332| 6600102| The session does not exist. | 1333| 6600109| The remote connection is not established. | 1334 1335**Example** 1336 1337```ts 1338import { BusinessError } from '@kit.BasicServicesKit'; 1339 1340let aVCastController: avSession.AVCastController; 1341currentAVSession.getAVCastController((err: BusinessError, avcontroller: avSession.AVCastController) => { 1342 if (err) { 1343 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 1344 } else { 1345 aVCastController = avcontroller; 1346 console.info('getAVCastController : SUCCESS'); 1347 } 1348}); 1349``` 1350 1351### getOutputDevice<sup>10+</sup> 1352 1353getOutputDevice(): Promise\<OutputDeviceInfo> 1354 1355Obtains information about the output device for this session. This API uses a promise to return the result. 1356 1357**Atomic service API**: This API can be used in atomic services since API version 12. 1358 1359**System capability**: SystemCapability.Multimedia.AVSession.Core 1360 1361**Return value** 1362 1363| Type | Description | 1364| ---------------------------------------------- | --------------------------------- | 1365| Promise<[OutputDeviceInfo](#outputdeviceinfo10)> | Promise used to return the output device information.| 1366 1367**Error codes** 1368 1369For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1370 1371| ID| Error Message| 1372| -------- | ---------------------------------------- | 1373| 6600101 | Session service exception. | 1374| 6600102 | The session does not exist. | 1375 1376**Example** 1377 1378```ts 1379import { BusinessError } from '@kit.BasicServicesKit'; 1380 1381currentAVSession.getOutputDevice().then((outputDeviceInfo: avSession.OutputDeviceInfo) => { 1382 console.info(`GetOutputDevice : SUCCESS : devices length : ${outputDeviceInfo.devices.length}`); 1383}).catch((err: BusinessError) => { 1384 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 1385}) 1386``` 1387 1388### getOutputDevice<sup>10+</sup> 1389 1390getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void 1391 1392Obtains information about the output device for this session. This API uses an asynchronous callback to return the result. 1393 1394**System capability**: SystemCapability.Multimedia.AVSession.Core 1395 1396**Parameters** 1397 1398| Name | Type | Mandatory| Description | 1399| -------- | ----------------------------------------------------- | ---- | ------------------------------ | 1400| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | Yes | Callback used to return the information obtained.| 1401 1402**Error codes** 1403 1404For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1405 1406| ID| Error Message| 1407| -------- | ---------------------------------------- | 1408| 6600101 | Session service exception. | 1409| 6600102 | The session does not exist. | 1410 1411**Example** 1412 1413```ts 1414import { BusinessError } from '@kit.BasicServicesKit'; 1415 1416currentAVSession.getOutputDevice((err: BusinessError, outputDeviceInfo: avSession.OutputDeviceInfo) => { 1417 if (err) { 1418 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 1419 } else { 1420 console.info(`GetOutputDevice : SUCCESS : devices length : ${outputDeviceInfo.devices.length}`); 1421 } 1422}); 1423``` 1424 1425### activate<sup>10+</sup> 1426 1427activate(): Promise\<void> 1428 1429Activates this session. A session can be used only after being activated. This API uses a promise to return the result. 1430 1431**Atomic service API**: This API can be used in atomic services since API version 12. 1432 1433**System capability**: SystemCapability.Multimedia.AVSession.Core 1434 1435**Return value** 1436 1437| Type | Description | 1438| -------------- | ----------------------------- | 1439| Promise\<void> | Promise used to return the result. If the session is activated, no value is returned; otherwise, an error object is returned.| 1440 1441**Error codes** 1442 1443For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1444 1445| ID| Error Message| 1446| -------- | ---------------------------------------- | 1447| 6600101 | Session service exception. | 1448| 6600102 | The session does not exist. | 1449 1450**Example** 1451 1452```ts 1453import { BusinessError } from '@kit.BasicServicesKit'; 1454 1455currentAVSession.activate().then(() => { 1456 console.info('Activate : SUCCESS '); 1457}).catch((err: BusinessError) => { 1458 console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); 1459}); 1460``` 1461 1462### activate<sup>10+</sup> 1463 1464activate(callback: AsyncCallback\<void>): void 1465 1466Activates this session. A session can be used only after being activated. This API uses an asynchronous callback to return the result. 1467 1468**System capability**: SystemCapability.Multimedia.AVSession.Core 1469 1470**Parameters** 1471 1472| Name | Type | Mandatory| Description | 1473| -------- | -------------------- | ---- | ---------- | 1474| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is activated, **err** is **undefined**; otherwise, **err** is an error object.| 1475 1476**Error codes** 1477 1478For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1479 1480| ID| Error Message| 1481| -------- | ---------------------------------------- | 1482| 6600101 | Session service exception. | 1483| 6600102 | The session does not exist. | 1484 1485**Example** 1486 1487```ts 1488import { BusinessError } from '@kit.BasicServicesKit'; 1489 1490currentAVSession.activate((err: BusinessError) => { 1491 if (err) { 1492 console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); 1493 } else { 1494 console.info('Activate : SUCCESS '); 1495 } 1496}); 1497``` 1498 1499### deactivate<sup>10+</sup> 1500 1501deactivate(): Promise\<void> 1502 1503Deactivates this session. You can use [activate](#activate10) to activate the session again. This API uses a promise to return the result. 1504 1505**Atomic service API**: This API can be used in atomic services since API version 12. 1506 1507**System capability**: SystemCapability.Multimedia.AVSession.Core 1508 1509**Return value** 1510 1511| Type | Description | 1512| -------------- | ----------------------------- | 1513| Promise\<void> | Promise used to return the result. If the session is deactivated, no value is returned; otherwise, an error object is returned.| 1514 1515**Error codes** 1516 1517For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1518 1519| ID| Error Message| 1520| -------- | ---------------------------------------- | 1521| 6600101 | Session service exception. | 1522| 6600102 | The session does not exist. | 1523 1524**Example** 1525 1526```ts 1527import { BusinessError } from '@kit.BasicServicesKit'; 1528 1529currentAVSession.deactivate().then(() => { 1530 console.info('Deactivate : SUCCESS '); 1531}).catch((err: BusinessError) => { 1532 console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); 1533}); 1534``` 1535 1536### deactivate<sup>10+</sup> 1537 1538deactivate(callback: AsyncCallback\<void>): void 1539 1540Deactivates this session. This API uses an asynchronous callback to return the result. 1541 1542Deactivates this session. You can use [activate](#activate10) to activate the session again. 1543 1544**System capability**: SystemCapability.Multimedia.AVSession.Core 1545 1546**Parameters** 1547 1548| Name | Type | Mandatory| Description | 1549| -------- | -------------------- | ---- | ---------- | 1550| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is deactivated, **err** is **undefined**; otherwise, **err** is an error object.| 1551 1552**Error codes** 1553 1554For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1555 1556| ID| Error Message| 1557| -------- | ---------------------------------------- | 1558| 6600101 | Session service exception. | 1559| 6600102 | The session does not exist. | 1560 1561**Example** 1562 1563```ts 1564import { BusinessError } from '@kit.BasicServicesKit'; 1565 1566currentAVSession.deactivate((err: BusinessError) => { 1567 if (err) { 1568 console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); 1569 } else { 1570 console.info('Deactivate : SUCCESS '); 1571 } 1572}); 1573``` 1574 1575### destroy<sup>10+</sup> 1576 1577destroy(): Promise\<void> 1578 1579Destroys this session. This API uses a promise to return the result. 1580 1581**Atomic service API**: This API can be used in atomic services since API version 12. 1582 1583**System capability**: SystemCapability.Multimedia.AVSession.Core 1584 1585**Return value** 1586 1587| Type | Description | 1588| -------------- | ----------------------------- | 1589| Promise\<void> | Promise used to return the result. If the session is destroyed, no value is returned; otherwise, an error object is returned.| 1590 1591**Error codes** 1592 1593For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1594 1595| ID| Error Message| 1596| -------- | ---------------------------------------- | 1597| 6600101 | Session service exception. | 1598| 6600102 | The session does not exist. | 1599 1600**Example** 1601 1602```ts 1603import { BusinessError } from '@kit.BasicServicesKit'; 1604 1605currentAVSession.destroy().then(() => { 1606 console.info('Destroy : SUCCESS '); 1607}).catch((err: BusinessError) => { 1608 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 1609}); 1610``` 1611 1612### destroy<sup>10+</sup> 1613 1614destroy(callback: AsyncCallback\<void>): void 1615 1616Destroys this session. This API uses an asynchronous callback to return the result. 1617 1618**System capability**: SystemCapability.Multimedia.AVSession.Core 1619 1620**Parameters** 1621 1622| Name | Type | Mandatory| Description | 1623| -------- | -------------------- | ---- | ---------- | 1624| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is destroyed, **err** is **undefined**; otherwise, **err** is an error object.| 1625 1626**Error codes** 1627 1628For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1629 1630| ID| Error Message| 1631| -------- | ---------------------------------------- | 1632| 6600101 | Session service exception. | 1633| 6600102 | The session does not exist. | 1634 1635**Example** 1636 1637```ts 1638import { BusinessError } from '@kit.BasicServicesKit'; 1639 1640currentAVSession.destroy((err: BusinessError) => { 1641 if (err) { 1642 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 1643 } else { 1644 console.info('Destroy : SUCCESS '); 1645 } 1646}); 1647``` 1648 1649### on('play')<sup>10+</sup> 1650 1651on(type: 'play', callback: () => void): void 1652 1653Subscribes to play command events. The subscription means that the application supports the play command. 1654 1655Only one callback can be registered for each playback command. If a new callback is registered, the previous callback is replaced. 1656 1657**Atomic service API**: This API can be used in atomic services since API version 12. 1658 1659**System capability**: SystemCapability.Multimedia.AVSession.Core 1660 1661**Parameters** 1662 1663| Name | Type | Mandatory| Description | 1664| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1665| type | string | Yes | Event type. The event **'play'** is triggered when the command for starting playback is sent to the session.| 1666| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1667 1668**Error codes** 1669 1670For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1671 1672| ID| Error Message| 1673| -------- | ---------------------------------------- | 1674| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1675| 6600101 | Session service exception. | 1676| 6600102 | The session does not exist. | 1677 1678**Example** 1679 1680```ts 1681currentAVSession.on('play', () => { 1682 console.info('on play entry'); 1683}); 1684``` 1685 1686### on('pause')<sup>10+</sup> 1687 1688on(type: 'pause', callback: () => void): void 1689 1690Subscribes to pause command events. The subscription means that the application supports the pause command. 1691 1692Only one callback can be registered for each playback command. If a new callback is registered, the previous callback is replaced. 1693 1694**Atomic service API**: This API can be used in atomic services since API version 12. 1695 1696**System capability**: SystemCapability.Multimedia.AVSession.Core 1697 1698**Parameters** 1699 1700| Name | Type | Mandatory| Description | 1701| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1702| type | string | Yes | Event type. The event **'pause'** is triggered when the command for pausing the playback is sent to the session.| 1703| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1704 1705**Error codes** 1706 1707For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1708 1709| ID| Error Message| 1710| -------- | ---------------------------------------- | 1711| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1712| 6600101 | Session service exception. | 1713| 6600102 | The session does not exist. | 1714 1715**Example** 1716 1717```ts 1718currentAVSession.on('pause', () => { 1719 console.info('on pause entry'); 1720}); 1721``` 1722 1723### on('stop')<sup>10+</sup> 1724 1725on(type:'stop', callback: () => void): void 1726 1727Subscribes to stop command events. The subscription means that the application supports the stop command. 1728 1729Only one callback can be registered for each playback command. If a new callback is registered, the previous callback is replaced. 1730 1731**Atomic service API**: This API can be used in atomic services since API version 12. 1732 1733**System capability**: SystemCapability.Multimedia.AVSession.Core 1734 1735**Parameters** 1736 1737| Name | Type | Mandatory| Description | 1738| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1739| type | string | Yes | Event type. The event **'stop'** is triggered when the command for stopping the playback is sent to the session.| 1740| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1741 1742**Error codes** 1743 1744For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1745 1746| ID| Error Message| 1747| -------- | ---------------------------------------- | 1748| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1749| 6600101 | Session service exception. | 1750| 6600102 | The session does not exist. | 1751 1752**Example** 1753 1754```ts 1755currentAVSession.on('stop', () => { 1756 console.info('on stop entry'); 1757}); 1758``` 1759 1760### on('playNext')<sup>10+</sup> 1761 1762on(type:'playNext', callback: () => void): void 1763 1764Subscribes to playNext command events. The subscription means that the application supports the playNext command. 1765 1766Only one callback can be registered for each playback command. If a new callback is registered, the previous callback is replaced. 1767 1768**Atomic service API**: This API can be used in atomic services since API version 12. 1769 1770**System capability**: SystemCapability.Multimedia.AVSession.Core 1771 1772**Parameters** 1773 1774| Name | Type | Mandatory| Description | 1775| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1776| type | string | Yes | Event type. The event **'playNext'** is triggered when the command for playing the next item is sent to the session.| 1777| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1778 1779**Error codes** 1780 1781For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1782 1783| ID| Error Message| 1784| -------- | ---------------------------------------- | 1785| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1786| 6600101 | Session service exception. | 1787| 6600102 | The session does not exist. | 1788 1789**Example** 1790 1791```ts 1792currentAVSession.on('playNext', () => { 1793 console.info('on playNext entry'); 1794}); 1795``` 1796 1797### on('playPrevious')<sup>10+</sup> 1798 1799on(type:'playPrevious', callback: () => void): void 1800 1801Subscribes to playPrevious command events. The subscription means that the application supports the playPrevious command. 1802 1803Only one callback can be registered for each playback command. If a new callback is registered, the previous callback is replaced. 1804 1805**Atomic service API**: This API can be used in atomic services since API version 12. 1806 1807**System capability**: SystemCapability.Multimedia.AVSession.Core 1808 1809**Parameters** 1810 1811| Name | Type | Mandatory| Description | 1812| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1813| type | string | Yes | Event type. The event **'playPrevious'** is triggered when the command for playing the previous item sent to the session.| 1814| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1815 1816**Error codes** 1817 1818For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1819 1820| ID| Error Message| 1821| -------- | ---------------------------------------- | 1822| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1823| 6600101 | Session service exception. | 1824| 6600102 | The session does not exist. | 1825 1826**Example** 1827 1828```ts 1829currentAVSession.on('playPrevious', () => { 1830 console.info('on playPrevious entry'); 1831}); 1832``` 1833 1834### on('fastForward')<sup>10+</sup> 1835 1836on(type: 'fastForward', callback: (time?: number) => void): void 1837 1838Subscribes to fastForward command events. The subscription means that the application supports the fastForward command. 1839 1840Only one callback can be registered for each playback command. If a new callback is registered, the previous callback is replaced. 1841 1842**Atomic service API**: This API can be used in atomic services since API version 12. 1843 1844**System capability**: SystemCapability.Multimedia.AVSession.Core 1845 1846**Parameters** 1847 1848| Name | Type | Mandatory| Description | 1849| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1850| type | string | Yes | Event type. The event **'fastForward'** is triggered when the command for fast forwarding is sent to the session.| 1851| callback | (time?: number) => void | Yes | Callback used for subscription. The **time** parameter in the callback indicates the time to seek to, in seconds. | 1852 1853**Error codes** 1854 1855For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1856 1857| ID| Error Message| 1858| -------- | ---------------------------------------- | 1859| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1860| 6600101 | Session service exception. | 1861| 6600102 | The session does not exist. | 1862 1863**Example** 1864 1865```ts 1866currentAVSession.on('fastForward', (time?: number) => { 1867 console.info('on fastForward entry'); 1868}); 1869``` 1870 1871### on('rewind')<sup>10+</sup> 1872 1873on(type:'rewind', callback: (time?: number) => void): void 1874 1875Subscribes to rewind command events. 1876 1877**Atomic service API**: This API can be used in atomic services since API version 12. 1878 1879**System capability**: SystemCapability.Multimedia.AVSession.Core 1880 1881**Parameters** 1882 1883| Name | Type | Mandatory| Description | 1884| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1885| type | string | Yes | Event type. The event **'rewind'** is triggered when the command for rewinding is sent to the session.| 1886| callback | (time?: number) => void | Yes | Callback used for subscription. The **time** parameter in the callback indicates the time to seek to, in seconds. | 1887 1888**Error codes** 1889 1890For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1891 1892| ID| Error Message| 1893| -------- | ---------------------------------------- | 1894| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1895| 6600101 | Session service exception. | 1896| 6600102 | The session does not exist. | 1897 1898**Example** 1899 1900```ts 1901currentAVSession.on('rewind', (time?: number) => { 1902 console.info('on rewind entry'); 1903}); 1904``` 1905 1906### on('playFromAssetId')<sup>11+</sup> 1907 1908on(type:'playFromAssetId', callback: (assetId: number) => void): void 1909 1910Subscribes to playback events of a given media ID. 1911 1912**Atomic service API**: This API can be used in atomic services since API version 12. 1913 1914**System capability**: SystemCapability.Multimedia.AVSession.Core 1915 1916**Parameters** 1917 1918| Name | Type | Mandatory| Description | 1919| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1920| type | string | Yes | Event type. The event **'playFromAssetId'** is triggered when the media ID is played.| 1921| callback | callback: (assetId: number) => void | Yes | Callback The **assetId** parameter in the callback indicates the media asset ID. | 1922 1923**Error codes** 1924 1925For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1926 1927| ID| Error Message| 1928| -------- | ---------------------------------------- | 1929| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1930| 6600101 | Session service exception. | 1931| 6600102 | The session does not exist. | 1932 1933**Example** 1934 1935```ts 1936currentAVSession.on('playFromAssetId', (assetId: number) => { 1937 console.info('on playFromAssetId entry'); 1938}); 1939``` 1940 1941### off('playFromAssetId')<sup>11+</sup> 1942 1943off(type: 'playFromAssetId', callback?: (assetId: number) => void): void 1944 1945Unsubscribes from playback events of a given media ID. 1946 1947**Atomic service API**: This API can be used in atomic services since API version 12. 1948 1949**System capability**: SystemCapability.Multimedia.AVSession.Core 1950 1951**Parameters** 1952 1953| Name | Type | Mandatory| Description | 1954| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 1955| type | string | Yes | Event type, which is **'playFromAssetId'** in this case.| 1956| callback | 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. | 1957 1958**Error codes** 1959 1960For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1961 1962| ID| Error Message| 1963| -------- | ---------------------------------------- | 1964| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1965| 6600101 | Session service exception. | 1966| 6600102 | The session does not exist. | 1967 1968**Example** 1969 1970```ts 1971currentAVSession.off('playFromAssetId'); 1972``` 1973 1974### on('seek')<sup>10+</sup> 1975 1976on(type: 'seek', callback: (time: number) => void): void 1977 1978Subscribes to seek command events. 1979 1980**Atomic service API**: This API can be used in atomic services since API version 12. 1981 1982**System capability**: SystemCapability.Multimedia.AVSession.Core 1983 1984**Parameters** 1985 1986| Name | Type | Mandatory| Description | 1987| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 1988| type | string | Yes | Event type. The event **'seek'** is triggered when the seek command is sent to the session.| 1989| callback | (time: number) => void | Yes | Callback used for subscription. The **time** parameter in the callback indicates the time to seek to, in milliseconds. | 1990 1991**Error codes** 1992 1993For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 1994 1995| ID| Error Message| 1996| -------- | ---------------------------------------- | 1997| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1998| 6600101 | Session service exception. | 1999| 6600102 | The session does not exist. | 2000 2001**Example** 2002 2003```ts 2004currentAVSession.on('seek', (time: number) => { 2005 console.info(`on seek entry time : ${time}`); 2006}); 2007``` 2008 2009### on('setSpeed')<sup>10+</sup> 2010 2011on(type: 'setSpeed', callback: (speed: number) => void): void 2012 2013Subscribes to setSpeed command events. 2014 2015**Atomic service API**: This API can be used in atomic services since API version 12. 2016 2017**System capability**: SystemCapability.Multimedia.AVSession.Core 2018 2019**Parameters** 2020 2021| Name | Type | Mandatory| Description | 2022| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 2023| type | string | Yes | Event type. The event **'setSpeed'** is triggered when the command for setting the playback speed is sent to the session.| 2024| callback | (speed: number) => void | Yes | Callback used for subscription. The **speed** parameter in the callback indicates the playback speed. | 2025 2026**Error codes** 2027 2028For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2029 2030| ID| Error Message| 2031| -------- | ---------------------------------------- | 2032| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2033| 6600101 | Session service exception. | 2034| 6600102 | The session does not exist. | 2035 2036**Example** 2037 2038```ts 2039currentAVSession.on('setSpeed', (speed: number) => { 2040 console.info(`on setSpeed speed : ${speed}`); 2041}); 2042``` 2043 2044### on('setLoopMode')<sup>10+</sup> 2045 2046on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void 2047 2048Subscribes to setLoopMode command events. 2049 2050**Atomic service API**: This API can be used in atomic services since API version 12. 2051 2052**System capability**: SystemCapability.Multimedia.AVSession.Core 2053 2054**Parameters** 2055 2056| Name | Type | Mandatory| Description | 2057| -------- | ------------------------------------- | ---- | ---- | 2058| type | string | Yes | Event type. The event **'setLoopMode'** is triggered when the command for setting the loop mode is sent to the session.| 2059| callback | (mode: [LoopMode](#loopmode10)) => void | Yes | Callback used for subscription. The **mode** parameter in the callback indicates the loop mode. | 2060 2061**Error codes** 2062 2063For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2064 2065| ID| Error Message| 2066| -------- | ---------------------------------------- | 2067| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2068| 6600101 | Session service exception. | 2069| 6600102 | The session does not exist. | 2070 2071**Example** 2072 2073```ts 2074currentAVSession.on('setLoopMode', (mode: avSession.LoopMode) => { 2075 console.info(`on setLoopMode mode : ${mode}`); 2076}); 2077``` 2078 2079### on('toggleFavorite')<sup>10+</sup> 2080 2081on(type: 'toggleFavorite', callback: (assetId: string) => void): void 2082 2083Subscribes to toggleFavorite command events. 2084 2085**Atomic service API**: This API can be used in atomic services since API version 12. 2086 2087**System capability**: SystemCapability.Multimedia.AVSession.Core 2088 2089**Parameters** 2090 2091| Name | Type | Mandatory| Description | 2092| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 2093| type | string | Yes | Event type. The event **'toggleFavorite'** is triggered when the command for favoriting the media asset is sent to the session.| 2094| callback | (assetId: string) => void | Yes | Callback used for subscription. The **assetId** parameter in the callback indicates the media asset ID. | 2095 2096**Error codes** 2097 2098For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2099 2100| ID| Error Message| 2101| -------- | ---------------------------------------- | 2102| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2103| 6600101 | Session service exception. | 2104| 6600102 | The session does not exist. | 2105 2106**Example** 2107 2108```ts 2109currentAVSession.on('toggleFavorite', (assetId: string) => { 2110 console.info(`on toggleFavorite mode : ${assetId}`); 2111}); 2112``` 2113 2114### on('skipToQueueItem')<sup>10+</sup> 2115 2116on(type: 'skipToQueueItem', callback: (itemId: number) => void): void 2117 2118Subscribes to the event that indicates an item in the playlist is selected. The session can play the selected item. 2119 2120**Atomic service API**: This API can be used in atomic services since API version 12. 2121 2122**System capability**: SystemCapability.Multimedia.AVSession.Core 2123 2124**Parameters** 2125 2126| Name | Type | Mandatory| Description | 2127| -------- | ------------------------ | ---- | ---------------------------------------------------------------------------------------- | 2128| type | string | Yes | Event type. The event **'skipToQueueItem'** is triggered when an item in the playlist is selected.| 2129| callback | (itemId: number) => void | Yes | Callback used for subscription. The **itemId** parameter in the callback indicates the ID of the selected item. | 2130 2131**Error codes** 2132 2133For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2134 2135| ID| Error Message| 2136| -------- | ---------------------------------------- | 2137| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2138| 6600101 | Session service exception. | 2139| 6600102 | The session does not exist. | 2140 2141**Example** 2142 2143```ts 2144currentAVSession.on('skipToQueueItem', (itemId: number) => { 2145 console.info(`on skipToQueueItem id : ${itemId}`); 2146}); 2147``` 2148 2149### on('handleKeyEvent')<sup>10+</sup> 2150 2151on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void 2152 2153Subscribes 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. 2154 2155**Atomic service API**: This API can be used in atomic services since API version 12. 2156 2157**System capability**: SystemCapability.Multimedia.AVSession.Core 2158 2159**Parameters** 2160 2161| Name | Type | Mandatory| Description | 2162| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2163| type | string | Yes | Event type. The event **'handleKeyEvent'** is triggered when a key event is sent to the session.| 2164| 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. | 2165 2166**Error codes** 2167 2168For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2169 2170| ID| Error Message| 2171| -------- | ---------------------------------------- | 2172| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2173| 6600101 | Session service exception. | 2174| 6600102 | The session does not exist. | 2175 2176**Example** 2177 2178```ts 2179import { KeyEvent } from '@kit.InputKit'; 2180 2181currentAVSession.on('handleKeyEvent', (event: KeyEvent) => { 2182 console.info(`on handleKeyEvent event : ${event}`); 2183}); 2184 2185``` 2186 2187### on('outputDeviceChange')<sup>10+</sup> 2188 2189on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void 2190 2191Subscribes 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. 2192 2193**Atomic service API**: This API can be used in atomic services since API version 12. 2194 2195**System capability**: SystemCapability.Multimedia.AVSession.Core 2196 2197**Parameters** 2198 2199| Name | Type | Mandatory| Description | 2200| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2201| type | string | Yes | Event type. The event **'outputDeviceChange'** is triggered when the output device changes.| 2202| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | Yes | Callback used for subscription. The **device** parameter in the callback indicates 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. | 2203 2204**Error codes** 2205 2206For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2207 2208| ID| Error Message| 2209| -------- | ---------------------------------------- | 2210| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2211| 6600101 | Session service exception. | 2212| 6600102 | The session does not exist. | 2213 2214**Example** 2215 2216```ts 2217currentAVSession.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { 2218 console.info(`on outputDeviceChange device : ${device}`); 2219}); 2220``` 2221 2222### on('commonCommand')<sup>10+</sup> 2223 2224on(type: 'commonCommand', callback: (command: string, args: {[key: string]: Object}) => void): void 2225 2226Subscribes to custom control command change events. 2227 2228**Atomic service API**: This API can be used in atomic services since API version 12. 2229 2230**System capability**: SystemCapability.Multimedia.AVSession.Core 2231 2232**Parameters** 2233 2234| Name | Type | Mandatory| Description | 2235| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2236| type | string | Yes | Event type. The event **'commonCommand'** is triggered when a custom control command changes.| 2237| 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](#sendcommoncommand10). | 2238 2239**Error codes** 2240 2241For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2242 2243| ID| Error Message| 2244| -------- | ------------------------------ | 2245| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2246| 6600101 | Session service exception. | 2247| 6600102 | The session does not exist. | 2248 2249**Example** 2250 2251```ts 2252import { BusinessError } from '@kit.BasicServicesKit'; 2253 2254let currentAVSession: avSession.AVSession | undefined = undefined; 2255let tag = "createNewSession"; 2256let context: Context = getContext(this); 2257 2258avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2259 if (err) { 2260 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2261 } else { 2262 currentAVSession = data; 2263 } 2264}); 2265if (currentAVSession !== undefined) { 2266 (currentAVSession as avSession.AVSession).on('commonCommand', (commonCommand, args) => { 2267 console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`); 2268 }); 2269} 2270``` 2271 2272### off('play')<sup>10+</sup> 2273 2274off(type: 'play', callback?: () => void): void 2275 2276Unsubscribes from play command events. 2277 2278After the callback is canceled, the list of supported commands must be updated. 2279 2280**Atomic service API**: This API can be used in atomic services since API version 12. 2281 2282**System capability**: SystemCapability.Multimedia.AVSession.Core 2283 2284**Parameters** 2285 2286| Name | Type | Mandatory| Description | 2287| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2288| type | string | Yes | Event type, which is **'play'** in this case.| 2289| 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. | 2290 2291**Error codes** 2292 2293For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2294 2295| ID| Error Message| 2296| -------- | ---------------------------------------- | 2297| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2298| 6600101 | Session service exception. | 2299| 6600102 | The session does not exist. | 2300 2301**Example** 2302 2303```ts 2304currentAVSession.off('play'); 2305``` 2306 2307### off('pause')<sup>10+</sup> 2308 2309off(type: 'pause', callback?: () => void): void 2310 2311Unsubscribes from pause command events. 2312 2313After the callback is canceled, the list of supported commands must be updated. 2314 2315**Atomic service API**: This API can be used in atomic services since API version 12. 2316 2317**System capability**: SystemCapability.Multimedia.AVSession.Core 2318 2319**Parameters** 2320 2321| Name | Type | Mandatory| Description | 2322| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2323| type | string | Yes | Event type, which is **'pause'** in this case.| 2324| 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.| 2325 2326**Error codes** 2327 2328For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2329 2330| ID| Error Message| 2331| -------- | ---------------------------------------- | 2332| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2333| 6600101 | Session service exception. | 2334| 6600102 | The session does not exist. | 2335 2336**Example** 2337 2338```ts 2339currentAVSession.off('pause'); 2340``` 2341 2342### off('stop')<sup>10+</sup> 2343 2344off(type: 'stop', callback?: () => void): void 2345 2346Unsubscribes from stop command events. 2347 2348After the callback is canceled, the list of supported commands must be updated. 2349 2350**Atomic service API**: This API can be used in atomic services since API version 12. 2351 2352**System capability**: SystemCapability.Multimedia.AVSession.Core 2353 2354**Parameters** 2355 2356| Name | Type | Mandatory| Description | 2357| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2358| type | string | Yes | Event type, which is **'stop'** in this case.| 2359| 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. | 2360 2361**Error codes** 2362 2363For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2364 2365| ID| Error Message| 2366| -------- | ---------------------------------------- | 2367| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2368| 6600101 | Session service exception. | 2369| 6600102 | The session does not exist. | 2370 2371**Example** 2372 2373```ts 2374currentAVSession.off('stop'); 2375``` 2376 2377### off('playNext')<sup>10+</sup> 2378 2379off(type: 'playNext', callback?: () => void): void 2380 2381Unsubscribes from playNext command events. 2382 2383After the callback is canceled, the list of supported commands must be updated. 2384 2385**Atomic service API**: This API can be used in atomic services since API version 12. 2386 2387**System capability**: SystemCapability.Multimedia.AVSession.Core 2388 2389**Parameters** 2390 2391| Name | Type | Mandatory| Description | 2392| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2393| type | string | Yes | Event type, which is **'playNext'** in this case.| 2394| 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. | 2395 2396**Error codes** 2397 2398For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2399 2400| ID| Error Message| 2401| -------- | ---------------------------------------- | 2402| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2403| 6600101 | Session service exception. | 2404| 6600102 | The session does not exist. | 2405 2406**Example** 2407 2408```ts 2409currentAVSession.off('playNext'); 2410``` 2411 2412### off('playPrevious')<sup>10+</sup> 2413 2414off(type: 'playPrevious', callback?: () => void): void 2415 2416Unsubscribes from playPrevious command events. 2417 2418After the callback is canceled, the list of supported commands must be updated. 2419 2420**Atomic service API**: This API can be used in atomic services since API version 12. 2421 2422**System capability**: SystemCapability.Multimedia.AVSession.Core 2423 2424**Parameters** 2425 2426| Name | Type | Mandatory| Description | 2427| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2428| type | string | Yes | Event type, which is **'playPrevious'** in this case.| 2429| 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. | 2430 2431**Error codes** 2432 2433For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2434 2435| ID| Error Message| 2436| -------- | ---------------------------------------- | 2437| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2438| 6600101 | Session service exception. | 2439| 6600102 | The session does not exist. | 2440 2441**Example** 2442 2443```ts 2444currentAVSession.off('playPrevious'); 2445``` 2446 2447### off('fastForward')<sup>10+</sup> 2448 2449off(type: 'fastForward', callback?: () => void): void 2450 2451Unsubscribes from fastForward command events. 2452 2453After the callback is canceled, the list of supported commands must be updated. 2454 2455**Atomic service API**: This API can be used in atomic services since API version 12. 2456 2457**System capability**: SystemCapability.Multimedia.AVSession.Core 2458 2459**Parameters** 2460 2461| Name | Type | Mandatory| Description | 2462| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2463| type | string | Yes | Event type, which is **'fastForward'** in this case.| 2464| 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. | 2465 2466**Error codes** 2467 2468For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2469 2470| ID| Error Message| 2471| -------- | ---------------------------------------- | 2472| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2473| 6600101 | Session service exception. | 2474| 6600102 | The session does not exist. | 2475 2476**Example** 2477 2478```ts 2479currentAVSession.off('fastForward'); 2480``` 2481 2482### off('rewind')<sup>10+</sup> 2483 2484off(type: 'rewind', callback?: () => void): void 2485 2486Unsubscribes from rewind command events. 2487 2488**Atomic service API**: This API can be used in atomic services since API version 12. 2489 2490**System capability**: SystemCapability.Multimedia.AVSession.Core 2491 2492**Parameters** 2493 2494| Name | Type | Mandatory| Description | 2495| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2496| type | string | Yes | Event type, which is **'rewind'** in this case.| 2497| 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. | 2498 2499**Error codes** 2500 2501For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2502 2503| ID| Error Message| 2504| -------- | ---------------------------------------- | 2505| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2506| 6600101 | Session service exception. | 2507| 6600102 | The session does not exist. | 2508 2509**Example** 2510 2511```ts 2512currentAVSession.off('rewind'); 2513``` 2514 2515### off('seek')<sup>10+</sup> 2516 2517off(type: 'seek', callback?: (time: number) => void): void 2518 2519Unsubscribes from seek command events. 2520 2521**Atomic service API**: This API can be used in atomic services since API version 12. 2522 2523**System capability**: SystemCapability.Multimedia.AVSession.Core 2524 2525**Parameters** 2526 2527| Name | Type | Mandatory| Description | 2528| -------- | ---------------------- | ---- | ----------------------------------------- | 2529| type | string | Yes | Event type, which is **'seek'** in this case. | 2530| 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. | 2531 2532**Error codes** 2533 2534For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2535 2536| ID| Error Message| 2537| -------- | ---------------------------------------- | 2538| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2539| 6600101 | Session service exception. | 2540| 6600102 | The session does not exist. | 2541 2542**Example** 2543 2544```ts 2545currentAVSession.off('seek'); 2546``` 2547 2548### off('setSpeed')<sup>10+</sup> 2549 2550off(type: 'setSpeed', callback?: (speed: number) => void): void 2551 2552Unsubscribes from setSpeed command events. 2553 2554**Atomic service API**: This API can be used in atomic services since API version 12. 2555 2556**System capability**: SystemCapability.Multimedia.AVSession.Core 2557 2558**Parameters** 2559 2560| Name | Type | Mandatory| Description | 2561| -------- | ----------------------- | ---- | -------------------------------------------| 2562| type | string | Yes | Event type, which is **'setSpeed'** in this case. | 2563| 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. | 2564 2565**Error codes** 2566 2567For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2568 2569| ID| Error Message| 2570| -------- | ---------------------------------------- | 2571| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2572| 6600101 | Session service exception. | 2573| 6600102 | The session does not exist. | 2574 2575**Example** 2576 2577```ts 2578currentAVSession.off('setSpeed'); 2579``` 2580 2581### off('setLoopMode')<sup>10+</sup> 2582 2583off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void 2584 2585Unsubscribes from setSpeed command events. 2586 2587**Atomic service API**: This API can be used in atomic services since API version 12. 2588 2589**System capability**: SystemCapability.Multimedia.AVSession.Core 2590 2591**Parameters** 2592 2593| Name | Type | Mandatory| Description | 2594| -------- | ------------------------------------- | ---- | ----- | 2595| type | string | Yes | Event type, which is **'setLoopMode'** in this case.| 2596| callback | (mode: [LoopMode](#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.| 2597 2598**Error codes** 2599 2600For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2601 2602| ID| Error Message| 2603| -------- | ---------------------------------------- | 2604| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2605| 6600101 | Session service exception. | 2606| 6600102 | The session does not exist. | 2607 2608**Example** 2609 2610```ts 2611currentAVSession.off('setLoopMode'); 2612``` 2613 2614### off('toggleFavorite')<sup>10+</sup> 2615 2616off(type: 'toggleFavorite', callback?: (assetId: string) => void): void 2617 2618Unsubscribes from toggleFavorite command events. 2619 2620**Atomic service API**: This API can be used in atomic services since API version 12. 2621 2622**System capability**: SystemCapability.Multimedia.AVSession.Core 2623 2624**Parameters** 2625 2626| Name | Type | Mandatory| Description | 2627| -------- | ------------------------- | ---- | -------------------------------------------------------- | 2628| type | string | Yes | Event type, which is **'toggleFavorite'** in this case. | 2629| 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. | 2630 2631**Error codes** 2632 2633For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2634 2635| ID| Error Message| 2636| -------- | ---------------------------------------- | 2637| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2638| 6600101 | Session service exception. | 2639| 6600102 | The session does not exist. | 2640 2641**Example** 2642 2643```ts 2644currentAVSession.off('toggleFavorite'); 2645``` 2646 2647### off('skipToQueueItem')<sup>10+</sup> 2648 2649off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void 2650 2651Unsubscribes from the event that indicates an item in the playlist is selected. 2652 2653**Atomic service API**: This API can be used in atomic services since API version 12. 2654 2655**System capability**: SystemCapability.Multimedia.AVSession.Core 2656 2657**Parameters** 2658 2659| Name | Type | Mandatory| Description | 2660| -------- | ------------------------ | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | 2661| type | string | Yes | Event type, which is **'skipToQueueItem'** in this case. | 2662| 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.| 2663 2664**Error codes** 2665 2666For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2667 2668| ID| Error Message| 2669| -------- | ---------------------------------------- | 2670| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2671| 6600101 | Session service exception. | 2672| 6600102 | The session does not exist. | 2673 2674**Example** 2675 2676```ts 2677currentAVSession.off('skipToQueueItem'); 2678``` 2679 2680### off('handleKeyEvent')<sup>10+</sup> 2681 2682off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void 2683 2684Unsubscribes from key events. 2685 2686**Atomic service API**: This API can be used in atomic services since API version 12. 2687 2688**System capability**: SystemCapability.Multimedia.AVSession.Core 2689 2690**Parameters** 2691 2692| Name | Type | Mandatory| Description | 2693| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2694| type | string | Yes | Event type, which is **'handleKeyEvent'** in this case. | 2695| 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. | 2696 2697**Error codes** 2698 2699For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2700 2701| ID| Error Message| 2702| -------- | ---------------------------------------- | 2703| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2704| 6600101 | Session service exception. | 2705| 6600102 | The session does not exist. | 2706 2707**Example** 2708 2709```ts 2710currentAVSession.off('handleKeyEvent'); 2711``` 2712 2713### off('outputDeviceChange')<sup>10+</sup> 2714 2715off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void 2716 2717Unsubscribes from playback device change events. 2718 2719**Atomic service API**: This API can be used in atomic services since API version 12. 2720 2721**System capability**: SystemCapability.Multimedia.AVSession.Core 2722 2723**Parameters** 2724 2725| Name | Type | Mandatory| Description | 2726| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | 2727| type | string | Yes | Event type, which is **'outputDeviceChange'** in this case. | 2728| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | No | Callback used for unsubscription. The **device** parameter in the callback indicates 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. | 2729 2730**Error codes** 2731 2732For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2733 2734| ID| Error Message| 2735| -------- | ---------------------------------------- | 2736| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2737| 6600101 | Session service exception. | 2738| 6600102 | The session does not exist. | 2739 2740**Example** 2741 2742```ts 2743currentAVSession.off('outputDeviceChange'); 2744``` 2745 2746 2747### off('commonCommand')<sup>10+</sup> 2748 2749off(type: 'commonCommand', callback?: (command: string, args: {[key:string]: Object}) => void): void 2750 2751Unsubscribes from custom control command change events. 2752 2753**Atomic service API**: This API can be used in atomic services since API version 12. 2754 2755**System capability**: SystemCapability.Multimedia.AVSession.Core 2756 2757**Parameters** 2758 2759| Name | Type | Mandatory| Description | 2760| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 2761| type | string | Yes | Event type, which is **'commonCommand'** in this case. | 2762| 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. | 2763 2764**Error codes** 2765 2766For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2767 2768| ID| Error Message| 2769| -------- | ---------------- | 2770| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2771| 6600101 | Session service exception. | 2772| 6600102 | The session does not exist. | 2773 2774**Example** 2775 2776```ts 2777currentAVSession.off('commonCommand'); 2778``` 2779 2780### on('answer')<sup>11+</sup> 2781 2782on(type: 'answer', callback: Callback\<void>): void; 2783 2784Subscribes to call answer events. 2785 2786**Atomic service API**: This API can be used in atomic services since API version 12. 2787 2788**System capability**: SystemCapability.Multimedia.AVSession.Core 2789 2790**Parameters** 2791 2792| Name | Type | Mandatory| Description | 2793| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2794| type | string | Yes | Event type. The event **'answer'** is triggered when a call is answered.| 2795| callback | Callback\<void> | Yes | Callback used to return the result. | 2796 2797**Error codes** 2798 2799For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2800 2801| ID| Error Message| 2802| -------- | ------------------------------ | 2803| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2804| 6600101 | Session service exception. | 2805| 6600102 | The session does not exist. | 2806 2807**Example** 2808 2809```ts 2810currentAVSession.on('answer', () => { 2811 console.info('on call answer'); 2812}); 2813``` 2814 2815### off('answer')<sup>11+</sup> 2816 2817off(type: 'answer', callback?: Callback\<void>): void; 2818 2819Unsubscribes from call answer events. 2820 2821**Atomic service API**: This API can be used in atomic services since API version 12. 2822 2823**System capability**: SystemCapability.Multimedia.AVSession.Core 2824 2825**Parameters** 2826 2827| Name | Type | Mandatory| Description | 2828| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2829| type | string | Yes | Event type, which is **'answer'** in this case.| 2830| 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. | 2831 2832**Error codes** 2833 2834For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2835 2836| ID| Error Message| 2837| -------- | ---------------------------------------- | 2838| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2839| 6600101 | Session service exception. | 2840| 6600102 | The session does not exist. | 2841 2842**Example** 2843 2844```ts 2845currentAVSession.off('answer'); 2846``` 2847 2848### on('hangUp')<sup>11+</sup> 2849 2850on(type: 'hangUp', callback: Callback\<void>): void; 2851 2852Subscribes to call hangup events. 2853 2854**Atomic service API**: This API can be used in atomic services since API version 12. 2855 2856**System capability**: SystemCapability.Multimedia.AVSession.Core 2857 2858**Parameters** 2859 2860| Name | Type | Mandatory| Description | 2861| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2862| type | string | Yes | Event type. The event **'hangUp'** is triggered when a call is hung up.| 2863| callback | Callback\<void> | Yes | Callback used to return the result. | 2864 2865**Error codes** 2866 2867For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2868 2869| ID| Error Message| 2870| -------- | ------------------------------ | 2871| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2872| 6600101 | Session service exception. | 2873| 6600102 | The session does not exist. | 2874 2875**Example** 2876 2877```ts 2878currentAVSession.on('hangUp', () => { 2879 console.info('on call hangUp'); 2880}); 2881``` 2882 2883### off('hangUp')<sup>11+</sup> 2884 2885off(type: 'hangUp', callback?: Callback\<void>): void; 2886 2887Unsubscribes from call answer events. 2888 2889**Atomic service API**: This API can be used in atomic services since API version 12. 2890 2891**System capability**: SystemCapability.Multimedia.AVSession.Core 2892 2893**Parameters** 2894 2895| Name | Type | Mandatory| Description | 2896| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2897| type | string | Yes | Event type, which is **'hangUp'** in this case.| 2898| 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. | 2899 2900**Error codes** 2901 2902For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2903 2904| ID| Error Message| 2905| -------- | ---------------------------------------- | 2906| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2907| 6600101 | Session service exception. | 2908| 6600102 | The session does not exist. | 2909 2910**Example** 2911 2912```ts 2913currentAVSession.off('hangUp'); 2914``` 2915 2916### on('toggleCallMute')<sup>11+</sup> 2917 2918on(type: 'toggleCallMute', callback: Callback\<void>): void; 2919 2920Subscribes to call mute events. 2921 2922**Atomic service API**: This API can be used in atomic services since API version 12. 2923 2924**System capability**: SystemCapability.Multimedia.AVSession.Core 2925 2926**Parameters** 2927 2928| Name | Type | Mandatory| Description | 2929| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2930| type | string | Yes | Event type. The event **'toggleCallMute'** is triggered when a call is muted or unmuted.| 2931| callback | Callback\<void> | Yes | Callback used to return the result. | 2932 2933**Error codes** 2934 2935For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2936 2937| ID| Error Message| 2938| -------- | ------------------------------ | 2939| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2940| 6600101 | Session service exception. | 2941| 6600102 | The session does not exist. | 2942 2943**Example** 2944 2945```ts 2946currentAVSession.on('toggleCallMute', () => { 2947 console.info('on call toggleCallMute'); 2948}); 2949``` 2950 2951### off('toggleCallMute')<sup>11+</sup> 2952 2953off(type: 'toggleCallMute', callback?: Callback\<void>): void; 2954 2955Unsubscribes from call mute events. 2956 2957**Atomic service API**: This API can be used in atomic services since API version 12. 2958 2959**System capability**: SystemCapability.Multimedia.AVSession.Core 2960 2961**Parameters** 2962 2963| Name | Type | Mandatory| Description | 2964| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2965| type | string | Yes | Event type, which is **'toggleCallMute'** in this case.| 2966| 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. | 2967 2968**Error codes** 2969 2970For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 2971 2972| ID| Error Message| 2973| -------- | ---------------------------------------- | 2974| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2975| 6600101 | Session service exception. | 2976| 6600102 | The session does not exist. | 2977 2978**Example** 2979 2980```ts 2981currentAVSession.off('toggleCallMute'); 2982``` 2983 2984### on('castDisplayChange')<sup>12+</sup> 2985 2986on(type: 'castDisplayChange', callback: Callback\<CastDisplayInfo>): void 2987 2988Subscribes to cast display change events in the case of extended screens. 2989 2990**Atomic service API**: This API can be used in atomic services since API version 12. 2991 2992**System capability**: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast 2993 2994**Parameters** 2995 2996| Name | Type | Mandatory| Description | 2997| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 2998| type | string | Yes | Event type. The event **'castDisplayChange'** is triggered when the cast display in the case of extended screens changes.| 2999| callback | Callback<[CastDisplayInfo](#castdisplayinfo12)> | Yes | Callback used to return the information about the cast display. | 3000 3001**Error codes** 3002 3003For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3004 3005| ID| Error Message| 3006| -------- | ---------------------------------------- | 3007| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3008| 6600101 | Session service exception. | 3009| 6600102 | The session does not exist. | 3010 3011**Example** 3012 3013```ts 3014let castDisplay: avSession.CastDisplayInfo; 3015currentAVSession.on('castDisplayChange', (display: avSession.CastDisplayInfo) => { 3016 if (display.state === avSession.CastDisplayState.STATE_ON) { 3017 castDisplay = display; 3018 console.info(`Succeeded in castDisplayChange display : ${display.id} ON`); 3019 } else if (display.state === avSession.CastDisplayState.STATE_OFF){ 3020 console.info(`Succeeded in castDisplayChange display : ${display.id} OFF`); 3021 } 3022}); 3023``` 3024### off('castDisplayChange')<sup>12+</sup> 3025 3026 off(type: 'castDisplayChange', callback?: Callback\<CastDisplayInfo>): void 3027 3028Unsubscribes from cast display change events in the case of extended screens. 3029 3030**Atomic service API**: This API can be used in atomic services since API version 12. 3031 3032**System capability**: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast 3033 3034**Parameters** 3035 3036| Name | Type | Mandatory| Description | 3037| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3038| type | string | Yes | Event type, which is **'castDisplayChange'** in this case.| 3039| callback | Callback<[CastDisplayInfo](#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. | 3040 3041**Error codes** 3042 3043For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3044 3045| ID| Error Message| 3046| -------- | ---------------------------------------- | 3047| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3048| 6600101 | Session service exception. | 3049| 6600102 | The session does not exist. | 3050 3051**Example** 3052 3053```ts 3054currentAVSession.off('castDisplayChange'); 3055``` 3056 3057### stopCasting<sup>10+</sup> 3058 3059stopCasting(callback: AsyncCallback\<void>): void 3060 3061Stops castings. This API uses an asynchronous callback to return the result. 3062 3063**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3064 3065**Parameters** 3066 3067| Name | Type | Mandatory| Description | 3068| -------- | ------------------------------------- | ---- | ------------------------------------- | 3069| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object.| 3070 3071**Error codes** 3072 3073For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3074 3075| ID| Error Message| 3076| -------- | ---------------------------------------- | 3077| 6600109 | The remote connection is not established. | 3078 3079**Example** 3080 3081```ts 3082import { BusinessError } from '@kit.BasicServicesKit'; 3083 3084currentAVSession.stopCasting((err: BusinessError) => { 3085 if (err) { 3086 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 3087 } else { 3088 console.info('stopCasting successfully'); 3089 } 3090}); 3091``` 3092 3093### stopCasting<sup>10+</sup> 3094 3095stopCasting(): Promise\<void> 3096 3097Stops castings. This API uses a promise to return the result. 3098 3099**Atomic service API**: This API can be used in atomic services since API version 12. 3100 3101**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3102 3103**Return value** 3104 3105| Type | Description | 3106| -------------- | ----------------------------- | 3107| Promise\<void> | Promise used to return the result. If casting stops, no value is returned; otherwise, an error object is returned.| 3108 3109**Error codes** 3110 3111For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3112 3113| ID| Error Message| 3114| -------- | ---------------------------------------- | 3115| 6600109 | The remote connection is not established. | 3116 3117**Example** 3118 3119```ts 3120import { BusinessError } from '@kit.BasicServicesKit'; 3121 3122currentAVSession.stopCasting().then(() => { 3123 console.info('stopCasting successfully'); 3124}).catch((err: BusinessError) => { 3125 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 3126}); 3127``` 3128 3129### getOutputDeviceSync<sup>10+</sup> 3130 3131getOutputDeviceSync(): OutputDeviceInfo 3132 3133Obtains the output device information. This API returns the result synchronously. 3134 3135**Atomic service API**: This API can be used in atomic services since API version 12. 3136 3137**System capability**: SystemCapability.Multimedia.AVSession.Core 3138 3139**Return value** 3140 3141| Type | Description | 3142| ----------------------------------------------- | --------------------------------- | 3143| [OutputDeviceInfo](#outputdeviceinfo10) | Information about the output device.| 3144 3145**Error codes** 3146 3147For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3148 3149| ID | Error Message| 3150|---------| --------------------------------------- | 3151| 6600101 | Session service exception. | 3152| 6600102 | The session does not exist. | 3153 3154**Example** 3155 3156```ts 3157import { BusinessError } from '@kit.BasicServicesKit'; 3158 3159try { 3160 let currentOutputDevice: avSession.OutputDeviceInfo = currentAVSession.getOutputDeviceSync(); 3161} catch (err) { 3162 let error = err as BusinessError; 3163 console.error(`getOutputDeviceSync error, error code: ${error.code}, error message: ${error.message}`); 3164} 3165``` 3166### getAllCastDisplays<sup>12+</sup> 3167 3168getAllCastDisplays(): Promise<Array\<CastDisplayInfo>> 3169 3170Obtains all displays that support extended screen projection in the current system. This API uses a promise to return the result. 3171 3172**Atomic service API**: This API can be used in atomic services since API version 12. 3173 3174**System capability**: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast 3175 3176**Return value** 3177 3178| Type | Description | 3179| ----------------------------------------------- | --------------------------------- | 3180| Promise<Array<[CastDisplayInfo](#castdisplayinfo12)>>| Promise used to return the information about all the cast displays.| 3181 3182**Error codes** 3183 3184For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3185 3186| ID | Error Message| 3187|---------| --------------------------------------- | 3188| 6600101 | Session service exception. | 3189| 6600102 | The session does not exist. | 3190 3191**Example** 3192 3193```ts 3194import { BusinessError } from '@kit.BasicServicesKit'; 3195 3196let castDisplay: avSession.CastDisplayInfo; 3197currentAVSession.getAllCastDisplays().then((data: Array< avSession.CastDisplayInfo >) => { 3198 if (data.length >= 1) { 3199 castDisplay = data[0]; 3200 } 3201 }).catch((err: BusinessError) => { 3202 console.error(`Failed to getAllCastDisplay. Code: ${err.code}, message: ${err.message}`); 3203 }); 3204``` 3205 3206## AVCastControlCommandType<sup>10+</sup> 3207 3208type AVCastControlCommandType = 'play' | 'pause' | 'stop' | 'playNext' | 'playPrevious' | 'fastForward' | 'rewind' | 3209 'seek' | 'setVolume' | 'setSpeed' | 'setLoopMode' | 'toggleFavorite' | 'toggleMute' 3210 3211Enumerates the commands that can be sent by a cast controller. 3212 3213**Atomic service API**: This API can be used in atomic services since API version 12. 3214 3215**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3216 3217| Type | Description | 3218| ---------------- | ------------------------------ | 3219| 'play' | Play the media. | 3220| 'pause' | Pause the playback. | 3221| 'stop' | Stop the playback. | 3222| 'playNext' | Play the next media asset. | 3223| 'playPrevious' | Play the previous media asset. | 3224| 'fastForward' | Fast-forward. | 3225| 'rewind' | Rewind. | 3226| 'seek' | Seek to a playback position. | 3227| 'setVolume' | Set the volume. | 3228| 'setSpeed' | Set the playback speed. | 3229| 'setLoopMode' | Set the loop mode. | 3230| 'toggleFavorite' | Favorite the media asset. | 3231| 'toggleMute' | Set the mute status. | 3232 3233## AVCastControlCommand<sup>10+</sup> 3234 3235Defines the command that can be sent by a cast controller. 3236 3237**Atomic service API**: This API can be used in atomic services since API version 12. 3238 3239**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3240 3241| Name | Type | Mandatory | Description | 3242| --------- | ------------------------------------------------------------ | --------- | ---------------------------------- | 3243| command | [AVCastControlCommandType](#avcastcontrolcommandtype10) | Yes | Command. | 3244| parameter | [media.PlaybackSpeed](../apis-media-kit/js-apis-media.md#playbackspeed8) | number | string | [LoopMode](#loopmode10) | No | Parameters carried in the command. | 3245 3246## AVCastController<sup>10+</sup> 3247 3248After a casting connection is set up, you can call [avSession.getAVCastController](#getavcastcontroller10) to obtain the cast controller. Through the controller, you can query the session ID, send commands and events to a session, and obtain session metadata and playback state information. 3249 3250### getAVPlaybackState<sup>10+</sup> 3251 3252getAVPlaybackState(callback: AsyncCallback\<AVPlaybackState>): void 3253 3254Obtains the remote playback state. This API uses an asynchronous callback to return the result. 3255 3256**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3257 3258**Parameters** 3259 3260| Name | Type | Mandatory | Description | 3261| -------- | ----------------------------------------------------- | --------- | -------------------------------------------------- | 3262| callback | AsyncCallback<[AVPlaybackState](#avplaybackstate10)\> | Yes | Callback used to return the remote playback state. | 3263 3264**Error codes** 3265 3266For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3267 3268| ID | Error Message | 3269| ------- | ------------------------- | 3270| 6600101 | Session service exception | 3271 3272**Example** 3273 3274```ts 3275import { BusinessError } from '@kit.BasicServicesKit'; 3276 3277aVCastController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { 3278 if (err) { 3279 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 3280 } else { 3281 console.info('getAVPlaybackState : SUCCESS'); 3282 } 3283}); 3284``` 3285 3286### getAVPlaybackState<sup>10+</sup> 3287 3288getAVPlaybackState(): Promise\<AVPlaybackState> 3289 3290Obtains the remote playback state. This API uses a promise to return the result. 3291 3292**Atomic service API**: This API can be used in atomic services since API version 12. 3293 3294**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3295 3296**Return value** 3297 3298| Type | Description | 3299| ----------------------------------------------- | ------------------------------------------------- | 3300| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise used to return the remote playback state. | 3301 3302**Error codes** 3303 3304For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3305 3306| ID | Error Message | 3307| ------- | ------------------------- | 3308| 6600101 | Session service exception | 3309 3310**Example** 3311 3312```ts 3313import { BusinessError } from '@kit.BasicServicesKit'; 3314 3315aVCastController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { 3316 console.info('getAVPlaybackState : SUCCESS'); 3317}).catch((err: BusinessError) => { 3318 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 3319}); 3320``` 3321 3322### sendControlCommand<sup>10+</sup> 3323 3324sendControlCommand(command: AVCastControlCommand): Promise\<void> 3325 3326Sends a control command to the session through the controller. This API uses a promise to return the result. 3327 3328 3329**Atomic service API**: This API can be used in atomic services since API version 12. 3330 3331**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3332 3333**Parameters** 3334 3335| Name | Type | Mandatory | Description | 3336| ------- | ----------------------------------------------- | --------- | ---------------- | 3337| command | [AVCastControlCommand](#avcastcontrolcommand10) | Yes | Command to send. | 3338 3339**Return value** 3340 3341| Type | Description | 3342| -------------- | ------------------------------------------------------------ | 3343| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned. | 3344 3345**Error codes** 3346 3347For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3348 3349| ID | Error Message | 3350| ------- | ------------------------------------------------------------ | 3351| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 3352| 6600101 | Session service exception. | 3353| 6600105 | Invalid session command. | 3354| 6600109 | The remote connection is not established. | 3355 3356**Example** 3357 3358```ts 3359import { BusinessError } from '@kit.BasicServicesKit'; 3360 3361let avCommand: avSession.AVCastControlCommand = {command:'play'}; 3362aVCastController.sendControlCommand(avCommand).then(() => { 3363 console.info('SendControlCommand successfully'); 3364}).catch((err: BusinessError) => { 3365 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 3366}); 3367``` 3368 3369### sendControlCommand<sup>10+</sup> 3370 3371sendControlCommand(command: AVCastControlCommand, callback: AsyncCallback\<void>): void 3372 3373Sends a control command to the session through the controller. This API uses an asynchronous callback to return the result. 3374 3375 3376**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3377 3378**Parameters** 3379 3380| Name | Type | Mandatory | Description | 3381| -------- | ----------------------------------------------- | --------- | ------------------------------------------------------------ | 3382| command | [AVCastControlCommand](#avcastcontrolcommand10) | Yes | Command to send. | 3383| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 3384 3385**Error codes** 3386 3387For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3388 3389| ID | Error Message | 3390| ------- | ------------------------------------------------------------ | 3391| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 3392| 6600101 | Session service exception. | 3393| 6600105 | Invalid session command. | 3394| 6600109 | The remote connection is not established. | 3395 3396**Example** 3397 3398```ts 3399import { BusinessError } from '@kit.BasicServicesKit'; 3400 3401let avCommand: avSession.AVCastControlCommand = {command:'play'}; 3402aVCastController.sendControlCommand(avCommand, (err: BusinessError) => { 3403 if (err) { 3404 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 3405 } else { 3406 console.info('SendControlCommand successfully'); 3407 } 3408}); 3409``` 3410 3411### prepare<sup>10+</sup> 3412 3413prepare(item: AVQueueItem, callback: AsyncCallback\<void>): void 3414 3415Prepares for the playback of a media asset, that is, loads and buffers a media asset. This API uses an asynchronous callback to return the result. 3416 3417**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3418 3419**Parameters** 3420 3421| Name | Type | Mandatory | Description | 3422| -------- | ----------------------------- | --------- | ------------------------------------------------------------ | 3423| item | [AVQueueItem](#avqueueitem10) | Yes | Attributes of an item in the playlist. | 3424| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 3425 3426**Error codes** 3427 3428For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3429 3430| ID | Error Message | 3431| ------- | ------------------------------------------------------------ | 3432| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 3433| 6600101 | Session service exception. | 3434| 6600109 | The remote connection is not established. | 3435 3436**Example** 3437 3438```ts 3439import { BusinessError } from '@kit.BasicServicesKit'; 3440 3441// Set playback parameters. 3442let playItem: avSession.AVQueueItem = { 3443 itemId: 0, 3444 description: { 3445 assetId: '12345', 3446 mediaType: 'AUDIO', 3447 mediaUri: 'http://resource1_address', 3448 mediaSize: 12345, 3449 startPosition: 0, 3450 duration: 0, 3451 artist: 'mysong', 3452 albumTitle: 'song1_title', 3453 albumCoverUri: "http://resource1_album_address", 3454 lyricUri: "http://resource1_lyric_address", 3455 appName: 'MyMusic' 3456 } 3457}; 3458// Prepare for playback. This operation triggers loading and buffering, but not the actual playback. 3459aVCastController.prepare(playItem, (err: BusinessError) => { 3460 if (err) { 3461 console.error(`prepare BusinessError: code: ${err.code}, message: ${err.message}`); 3462 } else { 3463 console.info('prepare successfully'); 3464 } 3465}); 3466``` 3467 3468 3469### prepare<sup>10+</sup> 3470 3471prepare(item: AVQueueItem): Promise\<void> 3472 3473Prepares for the playback of a media asset, that is, loads and buffers a media asset. This API uses a promise to return the result. 3474 3475 3476**Atomic service API**: This API can be used in atomic services since API version 12. 3477 3478**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3479 3480**Parameters** 3481 3482| Name | Type | Mandatory | Description | 3483| ---- | ----------------------------- | --------- | -------------------------------------- | 3484| item | [AVQueueItem](#avqueueitem10) | Yes | Attributes of an item in the playlist. | 3485 3486**Return value** 3487 3488| Type | Description | 3489| -------------- | ------------------------------------------------------------ | 3490| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned. | 3491 3492**Error codes** 3493 3494For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3495 3496| ID | Error Message | 3497| ------- | ------------------------------------------------------------ | 3498| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 3499| 6600101 | Session service exception. | 3500| 6600109 | The remote connection is not established. | 3501 3502 3503**Example** 3504 3505```ts 3506import { BusinessError } from '@kit.BasicServicesKit'; 3507 3508// Set playback parameters. 3509let playItem: avSession.AVQueueItem = { 3510 itemId: 0, 3511 description: { 3512 assetId: '12345', 3513 mediaType: 'AUDIO', 3514 mediaUri: 'http://resource1_address', 3515 mediaSize: 12345, 3516 startPosition: 0, 3517 duration: 0, 3518 artist: 'mysong', 3519 albumTitle: 'song1_title', 3520 albumCoverUri: "http://resource1_album_address", 3521 lyricUri: "http://resource1_lyric_address", 3522 appName: 'MyMusic' 3523 } 3524}; 3525// Prepare for playback. This operation triggers loading and buffering, but not the actual playback. 3526aVCastController.prepare(playItem).then(() => { 3527 console.info('prepare successfully'); 3528}).catch((err: BusinessError) => { 3529 console.error(`prepare BusinessError: code: ${err.code}, message: ${err.message}`); 3530}); 3531``` 3532 3533### start<sup>10+</sup> 3534 3535start(item: AVQueueItem, callback: AsyncCallback\<void>): void 3536 3537Prepares for the playback of a media asset. This API uses an asynchronous callback to return the result. 3538 3539**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3540 3541**Parameters** 3542 3543| Name | Type | Mandatory | Description | 3544| -------- | ----------------------------- | --------- | ------------------------------------------------------------ | 3545| item | [AVQueueItem](#avqueueitem10) | Yes | Attributes of an item in the playlist. | 3546| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 3547 3548**Error codes** 3549 3550For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3551 3552| ID | Error Message | 3553| ------- | ------------------------------------------------------------ | 3554| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 3555| 6600101 | Session service exception. | 3556| 6600109 | The remote connection is not established. | 3557 3558**Example** 3559 3560```ts 3561import { BusinessError } from '@kit.BasicServicesKit'; 3562 3563// Set playback parameters. 3564let playItem: avSession.AVQueueItem = { 3565 itemId: 0, 3566 description: { 3567 assetId: '12345', 3568 mediaType: 'AUDIO', 3569 mediaUri: 'http://resource1_address', 3570 mediaSize: 12345, 3571 startPosition: 0, 3572 duration: 0, 3573 artist: 'mysong', 3574 albumTitle: 'song1_title', 3575 albumCoverUri: "http://resource1_album_address", 3576 lyricUri: "http://resource1_lyric_address", 3577 appName: 'MyMusic' 3578 } 3579}; 3580 3581// Start playback. 3582aVCastController.start(playItem, (err: BusinessError) => { 3583 if (err) { 3584 console.error(`start BusinessError: code: ${err.code}, message: ${err.message}`); 3585 } else { 3586 console.info('start successfully'); 3587 } 3588}); 3589``` 3590 3591### start<sup>10+</sup> 3592 3593start(item: AVQueueItem): Promise\<void> 3594 3595Prepares for the playback of a media asset. This API uses a promise to return the result. 3596 3597 3598**Atomic service API**: This API can be used in atomic services since API version 12. 3599 3600**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3601 3602**Parameters** 3603 3604| Name | Type | Mandatory | Description | 3605| ---- | ----------------------------- | --------- | -------------------------------------- | 3606| item | [AVQueueItem](#avqueueitem10) | Yes | Attributes of an item in the playlist. | 3607 3608**Return value** 3609 3610| Type | Description | 3611| -------------- | ------------------------------------------------------------ | 3612| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned. | 3613 3614**Error codes** 3615 3616For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3617 3618| ID | Error Message | 3619| ------- | ------------------------------------------------------------ | 3620| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 3621| 6600101 | Session service exception. | 3622| 6600109 | The remote connection is not established. | 3623 3624 3625**Example** 3626 3627```ts 3628import { BusinessError } from '@kit.BasicServicesKit'; 3629 3630// Set playback parameters. 3631let playItem: avSession.AVQueueItem = { 3632 itemId: 0, 3633 description: { 3634 assetId: '12345', 3635 mediaType: 'AUDIO', 3636 mediaUri: 'http://resource1_address', 3637 mediaSize: 12345, 3638 startPosition: 0, 3639 duration: 0, 3640 artist: 'mysong', 3641 albumTitle: 'song1_title', 3642 albumCoverUri: "http://resource1_album_address", 3643 lyricUri: "http://resource1_lyric_address", 3644 appName: 'MyMusic' 3645 } 3646}; 3647// Start playback. 3648aVCastController.start(playItem).then(() => { 3649 console.info('start successfully'); 3650}).catch((err: BusinessError) => { 3651 console.error(`start BusinessError: code: ${err.code}, message: ${err.message}`); 3652}); 3653``` 3654 3655### getCurrentItem<sup>10+</sup> 3656 3657getCurrentItem(callback: AsyncCallback\<AVQueueItem>): void 3658 3659Obtains the information about the media asset that is being played. This API uses an asynchronous callback to return the result. 3660 3661**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3662 3663**Parameters** 3664 3665| Name | Type | Mandatory | Description | 3666| -------- | --------------------------------------------- | --------- | ------------------------------------------------------------ | 3667| callback | AsyncCallback\<[AVQueueItem](#avqueueitem10)> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 3668 3669**Error codes** 3670 3671For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3672 3673| ID | Error Message | 3674| ------- | -------------------------- | 3675| 6600101 | Session service exception. | 3676 3677**Example** 3678 3679```ts 3680import { BusinessError } from '@kit.BasicServicesKit'; 3681 3682aVCastController.getCurrentItem((err: BusinessError, value: avSession.AVQueueItem) => { 3683 if (err) { 3684 console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); 3685 } else { 3686 console.info('getCurrentItem successfully'); 3687 } 3688}); 3689``` 3690 3691### getCurrentItem<sup>10+</sup> 3692 3693getCurrentItem(): Promise\<AVQueueItem> 3694 3695Obtains the information about the media asset that is being played. This API uses a promise to return the result. 3696 3697**Atomic service API**: This API can be used in atomic services since API version 12. 3698 3699**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3700 3701**Return value** 3702 3703| Type | Description | 3704| --------------------------------------- | ------------------------------------------------------------ | 3705| Promise\<[AVQueueItem](#avqueueitem10)> | Promise used to return the media asset obtained. If the operation fails, an error object is returned. | 3706 3707**Error codes** 3708 3709For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3710 3711| ID | Error Message | 3712| ------- | -------------------------- | 3713| 6600101 | Session service exception. | 3714 3715**Example** 3716 3717```ts 3718import { BusinessError } from '@kit.BasicServicesKit'; 3719 3720aVCastController.getCurrentItem().then((value: avSession.AVQueueItem) => { 3721 console.info('getCurrentItem successfully'); 3722}).catch((err: BusinessError) => { 3723 console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); 3724}); 3725``` 3726 3727### getValidCommands<sup>11+</sup> 3728 3729getValidCommands(callback: AsyncCallback<Array\<AVCastControlCommandType>>): void 3730 3731Obtains the supported commands. This API uses an asynchronous callback to return the result. 3732 3733**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3734 3735**Parameters** 3736 3737| Name | Type | Mandatory | Description | 3738| -------- | ------------------------------------------------------------ | --------- | ----------------------------------------------- | 3739| callback | Array<[AVCastControlCommandType](#avcastcontrolcommandtype10)> | Yes | Callback used to return the supported commands. | 3740 3741**Error codes** 3742 3743For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3744 3745| ID | Error Message | 3746| ------- | -------------------------- | 3747| 6600101 | Session service exception. | 3748 3749**Example** 3750 3751```ts 3752import { BusinessError } from '@kit.BasicServicesKit'; 3753 3754aVCastController.getValidCommands((err: BusinessError, state: avSession.AVCastControlCommandType) => { 3755 if (err) { 3756 console.error(`getValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 3757 } else { 3758 console.info('getValidCommands successfully'); 3759 } 3760}); 3761``` 3762 3763### getValidCommands<sup>11+</sup> 3764 3765getValidCommands(): Promise<Array\<AVCastControlCommandType>> 3766 3767Obtains the supported commands. This API uses a promise to return the result. 3768 3769**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3770 3771**Return value** 3772 3773| Type | Description | 3774| ------------------------------------------------------------ | ---------------------------------------------- | 3775| Promise<Array\<[AVCastControlCommandType](#avcastcontrolcommandtype10)>> | Promise used to return the supported commands. | 3776 3777**Error codes** 3778 3779For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3780 3781| ID | Error Message | 3782| ------- | -------------------------- | 3783| 6600101 | Session service exception. | 3784 3785**Example** 3786 3787```ts 3788import { BusinessError } from '@kit.BasicServicesKit'; 3789 3790aVCastController.getValidCommands().then((state: avSession.AVCastControlCommandType) => { 3791 console.info('getValidCommands successfully'); 3792}).catch((err: BusinessError) => { 3793 console.error(`getValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 3794}); 3795``` 3796 3797### processMediaKeyResponse<sup>12+</sup> 3798 3799processMediaKeyResponse(assetId: string, response: Uint8Array): Promise\<void> 3800 3801Processes the response to a media key request during online DRM resource projection. This API uses a promise to return the result. 3802 3803**Atomic service API**: This API can be used in atomic services since API version 12. 3804 3805**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3806 3807**Parameters** 3808 3809| Name | Type | Mandatory | Description | 3810| -------- | ---------- | --------- | ---------------------------------- | 3811| assetId | string | Yes | Media asset ID. | 3812| response | Uint8Array | Yes | Response to the media key request. | 3813 3814**Return value** 3815 3816| Type | Description | 3817| -------------- | ------------------------------------------------------------ | 3818| Promise\<void> | Promise used to return the result. If the response is processed successfully, no result is returned. Otherwise, an error object is returned. | 3819 3820**Error codes** 3821 3822For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3823 3824| ID | Error Message | 3825| ------- | ------------------------------------------------------------ | 3826| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 3827| 6600101 | Session service exception. | 3828 3829**Example** 3830 3831```ts 3832let keyRequestCallback: avSession.KeyRequestCallback = async(assetId: string, requestData: Uint8Array) => { 3833 // Obtain the DRM URL based on the asset ID. 3834 let drmUrl = 'http://license.xxx.xxx.com:8080/drmproxy/getLicense'; 3835 // Obtain a media key from the server. Assign a value based on service requirements. 3836 let licenseResponseData: Uint8Array = new Uint8Array(); 3837 console.info(`Succeeded in get license by ${drmUrl}.`); 3838 aVCastController.processMediaKeyResponse(assetId, licenseResponseData); 3839} 3840``` 3841 3842### release<sup>11+</sup> 3843 3844release(callback: AsyncCallback\<void>): void 3845 3846Releases this cast controller. This API uses an asynchronous callback to return the result. 3847 3848**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3849 3850**Parameters** 3851 3852| Name | Type | Mandatory | Description | 3853| -------- | -------------------- | --------- | ------------------------------------------------------------ | 3854| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the controller is released, **err** is **undefined**; otherwise, **err** is an error object. | 3855 3856**Error codes** 3857 3858For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3859 3860| ID | Error Message | 3861| ------- | -------------------------- | 3862| 6600101 | Session service exception. | 3863 3864**Example** 3865 3866```ts 3867import { BusinessError } from '@kit.BasicServicesKit'; 3868 3869aVCastController.release((err: BusinessError) => { 3870 if (err) { 3871 console.error(`release BusinessError: code: ${err.code}, message: ${err.message}`); 3872 } else { 3873 console.info('release successfully'); 3874 } 3875}); 3876``` 3877 3878### release<sup>11+</sup> 3879 3880release(): Promise\<void> 3881 3882Releases this cast controller. This API uses a promise to return the result. 3883 3884**Atomic service API**: This API can be used in atomic services since API version 12. 3885 3886**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3887 3888**Return value** 3889 3890| Type | Description | 3891| -------------- | ------------------------------------------------------------ | 3892| Promise\<void> | Promise used to return the result. If the controller is released, no value is returned; otherwise, an error object is returned. | 3893 3894**Error codes** 3895 3896For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3897 3898| ID | Error Message | 3899| ------- | -------------------------- | 3900| 6600101 | Session service exception. | 3901 3902**Example** 3903 3904```ts 3905import { BusinessError } from '@kit.BasicServicesKit'; 3906 3907aVCastController.release().then(() => { 3908 console.info('release successfully'); 3909}).catch((err: BusinessError) => { 3910 console.error(`release BusinessError: code: ${err.code}, message: ${err.message}`); 3911}); 3912 3913``` 3914 3915### on('playbackStateChange')<sup>10+</sup> 3916 3917on(type: 'playbackStateChange', filter: Array\<keyof AVPlaybackState> | 'all', callback: (state: AVPlaybackState) => void): void 3918 3919Subscribes to playback state change events. 3920 3921**Atomic service API**: This API can be used in atomic services since API version 12. 3922 3923**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3924 3925**Parameters** 3926 3927| Name | Type | Mandatory | Description | 3928| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 3929| type | string | Yes | Event type. The event **'playbackStateChange'** is triggered when the playback state changes. | 3930| filter | Array\<keyof [AVPlaybackState](#avplaybackstate10)\> | 'all' | Yes | The value **'all'** indicates that any playback state field change will trigger the event, and **Array<keyof [AVPlaybackState](#avplaybackstate10)\>** indicates that only changes to the listed playback state field will trigger the event. | 3931| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | Yes | Callback used for subscription. The **state** parameter in the callback indicates the changed playback state. | 3932 3933**Error codes** 3934 3935For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3936 3937| ID | Error Message | 3938| ------- | ------------------------------------------------------------ | 3939| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3940| 6600101 | Session service exception. | 3941 3942**Example** 3943 3944```ts 3945aVCastController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { 3946 console.info(`on playbackStateChange state : ${playbackState.state}`); 3947}); 3948 3949let playbackFilter: Array<keyof avSession.AVPlaybackState> = ['state', 'speed', 'loopMode']; 3950aVCastController.on('playbackStateChange', playbackFilter, (playbackState: avSession.AVPlaybackState) => { 3951 console.info(`on playbackStateChange state : ${playbackState.state}`); 3952}); 3953``` 3954 3955### off('playbackStateChange')<sup>10+</sup> 3956 3957off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void): void 3958 3959Unsubscribes from playback state change events. This API is called by the controller. 3960 3961**Atomic service API**: This API can be used in atomic services since API version 12. 3962 3963**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3964 3965**Parameters** 3966 3967| Name | Type | Mandatory | Description | 3968| -------- | ------------------------------------------------------ | --------- | ------------------------------------------------------------ | 3969| type | string | Yes | Event type, which is **'playbackStateChange'** in this case. | 3970| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | No | Callback used for unsubscription. The **state** parameter in the callback indicates the changed playback state.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 3971 3972**Error codes** 3973 3974For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 3975 3976| ID | Error Message | 3977| ------- | ------------------------------------------------------------ | 3978| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 3979| 6600101 | Session service exception. | 3980 3981**Example** 3982 3983```ts 3984aVCastController.off('playbackStateChange'); 3985``` 3986 3987### on('mediaItemChange')<sup>10+</sup> 3988 3989on(type: 'mediaItemChange', callback: Callback\<AVQueueItem>): void 3990 3991Subscribes to media asset change events. 3992 3993**Atomic service API**: This API can be used in atomic services since API version 12. 3994 3995**System capability**: SystemCapability.Multimedia.AVSession.AVCast 3996 3997**Parameters** 3998 3999| Name | Type | Mandatory | Description | 4000| -------- | ------------------------------------------------- | --------- | ------------------------------------------------------------ | 4001| type | string | Yes | Event type. The event **'mediaItemChange'** is triggered when the media content being played changes. | 4002| callback | (callback: [AVQueueItem](#avqueueitem10)) => void | Yes | Callback used for subscription. **AVQueueItem** is the media asset that is being played. | 4003 4004**Error codes** 4005 4006For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4007 4008| ID | Error Message | 4009| ------- | ------------------------------------------------------------ | 4010| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4011| 6600101 | Session service exception. | 4012 4013**Example** 4014 4015```ts 4016aVCastController.on('mediaItemChange', (item: avSession.AVQueueItem) => { 4017 console.info(`on mediaItemChange state : ${item.itemId}`); 4018}); 4019``` 4020 4021### off('mediaItemChange')<sup>10+</sup> 4022 4023off(type: 'mediaItemChange'): void 4024 4025Unsubscribes from media asset change events. 4026 4027**Atomic service API**: This API can be used in atomic services since API version 12. 4028 4029**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4030 4031**Parameters** 4032 4033| Name | Type | Mandatory | Description | 4034| ---- | ------ | --------- | -------------------------------------------------------- | 4035| type | string | Yes | Event type, which is **'mediaItemChange'** in this case. | 4036 4037**Error codes** 4038 4039For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4040 4041| ID | Error Message | 4042| ------- | ------------------------------------------------------------ | 4043| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4044| 6600101 | Session service exception. | 4045 4046**Example** 4047 4048```ts 4049aVCastController.off('mediaItemChange'); 4050``` 4051 4052### on('playNext')<sup>10+</sup> 4053 4054on(type: 'playNext', callback: Callback\<void>): void 4055 4056Subscribes to playNext command events. 4057 4058**Atomic service API**: This API can be used in atomic services since API version 12. 4059 4060**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4061 4062**Parameters** 4063 4064| Name | Type | Mandatory | Description | 4065| -------- | ---------------- | --------- | ------------------------------------------------------------ | 4066| type | string | Yes | Event type. The event **'playNext'** is triggered when the command for playing the next item is received. | 4067| callback | Callback\<void\> | Yes | Callback used to return the result. | 4068 4069**Error codes** 4070 4071For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4072 4073| ID | Error Message | 4074| ------- | ------------------------------------------------------------ | 4075| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4076| 6600101 | Session service exception. | 4077 4078**Example** 4079 4080```ts 4081aVCastController.on('playNext', () => { 4082 console.info('on playNext'); 4083}); 4084``` 4085 4086### off('playNext')<sup>10+</sup> 4087 4088off(type: 'playNext'): void 4089 4090Unsubscribes from playNext command events. 4091 4092**Atomic service API**: This API can be used in atomic services since API version 12. 4093 4094**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4095 4096**Parameters** 4097 4098| Name | Type | Mandatory | Description | 4099| ---- | ------ | --------- | ------------------------------------------------- | 4100| type | string | Yes | Event type, which is **'playNext'** in this case. | 4101 4102**Error codes** 4103 4104For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4105 4106| ID | Error Message | 4107| ------- | ------------------------------------------------------------ | 4108| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4109| 6600101 | Session service exception. | 4110 4111**Example** 4112 4113```ts 4114aVCastController.off('playNext'); 4115``` 4116 4117### on('playPrevious')<sup>10+</sup> 4118 4119on(type: 'playPrevious', callback: Callback\<void>): void 4120 4121Subscribes to playPrevious command events. 4122 4123**Atomic service API**: This API can be used in atomic services since API version 12. 4124 4125**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4126 4127**Parameters** 4128 4129| Name | Type | Mandatory | Description | 4130| -------- | ---------------- | --------- | ------------------------------------------------------------ | 4131| type | string | Yes | Event type. The event **'playPrevious'** is triggered when the command for playing the previous event is received. | 4132| callback | Callback\<void\> | Yes | Callback used to return the result. | 4133 4134**Error codes** 4135 4136For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4137 4138| ID | Error Message | 4139| ------- | ------------------------------------------------------------ | 4140| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4141| 6600101 | Session service exception. | 4142 4143**Example** 4144 4145```ts 4146aVCastController.on('playPrevious', () => { 4147 console.info('on playPrevious'); 4148}); 4149``` 4150 4151### off('playPrevious')<sup>10+</sup> 4152 4153off(type: 'playPrevious'): void 4154 4155Unsubscribes from playPrevious command events. 4156 4157**Atomic service API**: This API can be used in atomic services since API version 12. 4158 4159**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4160 4161**Parameters** 4162 4163| Name | Type | Mandatory | Description | 4164| ---- | ------ | --------- | ----------------------------------------------------- | 4165| type | string | Yes | Event type, which is **'playPrevious'** in this case. | 4166 4167**Error codes** 4168 4169For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4170 4171| ID | Error Message | 4172| ------- | ------------------------------------------------------------ | 4173| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4174| 6600101 | Session service exception. | 4175 4176**Example** 4177 4178```ts 4179aVCastController.off('playPrevious'); 4180``` 4181 4182### on('requestPlay')<sup>11+</sup> 4183 4184on(type: 'requestPlay', callback: Callback\<AVQueueItem>): void 4185 4186Subscribes to playback request events. 4187 4188**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4189 4190**Parameters** 4191 4192| Name | Type | Mandatory | Description | 4193| -------- | ---------------------------------------------- | --------- | ------------------------------------------------------------ | 4194| type | string | Yes | Event type. The event **'requestPlay'** is triggered when a playback request is received. | 4195| callback | (state: [AVQueueItem](#avqueueitem10)) => void | Yes | Callback used for subscription. **AVQueueItem** is the media asset that is being played. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 4196 4197**Error codes** 4198 4199For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4200 4201| ID | Error Message | 4202| ------- | ------------------------------------------------------------ | 4203| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4204| 6600101 | Session service exception. | 4205 4206**Example** 4207 4208```ts 4209aVCastController.on('requestPlay', (item: avSession.AVQueueItem) => { 4210 console.info(`on requestPlay state : ${item.itemId}`); 4211}); 4212``` 4213 4214### off('requestPlay')<sup>11+</sup> 4215 4216off(type: 'requestPlay', callback?: Callback\<AVQueueItem>): void 4217 4218Unsubscribes from playback request events. 4219 4220**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4221 4222**Parameters** 4223 4224| Name | Type | Mandatory | Description | 4225| -------- | ---------------------------------------------- | --------- | ------------------------------------------------------------ | 4226| type | string | Yes | Event type, which is **'requestPlay'** in this case. | 4227| callback | (state: [AVQueueItem](#avqueueitem10)) => void | No | Callback used for unsubscription. **AVQueueItem** is the media asset that is being played. 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. | 4228 4229**Error codes** 4230 4231For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4232 4233| ID | Error Message | 4234| ------- | ------------------------------------------------------------ | 4235| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4236| 6600101 | Session service exception. | 4237 4238**Example** 4239 4240```ts 4241aVCastController.off('requestPlay'); 4242``` 4243 4244### on('endOfStream')<sup>11+</sup> 4245 4246on(type: 'endOfStream', callback: Callback\<void>): void 4247 4248Subscribes to playback end events. 4249 4250**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4251 4252**Parameters** 4253 4254| Name | Type | Mandatory | Description | 4255| -------- | ---------------- | --------- | ------------------------------------------------------------ | 4256| type | string | Yes | Event type. The event **'endOfStream'** is triggered when the playback operation is complete. | 4257| callback | Callback\<void\> | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 4258 4259**Error codes** 4260 4261For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4262 4263| ID | Error Message | 4264| ------- | ------------------------------------------------------------ | 4265| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4266| 6600101 | Session service exception. | 4267 4268**Example** 4269 4270```ts 4271aVCastController.on('endOfStream', () => { 4272 console.info('on endOfStream'); 4273}); 4274``` 4275 4276### off('endOfStream')<sup>11+</sup> 4277 4278off(type: 'endOfStream', callback?: Callback\<void>): void 4279 4280Unsubscribes from the playback end events. 4281 4282**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4283 4284**Parameters** 4285 4286| Name | Type | Mandatory | Description | 4287| -------- | ---------------- | --------- | ------------------------------------------------------------ | 4288| type | string | Yes | Event type, which is **'endOfStream'** in this case. | 4289| callback | Callback\<void\> | 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. | 4290 4291**Error codes** 4292 4293For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4294 4295| ID | Error Message | 4296| ------- | ------------------------------------------------------------ | 4297| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4298| 6600101 | Session service exception. | 4299 4300**Example** 4301 4302```ts 4303aVCastController.off('endOfStream'); 4304``` 4305 4306### on('seekDone')<sup>10+</sup> 4307 4308on(type: 'seekDone', callback: Callback\<number>): void 4309 4310Subscribes to seek done events. 4311 4312**Atomic service API**: This API can be used in atomic services since API version 12. 4313 4314**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4315 4316**Parameters** 4317 4318| Name | Type | Mandatory | Description | 4319| -------- | ------------------ | --------- | ------------------------------------------------------------ | 4320| type | string | Yes | Event type. The event **'seekDone'** is triggered when the seek operation is complete. | 4321| callback | Callback\<number\> | Yes | Callback used to return the position after the seek operation. | 4322 4323**Error codes** 4324 4325For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4326 4327| ID | Error Message | 4328| ------- | ------------------------------------------------------------ | 4329| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4330| 6600101 | Session service exception. | 4331 4332**Example** 4333 4334```ts 4335aVCastController.on('seekDone', (pos: number) => { 4336 console.info(`on seekDone pos: ${pos} `); 4337}); 4338``` 4339 4340### off('seekDone')<sup>10+</sup> 4341 4342off(type: 'seekDone'): void 4343 4344Unsubscribes from the seek done events. 4345 4346**Atomic service API**: This API can be used in atomic services since API version 12. 4347 4348**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4349 4350**Parameters** 4351 4352| Name | Type | Mandatory | Description | 4353| ---- | ------ | --------- | ------------------------------------------------- | 4354| type | string | Yes | Event type, which is **'seekDone'** in this case. | 4355 4356**Error codes** 4357 4358For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4359 4360| ID | Error Message | 4361| ------- | ------------------------------------------------------------ | 4362| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4363| 6600101 | Session service exception. | 4364 4365**Example** 4366 4367```ts 4368aVCastController.off('seekDone'); 4369``` 4370 4371### on('validCommandChange')<sup>11+</sup> 4372 4373on(type: 'validCommandChange', callback: Callback\<Array\<AVCastControlCommandType>>) 4374 4375Subscribes to valid command change events. 4376 4377**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4378 4379**Parameters** 4380 4381| Name | Type | Mandatory | Description | 4382| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 4383| type | string | Yes | Event type. The event **'validCommandChange'** is triggered when the valid commands supported by the session changes. | 4384| callback | Callback<Array<[AVCastControlCommandType](#avcastcontrolcommandtype10)\>\> | Yes | Callback used for subscription. The **commands** parameter in the callback is a set of valid commands. | 4385 4386**Error codes** 4387 4388For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4389 4390| ID | Error Message | 4391| ------- | ------------------------------------------------------------ | 4392| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4393| 6600101 | Session service exception. | 4394| 6600103 | The session controller does not exist. | 4395 4396**Example** 4397 4398```ts 4399aVCastController.on('validCommandChange', (validCommands: avSession.AVCastControlCommandType[]) => { 4400 console.info(`validCommandChange : SUCCESS : size : ${validCommands.length}`); 4401 console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); 4402}); 4403``` 4404 4405### off('validCommandChange')<sup>11+</sup> 4406 4407off(type: 'validCommandChange', callback?: Callback\<Array\<AVCastControlCommandType>>) 4408 4409Unsubscribes from valid command change events. This API is called by the controller. 4410 4411**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4412 4413**Parameters** 4414 4415| Name | Type | Mandatory | Description | 4416| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 4417| type | string | Yes | Event type, which is **'validCommandChange'** in this case. | 4418| callback | Callback<Array<[AVCastControlCommandType](#avcastcontrolcommandtype10)\>\> | No | Callback used for unsubscription. The **commands** parameter in the callback is a set of valid commands.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 4419 4420**Error codes** 4421 4422For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4423 4424| ID | Error Message | 4425| ------- | ------------------------------------------------------------ | 4426| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4427| 6600101 | Session service exception. | 4428| 6600103 | The session controller does not exist. | 4429 4430**Example** 4431 4432```ts 4433aVCastController.off('validCommandChange'); 4434``` 4435 4436### on('error')<sup>10+</sup> 4437 4438on(type: 'error', callback: ErrorCallback): void 4439 4440Subscribes to remote AVPlayer errors. This event is used only for error prompt and does not require the user to stop playback control. 4441 4442**Atomic service API**: This API can be used in atomic services since API version 12. 4443 4444**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4445 4446**Parameters** 4447 4448| Name | Type | Mandatory | Description | 4449| -------- | ------------- | --------- | ------------------------------------------------------------ | 4450| type | string | Yes | Event type, which is **'error'** in this case. This event can be triggered by both user operations and the system. | 4451| callback | ErrorCallback | Yes | Callback used to return the error code ID and error message. | 4452 4453**Error codes** 4454 4455For details about the error codes, see [Media Error Codes](../apis-media-kit/errorcode-media.md) and [AVSession Management Error Codes](errorcode-avsession.md). 4456 4457| ID | Error Message | 4458| ------- | ------------------------------------------------------------ | 4459| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4460| 5400101 | No memory. | 4461| 5400102 | Operation not allowed. | 4462| 5400103 | I/O error. | 4463| 5400104 | Time out. | 4464| 5400105 | Service died. | 4465| 5400106 | Unsupport format. | 4466| 6600101 | Session service exception. | 4467 4468**Example** 4469 4470```ts 4471import { BusinessError } from '@kit.BasicServicesKit'; 4472 4473aVCastController.on('error', (error: BusinessError) => { 4474 console.info(`error happened, error code: ${error.code}, error message : ${error.message}.`) 4475}) 4476``` 4477 4478### off('error')<sup>10+</sup> 4479 4480off(type: 'error'): void 4481 4482Unsubscribes from remote AVPlayer errors. 4483 4484**Atomic service API**: This API can be used in atomic services since API version 12. 4485 4486**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4487 4488**Parameters** 4489 4490| Name | Type | Mandatory | Description | 4491| ---- | ------ | --------- | ---------------------------------------------- | 4492| type | string | Yes | Event type, which is **'error'** in this case. | 4493 4494**Error codes** 4495 4496For details about the error codes, see [Media Error Codes](../apis-media-kit/errorcode-media.md) and [AVSession Management Error Codes](errorcode-avsession.md). 4497 4498| ID | Error Message | 4499| ------- | ------------------------------------------------------------ | 4500| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4501| 5400101 | No memory. | 4502| 5400102 | Operation not allowed. | 4503| 5400103 | I/O error. | 4504| 5400104 | Time out. | 4505| 5400105 | Service died. | 4506| 5400106 | Unsupport format. | 4507| 6600101 | Session service exception. | 4508 4509**Example** 4510 4511```ts 4512aVCastController.off('error') 4513``` 4514 4515### on('keyRequest')<sup>12+</sup> 4516 4517on(type: 'keyRequest', callback: KeyRequestCallback): void 4518 4519Subscribes to media key requests during the cast of online DRM resources. 4520 4521**Atomic service API**: This API can be used in atomic services since API version 12. 4522 4523**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4524 4525**Parameters** 4526 4527| Name | Type | Mandatory | Description | 4528| -------- | ------------------------------------------- | --------- | ------------------------------------------------------------ | 4529| type | string | Yes | Event type. The event **'keyRequest'** is triggered when a media key request is required during the cast of online DRM resources. | 4530| callback | [KeyRequestCallback](#keyrequestcallback12) | Yes | Callback used to request the media resources and media key. | 4531 4532 4533**Error codes** 4534 4535For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4536 4537| ID | Error Message | 4538| ------- | ------------------------------------------------------------ | 4539| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4540| 6600101 | Session service exception. | 4541 4542**Example** 4543 4544```ts 4545let keyRequestCallback: avSession.KeyRequestCallback = async(assetId: string, requestData: Uint8Array) => { 4546 console.info(`Succeeded in keyRequestCallback. assetId: ${assetId}, requestData: ${requestData}`); 4547} 4548aVCastController.on('keyRequest', keyRequestCallback); 4549``` 4550### off('keyRequest')<sup>12+</sup> 4551 4552off(type: 'keyRequest', callback?: KeyRequestCallback): void 4553 4554Unsubscribes from media key requests during the cast of online DRM resources. 4555 4556**Atomic service API**: This API can be used in atomic services since API version 12. 4557 4558**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4559 4560**Parameters** 4561 4562| Name | Type | Mandatory | Description | 4563| -------- | ------------------------------------------- | --------- | ------------------------------------------------------------ | 4564| type | string | Yes | Event type, which is **'keyRequest'** in this case. | 4565| callback | [KeyRequestCallback](#keyrequestcallback12) | 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. | 4566 4567**Error codes** 4568 4569For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4570 4571| ID | Error Message | 4572| ------- | ------------------------------------------------------------ | 4573| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 4574| 6600101 | Session service exception. | 4575 4576**Example** 4577 4578```ts 4579aVCastController.off('keyRequest'); 4580``` 4581## KeyRequestCallback<sup>12+</sup> 4582type KeyRequestCallback = (assetId: string, requestData: Uint8Array) => void 4583 4584Describes the callback invoked for the media key request event. 4585 4586**Atomic service API**: This API can be used in atomic services since API version 12. 4587 4588**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4589 4590**Parameters** 4591 4592| Name | Type | Mandatory | Description | 4593| ----------- | ---------- | --------- | -------------------------------------- | 4594| assetId | string | Yes | Media asset ID. | 4595| requestData | Uint8Array | Yes | Data carried in the media key request. | 4596 4597**Example** 4598<!--code_no_check--> 4599```ts 4600let keyRequestCallback: avSession.KeyRequestCallback = async(assetId: string, requestData: Uint8Array) => { 4601 console.info(`Succeeded in keyRequestCallback. assetId: ${assetId}, requestData: ${requestData}`); 4602} 4603``` 4604 4605## CastDisplayState<sup>12+</sup> 4606 4607Enumerates the states of the cast display. 4608 4609**Atomic service API**: This API can be used in atomic services since API version 12. 4610 4611**System capability**: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast 4612 4613| Name | Value | Description | 4614| --------- | ----- | ------------------------------------------------------------ | 4615| STATE_OFF | 1 | The device is disconnected, and the extended screen does not display any content. | 4616| STATE_ON | 2 | The device is connected, and the extended screen is available. | 4617 4618 4619## CastDisplayInfo<sup>12+</sup> 4620 4621Describes the information about the cast display in the case of extended screens. 4622 4623**Atomic service API**: This API can be used in atomic services since API version 12. 4624 4625**System capability**: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast 4626 4627| Name | Type | Read-Only | Optional | Description | 4628| ------ | --------------------------------------- | --------- | -------- | ------------------------------------------------------------ | 4629| id | number | No | No | ID of the cast display. The value must be an integer. | 4630| name | string | No | No | Name of the cast display. | 4631| state | [CastDisplayState](#castdisplaystate12) | No | No | State of the cast display. | 4632| width | number | No | No | Screen width of the cast display, in px. The value must be an integer. | 4633| height | number | No | No | Screen height of the cast display, in px. The value must be an integer. | 4634 4635## ConnectionState<sup>10+</sup> 4636 4637Enumerates the connection states. 4638 4639**Atomic service API**: This API can be used in atomic services since API version 12. 4640 4641**System capability**: SystemCapability.Multimedia.AVSession.Core 4642 4643| Name | Value | Description | 4644| ------------------ | ----- | --------------------------- | 4645| STATE_CONNECTING | 0 | The device is connecting. | 4646| STATE_CONNECTED | 1 | The device is connected. | 4647| STATE_DISCONNECTED | 6 | The device is disconnected. | 4648 4649## AVMetadata<sup>10+</sup> 4650 4651Describes the media metadata. 4652 4653**System capability**: SystemCapability.Multimedia.AVSession.Core 4654 4655| Name | Type | Mandatory | Description | 4656| --------------------------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 4657| assetId | string | Yes | Media asset ID. It is the unique ID of a song and defined by the application.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4658| title | string | No | Title.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4659| artist | string | No | Artist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4660| author | string | No | Author.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4661| avQueueName<sup>12+</sup> | string | No | Playlist name. | 4662| avQueueId<sup>11+</sup> | string | No | Unique ID of the playlist. | 4663| avQueueImage<sup>11+</sup> | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | string | No | Cover image of the playlist, which can be pixel data of an image or an image path (local path or Internet path).<br>When the data type configured by running **setAVMetadata** is **PixelMap**, the data obtained by calling **getAVMetadata** is a pixel map. When the configured data type is **string**, the data obtained is a URL. | 4664| album | string | No | Album name.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4665| writer | string | No | Writer.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4666| composer | string | No | composer. | 4667| duration | number | No | Media duration, in ms.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4668| mediaImage | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | string | No | Pixel map or image path (local path or network path) of the image.<br>When the data type configured by running **setAVMetadata** is **PixelMap**, the data obtained by calling **getAVMetadata** is a pixel map. When the configured data type is **string**, the data obtained is a URL.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4669| publishDate | Date | No | Release date. | 4670| subtitle | string | No | Subtitle.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4671| description | string | No | Media description.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4672| lyric | string | No | Lyrics. The application needs to combine the lyrics into a string. | 4673| previousAssetId | string | No | ID of the previous media asset.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4674| nextAssetId | string | No | ID of the next media asset.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4675| filter<sup>11+</sup> | number | No | Protocol supported by the media session. The default value is **TYPE_CAST_PLUS_STREAM**. For details, see [ProtocolType](#protocoltype11).<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4676| drmSchemes<sup>12+</sup> | Array\<string> | No | DRM scheme supported by the media session. The value is the UUID of the DRM scheme. | 4677| skipIntervals<sup>11+</sup> | [SkipIntervals](#skipintervals11) | No | Fast-forward or rewind interval supported by the media session. The default value is **SECONDS_15**, that is, 15 seconds. | 4678| displayTags<sup>11+</sup> | number | No | Display tags of the media asset. For details, see [DisplayTag](#displaytag11). | 4679 4680## AVMediaDescription<sup>10+</sup> 4681 4682Describes the attributes related to the media metadata in the playlist. 4683 4684**System capability**: SystemCapability.Multimedia.AVSession.Core 4685 4686| Name | Type | Mandatory | Description | 4687| ------------------------- | ------------------------- | --------- | ------------------------------------------------------------ | 4688| assetId | string | Yes | Media ID in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4689| title | string | No | Name of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4690| subtitle | string | No | Subname of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4691| description | string | No | Description of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4692| mediaImage | image.PixelMap \| string | No | Pixel map of the image of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4693| extras | {[key: string]: Object} | No | Additional fields of the media asset in the playlist. | 4694| mediaUri | string | No | URI of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4695| mediaType | string | No | Type of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4696| mediaSize | number | No | Size of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4697| albumTitle | string | No | Album name of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4698| albumCoverUri | string | No | URI of the album title of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4699| lyricContent | string | No | Lyric content of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4700| lyricUri | string | No | Lyric URI of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4701| artist | string | No | Author of the lyric of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4702| fdSrc | media.AVFileDescriptor | No | Handle to the local media file in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4703| dataSrc<sup>12+</sup> | media.AVDataSrcDescriptor | No | Descriptor of the data source in the playlist. | 4704| drmScheme<sup>12+</sup> | string | No | DRM scheme supported by the playlist. The value is the UUID of the DRM scheme. | 4705| duration | number | No | Playback duration of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4706| startPosition | number | No | Start position for playing the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4707| creditsPosition | number | No | Position for playing the closing credits of the media asset in the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4708| appName | string | No | Name of the application provided by the playlist.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4709| displayTags<sup>11+</sup> | number | No | Display tags of the media asset. For details, see [DisplayTag](#displaytag11).<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4710 4711## AVQueueItem<sup>10+</sup> 4712 4713Describes the attributes of an item in the playlist. 4714 4715**Atomic service API**: This API can be used in atomic services since API version 12. 4716 4717**System capability**: SystemCapability.Multimedia.AVSession.Core 4718 4719| Name | Type | Mandatory | Description | 4720| ----------- | ------------------------------------------- | --------- | ------------------------------------------- | 4721| itemId | number | Yes | ID of an item in the playlist. | 4722| description | [AVMediaDescription](#avmediadescription10) | No | Media metadata of the item in the playlist. | 4723 4724## AVPlaybackState<sup>10+</sup> 4725 4726Describes the information related to the media playback state. 4727 4728**System capability**: SystemCapability.Multimedia.AVSession.Core 4729 4730| Name | Type | Mandatory | Description | 4731| -------------------------- | --------------------------------------- | --------- | ------------------------------------------------------------ | 4732| state | [PlaybackState](#playbackstate10) | No | Playback state.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4733| speed | number | No | Playback speed.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4734| position | [PlaybackPosition](#playbackposition10) | No | Playback position.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4735| bufferedTime | number | No | Buffered time.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4736| loopMode | [LoopMode](#loopmode10) | No | Loop mode.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4737| isFavorite | boolean | No | Whether the media asset is favorited.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4738| activeItemId<sup>10+</sup> | number | No | ID of the item that is being played.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4739| volume<sup>10+</sup> | number | No | Media volume.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4740| maxVolume<sup>11+</sup> | number | No | Maximum volume.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4741| muted<sup>11+</sup> | boolean | No | Mute status. The value **true** means the muted state.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4742| duration<sup>11+</sup> | number | No | Duration of the media asset. | 4743| videoWidth<sup>11+</sup> | number | No | Video width of the media asset, in px.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4744| videoHeight<sup>11+</sup> | number | No | Video height of the media asset, in px.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4745| extras<sup>10+</sup> | {[key: string]: Object} | No | Custom media data.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 4746 4747## PlaybackPosition<sup>10+</sup> 4748 4749Describes the information related to the playback position. 4750 4751**Atomic service API**: This API can be used in atomic services since API version 12. 4752 4753**System capability**: SystemCapability.Multimedia.AVSession.Core 4754 4755| Name | Type | Mandatory | Description | 4756| ----------- | ------ | --------- | -------------------- | 4757| elapsedTime | number | Yes | Elapsed time, in ms. | 4758| updateTime | number | Yes | Updated time, in ms. | 4759 4760## CallMetadata<sup>11+</sup> 4761 4762Defines the attributes related to call metadata. 4763 4764**Atomic service API**: This API can be used in atomic services since API version 12. 4765 4766**System capability**: SystemCapability.Multimedia.AVSession.Core 4767 4768| Name | Type | Mandatory | Description | 4769| ----------- | ------------------------------------------------------------ | --------- | ------------------------------ | 4770| name | string | No | Name (alias) of the caller. | 4771| phoneNumber | string | No | Phone number of the caller. | 4772| avatar | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | No | Profile picture of the caller. | 4773 4774## AVCallState<sup>11+</sup> 4775 4776Defines the attributes related to the call state. 4777 4778**Atomic service API**: This API can be used in atomic services since API version 12. 4779 4780**System capability**: SystemCapability.Multimedia.AVSession.Core 4781 4782| Name | Type | Mandatory | Description | 4783| ----- | ------------------------- | --------- | ------------------------------------------------------------ | 4784| state | [CallState](#callstate11) | Yes | Call state. | 4785| muted | boolean | Yes | Whether the microphone is muted.<br>**true**: The microphone is muted.<br>**false**: The microphone is not muted. | 4786 4787## CallState<sup>11+</sup> 4788 4789Enumerates the call states. 4790 4791**Atomic service API**: This API can be used in atomic services since API version 12. 4792 4793**System capability**: SystemCapability.Multimedia.AVSession.Core 4794 4795| Name | Value | Description | 4796| ------------------------ | ----- | ----------------------------------- | 4797| CALL_STATE_IDLE | 0 | The phone is idle. | 4798| CALL_STATE_INCOMING | 1 | The phone is ringing. | 4799| CALL_STATE_ACTIVE | 2 | The call is connected. | 4800| CALL_STATE_DIALING | 3 | The caller is dialing. | 4801| CALL_STATE_WAITING | 4 | The call is waiting for connection. | 4802| CALL_STATE_HOLDING | 5 | The call is placed on hold. | 4803| CALL_STATE_DISCONNECTING | 6 | The call is disconnecting. | 4804 4805## DisplayTag<sup>11+</sup> 4806 4807Enumerates the display tags of the media asset, which is a special type identifier of the media audio source. 4808 4809**System capability**: SystemCapability.Multimedia.AVSession.Core 4810 4811| Name | Value | Description | 4812| --------------- | ----- | ----------- | 4813| TAG_AUDIO_VIVID | 1 | AUDIO VIVID | 4814 4815## AVCastCategory<sup>10+</sup> 4816 4817Enumerates the cast categories. 4818 4819**Atomic service API**: This API can be used in atomic services since API version 12. 4820 4821**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4822 4823| Name | Value | Description | 4824| --------------- | ----- | ------------------------------------------------------------ | 4825| CATEGORY_LOCAL | 0 | Local playback. The sound is played from the local device or a connected Bluetooth headset by default. | 4826| CATEGORY_REMOTE | 1 | Remote playback. The sound or images are played from a remote device. | 4827 4828## DeviceType<sup>10+</sup> 4829 4830Enumerates the output device types. 4831 4832**Atomic service API**: This API can be used in atomic services since API version 12. 4833 4834| Name | Value | Description | 4835| ------------------------- | ----- | ------------------------------------------------------------ | 4836| DEVICE_TYPE_LOCAL | 0 | Local device.<br> **System capability**: SystemCapability.Multimedia.AVSession.Core | 4837| DEVICE_TYPE_BLUETOOTH | 10 | Bluetooth device.<br> **System capability**: SystemCapability.Multimedia.AVSession.Core | 4838| DEVICE_TYPE_TV | 2 | TV.<br> **System capability**: SystemCapability.Multimedia.AVSession.AVCast | 4839| DEVICE_TYPE_SMART_SPEAKER | 3 | Speaker.<br> **System capability**: SystemCapability.Multimedia.AVSession.AVCast | 4840 4841## DeviceInfo<sup>10+</sup> 4842 4843Describes the information related to the output device. 4844 4845**Atomic service API**: This API can be used in atomic services since API version 12. 4846 4847| Name | Type | Mandatory | Description | 4848| -------------------------------------- | -------------- | --------- | ------------------------------------------------------------ | 4849| castCategory | AVCastCategory | Yes | Cast category.<br> **System capability**: SystemCapability.Multimedia.AVSession.Core | 4850| deviceId | string | Yes | ID of the output device.<br> **System capability**: SystemCapability.Multimedia.AVSession.Core | 4851| deviceName | string | Yes | Name of the output device.<br>**System capability**: SystemCapability.Multimedia.AVSession.Core | 4852| deviceType | DeviceType | Yes | Type of the output device.<br>**System capability**: SystemCapability.Multimedia.AVSession.Core | 4853| supportedProtocols<sup>11+</sup> | number | No | Protocol supported by the output device. The default value is **TYPE_LOCAL**. For details, see [ProtocolType](#protocoltype11).<br> **System capability**: SystemCapability.Multimedia.AVSession.AVCast | 4854| supportedDrmCapabilities<sup>12+</sup> | Array\<string> | No | DRM capability supported by the output device.<br> **System capability**: SystemCapability.Multimedia.AVSession.AVCast | 4855 4856## OutputDeviceInfo<sup>10+</sup> 4857 4858Describes the information related to the output device. 4859 4860**Atomic service API**: This API can be used in atomic services since API version 12. 4861 4862**System capability**: SystemCapability.Multimedia.AVSession.Core 4863 4864| Name | Type | Mandatory | Description | 4865| ------- | ------------------- | --------- | --------------- | 4866| devices | Array\<DeviceInfo\> | Yes | Output devices. | 4867 4868## LoopMode<sup>10+</sup> 4869 4870Enumerates the loop modes of media playback. 4871 4872**Atomic service API**: This API can be used in atomic services since API version 12. 4873 4874**System capability**: SystemCapability.Multimedia.AVSession.Core 4875 4876| Name | Value | Description | 4877| ------------------------------ | ----- | -------------------- | 4878| LOOP_MODE_SEQUENCE | 0 | Sequential playback. | 4879| LOOP_MODE_SINGLE | 1 | Single loop. | 4880| LOOP_MODE_LIST | 2 | Playlist loop. | 4881| LOOP_MODE_SHUFFLE | 3 | Shuffle. | 4882| LOOP_MODE_CUSTOM<sup>11+</sup> | 4 | Custom playback. | 4883 4884## PlaybackState<sup>10+</sup> 4885 4886Enumerates the media playback states. 4887 4888**Atomic service API**: This API can be used in atomic services since API version 12. 4889 4890**System capability**: SystemCapability.Multimedia.AVSession.Core 4891 4892| Name | Value | Description | 4893| -------------------------------------- | ----- | ------------------ | 4894| PLAYBACK_STATE_INITIAL | 0 | Initial. | 4895| PLAYBACK_STATE_PREPARE | 1 | Preparing. | 4896| PLAYBACK_STATE_PLAY | 2 | Playing. | 4897| PLAYBACK_STATE_PAUSE | 3 | Paused. | 4898| PLAYBACK_STATE_FAST_FORWARD | 4 | Fast-forward. | 4899| PLAYBACK_STATE_REWIND | 5 | Rewind. | 4900| PLAYBACK_STATE_STOP | 6 | Stop the playback. | 4901| PLAYBACK_STATE_COMPLETED | 7 | Playback complete. | 4902| PLAYBACK_STATE_RELEASED | 8 | Released. | 4903| PLAYBACK_STATE_ERROR | 9 | Error. | 4904| PLAYBACK_STATE_IDLE<sup>11+</sup> | 10 | Idle. | 4905| PLAYBACK_STATE_BUFFERING<sup>11+</sup> | 11 | Buffering. | 4906 4907## AVSessionController<sup>10+</sup> 4908 4909Through the AV session controller, you can query the session ID, send commands and events to a session, and obtain session metadata and playback state information. 4910 4911### Attributes 4912 4913**Atomic service API**: This API can be used in atomic services since API version 12. 4914 4915**System capability**: SystemCapability.Multimedia.AVSession.Core 4916 4917| Name | Type | Readable | Writable | Description | 4918| :-------- | :----- | :------- | :------- | :------------------------------------------------------- | 4919| sessionId | string | Yes | No | Unique session ID of the **AVSessionController** object. | 4920 4921 4922**Example** 4923 4924```ts 4925import { BusinessError } from '@kit.BasicServicesKit'; 4926 4927let AVSessionController: avSession.AVSessionController; 4928avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { 4929 AVSessionController = controller; 4930}).catch((err: BusinessError) => { 4931 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 4932}); 4933``` 4934 4935### getAVPlaybackState<sup>10+</sup> 4936 4937getAVPlaybackState(callback: AsyncCallback\<AVPlaybackState>): void 4938 4939Obtains the remote playback state. This API uses an asynchronous callback to return the result. 4940 4941**System capability**: SystemCapability.Multimedia.AVSession.Core 4942 4943**Parameters** 4944 4945| Name | Type | Mandatory | Description | 4946| -------- | ----------------------------------------------------- | --------- | -------------------------------------------------- | 4947| callback | AsyncCallback<[AVPlaybackState](#avplaybackstate10)\> | Yes | Callback used to return the remote playback state. | 4948 4949**Error codes** 4950 4951For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4952 4953| ID | Error Message | 4954| ------- | -------------------------------------- | 4955| 6600101 | Session service exception. | 4956| 6600102 | The session does not exist. | 4957| 6600103 | The session controller does not exist. | 4958 4959**Example** 4960 4961```ts 4962import { BusinessError } from '@kit.BasicServicesKit'; 4963 4964avsessionController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { 4965 if (err) { 4966 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 4967 } else { 4968 console.info('getAVPlaybackState : SUCCESS'); 4969 } 4970}); 4971``` 4972 4973### getAVPlaybackState<sup>10+</sup> 4974 4975getAVPlaybackState(): Promise\<AVPlaybackState> 4976 4977Obtains the remote playback state. This API uses a promise to return the result. 4978 4979**Atomic service API**: This API can be used in atomic services since API version 12. 4980 4981**System capability**: SystemCapability.Multimedia.AVSession.Core 4982 4983**Return value** 4984 4985| Type | Description | 4986| ----------------------------------------------- | ------------------------------------------------- | 4987| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise used to return the remote playback state. | 4988 4989**Error codes** 4990 4991For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 4992 4993| ID | Error Message | 4994| ------- | -------------------------------------- | 4995| 6600101 | Session service exception. | 4996| 6600102 | The session does not exist. | 4997| 6600103 | The session controller does not exist. | 4998 4999**Example** 5000 5001```ts 5002import { BusinessError } from '@kit.BasicServicesKit'; 5003 5004avsessionController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { 5005 console.info('getAVPlaybackState : SUCCESS'); 5006}).catch((err: BusinessError) => { 5007 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 5008}); 5009``` 5010 5011### getAVMetadata<sup>10+</sup> 5012 5013getAVMetadata(): Promise\<AVMetadata> 5014 5015Obtains the session metadata. This API uses a promise to return the result. 5016 5017**Atomic service API**: This API can be used in atomic services since API version 12. 5018 5019**System capability**: SystemCapability.Multimedia.AVSession.Core 5020 5021**Return value** 5022 5023| Type | Description | 5024| ------------------------------------- | --------------------------------------------- | 5025| Promise<[AVMetadata](#avmetadata10)\> | Promise used to return the metadata obtained. | 5026 5027**Error codes** 5028 5029For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5030 5031| ID | Error Message | 5032| ------- | -------------------------------------- | 5033| 6600101 | Session service exception. | 5034| 6600102 | The session does not exist. | 5035| 6600103 | The session controller does not exist. | 5036 5037**Example** 5038 5039```ts 5040import { BusinessError } from '@kit.BasicServicesKit'; 5041 5042avsessionController.getAVMetadata().then((metadata: avSession.AVMetadata) => { 5043 console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); 5044}).catch((err: BusinessError) => { 5045 console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 5046}); 5047``` 5048 5049### getAVMetadata<sup>10+</sup> 5050 5051getAVMetadata(callback: AsyncCallback\<AVMetadata>): void 5052 5053Obtains the session metadata. This API uses an asynchronous callback to return the result. 5054 5055**System capability**: SystemCapability.Multimedia.AVSession.Core 5056 5057**Parameters** 5058 5059| Name | Type | Mandatory | Description | 5060| -------- | ------------------------------------------- | --------- | ---------------------------------------------- | 5061| callback | AsyncCallback<[AVMetadata](#avmetadata10)\> | Yes | Callback used to return the metadata obtained. | 5062 5063**Error codes** 5064 5065For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5066 5067| ID | Error Message | 5068| ------- | -------------------------------------- | 5069| 6600101 | Session service exception. | 5070| 6600102 | The session does not exist. | 5071| 6600103 | The session controller does not exist. | 5072 5073**Example** 5074 5075```ts 5076import { BusinessError } from '@kit.BasicServicesKit'; 5077 5078avsessionController.getAVMetadata((err: BusinessError, metadata: avSession.AVMetadata) => { 5079 if (err) { 5080 console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 5081 } else { 5082 console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); 5083 } 5084}); 5085``` 5086 5087### getAVQueueTitle<sup>10+</sup> 5088 5089getAVQueueTitle(): Promise\<string> 5090 5091Obtains the name of the playlist. This API uses a promise to return the result. 5092 5093**Atomic service API**: This API can be used in atomic services since API version 12. 5094 5095**System capability**: SystemCapability.Multimedia.AVSession.Core 5096 5097**Return value** 5098 5099| Type | Description | 5100| ---------------- | ----------------------------------------- | 5101| Promise<string\> | Promise used to return the playlist name. | 5102 5103**Error codes** 5104 5105For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5106 5107| ID | Error Message | 5108| ------- | -------------------------------------- | 5109| 6600101 | Session service exception. | 5110| 6600102 | The session does not exist. | 5111| 6600103 | The session controller does not exist. | 5112 5113**Example** 5114 5115```ts 5116import { BusinessError } from '@kit.BasicServicesKit'; 5117 5118avsessionController.getAVQueueTitle().then((title: string) => { 5119 console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); 5120}).catch((err: BusinessError) => { 5121 console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 5122}); 5123``` 5124 5125### getAVQueueTitle<sup>10+</sup> 5126 5127getAVQueueTitle(callback: AsyncCallback\<string>): void 5128 5129Obtains the name of the playlist. This API uses an asynchronous callback to return the result. 5130 5131**System capability**: SystemCapability.Multimedia.AVSession.Core 5132 5133**Parameters** 5134 5135| Name | Type | Mandatory | Description | 5136| -------- | ---------------------- | --------- | ------------------------------------------ | 5137| callback | AsyncCallback<string\> | Yes | Callback used to return the playlist name. | 5138 5139**Error codes** 5140 5141For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5142 5143| ID | Error Message | 5144| ------- | -------------------------------------- | 5145| 6600101 | Session service exception. | 5146| 6600102 | The session does not exist. | 5147| 6600103 | The session controller does not exist. | 5148 5149**Example** 5150 5151```ts 5152import { BusinessError } from '@kit.BasicServicesKit'; 5153 5154avsessionController.getAVQueueTitle((err: BusinessError, title: string) => { 5155 if (err) { 5156 console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 5157 } else { 5158 console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); 5159 } 5160}); 5161``` 5162 5163### getAVQueueItems<sup>10+</sup> 5164 5165getAVQueueItems(): Promise\<Array\<AVQueueItem>> 5166 5167Obtains the information related to the items in the queue. This API uses a promise to return the result. 5168 5169**Atomic service API**: This API can be used in atomic services since API version 12. 5170 5171**System capability**: SystemCapability.Multimedia.AVSession.Core 5172 5173**Return value** 5174 5175| Type | Description | 5176| ----------------------------------------------- | ---------------------------------------------- | 5177| Promise<Array<[AVQueueItem](#avqueueitem10)\>\> | Promise used to return the items in the queue. | 5178 5179**Error codes** 5180 5181For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5182 5183| ID | Error Message | 5184| ------- | -------------------------------------- | 5185| 6600101 | Session service exception. | 5186| 6600102 | The session does not exist. | 5187| 6600103 | The session controller does not exist. | 5188 5189**Example** 5190 5191```ts 5192import { BusinessError } from '@kit.BasicServicesKit'; 5193 5194avsessionController.getAVQueueItems().then((items: avSession.AVQueueItem[]) => { 5195 console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); 5196}).catch((err: BusinessError) => { 5197 console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 5198}); 5199``` 5200 5201### getAVQueueItems<sup>10+</sup> 5202 5203getAVQueueItems(callback: AsyncCallback\<Array\<AVQueueItem>>): void 5204 5205Obtains the information related to the items in the playlist. This API uses an asynchronous callback to return the result. 5206 5207**System capability**: SystemCapability.Multimedia.AVSession.Core 5208 5209**Parameters** 5210 5211| Name | Type | Mandatory | Description | 5212| -------- | ----------------------------------------------------- | --------- | -------------------------------------------------- | 5213| callback | AsyncCallback<Array<[AVQueueItem](#avqueueitem10)\>\> | Yes | Callback used to return the items in the playlist. | 5214 5215**Error codes** 5216 5217For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5218 5219| ID | Error Message | 5220| ------- | -------------------------------------- | 5221| 6600101 | Session service exception. | 5222| 6600102 | The session does not exist. | 5223| 6600103 | The session controller does not exist. | 5224 5225**Example** 5226 5227```ts 5228import { BusinessError } from '@kit.BasicServicesKit'; 5229 5230avsessionController.getAVQueueItems((err: BusinessError, items: avSession.AVQueueItem[]) => { 5231 if (err) { 5232 console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 5233 } else { 5234 console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); 5235 } 5236}); 5237``` 5238 5239### skipToQueueItem<sup>10+</sup> 5240 5241skipToQueueItem(itemId: number): Promise\<void> 5242 5243Sends the ID of an item in the playlist to the session for processing. The session can play the song. This API uses a promise to return the result. 5244 5245**Atomic service API**: This API can be used in atomic services since API version 12. 5246 5247**System capability**: SystemCapability.Multimedia.AVSession.Core 5248 5249**Parameters** 5250 5251| Name | Type | Mandatory | Description | 5252| ------ | ------ | --------- | ------------------------------ | 5253| itemId | number | Yes | ID of an item in the playlist. | 5254 5255**Return value** 5256 5257| Type | Description | 5258| -------------- | ------------------------------------------------------------ | 5259| Promise\<void> | Promise used to return the result. If the item ID is sent, no value is returned; otherwise, an error object is returned. | 5260 5261**Error codes** 5262 5263For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5264 5265| ID | Error Message | 5266| ------- | ------------------------------------------------------------ | 5267| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 5268| 6600101 | Session service exception. | 5269| 6600102 | The session does not exist. | 5270| 6600103 | The session controller does not exist. | 5271 5272**Example** 5273 5274```ts 5275import { BusinessError } from '@kit.BasicServicesKit'; 5276 5277let queueItemId = 0; 5278avsessionController.skipToQueueItem(queueItemId).then(() => { 5279 console.info('SkipToQueueItem successfully'); 5280}).catch((err: BusinessError) => { 5281 console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); 5282}); 5283``` 5284 5285### skipToQueueItem<sup>10+</sup> 5286 5287skipToQueueItem(itemId: number, callback: AsyncCallback\<void>): void 5288 5289Sends the ID of an item in the playlist to the session for processing. The session can play the song. This API uses an asynchronous callback to return the result. 5290 5291**System capability**: SystemCapability.Multimedia.AVSession.Core 5292 5293**Parameters** 5294 5295| Name | Type | Mandatory | Description | 5296| -------- | -------------------- | --------- | ------------------------------------------------------------ | 5297| itemId | number | Yes | ID of an item in the playlist. | 5298| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object. | 5299 5300**Error codes** 5301 5302For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5303 5304| ID | Error Message | 5305| ------- | ------------------------------------------------------------ | 5306| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 5307| 6600101 | Session service exception. | 5308| 6600102 | The session does not exist. | 5309| 6600103 | The session controller does not exist. | 5310 5311**Example** 5312 5313```ts 5314import { BusinessError } from '@kit.BasicServicesKit'; 5315 5316let queueItemId = 0; 5317avsessionController.skipToQueueItem(queueItemId, (err: BusinessError) => { 5318 if (err) { 5319 console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); 5320 } else { 5321 console.info('SkipToQueueItem successfully'); 5322 } 5323}); 5324``` 5325 5326### getOutputDevice<sup>10+</sup> 5327 5328getOutputDevice(): Promise\<OutputDeviceInfo> 5329 5330Obtains the output device information. This API uses a promise to return the result. 5331 5332**Atomic service API**: This API can be used in atomic services since API version 12. 5333 5334**System capability**: SystemCapability.Multimedia.AVSession.Core 5335 5336**Return value** 5337 5338| Type | Description | 5339| ------------------------------------------------- | ------------------------------------------------ | 5340| Promise<[OutputDeviceInfo](#outputdeviceinfo10)\> | Promise used to return the information obtained. | 5341 5342**Error codes** 5343 5344For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5345 5346| ID | Error Message | 5347| ------ | -------------------------------------- | 5348| 600101 | Session service exception. | 5349| 600103 | The session controller does not exist. | 5350 5351**Example** 5352 5353```ts 5354import { BusinessError } from '@kit.BasicServicesKit'; 5355 5356avsessionController.getOutputDevice().then((deviceInfo: avSession.OutputDeviceInfo) => { 5357 console.info('GetOutputDevice : SUCCESS'); 5358}).catch((err: BusinessError) => { 5359 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 5360}); 5361``` 5362 5363### getOutputDevice<sup>10+</sup> 5364 5365getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void 5366 5367Obtains the output device information. This API uses an asynchronous callback to return the result. 5368 5369**System capability**: SystemCapability.Multimedia.AVSession.Core 5370 5371**Parameters** 5372 5373| Name | Type | Mandatory | Description | 5374| -------- | ------------------------------------------------------- | --------- | ------------------------------------------------- | 5375| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | Yes | Callback used to return the information obtained. | 5376 5377**Error codes** 5378 5379For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5380 5381| ID | Error Message | 5382| ------ | -------------------------------------- | 5383| 600101 | Session service exception. | 5384| 600103 | The session controller does not exist. | 5385 5386**Example** 5387 5388```ts 5389import { BusinessError } from '@kit.BasicServicesKit'; 5390 5391avsessionController.getOutputDevice((err: BusinessError, deviceInfo: avSession.OutputDeviceInfo) => { 5392 if (err) { 5393 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 5394 } else { 5395 console.info('GetOutputDevice : SUCCESS'); 5396 } 5397}); 5398``` 5399 5400### sendAVKeyEvent<sup>10+</sup> 5401 5402sendAVKeyEvent(event: KeyEvent): Promise\<void> 5403 5404Sends a key event to the session corresponding to this controller. This API uses a promise to return the result. 5405 5406**Atomic service API**: This API can be used in atomic services since API version 12. 5407 5408**System capability**: SystemCapability.Multimedia.AVSession.Core 5409 5410**Parameters** 5411 5412| Name | Type | Mandatory | Description | 5413| ----- | ------------------------------------------------- | --------- | ----------- | 5414| event | [KeyEvent](../apis-input-kit/js-apis-keyevent.md) | Yes | Key event. | 5415 5416**Error codes** 5417 5418For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5419 5420| ID | Error Message | 5421| ------ | ------------------------------------------------------------ | 5422| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 5423| 600101 | Session service exception. | 5424| 600102 | The session does not exist. | 5425| 600103 | The session controller does not exist. | 5426| 600105 | Invalid session command. | 5427| 600106 | The session is not activated. | 5428 5429**Return value** 5430 5431| Type | Description | 5432| -------------- | ------------------------------------------------------------ | 5433| Promise\<void> | Promise used to return the result. If the event is sent, no value is returned; otherwise, an error object is returned. | 5434 5435**Example** 5436 5437```ts 5438import { Key, KeyEvent } from '@kit.InputKit'; 5439import { BusinessError } from '@kit.BasicServicesKit'; 5440 5441let keyItem: Key = {code:0x49, pressedTime:2, deviceId:0}; 5442let event:KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; 5443 5444 5445avsessionController.sendAVKeyEvent(event).then(() => { 5446 console.info('SendAVKeyEvent Successfully'); 5447}).catch((err: BusinessError) => { 5448 console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 5449}); 5450``` 5451 5452### sendAVKeyEvent<sup>10+</sup> 5453 5454sendAVKeyEvent(event: KeyEvent, callback: AsyncCallback\<void>): void 5455 5456Sends a key event to the session corresponding to this controller. This API uses an asynchronous callback to return the result. 5457 5458**System capability**: SystemCapability.Multimedia.AVSession.Core 5459 5460**Parameters** 5461 5462| Name | Type | Mandatory | Description | 5463| -------- | ------------------------------------------------- | --------- | ------------------------------------------------------------ | 5464| event | [KeyEvent](../apis-input-kit/js-apis-keyevent.md) | Yes | Key event. | 5465| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the event is sent, **err** is **undefined**; otherwise, **err** is an error object. | 5466 5467**Error codes** 5468 5469For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5470 5471| ID | Error Message | 5472| ------ | ------------------------------------------------------------ | 5473| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 5474| 600101 | Session service exception. | 5475| 600102 | The session does not exist. | 5476| 600103 | The session controller does not exist. | 5477| 600105 | Invalid session command. | 5478| 600106 | The session is not activated. | 5479 5480**Example** 5481 5482```ts 5483import { Key, KeyEvent } from '@kit.InputKit'; 5484import { BusinessError } from '@kit.BasicServicesKit'; 5485 5486let keyItem: Key = {code:0x49, pressedTime:2, deviceId:0}; 5487let event:KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; 5488avsessionController.sendAVKeyEvent(event, (err: BusinessError) => { 5489 if (err) { 5490 console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 5491 } else { 5492 console.info('SendAVKeyEvent Successfully'); 5493 } 5494}); 5495``` 5496 5497### getLaunchAbility<sup>10+</sup> 5498 5499getLaunchAbility(): Promise\<WantAgent> 5500 5501Obtains the **WantAgent** object saved by the application in the session. This API uses a promise to return the result. 5502 5503**Atomic service API**: This API can be used in atomic services since API version 12. 5504 5505**System capability**: SystemCapability.Multimedia.AVSession.Core 5506 5507**Return value** 5508 5509| Type | Description | 5510| ------------------------------------------------------------ | ------------------------------------------------------------ | 5511| Promise<[WantAgent](../apis-ability-kit/js-apis-app-ability-wantAgent.md)\> | Promise used to return the object saved by calling [setLaunchAbility](#setlaunchability10). The object includes the application attribute, such as the bundle name, ability name, and device ID. | 5512 5513**Error codes** 5514 5515For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5516 5517| ID | Error Message | 5518| ------- | -------------------------------------- | 5519| 6600101 | Session service exception. | 5520| 6600102 | The session does not exist. | 5521| 6600103 | The session controller does not exist. | 5522 5523**Example** 5524 5525```ts 5526import { BusinessError } from '@kit.BasicServicesKit'; 5527 5528avsessionController.getLaunchAbility().then((agent: object) => { 5529 console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); 5530}).catch((err: BusinessError) => { 5531 console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 5532}); 5533``` 5534 5535### getLaunchAbility<sup>10+</sup> 5536 5537getLaunchAbility(callback: AsyncCallback\<WantAgent>): void 5538 5539Obtains the **WantAgent** object saved by the application in the session. This API uses an asynchronous callback to return the result. 5540 5541**System capability**: SystemCapability.Multimedia.AVSession.Core 5542 5543**Parameters** 5544 5545| Name | Type | Mandatory | Description | 5546| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 5547| callback | AsyncCallback<[WantAgent](../apis-ability-kit/js-apis-app-ability-wantAgent.md)\> | Yes | Callback used to return the object saved by calling [setLaunchAbility](#setlaunchability10). The object includes the application attribute, such as the bundle name, ability name, and device ID. | 5548 5549**Error codes** 5550 5551For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5552 5553| ID | Error Message | 5554| ------- | -------------------------------------- | 5555| 6600101 | Session service exception. | 5556| 6600102 | The session does not exist. | 5557| 6600103 | The session controller does not exist. | 5558 5559**Example** 5560 5561```ts 5562import { BusinessError } from '@kit.BasicServicesKit'; 5563 5564avsessionController.getLaunchAbility((err: BusinessError, agent: object) => { 5565 if (err) { 5566 console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 5567 } else { 5568 console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); 5569 } 5570}); 5571``` 5572 5573### getRealPlaybackPositionSync<sup>10+</sup> 5574 5575getRealPlaybackPositionSync(): number 5576 5577Obtains the playback position. 5578 5579**Atomic service API**: This API can be used in atomic services since API version 12. 5580 5581**System capability**: SystemCapability.Multimedia.AVSession.Core 5582 5583**Return value** 5584 5585| Type | Description | 5586| ------ | ----------------------------------- | 5587| number | Playback position, in milliseconds. | 5588 5589**Error codes** 5590 5591For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5592 5593| ID | Error Message | 5594| ------- | -------------------------------------- | 5595| 6600101 | Session service exception. | 5596| 6600103 | The session controller does not exist. | 5597 5598**Example** 5599 5600```ts 5601let time: number = avsessionController.getRealPlaybackPositionSync(); 5602``` 5603 5604### isActive<sup>10+</sup> 5605 5606isActive(): Promise\<boolean> 5607 5608Checks whether the session is activated. This API uses a promise to return the result. 5609 5610**Atomic service API**: This API can be used in atomic services since API version 12. 5611 5612**System capability**: SystemCapability.Multimedia.AVSession.Core 5613 5614**Return value** 5615 5616| Type | Description | 5617| ----------------- | ------------------------------------------------------------ | 5618| Promise<boolean\> | Promise used to return the activation state. If the session is activated, **true** is returned; otherwise, **false** is returned. | 5619 5620**Error codes** 5621 5622For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5623 5624| ID | Error Message | 5625| ------- | -------------------------------------- | 5626| 6600101 | Session service exception. | 5627| 6600102 | The session does not exist. | 5628| 6600103 | The session controller does not exist. | 5629 5630**Example** 5631 5632```ts 5633import { BusinessError } from '@kit.BasicServicesKit'; 5634 5635avsessionController.isActive().then((isActive: boolean) => { 5636 console.info(`IsActive : SUCCESS : isactive : ${isActive}`); 5637}).catch((err: BusinessError) => { 5638 console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); 5639}); 5640``` 5641 5642### isActive<sup>10+</sup> 5643 5644isActive(callback: AsyncCallback\<boolean>): void 5645 5646Checks whether the session is activated. This API uses an asynchronous callback to return the result. 5647 5648**System capability**: SystemCapability.Multimedia.AVSession.Core 5649 5650**Parameters** 5651 5652| Name | Type | Mandatory | Description | 5653| -------- | ----------------------- | --------- | ------------------------------------------------------------ | 5654| callback | AsyncCallback<boolean\> | Yes | Callback used to return the activation state. If the session is activated, **true** is returned; otherwise, **false** is returned. | 5655 5656**Error codes** 5657 5658For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5659 5660| ID | Error Message | 5661| ------- | -------------------------------------- | 5662| 6600101 | Session service exception. | 5663| 6600102 | The session does not exist. | 5664| 6600103 | The session controller does not exist. | 5665 5666**Example** 5667 5668```ts 5669import { BusinessError } from '@kit.BasicServicesKit'; 5670 5671avsessionController.isActive((err: BusinessError, isActive: boolean) => { 5672 if (err) { 5673 console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); 5674 } else { 5675 console.info(`IsActive : SUCCESS : isactive : ${isActive}`); 5676 } 5677}); 5678``` 5679 5680### destroy<sup>10+</sup> 5681 5682destroy(): Promise\<void> 5683 5684Destroys this controller. A controller can no longer be used after being destroyed. This API uses a promise to return the result. 5685 5686**Atomic service API**: This API can be used in atomic services since API version 12. 5687 5688**System capability**: SystemCapability.Multimedia.AVSession.Core 5689 5690**Return value** 5691 5692| Type | Description | 5693| -------------- | ------------------------------------------------------------ | 5694| Promise\<void> | Promise used to return the result. If the controller is destroyed, no value is returned; otherwise, an error object is returned. | 5695 5696**Error codes** 5697 5698For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5699 5700| ID | Error Message | 5701| ------- | -------------------------------------- | 5702| 6600101 | Session service exception. | 5703| 6600103 | The session controller does not exist. | 5704 5705**Example** 5706 5707```ts 5708import { BusinessError } from '@kit.BasicServicesKit'; 5709 5710avsessionController.destroy().then(() => { 5711 console.info('Destroy : SUCCESS '); 5712}).catch((err: BusinessError) => { 5713 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 5714}); 5715``` 5716 5717### destroy<sup>10+</sup> 5718 5719destroy(callback: AsyncCallback\<void>): void 5720 5721Destroys this controller. A controller can no longer be used after being destroyed. This API uses an asynchronous callback to return the result. 5722 5723**System capability**: SystemCapability.Multimedia.AVSession.Core 5724 5725**Parameters** 5726 5727| Name | Type | Mandatory | Description | 5728| -------- | -------------------- | --------- | ------------------------------------------------------------ | 5729| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the controller is destroyed, **err** is **undefined**; otherwise, **err** is an error object. | 5730 5731**Error codes** 5732 5733For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5734 5735| ID | Error Message | 5736| ------- | -------------------------------------- | 5737| 6600101 | Session service exception. | 5738| 6600103 | The session controller does not exist. | 5739 5740**Example** 5741 5742```ts 5743import { BusinessError } from '@kit.BasicServicesKit'; 5744 5745avsessionController.destroy((err: BusinessError) => { 5746 if (err) { 5747 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 5748 } else { 5749 console.info('Destroy : SUCCESS '); 5750 } 5751}); 5752``` 5753 5754### getValidCommands<sup>10+</sup> 5755 5756getValidCommands(): Promise\<Array\<AVControlCommandType>> 5757 5758Obtains valid commands supported by the session. This API uses a promise to return the result. 5759 5760**Atomic service API**: This API can be used in atomic services since API version 12. 5761 5762**System capability**: SystemCapability.Multimedia.AVSession.Core 5763 5764**Return value** 5765 5766| Type | Description | 5767| ------------------------------------------------------------ | ----------------------------------------------- | 5768| Promise<Array<[AVControlCommandType](#avcontrolcommandtype10)\>\> | Promise used to return a set of valid commands. | 5769 5770**Error codes** 5771 5772For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5773 5774| ID | Error Message | 5775| ------- | -------------------------------------- | 5776| 6600101 | Session service exception. | 5777| 6600102 | The session does not exist. | 5778| 6600103 | The session controller does not exist. | 5779 5780**Example** 5781 5782```ts 5783import { BusinessError } from '@kit.BasicServicesKit'; 5784 5785avsessionController.getValidCommands().then((validCommands: avSession.AVControlCommandType[]) => { 5786 console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); 5787}).catch((err: BusinessError) => { 5788 console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 5789}); 5790``` 5791 5792### getValidCommands<sup>10+</sup> 5793 5794getValidCommands(callback: AsyncCallback\<Array\<AVControlCommandType>>): void 5795 5796Obtains valid commands supported by the session. This API uses an asynchronous callback to return the result. 5797 5798**System capability**: SystemCapability.Multimedia.AVSession.Core 5799 5800**Parameters** 5801 5802| Name | Type | Mandatory | Description | 5803| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------ | 5804| callback | AsyncCallback\<Array\<[AVControlCommandType](#avcontrolcommandtype10)\>\> | Yes | Callback used to return a set of valid commands. | 5805 5806**Error codes** 5807 5808For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5809 5810| ID | Error Message | 5811| ------- | -------------------------------------- | 5812| 6600101 | Session service exception. | 5813| 6600102 | The session does not exist. | 5814| 6600103 | The session controller does not exist. | 5815 5816**Example** 5817 5818```ts 5819import { BusinessError } from '@kit.BasicServicesKit'; 5820 5821avsessionController.getValidCommands((err: BusinessError, validCommands: avSession.AVControlCommandType[]) => { 5822 if (err) { 5823 console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 5824 } else { 5825 console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); 5826 } 5827}); 5828``` 5829 5830### sendControlCommand<sup>10+</sup> 5831 5832sendControlCommand(command: AVControlCommand): Promise\<void> 5833 5834Sends a control command to the session through the controller. This API uses a promise to return the result. 5835 5836> **NOTE** 5837> 5838> Before using **sendControlCommand**, the controller must ensure that the corresponding listeners are registered for the media session. For details about how to register the listeners, see [on'play'](#onplay10), [on'pause'](#onpause10), and the like. 5839 5840**Atomic service API**: This API can be used in atomic services since API version 12. 5841 5842**System capability**: SystemCapability.Multimedia.AVSession.Core 5843 5844**Parameters** 5845 5846| Name | Type | Mandatory | Description | 5847| ------- | --------------------------------------- | --------- | ---------------- | 5848| command | [AVControlCommand](#avcontrolcommand10) | Yes | Command to send. | 5849 5850**Return value** 5851 5852| Type | Description | 5853| -------------- | ------------------------------------------------------------ | 5854| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned. | 5855 5856**Error codes** 5857 5858For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5859 5860| ID | Error Message | 5861| ------- | ------------------------------------------------------------ | 5862| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 5863| 6600101 | Session service exception. | 5864| 6600102 | The session does not exist. | 5865| 6600103 | The session controller does not exist. | 5866| 6600105 | Invalid session command. | 5867| 6600106 | The session is not activated. | 5868| 6600107 | Too many commands or events. | 5869 5870**Example** 5871 5872```ts 5873import { BusinessError } from '@kit.BasicServicesKit'; 5874 5875let avCommand: avSession.AVControlCommand = {command:'play'}; 5876avsessionController.sendControlCommand(avCommand).then(() => { 5877 console.info('SendControlCommand successfully'); 5878}).catch((err: BusinessError) => { 5879 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 5880}); 5881``` 5882 5883### sendControlCommand<sup>10+</sup> 5884 5885sendControlCommand(command: AVControlCommand, callback: AsyncCallback\<void>): void 5886 5887Sends a control command to the session through the controller. This API uses an asynchronous callback to return the result. 5888 5889> **NOTE** 5890> 5891> Before using **sendControlCommand**, the controller must ensure that the corresponding listeners are registered for the media session. For details about how to register the listeners, see [on'play'](#onplay10), [on'pause'](#onpause10), and the like. 5892 5893**System capability**: SystemCapability.Multimedia.AVSession.Core 5894 5895**Parameters** 5896 5897| Name | Type | Mandatory | Description | 5898| -------- | --------------------------------------- | --------- | ------------------------------------------------------------ | 5899| command | [AVControlCommand](#avcontrolcommand10) | Yes | Command to send. | 5900| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 5901 5902**Error codes** 5903 5904For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5905 5906| ID | Error Message | 5907| ------- | ------------------------------------------------------------ | 5908| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 5909| 6600101 | Session service exception. | 5910| 6600102 | The session does not exist. | 5911| 6600103 | The session controller does not exist. | 5912| 6600105 | Invalid session command. | 5913| 6600106 | The session is not activated. | 5914| 6600107 | Too many commands or events. | 5915 5916**Example** 5917 5918```ts 5919import { BusinessError } from '@kit.BasicServicesKit'; 5920 5921let avCommand: avSession.AVControlCommand = {command:'play'}; 5922avsessionController.sendControlCommand(avCommand, (err: BusinessError) => { 5923 if (err) { 5924 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 5925 } else { 5926 console.info('SendControlCommand successfully'); 5927 } 5928}); 5929``` 5930 5931### sendCommonCommand<sup>10+</sup> 5932 5933sendCommonCommand(command: string, args: {[key: string]: Object}): Promise\<void> 5934 5935Sends a custom control command to the session through the controller. This API uses a promise to return the result. 5936 5937**Atomic service API**: This API can be used in atomic services since API version 12. 5938 5939**System capability**: SystemCapability.Multimedia.AVSession.Core 5940 5941**Parameters** 5942 5943| Name | Type | Mandatory | Description | 5944| ------- | ----------------------- | --------- | ------------------------------------------------------------ | 5945| command | string | Yes | Name of the custom control command. | 5946| args | {[key: string]: Object} | Yes | Parameters in key-value pair format carried in the custom control command. | 5947 5948> **NOTE** 5949> 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). 5950 5951**Return value** 5952 5953| Type | Description | 5954| -------------- | ------------------------------------------------------------ | 5955| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned. | 5956 5957**Error codes** 5958 5959For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 5960 5961| ID | Error Message | 5962| ------- | ------------------------------------------------------------ | 5963| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 5964| 6600101 | Session service exception. | 5965| 6600102 | The session does not exist. | 5966| 6600103 | The session controller does not exist. | 5967| 6600105 | Invalid session command. | 5968| 6600106 | The session is not activated. | 5969| 6600107 | Too many commands or events. | 5970 5971**Example** 5972 5973```ts 5974import { BusinessError } from '@kit.BasicServicesKit'; 5975 5976let avSessionController: avSession.AVSessionController | undefined = undefined; 5977let currentAVSession: avSession.AVSession | undefined = undefined; 5978let tag = "createNewSession"; 5979let context: Context = getContext(this); 5980let sessionId: string = ""; 5981avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 5982 if (err) { 5983 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 5984 } else { 5985 currentAVSession = data; 5986 } 5987}); 5988if (currentAVSession !== undefined) { 5989 sessionId = (currentAVSession as avSession.AVSession).sessionId; 5990 avSession.createController(sessionId).then((controller: avSession.AVSessionController) => { 5991 avSessionController = controller; 5992 }).catch((err: BusinessError) => { 5993 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 5994 }); 5995} 5996 5997let commandName = "my_command"; 5998if (avSessionController !== undefined) { 5999 (avSessionController as avSession.AVSessionController).sendCommonCommand(commandName, {command : "This is my command"}).then(() => { 6000 console.info('SendCommonCommand successfully'); 6001 }).catch((err: BusinessError) => { 6002 console.error(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6003 }) 6004} 6005``` 6006 6007### sendCommonCommand<sup>10+</sup> 6008 6009sendCommonCommand(command: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void 6010 6011Sends a custom control command to the session through the controller. This API uses an asynchronous callback to return the result. 6012 6013**System capability**: SystemCapability.Multimedia.AVSession.Core 6014 6015**Parameters** 6016 6017| Name | Type | Mandatory | Description | 6018| -------- | ----------------------- | --------- | ------------------------------------------------------------ | 6019| command | string | Yes | Name of the custom control command. | 6020| args | {[key: string]: Object} | Yes | Parameters in key-value pair format carried in the custom control command. | 6021| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 6022 6023> **NOTE** 6024> 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). 6025 6026**Error codes** 6027 6028For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6029 6030| ID | Error Message | 6031| ------- | ------------------------------------------------------------ | 6032| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 6033| 6600101 | Session service exception. | 6034| 6600102 | The session does not exist. | 6035| 6600103 | The session controller does not exist. | 6036| 6600105 | Invalid session command. | 6037| 6600106 | The session is not activated. | 6038| 6600107 | Too many commands or events. | 6039 6040**Example** 6041 6042```ts 6043import { BusinessError } from '@kit.BasicServicesKit'; 6044let avSessionController: avSession.AVSessionController | undefined = undefined; 6045let currentAVSession: avSession.AVSession | undefined = undefined; 6046let tag = "createNewSession"; 6047let context: Context = getContext(this); 6048 6049avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6050 if (err) { 6051 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6052 } else { 6053 currentAVSession = data; 6054 } 6055}); 6056if (currentAVSession !== undefined) { 6057 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6058 avSessionController = controller; 6059 }).catch((err: BusinessError) => { 6060 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6061 }); 6062} 6063 6064let commandName = "my_command"; 6065if (avSessionController !== undefined) { 6066 (avSessionController as avSession.AVSessionController).sendCommonCommand(commandName, {command : "This is my command"}, (err: BusinessError) => { 6067 if (err) { 6068 console.error(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6069 } 6070 }) 6071} 6072``` 6073 6074### getExtras<sup>10+</sup> 6075 6076getExtras(): Promise\<{[key: string]: Object}> 6077 6078Obtains the custom media packet set by the provider. This API uses a promise to return the result. 6079 6080**Atomic service API**: This API can be used in atomic services since API version 12. 6081 6082**System capability**: SystemCapability.Multimedia.AVSession.Core 6083 6084**Return value** 6085 6086| Type | Description | 6087| --------------------------------- | ------------------------------------------------------------ | 6088| Promise<{[key: string]: Object}\> | Promise used to return the custom media packet. The content of the packet is the same as that set in **setExtras**. | 6089 6090**Error codes** 6091 6092For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6093 6094| ID | Error Message | 6095| ------- | ------------------------------------------------------------ | 6096| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 6097| 6600101 | Session service exception. | 6098| 6600102 | The session does not exist. | 6099| 6600103 | The session controller does not exist. | 6100| 6600105 | Invalid session command. | 6101| 6600107 | Too many commands or events. | 6102 6103**Example** 6104 6105```ts 6106import { BusinessError } from '@kit.BasicServicesKit'; 6107 6108let avSessionController: avSession.AVSessionController | undefined = undefined; 6109let currentAVSession: avSession.AVSession | undefined = undefined; 6110let tag = "createNewSession"; 6111let context: Context = getContext(this); 6112 6113avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6114 if (err) { 6115 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6116 } else { 6117 currentAVSession = data; 6118 } 6119}); 6120if (currentAVSession !== undefined) { 6121 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6122 avSessionController = controller; 6123 }).catch((err: BusinessError) => { 6124 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6125 }); 6126} 6127 6128if (avSessionController !== undefined) { 6129 (avSessionController as avSession.AVSessionController).getExtras().then((extras) => { 6130 console.info(`getExtras : SUCCESS : ${extras}`); 6131 }).catch((err: BusinessError) => { 6132 console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); 6133 }); 6134} 6135``` 6136 6137### getExtras<sup>10+</sup> 6138 6139getExtras(callback: AsyncCallback\<{[key: string]: Object}>): void 6140 6141Obtains the custom media packet set by the provider. This API uses an asynchronous callback to return the result. 6142 6143**System capability**: SystemCapability.Multimedia.AVSession.Core 6144 6145**Parameters** 6146 6147| Name | Type | Mandatory | Description | 6148| -------- | --------------------------------------- | --------- | ------------------------------------------------------------ | 6149| callback | AsyncCallback<{[key: string]: Object}\> | Yes | Callback used to return the custom media packet. The content of the packet is the same as that set in **setExtras**. | 6150 6151**Error codes** 6152 6153For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6154 6155| ID | Error Message | 6156| ------- | ------------------------------------------------------------ | 6157| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 6158| 6600101 | Session service exception. | 6159| 6600102 | The session does not exist. | 6160| 6600103 | The session controller does not exist. | 6161| 6600105 | Invalid session command. | 6162| 6600107 | Too many commands or events. | 6163 6164**Example** 6165 6166```ts 6167import { BusinessError } from '@kit.BasicServicesKit'; 6168 6169let avSessionController: avSession.AVSessionController | undefined = undefined; 6170let currentAVSession: avSession.AVSession | undefined = undefined; 6171let tag = "createNewSession"; 6172let context: Context = getContext(this); 6173 6174avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6175 if (err) { 6176 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6177 } else { 6178 currentAVSession = data; 6179 } 6180}); 6181if (currentAVSession !== undefined) { 6182 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6183 avSessionController = controller; 6184 }).catch((err: BusinessError) => { 6185 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6186 }); 6187} 6188 6189if (avSessionController !== undefined) { 6190 (avSessionController as avSession.AVSessionController).getExtras((err, extras) => { 6191 if (err) { 6192 console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); 6193 } else { 6194 console.info(`getExtras : SUCCESS : ${extras}`); 6195 } 6196 }); 6197} 6198``` 6199 6200### on('metadataChange')<sup>10+</sup> 6201 6202on(type: 'metadataChange', filter: Array\<keyof AVMetadata> | 'all', callback: (data: AVMetadata) => void) 6203 6204Subscribes to metadata change events. 6205 6206**Atomic service API**: This API can be used in atomic services since API version 12. 6207 6208**System capability**: SystemCapability.Multimedia.AVSession.Core 6209 6210**Parameters** 6211 6212| Name | Type | Mandatory | Description | 6213| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6214| type | string | Yes | Event type. The event **'metadataChange'** is triggered when the session metadata changes. | 6215| filter | Array\<keyof [AVMetadata](#avmetadata10)\> | 'all' | Yes | The value **'all'** indicates that any metadata field change will trigger the event, and **Array<keyof [AVMetadata](#avmetadata10)\>** indicates that only changes to the listed metadata field will trigger the event. | 6216| callback | (data: [AVMetadata](#avmetadata10)) => void | Yes | Callback used for subscription. The **data** parameter in the callback indicates the changed metadata. | 6217 6218**Error codes** 6219 6220For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6221 6222| ID | Error Message | 6223| ------- | ------------------------------------------------------------ | 6224| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6225| 6600101 | Session service exception. | 6226| 6600103 | The session controller does not exist. | 6227 6228**Example** 6229 6230```ts 6231avsessionController.on('metadataChange', 'all', (metadata: avSession.AVMetadata) => { 6232 console.info(`on metadataChange assetId : ${metadata.assetId}`); 6233}); 6234 6235avsessionController.on('metadataChange', ['assetId', 'title', 'description'], (metadata: avSession.AVMetadata) => { 6236 console.info(`on metadataChange assetId : ${metadata.assetId}`); 6237}); 6238 6239``` 6240 6241### off('metadataChange')<sup>10+</sup> 6242 6243off(type: 'metadataChange', callback?: (data: AVMetadata) => void) 6244 6245Unsubscribes from metadata change events. This API is called by the controller. 6246 6247**Atomic service API**: This API can be used in atomic services since API version 12. 6248 6249**System capability**: SystemCapability.Multimedia.AVSession.Core 6250 6251**Parameters** 6252 6253| Name | Type | Mandatory | Description | 6254| -------- | ------------------------------------------- | --------- | ------------------------------------------------------------ | 6255| type | string | Yes | Event type, which is **'metadataChange'** in this case. | 6256| callback | (data: [AVMetadata](#avmetadata10)) => void | No | Callback used for subscription. The **data** parameter in the callback indicates the changed metadata.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6257 6258**Error codes** 6259 6260For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6261 6262| ID | Error Message | 6263| ------- | ------------------------------------------------------------ | 6264| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6265| 6600101 | Session service exception. | 6266| 6600103 | The session controller does not exist. | 6267 6268**Example** 6269 6270```ts 6271avsessionController.off('metadataChange'); 6272``` 6273 6274### on('playbackStateChange')<sup>10+</sup> 6275 6276on(type: 'playbackStateChange', filter: Array\<keyof AVPlaybackState> | 'all', callback: (state: AVPlaybackState) => void) 6277 6278Subscribes to playback state change events. 6279 6280**Atomic service API**: This API can be used in atomic services since API version 12. 6281 6282**System capability**: SystemCapability.Multimedia.AVSession.Core 6283 6284**Parameters** 6285 6286| Name | Type | Mandatory | Description | 6287| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6288| type | string | Yes | Event type. The event **'playbackStateChange'** is triggered when the playback state changes. | 6289| filter | Array\<keyof [AVPlaybackState](#avplaybackstate10)\> | 'all' | Yes | The value **'all'** indicates that any playback state field change will trigger the event, and **Array<keyof [AVPlaybackState](#avplaybackstate10)\>** indicates that only changes to the listed playback state field will trigger the event. | 6290| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | Yes | Callback used for subscription. The **state** parameter in the callback indicates the changed playback state. | 6291 6292**Error codes** 6293 6294For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6295 6296| ID | Error Message | 6297| ------- | ------------------------------------------------------------ | 6298| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6299| 6600101 | Session service exception. | 6300| 6600103 | The session controller does not exist. | 6301 6302**Example** 6303 6304```ts 6305avsessionController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { 6306 console.info(`on playbackStateChange state : ${playbackState.state}`); 6307}); 6308 6309avsessionController.on('playbackStateChange', ['state', 'speed', 'loopMode'], (playbackState: avSession.AVPlaybackState) => { 6310 console.info(`on playbackStateChange state : ${playbackState.state}`); 6311}); 6312``` 6313 6314### off('playbackStateChange')<sup>10+</sup> 6315 6316off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void) 6317 6318Unsubscribes from playback state change events. This API is called by the controller. 6319 6320**Atomic service API**: This API can be used in atomic services since API version 12. 6321 6322**System capability**: SystemCapability.Multimedia.AVSession.Core 6323 6324**Parameters** 6325 6326| Name | Type | Mandatory | Description | 6327| -------- | ------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6328| type | string | Yes | Event type, which is **'playbackStateChange'** in this case. | 6329| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | No | Callback used for unsubscription. The **state** parameter in the callback indicates the changed playback state.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6330 6331**Error codes** 6332 6333For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6334 6335| ID | Error Message | 6336| ------- | ------------------------------------------------------------ | 6337| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6338| 6600101 | Session service exception. | 6339| 6600103 | The session controller does not exist. | 6340 6341**Example** 6342 6343```ts 6344avsessionController.off('playbackStateChange'); 6345``` 6346 6347### on('callMetadataChange')<sup>11+</sup> 6348 6349on(type: 'callMetadataChange', filter: Array\<keyof CallMetadata> | 'all', callback: Callback\<CallMetadata>): void; 6350 6351Subscribes to call metadata change events. 6352 6353**Atomic service API**: This API can be used in atomic services since API version 12. 6354 6355**System capability**: SystemCapability.Multimedia.AVSession.Core 6356 6357**Parameters** 6358 6359| Name | Type | Mandatory | Description | 6360| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6361| type | string | Yes | Event type. The event **'callMetadataChange'** is triggered when the call metadata changes. | 6362| filter | Array\<keyof [CallMetadata](#callmetadata11)\> | 'all' | Yes | The value **'all'** indicates that any call metadata field change will trigger the event, and **Array<keyof [CallMetadata](#callmetadata11)\>** indicates that only changes to the listed metadata field will trigger the event. | 6363| callback | Callback<[CallMetadata](#callmetadata11)\>\> | Yes | Callback used for subscription. The **callmetadata** parameter in the callback indicates the changed call metadata. | 6364 6365**Error codes** 6366 6367For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6368 6369| ID | Error Message | 6370| ------- | ------------------------------------------------------------ | 6371| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6372| 6600101 | Session service exception. | 6373| 6600103 | The session controller does not exist. | 6374 6375**Example** 6376 6377```ts 6378avsessionController.on('callMetadataChange', 'all', (callmetadata: avSession.CallMetadata) => { 6379 console.info(`on callMetadataChange state : ${callmetadata.name}`); 6380}); 6381 6382avsessionController.on('callMetadataChange', ['name'], (callmetadata: avSession.CallMetadata) => { 6383 console.info(`on callMetadataChange state : ${callmetadata.name}`); 6384}); 6385``` 6386 6387### off('callMetadataChange')<sup>11+</sup> 6388 6389off(type: 'callMetadataChange', callback?: Callback\<CallMetadata>): void; 6390 6391Unsubscribes from call metadata change events. 6392 6393**Atomic service API**: This API can be used in atomic services since API version 12. 6394 6395**System capability**: SystemCapability.Multimedia.AVSession.Core 6396 6397**Parameters** 6398 6399| Name | Type | Mandatory | Description | 6400| -------- | ------------------------------------------ | --------- | ------------------------------------------------------------ | 6401| type | string | Yes | Event type, which is **'callMetadataChange'** in this case. | 6402| callback | Callback<[CallMetadata](#callmetadata11)\> | No | Callback used for unsubscription. The **calldata** parameter in the callback indicates the changed call metadata.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6403 6404**Error codes** 6405 6406For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6407 6408| ID | Error Message | 6409| ------- | ------------------------------------------------------------ | 6410| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6411| 6600101 | Session service exception. | 6412| 6600103 | The session controller does not exist. | 6413 6414**Example** 6415 6416```ts 6417avsessionController.off('callMetadataChange'); 6418``` 6419 6420### on('callStateChange')<sup>11+</sup> 6421 6422on(type: 'callStateChange', filter: Array\<keyof AVCallState> | 'all', callback: Callback\<AVCallState>): void; 6423 6424Subscribes to call state change events. 6425 6426**Atomic service API**: This API can be used in atomic services since API version 12. 6427 6428**System capability**: SystemCapability.Multimedia.AVSession.Core 6429 6430**Parameters** 6431 6432| Name | Type | Mandatory | Description | 6433| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6434| type | string | Yes | Event type. The event **'callStateChange'** is triggered when the call state changes. | 6435| filter | Array<keyof [AVCallState](#avcallstate11)\> | 'all' | Yes | The value **'all'** indicates that any call state field change will trigger the event, and **Array<keyof [AVCallState](#avcallstate11)\>** indicates that only changes to the listed call state field will trigger the event. | 6436| callback | Callback<[AVCallState](#avcallstate11)\> | Yes | Callback used for subscription. The **callstate** parameter in the callback indicates the changed call state. | 6437 6438**Error codes** 6439 6440For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6441 6442| ID | Error Message | 6443| ------- | ------------------------------------------------------------ | 6444| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6445| 6600101 | Session service exception. | 6446| 6600103 | The session controller does not exist. | 6447 6448**Example** 6449 6450```ts 6451avsessionController.on('callStateChange', 'all', (callstate: avSession.AVCallState) => { 6452 console.info(`on callStateChange state : ${callstate.state}`); 6453}); 6454 6455avsessionController.on('callStateChange', ['state'], (callstate: avSession.AVCallState) => { 6456 console.info(`on callStateChange state : ${callstate.state}`); 6457}); 6458``` 6459 6460### off('callStateChange')<sup>11+</sup> 6461 6462off(type: 'callStateChange', callback?: Callback\<AVCallState>): void; 6463 6464Unsubscribes from call state change events. 6465 6466**Atomic service API**: This API can be used in atomic services since API version 12. 6467 6468**System capability**: SystemCapability.Multimedia.AVSession.Core 6469 6470**Parameters** 6471 6472| Name | Type | Mandatory | Description | 6473| -------- | ---------------------------------------- | --------- | ------------------------------------------------------------ | 6474| type | string | Yes | Event type, which is **'callStateChange'** in this case. | 6475| callback | Callback<[AVCallState](#avcallstate11)\> | No | Callback used for unsubscription. The **callstate** parameter in the callback indicates the changed call metadata.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6476 6477**Error codes** 6478 6479For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6480 6481| ID | Error Message | 6482| ------- | ------------------------------------------------------------ | 6483| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6484| 6600101 | Session service exception. | 6485| 6600103 | The session controller does not exist. | 6486 6487**Example** 6488 6489```ts 6490avsessionController.off('callMetadataChange'); 6491``` 6492 6493### on('sessionDestroy')<sup>10+</sup> 6494 6495on(type: 'sessionDestroy', callback: () => void) 6496 6497Subscribes to session destruction events. 6498 6499**Atomic service API**: This API can be used in atomic services since API version 12. 6500 6501**System capability**: SystemCapability.Multimedia.AVSession.Core 6502 6503**Parameters** 6504 6505| Name | Type | Mandatory | Description | 6506| -------- | ---------- | --------- | ------------------------------------------------------------ | 6507| type | string | Yes | Event type. The event **'sessionDestroy'** is triggered when a session is destroyed. | 6508| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 6509 6510**Error codes** 6511 6512For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6513 6514| ID | Error Message | 6515| ------- | ------------------------------------------------------------ | 6516| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6517| 6600101 | Session service exception. | 6518| 6600103 | The session controller does not exist. | 6519 6520**Example** 6521 6522```ts 6523avsessionController.on('sessionDestroy', () => { 6524 console.info('on sessionDestroy : SUCCESS '); 6525}); 6526``` 6527 6528### off('sessionDestroy')<sup>10+</sup> 6529 6530off(type: 'sessionDestroy', callback?: () => void) 6531 6532Unsubscribes from session destruction events. This API is called by the controller. 6533 6534**Atomic service API**: This API can be used in atomic services since API version 12. 6535 6536**System capability**: SystemCapability.Multimedia.AVSession.Core 6537 6538**Parameters** 6539 6540| Name | Type | Mandatory | Description | 6541| -------- | ---------- | --------- | ------------------------------------------------------------ | 6542| type | string | Yes | Event type, which is **'sessionDestroy'** in this case. | 6543| 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. | 6544 6545**Error codes** 6546 6547For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6548 6549| ID | Error Message | 6550| ------- | ------------------------------------------------------------ | 6551| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6552| 6600101 | Session service exception. | 6553| 6600103 | The session controller does not exist. | 6554 6555**Example** 6556 6557```ts 6558avsessionController.off('sessionDestroy'); 6559``` 6560 6561### on('activeStateChange')<sup>10+</sup> 6562 6563on(type: 'activeStateChange', callback: (isActive: boolean) => void) 6564 6565Subscribes to session activation state change events. 6566 6567**Atomic service API**: This API can be used in atomic services since API version 12. 6568 6569**System capability**: SystemCapability.Multimedia.AVSession.Core 6570 6571**Parameters** 6572 6573| Name | Type | Mandatory | Description | 6574| -------- | --------------------------- | --------- | ------------------------------------------------------------ | 6575| type | string | Yes | Event type. The event **'activeStateChange'** is triggered when the activation state of the session changes. | 6576| callback | (isActive: boolean) => void | Yes | Callback used for subscription. The **isActive** parameter in the callback specifies whether the session is activated. The value **true** means that the service is activated, and **false** means the opposite. | 6577 6578**Error codes** 6579 6580For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6581 6582| ID | Error Message | 6583| ------- | ------------------------------------------------------------ | 6584| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6585| 6600101 | Session service exception. | 6586| 6600103 | The session controller does not exist. | 6587 6588**Example** 6589 6590```ts 6591avsessionController.on('activeStateChange', (isActive: boolean) => { 6592 console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`); 6593}); 6594``` 6595 6596### off('activeStateChange')<sup>10+</sup> 6597 6598off(type: 'activeStateChange', callback?: (isActive: boolean) => void) 6599 6600Unsubscribes from session activation state change events. This API is called by the controller. 6601 6602**Atomic service API**: This API can be used in atomic services since API version 12. 6603 6604**System capability**: SystemCapability.Multimedia.AVSession.Core 6605 6606**Parameters** 6607 6608| Name | Type | Mandatory | Description | 6609| -------- | --------------------------- | --------- | ------------------------------------------------------------ | 6610| type | string | Yes | Event type, which is **'activeStateChange'** in this case. | 6611| callback | (isActive: boolean) => void | No | Callback used for unsubscription. The **isActive** parameter in the callback specifies whether the session is activated. The value **true** means that the session is activated, and **false** means the opposite.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6612 6613**Error codes** 6614 6615For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6616 6617| ID | Error Message | 6618| ------- | ------------------------------------------------------------ | 6619| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6620| 6600101 | Session service exception. | 6621| 6600103 | The session controller does not exist. | 6622 6623**Example** 6624 6625```ts 6626avsessionController.off('activeStateChange'); 6627``` 6628 6629### on('validCommandChange')<sup>10+</sup> 6630 6631on(type: 'validCommandChange', callback: (commands: Array\<AVControlCommandType>) => void) 6632 6633Subscribes to valid command change events. 6634 6635**Atomic service API**: This API can be used in atomic services since API version 12. 6636 6637**System capability**: SystemCapability.Multimedia.AVSession.Core 6638 6639**Parameters** 6640 6641| Name | Type | Mandatory | Description | 6642| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6643| type | string | Yes | Event type. The event **'validCommandChange'** is triggered when the valid commands supported by the session changes. | 6644| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | Yes | Callback used for subscription. The **commands** parameter in the callback is a set of valid commands. | 6645 6646**Error codes** 6647 6648For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6649 6650| ID | Error Message | 6651| ------- | ------------------------------------------------------------ | 6652| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6653| 6600101 | Session service exception. | 6654| 6600103 | The session controller does not exist. | 6655 6656**Example** 6657 6658```ts 6659avsessionController.on('validCommandChange', (validCommands: avSession.AVControlCommandType[]) => { 6660 console.info(`validCommandChange : SUCCESS : size : ${validCommands.length}`); 6661 console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); 6662}); 6663``` 6664 6665### off('validCommandChange')<sup>10+</sup> 6666 6667off(type: 'validCommandChange', callback?: (commands: Array\<AVControlCommandType>) => void) 6668 6669Unsubscribes from valid command change events. This API is called by the controller. 6670 6671**Atomic service API**: This API can be used in atomic services since API version 12. 6672 6673**System capability**: SystemCapability.Multimedia.AVSession.Core 6674 6675**Parameters** 6676 6677| Name | Type | Mandatory | Description | 6678| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6679| type | string | Yes | Event type, which is **'validCommandChange'** in this case. | 6680| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | No | Callback used for unsubscription. The **commands** parameter in the callback is a set of valid commands.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6681 6682**Error codes** 6683 6684For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6685 6686| ID | Error Message | 6687| ------- | ------------------------------------------------------------ | 6688| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6689| 6600101 | Session service exception. | 6690| 6600103 | The session controller does not exist. | 6691 6692**Example** 6693 6694```ts 6695avsessionController.off('validCommandChange'); 6696``` 6697 6698### on('outputDeviceChange')<sup>10+</sup> 6699 6700on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void 6701 6702Subscribes to output device change events. 6703 6704**Atomic service API**: This API can be used in atomic services since API version 12. 6705 6706**System capability**: SystemCapability.Multimedia.AVSession.Core 6707 6708**Parameters** 6709 6710| Name | Type | Mandatory | Description | 6711| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6712| type | string | Yes | Event type. The event **'outputDeviceChange'** is triggered when the output device changes. | 6713| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | Yes | Callback used for subscription. The **device** parameter in the callback indicates the output device information. | 6714 6715**Error codes** 6716 6717For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6718 6719| ID | Error Message | 6720| ------- | ------------------------------------------------------------ | 6721| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6722| 6600101 | Session service exception. | 6723| 6600103 | The session controller does not exist. | 6724 6725**Example** 6726 6727```ts 6728avsessionController.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { 6729 console.info(`on outputDeviceChange state: ${state}, device : ${device}`); 6730}); 6731``` 6732 6733### off('outputDeviceChange')<sup>10+</sup> 6734 6735off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void 6736 6737Unsubscribes from output device change events. This API is called by the controller. 6738 6739**Atomic service API**: This API can be used in atomic services since API version 12. 6740 6741**System capability**: SystemCapability.Multimedia.AVSession.Core 6742 6743**Parameters** 6744 6745| Name | Type | Mandatory | Description | 6746| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6747| type | string | Yes | Event type, which is **'outputDeviceChange'** in this case. | 6748| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | No | Callback used for unsubscription. The **device** parameter in the callback indicates 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. | 6749 6750**Error codes** 6751 6752For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6753 6754| ID | Error Message | 6755| ------- | ------------------------------------------------------------ | 6756| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6757| 6600101 | Session service exception. | 6758| 6600103 | The session controller does not exist. | 6759 6760**Example** 6761 6762```ts 6763avsessionController.off('outputDeviceChange'); 6764``` 6765 6766### on('sessionEvent')<sup>10+</sup> 6767 6768on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key:string]: Object}) => void): void 6769 6770Subscribes to session event change events. This API is called by the controller. 6771 6772**Atomic service API**: This API can be used in atomic services since API version 12. 6773 6774**System capability**: SystemCapability.Multimedia.AVSession.Core 6775 6776**Parameters** 6777 6778| Name | Type | Mandatory | Description | 6779| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6780| type | string | Yes | Event type. The event **'sessionEvent'** is triggered when the session event changes. | 6781| callback | (sessionEvent: string, args: {[key:string]: object}) => void | Yes | Callback used for subscription. **sessionEvent** in the callback indicates the name of the session event that changes, and **args** indicates the parameters carried in the event. | 6782 6783**Error codes** 6784 6785For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6786 6787| ID | Error Message | 6788| ------- | ------------------------------------------------------------ | 6789| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6790| 6600101 | Session service exception. | 6791| 6600103 | The session controller does not exist. | 6792 6793**Example** 6794 6795```ts 6796import { BusinessError } from '@kit.BasicServicesKit'; 6797 6798let avSessionController: avSession.AVSessionController | undefined = undefined; 6799let currentAVSession: avSession.AVSession | undefined = undefined; 6800let tag = "createNewSession"; 6801let context: Context = getContext(this); 6802 6803avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6804 if (err) { 6805 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6806 } else { 6807 currentAVSession = data; 6808 } 6809}); 6810if (currentAVSession !== undefined) { 6811 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6812 avSessionController = controller; 6813 }).catch((err: BusinessError) => { 6814 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6815 }); 6816} 6817 6818if (avSessionController !== undefined) { 6819 (avSessionController as avSession.AVSessionController).on('sessionEvent', (sessionEvent, args) => { 6820 console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`); 6821 }); 6822} 6823``` 6824 6825### off('sessionEvent')<sup>10+</sup> 6826 6827off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key:string]: Object}) => void): void 6828 6829Unsubscribes from session event change events. This API is called by the controller. 6830 6831**Atomic service API**: This API can be used in atomic services since API version 12. 6832 6833**System capability**: SystemCapability.Multimedia.AVSession.Core 6834 6835**Parameters** 6836 6837| Name | Type | Mandatory | Description | 6838| -------- | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6839| type | string | Yes | Event type, which is **'sessionEvent'** in this case. | 6840| callback | (sessionEvent: string, args: {[key:string]: Object}) => void | No | Callback used for unsubscription. **sessionEvent** in the callback indicates the name of the session event that changes, and **args** indicates the parameters carried in the event.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6841 6842**Error codes** 6843 6844For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6845 6846| ID | Error Message | 6847| ------- | ------------------------------------------------------------ | 6848| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6849| 6600101 | Session service exception. | 6850| 6600103 | The session controller does not exist. | 6851 6852**Example** 6853 6854```ts 6855avsessionController.off('sessionEvent'); 6856``` 6857 6858### on('queueItemsChange')<sup>10+</sup> 6859 6860on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void 6861 6862Subscribes to playlist item change events. This API is called by the controller. 6863 6864**Atomic service API**: This API can be used in atomic services since API version 12. 6865 6866**System capability**: SystemCapability.Multimedia.AVSession.Core 6867 6868**Parameters** 6869 6870| Name | Type | Mandatory | Description | 6871| -------- | ------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6872| type | string | Yes | Event type. The event **'queueItemsChange'** is triggered when one or more items in the playlist changes. | 6873| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | Yes | Callback used for subscription. The **items** parameter in the callback indicates the changed items in the playlist. | 6874 6875**Error codes** 6876 6877For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6878 6879| ID | Error Message | 6880| ------- | ------------------------------------------------------------ | 6881| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6882| 6600101 | Session service exception. | 6883| 6600103 | The session controller does not exist. | 6884 6885**Example** 6886 6887```ts 6888avsessionController.on('queueItemsChange', (items: avSession.AVQueueItem[]) => { 6889 console.info(`OnQueueItemsChange, items length is ${items.length}`); 6890}); 6891``` 6892 6893### off('queueItemsChange')<sup>10+</sup> 6894 6895off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void 6896 6897Unsubscribes from playback item change events. This API is called by the controller. 6898 6899**Atomic service API**: This API can be used in atomic services since API version 12. 6900 6901**System capability**: SystemCapability.Multimedia.AVSession.Core 6902 6903**Parameters** 6904 6905| Name | Type | Mandatory | Description | 6906| -------- | ------------------------------------------------------ | --------- | ------------------------------------------------------------ | 6907| type | string | Yes | Event type, which is **'queueItemsChange'** in this case. | 6908| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | No | Callback used for unsubscription. The **items** parameter in the callback indicates the changed items in the playlist.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6909 6910**Error codes** 6911 6912For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6913 6914| ID | Error Message | 6915| ------- | ------------------------------------------------------------ | 6916| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6917| 6600101 | Session service exception. | 6918| 6600103 | The session controller does not exist. | 6919 6920**Example** 6921 6922```ts 6923avsessionController.off('queueItemsChange'); 6924``` 6925 6926### on('queueTitleChange')<sup>10+</sup> 6927 6928on(type: 'queueTitleChange', callback: (title: string) => void): void 6929 6930Subscribes to playlist name change events. This API is called by the controller. 6931 6932**Atomic service API**: This API can be used in atomic services since API version 12. 6933 6934**System capability**: SystemCapability.Multimedia.AVSession.Core 6935 6936**Parameters** 6937 6938| Name | Type | Mandatory | Description | 6939| -------- | ----------------------- | --------- | ------------------------------------------------------------ | 6940| type | string | Yes | Event type. The event **'queueTitleChange'** is triggered when the playlist name changes. | 6941| callback | (title: string) => void | Yes | Callback used for subscription. The **title** parameter in the callback indicates the changed playlist name. | 6942 6943**Error codes** 6944 6945For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6946 6947| ID | Error Message | 6948| ------- | ------------------------------------------------------------ | 6949| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6950| 6600101 | Session service exception. | 6951| 6600103 | The session controller does not exist. | 6952 6953**Example** 6954 6955```ts 6956avsessionController.on('queueTitleChange', (title: string) => { 6957 console.info(`queueTitleChange, title is ${title}`); 6958}); 6959``` 6960 6961### off('queueTitleChange')<sup>10+</sup> 6962 6963off(type: 'queueTitleChange', callback?: (title: string) => void): void 6964 6965Unsubscribes from playlist name change events. This API is called by the controller. 6966 6967**Atomic service API**: This API can be used in atomic services since API version 12. 6968 6969**System capability**: SystemCapability.Multimedia.AVSession.Core 6970 6971**Parameters** 6972 6973| Name | Type | Mandatory | Description | 6974| -------- | ----------------------- | --------- | ------------------------------------------------------------ | 6975| type | string | Yes | Event type, which is **'queueTitleChange'** in this case. | 6976| callback | (title: string) => void | No | Callback used for unsubscription. The **items** parameter in the callback indicates the changed playlist name.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 6977 6978**Error codes** 6979 6980For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 6981 6982| ID | Error Message | 6983| ------- | ------------------------------------------------------------ | 6984| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 6985| 6600101 | Session service exception. | 6986| 6600103 | The session controller does not exist. | 6987 6988**Example** 6989 6990```ts 6991avsessionController.off('queueTitleChange'); 6992``` 6993 6994### on('extrasChange')<sup>10+</sup> 6995 6996on(type: 'extrasChange', callback: (extras: {[key:string]: Object}) => void): void 6997 6998Subscribes to custom media packet change events. This API is called by the controller. 6999 7000**Atomic service API**: This API can be used in atomic services since API version 12. 7001 7002**System capability**: SystemCapability.Multimedia.AVSession.Core 7003 7004**Parameters** 7005 7006| Name | Type | Mandatory | Description | 7007| -------- | ---------------------------------------- | --------- | ------------------------------------------------------------ | 7008| type | string | Yes | Event type. The event **'extrasChange'** is triggered when the provider sets a custom media packet. | 7009| callback | (extras: {[key:string]: object}) => void | Yes | Callback used for subscription. The **extras** parameter in the callback indicates the custom media packet set by the provider. This packet is the same as that set in **dispatchSessionEvent**. | 7010 7011**Error codes** 7012 7013For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7014 7015| ID | Error Message | 7016| ------- | ------------------------------------------------------------ | 7017| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 7018| 6600101 | Session service exception. | 7019| 6600103 | The session controller does not exist. | 7020 7021**Example** 7022 7023```ts 7024import { BusinessError } from '@kit.BasicServicesKit'; 7025 7026let avSessionController: avSession.AVSessionController | undefined = undefined; 7027let currentAVSession: avSession.AVSession | undefined = undefined; 7028let tag = "createNewSession"; 7029let context: Context = getContext(this); 7030 7031avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 7032 if (err) { 7033 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 7034 } else { 7035 currentAVSession = data; 7036 } 7037}); 7038if (currentAVSession !== undefined) { 7039 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 7040 avSessionController = controller; 7041 }).catch((err: BusinessError) => { 7042 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 7043 }); 7044} 7045 7046if (avSessionController !== undefined) { 7047 (avSessionController as avSession.AVSessionController).on('extrasChange', (extras) => { 7048 console.info(`Caught extrasChange event,the new extra is: ${JSON.stringify(extras)}`); 7049 }); 7050} 7051``` 7052 7053### off('extrasChange')<sup>10+</sup> 7054 7055off(type: 'extrasChange', callback?: (extras: {[key:string]: Object}) => void): void 7056 7057Unsubscribes from custom media packet change events. This API is called by the controller. 7058 7059**Atomic service API**: This API can be used in atomic services since API version 12. 7060 7061**System capability**: SystemCapability.Multimedia.AVSession.Core 7062 7063**Parameters** 7064 7065| Name | Type | Mandatory | Description | 7066| -------- | -------------------------------- | --------- | ------------------------------------------------------------ | 7067| type | string | Yes | Event type, which is **'extrasChange'** in this case. | 7068| callback | ({[key:string]: Object}) => void | No | Callback used for unsubscription.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 7069 7070**Error codes** 7071 7072For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7073 7074| ID | Error Message | 7075| ------- | ------------------------------------------------------------ | 7076| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 7077| 6600101 | Session service exception. | 7078| 6600103 | The session controller does not exist. | 7079 7080**Example** 7081 7082```ts 7083avsessionController.off('extrasChange'); 7084``` 7085 7086### getAVPlaybackStateSync<sup>10+</sup> 7087 7088getAVPlaybackStateSync(): AVPlaybackState; 7089 7090Obtains the playback state of this session. This API returns the result synchronously. 7091 7092**Atomic service API**: This API can be used in atomic services since API version 12. 7093 7094**System capability**: SystemCapability.Multimedia.AVSession.Core 7095 7096**Return value** 7097 7098| Type | Description | 7099| ------------------------------------- | ------------------------------ | 7100| [AVPlaybackState](#avplaybackstate10) | Playback state of the session. | 7101 7102**Error codes** 7103 7104For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7105 7106| ID | Error Message | 7107| ------- | -------------------------------------- | 7108| 6600101 | Session service exception. | 7109| 6600102 | The session does not exist. | 7110| 6600103 | The session controller does not exist. | 7111 7112**Example** 7113 7114```ts 7115import { BusinessError } from '@kit.BasicServicesKit'; 7116 7117try { 7118 let playbackState: avSession.AVPlaybackState = avsessionController.getAVPlaybackStateSync(); 7119} catch (err) { 7120 let error = err as BusinessError; 7121 console.info(`getAVPlaybackStateSync error, error code: ${error.code}, error message: ${error.message}`); 7122} 7123``` 7124 7125### getAVMetadataSync<sup>10+</sup> 7126 7127getAVMetadataSync(): AVMetadata 7128 7129Obtains the session metadata. This API returns the result synchronously. 7130 7131**Atomic service API**: This API can be used in atomic services since API version 12. 7132 7133**System capability**: SystemCapability.Multimedia.AVSession.Core 7134 7135**Return value** 7136 7137| Type | Description | 7138| --------------------------- | ----------------- | 7139| [AVMetadata](#avmetadata10) | Session metadata. | 7140 7141**Error codes** 7142 7143For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7144 7145| ID | Error Message | 7146| ------- | -------------------------------------- | 7147| 6600101 | Session service exception. | 7148| 6600102 | The session does not exist. | 7149| 6600103 | The session controller does not exist. | 7150 7151**Example** 7152```ts 7153import { BusinessError } from '@kit.BasicServicesKit'; 7154 7155try { 7156 let metaData: avSession.AVMetadata = avsessionController.getAVMetadataSync(); 7157} catch (err) { 7158 let error = err as BusinessError; 7159 console.info(`getAVMetadataSync error, error code: ${error.code}, error message: ${error.message}`); 7160} 7161``` 7162 7163### getAVCallState<sup>11+</sup> 7164 7165getAVCallState(): Promise\<AVCallState> 7166 7167Obtains the call state. This API uses a promise to return the result. 7168 7169**System capability**: SystemCapability.Multimedia.AVSession.Core 7170 7171**Return value** 7172 7173| Type | Description | 7174| --------------------------------------- | ----------------------------------------------- | 7175| Promise<[AVCallState](#avcallstate11)\> | Promise used to return the call state obtained. | 7176 7177**Error codes** 7178 7179For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7180 7181| ID | Error Message | 7182| ------- | -------------------------------------- | 7183| 6600101 | Session service exception. | 7184| 6600102 | The session does not exist. | 7185| 6600103 | The session controller does not exist. | 7186 7187**Example** 7188 7189```ts 7190import { BusinessError } from '@kit.BasicServicesKit'; 7191 7192avsessionController.getAVCallState().then((callstate: avSession.AVCallState) => { 7193 console.info(`getAVCallState : SUCCESS : state : ${callstate.state}`); 7194}).catch((err: BusinessError) => { 7195 console.error(`getAVCallState BusinessError: code: ${err.code}, message: ${err.message}`); 7196}); 7197``` 7198 7199### getAVCallState<sup>11+</sup> 7200 7201getAVCallState(callback: AsyncCallback\<AVCallState>): void 7202 7203Obtains the call state. This API uses an asynchronous callback to return the result. 7204 7205**System capability**: SystemCapability.Multimedia.AVSession.Core 7206 7207**Parameters** 7208 7209| Name | Type | Mandatory | Description | 7210| -------- | --------------------------------------------- | --------- | ------------------------------------------------ | 7211| callback | AsyncCallback<[AVCallState](#avcallstate11)\> | Yes | Callback used to return the call state obtained. | 7212 7213**Error codes** 7214 7215For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7216 7217| ID | Error Message | 7218| ------- | -------------------------------------- | 7219| 6600101 | Session service exception. | 7220| 6600102 | The session does not exist. | 7221| 6600103 | The session controller does not exist. | 7222 7223**Example** 7224 7225```ts 7226import { BusinessError } from '@kit.BasicServicesKit'; 7227 7228avsessionController.getAVCallState((err: BusinessError, callstate: avSession.AVCallState) => { 7229 if (err) { 7230 console.error(`getAVCallState BusinessError: code: ${err.code}, message: ${err.message}`); 7231 } else { 7232 console.info(`getAVCallState : SUCCESS : state : ${callstate.state}`); 7233 } 7234}); 7235``` 7236 7237### getCallMetadata<sup>11+</sup> 7238 7239getCallMetadata(): Promise\<CallMetadata> 7240 7241Obtains the call metadata. This API uses a promise to return the result. 7242 7243**System capability**: SystemCapability.Multimedia.AVSession.Core 7244 7245**Return value** 7246 7247| Type | Description | 7248| ----------------------------------------- | --------------------------------------------- | 7249| Promise<[CallMetadata](#callmetadata11)\> | Promise used to return the metadata obtained. | 7250 7251**Error codes** 7252 7253For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7254 7255| ID | Error Message | 7256| ------- | -------------------------------------- | 7257| 6600101 | Session service exception. | 7258| 6600102 | The session does not exist. | 7259| 6600103 | The session controller does not exist. | 7260 7261**Example** 7262 7263```ts 7264import { BusinessError } from '@kit.BasicServicesKit'; 7265 7266avsessionController.getCallMetadata().then((calldata: avSession.CallMetadata) => { 7267 console.info(`getCallMetadata : SUCCESS : name : ${calldata.name}`); 7268}).catch((err: BusinessError) => { 7269 console.error(`getCallMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 7270}); 7271``` 7272 7273### getCallMetadata<sup>11+</sup> 7274 7275getCallMetadata(callback: AsyncCallback\<CallMetadata>): void 7276 7277Obtains the call metadata. This API uses an asynchronous callback to return the result. 7278 7279**System capability**: SystemCapability.Multimedia.AVSession.Core 7280 7281**Parameters** 7282 7283| Name | Type | Mandatory | Description | 7284| -------- | ----------------------------------------------- | --------- | ---------------------------------------------- | 7285| callback | AsyncCallback<[CallMetadata](#callmetadata11)\> | Yes | Callback used to return the metadata obtained. | 7286 7287**Error codes** 7288 7289For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7290 7291| ID | Error Message | 7292| ------- | -------------------------------------- | 7293| 6600101 | Session service exception. | 7294| 6600102 | The session does not exist. | 7295| 6600103 | The session controller does not exist. | 7296 7297**Example** 7298 7299```ts 7300import { BusinessError } from '@kit.BasicServicesKit'; 7301 7302avsessionController.getCallMetadata((err: BusinessError, calldata: avSession.CallMetadata) => { 7303 if (err) { 7304 console.error(`getCallMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 7305 } else { 7306 console.info(`getCallMetadata : SUCCESS : name : ${calldata.name}`); 7307 } 7308}); 7309``` 7310 7311### getAVQueueTitleSync<sup>10+</sup> 7312 7313getAVQueueTitleSync(): string 7314 7315Obtains the name of the playlist of this session. This API returns the result synchronously. 7316 7317**Atomic service API**: This API can be used in atomic services since API version 12. 7318 7319**System capability**: SystemCapability.Multimedia.AVSession.Core 7320 7321**Return value** 7322 7323| Type | Description | 7324| ------ | -------------- | 7325| string | Playlist name. | 7326 7327**Error codes** 7328 7329For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7330 7331| ID | Error Message | 7332| ------- | -------------------------------------- | 7333| 6600101 | Session service exception. | 7334| 6600102 | The session does not exist. | 7335| 6600103 | The session controller does not exist. | 7336 7337**Example** 7338 7339```ts 7340import { BusinessError } from '@kit.BasicServicesKit'; 7341 7342try { 7343 let currentQueueTitle: string = avsessionController.getAVQueueTitleSync(); 7344} catch (err) { 7345 let error = err as BusinessError; 7346 console.error(`getAVQueueTitleSync error, error code: ${error.code}, error message: ${error.message}`); 7347} 7348``` 7349 7350### getAVQueueItemsSync<sup>10+</sup> 7351 7352getAVQueueItemsSync(): Array\<AVQueueItem\> 7353 7354Obtains the information related to the items in the playlist of this session. This API returns the result synchronously. 7355 7356**Atomic service API**: This API can be used in atomic services since API version 12. 7357 7358**System capability**: SystemCapability.Multimedia.AVSession.Core 7359 7360**Return value** 7361 7362| Type | Description | 7363| ------------------------------------- | ------------------- | 7364| Array<[AVQueueItem](#avqueueitem10)\> | Items in the queue. | 7365 7366**Error codes** 7367 7368For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7369 7370| ID | Error Message | 7371| ------- | -------------------------------------- | 7372| 6600101 | Session service exception. | 7373| 6600102 | The session does not exist. | 7374| 6600103 | The session controller does not exist. | 7375 7376**Example** 7377 7378```ts 7379import { BusinessError } from '@kit.BasicServicesKit'; 7380 7381try { 7382 let currentQueueItems: Array<avSession.AVQueueItem> = avsessionController.getAVQueueItemsSync(); 7383} catch (err) { 7384 let error = err as BusinessError; 7385 console.error(`getAVQueueItemsSync error, error code: ${error.code}, error message: ${error.message}`); 7386} 7387``` 7388 7389### getOutputDeviceSync<sup>10+</sup> 7390 7391getOutputDeviceSync(): OutputDeviceInfo 7392 7393Obtains the output device information. This API returns the result synchronously. 7394 7395**Atomic service API**: This API can be used in atomic services since API version 12. 7396 7397**System capability**: SystemCapability.Multimedia.AVSession.Core 7398 7399**Return value** 7400 7401| Type | Description | 7402| --------------------------------------- | ------------------------------------ | 7403| [OutputDeviceInfo](#outputdeviceinfo10) | Information about the output device. | 7404 7405**Error codes** 7406 7407For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7408 7409| ID | Error Message | 7410| ------- | -------------------------------------- | 7411| 6600101 | Session service exception. | 7412| 6600103 | The session controller does not exist. | 7413 7414**Example** 7415 7416```ts 7417import { BusinessError } from '@kit.BasicServicesKit'; 7418 7419try { 7420 let currentOutputDevice: avSession.OutputDeviceInfo = avsessionController.getOutputDeviceSync(); 7421} catch (err) { 7422 let error = err as BusinessError; 7423 console.error(`getOutputDeviceSync error, error code: ${error.code}, error message: ${error.message}`); 7424} 7425``` 7426 7427### isActiveSync<sup>10+</sup> 7428 7429isActiveSync(): boolean 7430 7431Checks whether the session is activated. This API returns the result synchronously. 7432 7433**Atomic service API**: This API can be used in atomic services since API version 12. 7434 7435**System capability**: SystemCapability.Multimedia.AVSession.Core 7436 7437**Return value** 7438 7439| Type | Description | 7440| ------- | ------------------------------------------------------------ | 7441| boolean | Returns **true** is returned if the session is activated; returns **false** otherwise. | 7442 7443**Error codes** 7444 7445For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7446 7447| ID | Error Message | 7448| ------- | -------------------------------------- | 7449| 6600101 | Session service exception. | 7450| 6600102 | The session does not exist. | 7451| 6600103 | The session controller does not exist. | 7452 7453**Example** 7454 7455```ts 7456import { BusinessError } from '@kit.BasicServicesKit'; 7457 7458try { 7459 let isActive: boolean = avsessionController.isActiveSync(); 7460} catch (err) { 7461 let error = err as BusinessError; 7462 console.error(`isActiveSync error, error code: ${error.code}, error message: ${error.message}`); 7463} 7464``` 7465 7466### getValidCommandsSync<sup>10+</sup> 7467 7468getValidCommandsSync(): Array\<AVControlCommandType\> 7469 7470Obtains valid commands supported by the session. This API returns the result synchronously. 7471 7472**Atomic service API**: This API can be used in atomic services since API version 12. 7473 7474**System capability**: SystemCapability.Multimedia.AVSession.Core 7475 7476**Return value** 7477 7478| Type | Description | 7479| ------------------------------------------------------- | ------------------------ | 7480| Array<[AVControlCommandType](#avcontrolcommandtype10)\> | A set of valid commands. | 7481 7482**Error codes** 7483 7484For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 7485 7486| ID | Error Message | 7487| ------- | -------------------------------------- | 7488| 6600101 | Session service exception. | 7489| 6600102 | The session does not exist. | 7490| 6600103 | The session controller does not exist. | 7491 7492**Example** 7493 7494```ts 7495import { BusinessError } from '@kit.BasicServicesKit'; 7496 7497try { 7498 let validCommands: Array<avSession.AVControlCommandType> = avsessionController.getValidCommandsSync(); 7499} catch (err) { 7500 let error = err as BusinessError; 7501 console.error(`getValidCommandsSync error, error code: ${error.code}, error message: ${error.message}`); 7502} 7503``` 7504 7505## AVControlCommandType<sup>10+</sup> 7506 7507type AVControlCommandType = 'play' | 'pause' | 'stop' | 'playNext' | 'playPrevious' | 'fastForward' | 'rewind' | 7508 'seek' | 'setSpeed' | 'setLoopMode' | 'toggleFavorite' | 'playFromAssetId' | 'answer' | 'hangUp' | 'toggleCallMute' 7509 7510Enumerates the commands that can be sent to a session. 7511 7512You can use the union of the strings listed in the following table. 7513 7514**Atomic service API**: This API can be used in atomic services since API version 12. 7515 7516**System capability**: SystemCapability.Multimedia.AVSession.Core 7517 7518| Type | Description | 7519| ----------------- | ------------------------------------------------- | 7520| 'play' | Play the media. | 7521| 'pause' | Pause the playback. | 7522| 'stop' | Stop the playback. | 7523| 'playNext' | Play the next media asset. | 7524| 'playPrevious' | Play the previous media asset. | 7525| 'fastForward' | Fast-forward. | 7526| 'rewind' | Rewind. | 7527| 'seek' | Seek to a playback position. | 7528| 'setSpeed' | Set the playback speed. | 7529| 'setLoopMode' | Set the loop mode. | 7530| 'toggleFavorite' | Favorite the media asset. | 7531| 'playFromAssetId' | Play the media asset with the specified asset ID. | 7532| 'answer' | Answer a call. | 7533| 'hangUp' | The call is disconnecting. | 7534| 'toggleCallMute' | Set the mute status for a call. | 7535 7536## AVControlCommand<sup>10+</sup> 7537 7538Describes the command that can be sent to the session. 7539 7540**Atomic service API**: This API can be used in atomic services since API version 12. 7541 7542**System capability**: SystemCapability.Multimedia.AVSession.Core 7543 7544| Name | Type | Mandatory | Description | 7545| --------- | --------------------------------------------------- | --------- | ---------------------------------- | 7546| command | [AVControlCommandType](#avcontrolcommandtype10) | Yes | Command. | 7547| parameter | [LoopMode](#loopmode10) | string | number | No | Parameters carried in the command. | 7548 7549## AVSessionErrorCode<sup>10+</sup> 7550 7551Enumerates the error codes used in the media session. 7552 7553**Atomic service API**: This API can be used in atomic services since API version 12. 7554 7555**System capability**: SystemCapability.Multimedia.AVSession.Core 7556 7557| Name | Value | Description | 7558| ------------------------------------ | ------- | ----------------------------------------- | 7559| ERR_CODE_SERVICE_EXCEPTION | 6600101 | Session service exception. | 7560| ERR_CODE_SESSION_NOT_EXIST | 6600102 | The session does not exist. | 7561| ERR_CODE_CONTROLLER_NOT_EXIST | 6600103 | The session controller does not exist. | 7562| ERR_CODE_REMOTE_CONNECTION_ERR | 6600104 | The remote session connection failed. | 7563| ERR_CODE_COMMAND_INVALID | 6600105 | Invalid session command. | 7564| ERR_CODE_SESSION_INACTIVE | 6600106 | The session is not activated. | 7565| ERR_CODE_MESSAGE_OVERLOAD | 6600107 | Too many commands or events. | 7566| ERR_CODE_DEVICE_CONNECTION_FAILED | 6600108 | Device connection failed. | 7567| ERR_CODE_REMOTE_CONNECTION_NOT_EXIST | 6600109 | The remote connection is not established. | 7568 7569## SkipIntervals<sup>11+</sup> 7570 7571Enumerates the fast-forward or rewind intervals supported by the media session. 7572 7573**System capability**: SystemCapability.Multimedia.AVSession.Core 7574 7575| Name | Value | Description | 7576| ---------- | ----- | ----------------------- | 7577| SECONDS_10 | 10 | The time is 10 seconds. | 7578| SECONDS_15 | 15 | The time is 15 seconds. | 7579| SECONDS_30 | 30 | The time is 30 seconds. |