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 '@ohos.multimedia.avsession'; 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**System capability**: SystemCapability.Multimedia.AVSession.Core 28 29**Parameters** 30 31| Name| Type | Mandatory| Description | 32| ------ | ------------------------------- | ---- | ------------------------------ | 33| context| [Context](js-apis-inner-app-context.md) | Yes| Application context, which provides application environment information.| 34| tag | string | Yes | Custom session name. | 35| type | [AVSessionType](#avsessiontype10) | Yes | Session type, which can be audio or video.| 36 37**Return value** 38 39| Type | Description | 40| --------------------------------- | ------------------------------------------------------------ | 41| 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.| 42 43**Error codes** 44 45For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 46 47| ID| Error Message| 48| -------- | ---------------------------------------- | 49| 6600101 | Session service exception. | 50 51**Example** 52 53```ts 54import { BusinessError } from '@ohos.base'; 55 56let currentAVSession: avSession.AVSession; 57let tag = "createNewSession"; 58let context: Context = getContext(this); 59let sessionId: string; // Used as an input parameter of subsequent functions. 60 61avSession.createAVSession(context, tag, "audio").then((data: avSession.AVSession) => { 62 currentAVSession = data; 63 sessionId = currentAVSession.sessionId; 64 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 65}).catch((err: BusinessError) => { 66 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 67}); 68``` 69 70## avSession.createAVSession<sup>10+</sup> 71 72createAVSession(context: Context, tag: string, type: AVSessionType, callback: AsyncCallback\<AVSession>): void 73 74Creates 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. 75 76**System capability**: SystemCapability.Multimedia.AVSession.Core 77 78**Parameters** 79 80| Name | Type | Mandatory| Description | 81| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 82| context| [Context](js-apis-inner-app-context.md) | Yes| Application context, which provides application environment information. | 83| tag | string | Yes | Custom session name. | 84| type | [AVSessionType](#avsessiontype10) | Yes | Session type, which can be audio or video. | 85| 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.| 86 87**Error codes** 88 89For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 90 91| ID| Error Message| 92| -------- | ---------------------------------------- | 93| 6600101 | Session service exception. | 94 95**Example** 96 97```ts 98import { BusinessError } from '@ohos.base'; 99 100let currentAVSession: avSession.AVSession; 101let tag = "createNewSession"; 102let context: Context = getContext(this); 103let sessionId: string; // Used as an input parameter of subsequent functions. 104 105avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 106 if (err) { 107 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 108 } else { 109 currentAVSession = data; 110 sessionId = currentAVSession.sessionId; 111 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 112 } 113}); 114``` 115 116## avSession.getAllSessionDescriptors 117 118getAllSessionDescriptors(): Promise\<Array\<Readonly\<AVSessionDescriptor>>> 119 120Obtains the descriptors of all sessions. This API uses a promise to return the result. 121 122**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 123 124**System capability**: SystemCapability.Multimedia.AVSession.Manager 125 126**System API**: This is a system API. 127 128**Return value** 129 130| Type | Description | 131| ------------------------------------------------------------ | --------------------------------------------- | 132| Promise\<Array\<Readonly\<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | Promise used to return an array of **AVSessionDescriptor** objects, each of which is read only.| 133 134**Error codes** 135 136For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 137 138| ID| Error Message| 139| -------- | ---------------------------------------- | 140| 6600101 | Session service exception. | 141 142**Example** 143 144```ts 145import { BusinessError } from '@ohos.base'; 146 147avSession.getAllSessionDescriptors().then((descriptors: avSession.AVSessionDescriptor) => { 148 console.info(`getAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); 149 if(descriptors.length > 0 ){ 150 console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); 151 console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); 152 console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); 153 } 154}).catch((err: BusinessError) => { 155 console.error(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); 156}); 157``` 158 159## avSession.getAllSessionDescriptors 160 161getAllSessionDescriptors(callback: AsyncCallback\<Array\<Readonly\<AVSessionDescriptor>>>): void 162 163Obtains the descriptors of all sessions. This API uses an asynchronous callback to return the result. 164 165**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES 166 167**System capability**: SystemCapability.Multimedia.AVSession.Manager 168 169**System API**: This is a system API. 170 171**Parameters** 172 173| Name | Type | Mandatory| Description | 174| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 175| callback | AsyncCallback<Array<Readonly<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | Yes | Callback used to return an array of **AVSessionDescriptor** objects, each of which is read only.| 176 177**Error codes** 178 179For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 180 181| ID| Error Message| 182| -------- | ---------------------------------------- | 183| 6600101 |Session service exception. | 184 185**Example** 186 187```ts 188import { BusinessError } from '@ohos.base'; 189 190avSession.getAllSessionDescriptors((err: BusinessError, descriptors: avSession.AVSessionDescriptor) => { 191 if (err) { 192 console.error(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); 193 } else { 194 console.info(`GetAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); 195 if(descriptors.length > 0 ){ 196 console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); 197 console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); 198 console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); 199 } 200 } 201}); 202``` 203 204## avSession.getHistoricalSessionDescriptors<sup>10+</sup> 205 206getHistoricalSessionDescriptors(maxSize?: number): Promise\<Array\<Readonly\<AVSessionDescriptor>>> 207 208Obtains the descriptors of all sessions. This API uses a promise to return the result. 209 210**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES 211 212**System capability**: SystemCapability.Multimedia.AVSession.Manager 213 214**System API**: This is a system API. 215 216**Parameters** 217 218| Name | Type | Mandatory| Description | 219| -------- | ------ | ---- | -----------------------------------------------------------------| 220| maxSize | number | No | Maximum number of descriptors to obtain. The value ranges from 0 to 10. If this parameter is left blank, the default value **3** is used.| 221 222**Return value** 223 224| Type | Description | 225| --------------------------------------------------------------------------- | -------------------------------------- | 226| Promise\<Array\<Readonly\<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | Promise used to return an array of **AVSessionDescriptor** objects, each of which is read only.| 227 228**Error codes** 229 230For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 231 232| ID| Error Message| 233| -------- | ---------------------------------------- | 234| 6600101 | Session service exception. | 235 236**Example** 237 238```ts 239import { BusinessError } from '@ohos.base'; 240 241avSession.getHistoricalSessionDescriptors().then((descriptors: avSession.AVSessionDescriptor) => { 242 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); 243 if(descriptors.length > 0 ){ 244 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); 245 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); 246 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); 247 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`); 248 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`); 249 } 250}).catch((err: BusinessError) => { 251 console.error(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); 252}); 253``` 254 255## avSession.getHistoricalSessionDescriptors<sup>10+</sup> 256 257getHistoricalSessionDescriptors(maxSize: number, callback: AsyncCallback\<Array\<Readonly\<AVSessionDescriptor>>>): void 258 259Obtains the descriptors of all sessions. This API uses an asynchronous callback to return the result. 260 261**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES 262 263**System capability**: SystemCapability.Multimedia.AVSession.Manager 264 265**System API**: This is a system API. 266 267**Parameters** 268 269| Name | Type | Mandatory| Description | 270| -------- | ------------------------------------------------------------------------------ | ---- | -----------------------------------------------------------------| 271| maxSize | number | Yes | Maximum number of descriptors to obtain. The value ranges from 0 to 10. If this parameter is left blank, the default value **3** is used.| 272| callback | AsyncCallback<Array<Readonly<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | Yes | Callback used to return an array of **AVSessionDescriptor** objects, each of which is read only. | 273 274**Error codes** 275 276For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 277 278| ID| Error Message| 279| -------- | ---------------------------------------- | 280| 6600101 |Session service exception. | 281 282**Example** 283 284```ts 285import { BusinessError } from '@ohos.base'; 286 287avSession.getHistoricalSessionDescriptors(1, (err: BusinessError, descriptors: avSession.AVSessionDescriptor) => { 288 if (err) { 289 console.error(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); 290 } else { 291 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); 292 if(descriptors.length > 0 ){ 293 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); 294 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); 295 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); 296 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`); 297 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`); 298 } 299 } 300}); 301``` 302 303## avSession.createController 304 305createController(sessionId: string): Promise\<AVSessionController> 306 307Creates a session controller based on the session ID. Multiple session controllers can be created. This API uses a promise to return the result. 308 309**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 310 311**System capability**: SystemCapability.Multimedia.AVSession.Manager 312 313**System API**: This is a system API. 314 315**Parameters** 316 317| Name | Type | Mandatory| Description | 318| --------- | ------ | ---- | -------- | 319| sessionId | string | Yes | Session ID.| 320 321**Return value** 322 323| Type | Description | 324| ----------------------------------------------------- | ------------------------------------------------------------ | 325| Promise<[AVSessionController](#avsessioncontroller10)\> | Promise used to return the session controller created, which can be used to obtain the session ID, send commands and events to sessions, and obtain metadata and playback state information.| 326 327**Error codes** 328 329For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 330 331| ID| Error Message| 332| -------- | ---------------------------------------- | 333| 6600101 | Session service exception. | 334| 6600102 | The session does not exist. | 335 336**Example** 337 338```ts 339import { BusinessError } from '@ohos.base'; 340 341let currentAVSession: avSession.AVSession | undefined = undefined; 342let tag = "createNewSession"; 343let context: Context = getContext(this); 344let sessionId: string = ""; // Used as an input parameter of subsequent functions. 345 346avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 347 if (err) { 348 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 349 } else { 350 currentAVSession = data; 351 if (currentAVSession !== undefined) { 352 sessionId = currentAVSession.sessionId; 353 } 354 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 355 } 356}); 357 358let currentAVcontroller: avSession.AVSessionController | undefined = undefined; 359avSession.createController(sessionId).then((avcontroller: avSession.AVSessionController) => { 360 currentAVcontroller = avcontroller; 361 console.info('CreateController : SUCCESS '); 362}).catch((err: BusinessError) => { 363 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 364}); 365``` 366 367## avSession.createController 368 369createController(sessionId: string, callback: AsyncCallback\<AVSessionController>): void 370 371Creates a session controller based on the session ID. Multiple session controllers can be created. This API uses an asynchronous callback to return the result. 372 373**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 374 375**System capability**: SystemCapability.Multimedia.AVSession.Manager 376 377**System API**: This is a system API. 378 379**Parameters** 380 381| Name | Type | Mandatory| Description | 382| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 383| sessionId | string | Yes | Session ID. | 384| callback | AsyncCallback<[AVSessionController](#avsessioncontroller10)\> | Yes | Callback used to return the session controller created, which can be used to obtain the session ID,<br>send commands and events to sessions, and obtain metadata and playback state information.| 385 386**Error codes** 387 388For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 389 390| ID| Error Message| 391| -------- | ---------------------------------------- | 392| 6600101 | Session service exception. | 393| 6600102 | The session does not exist. | 394 395**Example** 396 397```ts 398import { BusinessError } from '@ohos.base'; 399 400let currentAVSession: avSession.AVSession | undefined = undefined; 401let tag = "createNewSession"; 402let context: Context = getContext(this); 403let sessionId: string = ""; // Used as an input parameter of subsequent functions. 404 405avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 406 if (err) { 407 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 408 } else { 409 currentAVSession = data; 410 if (currentAVSession !== undefined) { 411 sessionId = currentAVSession.sessionId; 412 } 413 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 414 } 415}); 416 417let currentAVcontroller: avSession.AVSessionController | undefined = undefined; 418avSession.createController(sessionId, (err: BusinessError, avcontroller: avSession.AVSessionController) => { 419 if (err) { 420 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 421 } else { 422 currentAVcontroller = avcontroller; 423 console.info('CreateController : SUCCESS '); 424 } 425}); 426``` 427 428## avSession.castAudio 429 430castAudio(session: SessionToken | 'all', audioDevices: Array<audio.AudioDeviceDescriptor>): Promise\<void> 431 432Casts a session to a list of devices. This API uses a promise to return the result. 433 434Before calling this API, import the **ohos.multimedia.audio** module to obtain the descriptors of these audio devices. 435 436**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 437 438**System capability**: SystemCapability.Multimedia.AVSession.Manager 439 440**System API**: This is a system API. 441 442**Parameters** 443 444| Name | Type | Mandatory| Description| 445| ------------ | -------------- |------|------| 446| session | [SessionToken](#sessiontoken) | 'all' | Yes | Session token. **SessionToken** indicates a specific token, and **'all'** indicates all tokens.| 447| audioDevices | Array\<[audio.AudioDeviceDescriptor](js-apis-audio.md#audiodevicedescriptor)\> | Yes | Audio devices. | 448 449**Return value** 450 451| Type | Description | 452| -------------- | ----------------------------- | 453| Promise\<void> | Promise used to return the result. If casting is successful, no value is returned; otherwise, an error object is returned.| 454 455**Error codes** 456 457For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 458 459| ID| Error Message| 460| -------- | ---------------------------------------- | 461| 6600101 | Session service exception. | 462| 6600102 | The session does not exist. | 463| 6600104 | The remote session connection failed. | 464 465**Example** 466 467```ts 468import audio from '@ohos.multimedia.audio'; 469import { BusinessError } from '@ohos.base'; 470 471let audioManager = audio.getAudioManager(); 472let audioRoutingManager = audioManager.getRoutingManager(); 473let audioDevices: audio.AudioDeviceDescriptors | undefined = undefined; 474audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => { 475 audioDevices = data; 476 console.info(`Promise returned to indicate that the device list is obtained.`); 477}).catch((err: BusinessError) => { 478 console.error(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`); 479}); 480 481if (audioDevices !== undefined) { 482 avSession.castAudio('all', audioDevices as audio.AudioDeviceDescriptors).then(() => { 483 console.info(`CreateController : SUCCESS`); 484 }).catch((err: BusinessError) => { 485 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 486 }); 487} 488``` 489 490## avSession.castAudio 491 492castAudio(session: SessionToken | 'all', audioDevices: Array<audio.AudioDeviceDescriptor>, callback: AsyncCallback\<void>): void 493 494Casts a session to a list of devices. This API uses an asynchronous callback to return the result. 495 496Before calling this API, import the **ohos.multimedia.audio** module to obtain the descriptors of these audio devices. 497 498**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 499 500**System capability**: SystemCapability.Multimedia.AVSession.Manager 501 502**System API**: This is a system API. 503 504**Parameters** 505 506| Name | Type | Mandatory| Description | 507| ------------ |--------------------------------------------| ---- | ------------------------------------------------------------ | 508| session | [SessionToken](#sessiontoken) | 'all' | Yes | Session token. **SessionToken** indicates a specific token, and **'all'** indicates all tokens.| 509| audioDevices | Array\<[audio.AudioDeviceDescriptor](js-apis-audio.md#audiodevicedescriptor)\> | Yes | Audio devices.| 510| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the casting is successful, **err** is **undefined**; otherwise, **err** is an error object. | 511 512**Error codes** 513 514For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 515 516| ID| Error Message| 517| -------- | ---------------------------------------- | 518| 6600101 | Session service exception. | 519| 6600102 | The session does not exist. | 520| 6600104 | The remote session connection failed. | 521 522**Example** 523 524```ts 525import audio from '@ohos.multimedia.audio'; 526import { BusinessError } from '@ohos.base'; 527 528let audioManager = audio.getAudioManager(); 529let audioRoutingManager = audioManager.getRoutingManager(); 530let audioDevices: audio.AudioDeviceDescriptors | undefined = undefined; 531audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => { 532 audioDevices = data; 533 console.info(`Promise returned to indicate that the device list is obtained.`); 534}).catch((err: BusinessError) => { 535 console.error(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`); 536}); 537 538if (audioDevices !== undefined) { 539 avSession.castAudio('all', audioDevices as audio.AudioDeviceDescriptors, (err: BusinessError) => { 540 if (err) { 541 console.error(`CastAudio BusinessError: code: ${err.code}, message: ${err.message}`); 542 } else { 543 console.info(`CastAudio : SUCCESS `); 544 } 545 }); 546} 547``` 548 549## SessionToken 550 551Describes the information about a session token. 552 553**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 554 555**System capability**: SystemCapability.Multimedia.AVSession.Manager 556 557**System API**: This is a system API. 558 559| Name | Type | Mandatory| Description | 560| :-------- | :----- | :--- | :----------- | 561| sessionId | string | Yes | Session ID. | 562| pid | number | No | Process ID of the session.| 563| uid | number | No | User ID. | 564 565## avSession.on('sessionCreate') 566 567on(type: 'sessionCreate', callback: (session: AVSessionDescriptor) => void): void 568 569Subscribes to session creation events. 570 571**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 572 573**System capability**: SystemCapability.Multimedia.AVSession.Manager 574 575**System API**: This is a system API. 576 577**Parameters** 578 579| Name | Type | Mandatory| Description | 580| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 581| type | string | Yes | Event type. The event **'sessionCreate'** is triggered when a session is created.| 582| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | Yes | Callback used to report the session descriptor.| 583 584**Error codes** 585 586For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 587 588| ID| Error Message| 589| -------- | ---------------------------------------- | 590| 6600101 | Session service exception. | 591 592**Example** 593 594```ts 595avSession.on('sessionCreate', (descriptor: avSession.AVSessionDescriptor) => { 596 console.info(`on sessionCreate : isActive : ${descriptor.isActive}`); 597 console.info(`on sessionCreate : type : ${descriptor.type}`); 598 console.info(`on sessionCreate : sessionTag : ${descriptor.sessionTag}`); 599}); 600 601``` 602 603## avSession.on('sessionDestroy') 604 605on(type: 'sessionDestroy', callback: (session: AVSessionDescriptor) => void): void 606 607Subscribes to session destruction events. 608 609**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 610 611**System capability**: SystemCapability.Multimedia.AVSession.Manager 612 613**System API**: This is a system API. 614 615**Parameters** 616 617| Name | Type | Mandatory| Description | 618| -------- | ---------------| ---- | ------------------------------------------------------------ | 619| type | string | Yes | Event type. The event **'sessionDestroy'** is triggered when a session is destroyed.| 620| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | Yes | Callback used to report the session descriptor.| 621 622**Error codes** 623 624For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 625 626| ID| Error Message| 627| -------- | ---------------------------------------- | 628| 6600101 | Session service exception. | 629 630**Example** 631 632```ts 633avSession.on('sessionDestroy', (descriptor: avSession.AVSessionDescriptor) => { 634 console.info(`on sessionDestroy : isActive : ${descriptor.isActive}`); 635 console.info(`on sessionDestroy : type : ${descriptor.type}`); 636 console.info(`on sessionDestroy : sessionTag : ${descriptor.sessionTag}`); 637}); 638``` 639 640## avSession.on('topSessionChange') 641 642on(type: 'topSessionChange', callback: (session: AVSessionDescriptor) => void): void 643 644Subscribes to top session change events. 645 646**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 647 648**System capability**: SystemCapability.Multimedia.AVSession.Manager 649 650**System API**: This is a system API. 651 652**Parameters** 653 654| Name | Type | Mandatory| Description | 655| -------- | --------------------| ---- | ------------------------------------------------------------ | 656| type | string | Yes | Event type. The event **'topSessionChange'** is triggered when the top session is changed.| 657| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | Yes | Callback used to report the session descriptor.| 658 659**Error codes** 660 661For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 662 663| ID| Error Message| 664| -------- | ---------------------------------------- | 665| 6600101 | Session service exception. | 666 667**Example** 668 669```ts 670avSession.on('topSessionChange', (descriptor: avSession.AVSessionDescriptor) => { 671 console.info(`on topSessionChange : isActive : ${descriptor.isActive}`); 672 console.info(`on topSessionChange : type : ${descriptor.type}`); 673 console.info(`on topSessionChange : sessionTag : ${descriptor.sessionTag}`); 674}); 675``` 676 677## avSession.off('sessionCreate') 678 679off(type: 'sessionCreate', callback?: (session: AVSessionDescriptor) => void): void 680 681Unsubscribes from session creation events. 682 683**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 684 685**System capability**: SystemCapability.Multimedia.AVSession.Manager 686 687**System API**: This is a system API. 688 689**Parameters** 690 691| Name | Type | Mandatory| Description | 692| -------- | ----------| ---- | ----------| 693| type | string | Yes | Event type, which is **'sessionCreate'** in this case.| 694| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **session** parameter in the callback describes a media session. The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 695 696**Error codes** 697 698For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 699 700| ID| Error Message| 701| -------- | ---------------------------------------- | 702| 6600101 | Session service exception. | 703 704**Example** 705 706```ts 707avSession.off('sessionCreate'); 708``` 709 710## avSession.off('sessionDestroy') 711 712off(type: 'sessionDestroy', callback?: (session: AVSessionDescriptor) => void): void 713 714Unsubscribes from session destruction events. 715 716**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 717 718**System capability**: SystemCapability.Multimedia.AVSession.Manager 719 720**System API**: This is a system API. 721 722**Parameters** 723 724| Name | Type | Mandatory| Description | 725| -------- | -----------| ---- | -------------------------| 726| type | string | Yes | Event type, which is **'sessionDestroy'** in this case.| 727| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **session** parameter in the callback describes a media session. The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.| 728 729**Error codes** 730 731For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 732 733| ID| Error Message| 734| -------- | ---------------------------------------- | 735| 6600101 | Session service exception. | 736 737**Example** 738 739```ts 740avSession.off('sessionDestroy'); 741``` 742 743## avSession.off('topSessionChange') 744 745off(type: 'topSessionChange', callback?: (session: AVSessionDescriptor) => void): void 746 747Unsubscribes from top session change events. 748 749**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 750 751**System capability**: SystemCapability.Multimedia.AVSession.Manager 752 753**System API**: This is a system API. 754 755**Parameters** 756 757| Name | Type | Mandatory| Description | 758| -------- | -----------------| ---- | ---------------------------- | 759| type | string | Yes | Event type, which is **'topSessionChange'** in this case.| 760| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | No | Callback used for unsubscription. If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **session** parameter in the callback describes a media session. The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.| 761 762**Error codes** 763 764For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 765 766| ID| Error Message| 767| -------- | ---------------------------------------- | 768| 6600101 | Session service exception. | 769 770**Example** 771 772```ts 773avSession.off('topSessionChange'); 774``` 775 776## avSession.on('sessionServiceDie') 777 778on(type: 'sessionServiceDie', callback: () => void): void 779 780Subscribes to session service death events. 781 782**System capability**: SystemCapability.Multimedia.AVSession.Core 783 784**System API**: This is a system API. 785 786**Parameters** 787 788| Name | Type | Mandatory| Description | 789| -------- | -------------------- | ---- | ------------------------------------------------------------ | 790| type | string | Yes | Event type. The event **'sessionServiceDie'** is triggered when the session service dies.| 791| callback | callback: () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 792 793**Error codes** 794 795For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 796 797| ID| Error Message| 798| -------- | ---------------------------------------- | 799| 6600101 | Session service exception. | 800 801**Example** 802 803```ts 804avSession.on('sessionServiceDie', () => { 805 console.info(`on sessionServiceDie : session is Died `); 806}); 807``` 808 809## avSession.off('sessionServiceDie') 810 811off(type: 'sessionServiceDie', callback?: () => void): void 812 813Unsubscribes from session service death events. 814 815**System capability**: SystemCapability.Multimedia.AVSession.Core 816 817**System API**: This is a system API. 818 819**Parameters** 820 821| Name | Type | Mandatory | Description | 822| ------ | ---------------------- | ---- | ------------------------------------------------------- | 823| type | string | Yes | Event type. The event **'sessionServiceDie'** is triggered when the session service dies.| 824| 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. | 825 826**Error codes** 827 828For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 829 830| ID| Error Message| 831| -------- | ---------------------------------------- | 832| 6600101 | Session service exception. | 833 834**Example** 835 836```ts 837avSession.off('sessionServiceDie'); 838``` 839 840## avSession.sendSystemAVKeyEvent 841 842sendSystemAVKeyEvent(event: KeyEvent, callback: AsyncCallback\<void>): void 843 844Sends a system key event to the top session. This API uses an asynchronous callback to return the result. 845 846**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 847 848**System capability**: SystemCapability.Multimedia.AVSession.Manager 849 850**System API**: This is a system API. 851 852**Parameters** 853 854| Name | Type | Mandatory| Description | 855| -------- | ------------------------------------------------------------ | ---- | ------------------------------------- | 856| event | [KeyEvent](js-apis-keyevent.md) | Yes | Key event. | 857| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the event is sent, **err** is **undefined**; otherwise, **err** is an error object.| 858 859**Error codes** 860 861For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 862 863| ID| Error Message| 864| -------- | ---------------------------------------- | 865| 6600101 | Session service exception. | 866| 6600105 | Invalid session command. | 867 868**Example** 869 870```ts 871import keyEvent from '@ohos.multimodalInput.keyEvent'; 872import { BusinessError } from '@ohos.base'; 873 874let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; 875let event: keyEvent.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}; 876 877avSession.sendSystemAVKeyEvent(event, (err: BusinessError) => { 878 if (err) { 879 console.error(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 880 } else { 881 console.info(`SendSystemAVKeyEvent : SUCCESS `); 882 } 883}); 884``` 885 886## avSession.sendSystemAVKeyEvent 887 888sendSystemAVKeyEvent(event: KeyEvent): Promise\<void> 889 890Sends a system key event to the top session. This API uses a promise to return the result. 891 892**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 893 894**System capability**: SystemCapability.Multimedia.AVSession.Manager 895 896**System API**: This is a system API. 897 898**Parameters** 899 900| Name| Type | Mandatory| Description | 901| ------ | ------------------------------- | ---- | ---------- | 902| event | [KeyEvent](js-apis-keyevent.md) | Yes | Key event.| 903 904**Return value** 905 906| Type | Description | 907| -------------- | ----------------------------- | 908| Promise\<void> | Promise used to return the result. If the event is sent, no value is returned; otherwise, an error object is returned.| 909 910**Error codes** 911 912For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 913 914| ID| Error Message| 915| -------- | ---------------------------------------- | 916| 6600101 | Session service exception. | 917| 6600105 | Invalid session command. | 918 919**Example** 920 921```ts 922import keyEvent from '@ohos.multimodalInput.keyEvent'; 923import { BusinessError } from '@ohos.base'; 924 925let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; 926let event: keyEvent.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}; 927 928avSession.sendSystemAVKeyEvent(event).then(() => { 929 console.info(`SendSystemAVKeyEvent Successfully`); 930}).catch((err: BusinessError) => { 931 console.error(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 932}); 933``` 934 935## avSession.sendSystemControlCommand 936 937sendSystemControlCommand(command: AVControlCommand, callback: AsyncCallback\<void>): void 938 939Sends a system control command to the top session. This API uses an asynchronous callback to return the result. 940 941**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 942 943**System capability**: SystemCapability.Multimedia.AVSession.Manager 944 945**System API**: This is a system API. 946 947**Parameters** 948 949| Name | Type | Mandatory| Description | 950| -------- | ------------------------------------- | ---- | ------------------------------------- | 951| command | [AVControlCommand](#avcontrolcommand10) | Yes | Command to send. | 952| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object.| 953 954**Error codes** 955 956For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 957 958| ID| Error Message| 959| -------- | ---------------------------------------- | 960| 6600101 | Session service exception. | 961| 6600105 | Invalid session command. | 962| 6600107 | Too many commands or events. | 963 964**Example** 965 966```ts 967import avSession from '@ohos.multimedia.avsession'; 968 969let cmd : avSession.AVControlCommandType = 'play'; 970// let cmd : avSession.AVControlCommandType = 'pause'; 971// let cmd : avSession.AVControlCommandType = 'stop'; 972// let cmd : avSession.AVControlCommandType = 'playNext'; 973// let cmd : avSession.AVControlCommandType = 'playPrevious'; 974// let cmd : avSession.AVControlCommandType = 'fastForward'; 975// let cmd : avSession.AVControlCommandType = 'rewind'; 976let avcommand: avSession.AVControlCommand = {command:cmd}; 977// let cmd : avSession.AVControlCommandType = 'seek'; 978// let avcommand = {command:cmd, parameter:10}; 979// let cmd : avSession.AVControlCommandType = 'setSpeed'; 980// let avcommand = {command:cmd, parameter:2.6}; 981// let cmd : avSession.AVControlCommandType = 'setLoopMode'; 982// let avcommand = {command:cmd, parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; 983// let cmd : avSession.AVControlCommandType = 'toggleFavorite'; 984// let avcommand = {command:cmd, parameter:"false"}; 985avSession.sendSystemControlCommand(avcommand, (err) => { 986 if (err) { 987 console.error(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 988 } else { 989 console.info(`sendSystemControlCommand successfully`); 990 } 991}); 992``` 993 994## avSession.sendSystemControlCommand 995 996sendSystemControlCommand(command: AVControlCommand): Promise\<void> 997 998Sends a system control command to the top session. This API uses a promise to return the result. 999 1000**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 1001 1002**System capability**: SystemCapability.Multimedia.AVSession.Manager 1003 1004**System API**: This is a system API. 1005 1006**Parameters** 1007 1008| Name | Type | Mandatory| Description | 1009| ------- | ------------------------------------- | ---- | ----------------------------------- | 1010| command | [AVControlCommand](#avcontrolcommand10) | Yes | Command to send.| 1011 1012**Return value** 1013 1014| Type | Description | 1015| -------------- | ----------------------------- | 1016| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned.| 1017 1018**Error codes** 1019 1020For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1021 1022| ID| Error Message| 1023| -------- | ---------------------------------------- | 1024| 6600101 | Session service exception. | 1025| 6600105 | Invalid session command. | 1026| 6600107 | Too many commands or events. | 1027 1028**Example** 1029 1030```ts 1031import avSession from '@ohos.multimedia.avsession'; 1032import { BusinessError } from '@ohos.base'; 1033 1034let cmd : avSession.AVControlCommandType = 'play'; 1035// let cmd : avSession.AVControlCommandType = 'pause'; 1036// let cmd : avSession.AVControlCommandType = 'stop'; 1037// let cmd : avSession.AVControlCommandType = 'playNext'; 1038// let cmd : avSession.AVControlCommandType = 'playPrevious'; 1039// let cmd : avSession.AVControlCommandType = 'fastForward'; 1040// let cmd : avSession.AVControlCommandType = 'rewind'; 1041let avcommand: avSession.AVControlCommand = {command:cmd}; 1042// let cmd : avSession.AVControlCommandType = 'seek'; 1043// let avcommand = {command:cmd, parameter:10}; 1044// let cmd : avSession.AVControlCommandType = 'setSpeed'; 1045// let avcommand = {command:cmd, parameter:2.6}; 1046// let cmd : avSession.AVControlCommandType = 'setLoopMode'; 1047// let avcommand = {command:cmd, parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; 1048// let cmd : avSession.AVControlCommandType = 'toggleFavorite'; 1049// let avcommand = {command:cmd, parameter:"false"}; 1050avSession.sendSystemControlCommand(avcommand).then(() => { 1051 console.info(`SendSystemControlCommand successfully`); 1052}).catch((err: BusinessError) => { 1053 console.error(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 1054}); 1055``` 1056 1057## ProtocolType<sup>10+</sup> 1058 1059Enumerates the protocol types supported by the remote device. 1060 1061**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1062 1063**System API**: This is a system API. 1064 1065| Name | Value | Description | 1066| --------------------------- | ---- | ----------- | 1067| TYPE_LOCAL | 0 | Local device. | 1068| TYPE_CAST_PLUS_MIRROR | 1 | Cast+ mirror mode.| 1069| TYPE_CAST_PLUS_STREAM | 2 | Cast+ stream mode.| 1070 1071## avSession.startCastDeviceDiscovery<sup>10+</sup> 1072 1073startCastDeviceDiscovery(callback: AsyncCallback\<void>): void 1074 1075Starts cast-enabled device discovery. This API uses an asynchronous callback to return the result. 1076 1077**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1078 1079**System API**: This is a system API. 1080 1081**Parameters** 1082 1083| Name | Type | Mandatory| Description | 1084| -------- | ------------------------------------- | ---- | ------------------------------------- | 1085| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent and device discovery starts, **err** is **undefined**; otherwise, **err** is an error object.| 1086 1087 1088**Example** 1089 1090```ts 1091import { BusinessError } from '@ohos.base'; 1092 1093avSession.startCastDeviceDiscovery((err: BusinessError) => { 1094 if (err) { 1095 console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1096 } else { 1097 console.info(`startCastDeviceDiscovery successfully`); 1098 } 1099}); 1100``` 1101 1102## avSession.startCastDeviceDiscovery<sup>10+</sup> 1103 1104startCastDeviceDiscovery(filter: number, callback: AsyncCallback\<void>): void 1105 1106Starts cast-enabled device discovery with filter criteria specified. This API uses an asynchronous callback to return the result. 1107 1108**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1109 1110**System API**: This is a system API. 1111 1112**Parameters** 1113 1114| Name | Type | Mandatory| Description | 1115| -------- | ------------------------------------- | ---- | ------------------------------------- | 1116| filter | number | Yes| Filter criteria for device discovery. The value consists of **ProtocolType**s.| 1117| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent and device discovery starts, **err** is **undefined**; otherwise, **err** is an error object.| 1118 1119 1120**Example** 1121 1122```ts 1123import { BusinessError } from '@ohos.base'; 1124 1125let filter = 2; 1126avSession.startCastDeviceDiscovery(filter, (err: BusinessError) => { 1127 if (err) { 1128 console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1129 } else { 1130 console.info(`startCastDeviceDiscovery successfully`); 1131 } 1132}); 1133``` 1134 1135## avSession.startCastDeviceDiscovery<sup>10+</sup> 1136 1137startCastDeviceDiscovery(filter?: number): Promise\<void> 1138 1139Starts cast-enabled device discovery. This API uses a promise to return the result. 1140 1141**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1142 1143**System API**: This is a system API. 1144 1145**Parameters** 1146 1147| Name | Type | Mandatory| Description | 1148| -------- | ------------------------------------- | ---- | ------------------------------------- | 1149| filter | number | No| Filter criteria for device discovery. The value consists of **ProtocolType**s.| 1150 1151**Return value** 1152 1153| Type | Description | 1154| -------------- | ----------------------------- | 1155| Promise\<void> | Promise used to return the result. If the command is sent and device discovery starts, no value is returned; otherwise, an error object is returned.| 1156 1157**Example** 1158 1159```ts 1160import { BusinessError } from '@ohos.base'; 1161 1162let filter = 2; 1163avSession.startCastDeviceDiscovery(filter).then(() => { 1164 console.info(`startCastDeviceDiscovery successfully`); 1165}).catch((err: BusinessError) => { 1166 console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1167}); 1168``` 1169 1170## avSession.stopCastDeviceDiscovery<sup>10+</sup> 1171 1172stopCastDeviceDiscovery(callback: AsyncCallback\<void>): void 1173 1174Stops cast-enabled device discovery. This API uses an asynchronous callback to return the result. 1175 1176**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1177 1178**System API**: This is a system API. 1179 1180**Parameters** 1181 1182| Name | Type | Mandatory| Description | 1183| -------- | ------------------------------------- | ---- | ------------------------------------- | 1184| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If device discovery stops, **err** is **undefined**; otherwise, **err** is an error object.| 1185 1186 1187**Example** 1188 1189```ts 1190import { BusinessError } from '@ohos.base'; 1191 1192avSession.stopCastDeviceDiscovery((err: BusinessError) => { 1193 if (err) { 1194 console.error(`stopCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1195 } else { 1196 console.info(`stopCastDeviceDiscovery successfully`); 1197 } 1198}); 1199``` 1200 1201## avSession.stopCastDeviceDiscovery<sup>10+</sup> 1202 1203stopCastDeviceDiscovery(): Promise\<void> 1204 1205Stops cast-enabled device discovery. This API uses a promise to return the result. 1206 1207**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1208 1209**System API**: This is a system API. 1210 1211**Return value** 1212 1213| Type | Description | 1214| -------------- | ----------------------------- | 1215| Promise\<void> | Promise used to return the result. If device discovery stops, no value is returned; otherwise, an error object is returned.| 1216 1217**Example** 1218 1219```ts 1220import { BusinessError } from '@ohos.base'; 1221 1222avSession.stopCastDeviceDiscovery().then(() => { 1223 console.info(`startCastDeviceDiscovery successfully`); 1224}).catch((err: BusinessError) => { 1225 console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1226}); 1227``` 1228 1229## avSession.setDiscoverable<sup>10+</sup> 1230 1231setDiscoverable(enable: boolean, callback: AsyncCallback\<void>): void 1232 1233Sets whether to allow the device discoverable. A discoverable device can be used as the cast receiver. This API uses an asynchronous callback to return the result. 1234 1235**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1236 1237**System API**: This is a system API. 1238 1239**Parameters** 1240 1241| Name | Type | Mandatory| Description | 1242| -------- | ------------------------------------- | ---- | ------------------------------------- | 1243| enable | boolean | Yes| Whether to allow the device discoverable. The value **true** means to allow the device discoverable, and **false** means the opposite.| 1244| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1245 1246 1247**Example** 1248 1249```ts 1250import { BusinessError } from '@ohos.base'; 1251 1252avSession.setDiscoverable(true, (err: BusinessError) => { 1253 if (err) { 1254 console.error(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); 1255 } else { 1256 console.info(`setDiscoverable successfully`); 1257 } 1258}); 1259``` 1260 1261## avSession.setDiscoverable<sup>10+</sup> 1262 1263setDiscoverable(enable: boolean): Promise\<void> 1264 1265Sets whether to allow the device discoverable. A discoverable device can be used as the cast receiver. This API uses a promise to return the result. 1266 1267**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1268 1269**System API**: This is a system API. 1270 1271**Parameters** 1272 1273| Name | Type | Mandatory| Description | 1274| -------- | ------------------------------------- | ---- | ------------------------------------- | 1275| enable | boolean | Yes| Whether to allow the device discoverable. The value **true** means to allow the device discoverable, and **false** means the opposite.| 1276 1277**Return value** 1278 1279| Type | Description | 1280| -------------- | ----------------------------- | 1281| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 1282 1283**Example** 1284 1285```ts 1286import { BusinessError } from '@ohos.base'; 1287 1288avSession.setDiscoverable(true).then(() => { 1289 console.info(`setDiscoverable successfully`); 1290}).catch((err: BusinessError) => { 1291 console.error(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); 1292}); 1293``` 1294 1295## avSession.on('deviceAvailable')<sup>10+</sup> 1296 1297on(type: 'deviceAvailable', callback: (device: OutputDeviceInfo) => void): void 1298 1299Subscribes to device discovery events. 1300 1301**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1302 1303**System API**: This is a system API. 1304 1305**Parameters** 1306 1307| Name | Type | Mandatory| Description | 1308| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1309| type | string | Yes | Event type. The event **'deviceAvailable'** is triggered when a device is discovered.| 1310| callback | (device: OutputDeviceInfo) => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1311 1312**Example** 1313 1314```ts 1315import avSession from '@ohos.multimedia.avsession'; 1316 1317let castDevice: avSession.OutputDeviceInfo; 1318avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { 1319 castDevice = device; 1320 console.info(`on deviceAvailable : ${device} `); 1321}); 1322``` 1323 1324## avSession.off('deviceAvailable')<sup>10+</sup> 1325 1326off(type: 'deviceAvailable', callback?: (device: OutputDeviceInfo) => void): void 1327 1328Unsubscribes from device discovery events. 1329 1330**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1331 1332**System API**: This is a system API. 1333 1334**Parameters** 1335 1336| Name | Type | Mandatory | Description | 1337| ------ | ---------------------- | ---- | ------------------------------------------------------- | 1338| type | string | Yes | Event type. The event **'deviceAvailable'** is triggered when a device is discovered.| 1339 1340**Example** 1341 1342```ts 1343avSession.off('deviceAvailable'); 1344``` 1345 1346## avSession.getAVCastController<sup>10+</sup> 1347 1348getAVCastController(sessionId: string, callback: AsyncCallback\<AVCastController>): void 1349 1350Obtains the cast controller when a casting connection is set up. This API uses an asynchronous callback to return the result. 1351 1352**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1353 1354**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES 1355 1356**System API**: This is a system API. 1357 1358**Parameters** 1359 1360| Name | Type | Mandatory| Description | 1361| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 1362| sessionId | string | Yes |Session ID.| 1363| callback | AsyncCallback<[AVCastController](#avcastcontroller10)\> | Yes | Callback used to return the cast controller.| 1364 1365**Error codes** 1366 1367For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1368 1369| ID| Error Message| 1370| -------- | ---------------------------------------- | 1371| 6600101 | Session service exception | 1372| 6600102 | session does not exist | 1373 1374**Example** 1375 1376```ts 1377import { BusinessError } from '@ohos.base'; 1378 1379let currentAVSession: avSession.AVSession | undefined = undefined; 1380let tag = "createNewSession"; 1381let context: Context = getContext(this); 1382let sessionId: string = ""; // Used as an input parameter of subsequent functions. 1383 1384avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1385 if (err) { 1386 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1387 } else { 1388 currentAVSession = data; 1389 if (currentAVSession !== undefined) { 1390 sessionId = currentAVSession.sessionId; 1391 } 1392 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1393 } 1394}); 1395 1396let aVCastController: avSession.AVCastController; 1397avSession.getAVCastController(sessionId , (err: BusinessError, avcontroller: avSession.AVCastController) => { 1398 if (err) { 1399 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 1400 } else { 1401 aVCastController = avcontroller; 1402 console.info('getAVCastController : SUCCESS '); 1403 } 1404}); 1405``` 1406 1407## avSession.getAVCastController<sup>10+</sup> 1408 1409getAVCastController(sessionId: string): Promise\<AVCastController>; 1410 1411Obtains the cast controller when a casting connection is set up. This API uses a promise to return the result. 1412 1413**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1414 1415**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES 1416 1417**System API**: This is a system API. 1418 1419**Parameters** 1420 1421| Name | Type | Mandatory| Description | 1422| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 1423| sessionId | string | Yes |Session ID.| 1424 1425**Return value** 1426 1427| Type | Description | 1428| --------- | ------------------------------------------------------------ | 1429| Promise<[AVCastController](#avcastcontroller10)\> | Promise used to return the cast controller.| 1430 1431**Error codes** 1432 1433For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1434 1435| ID| Error Message| 1436| -------- | ---------------------------------------- | 1437| 6600101 | server exception | 1438| 6600102 | The session does not exist | 1439 1440**Example** 1441 1442```ts 1443import { BusinessError } from '@ohos.base'; 1444 1445let currentAVSession: avSession.AVSession | undefined = undefined; 1446let tag = "createNewSession"; 1447let context: Context = getContext(this); 1448let sessionId: string = ""; // Used as an input parameter of subsequent functions. 1449 1450avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1451 if (err) { 1452 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1453 } else { 1454 currentAVSession = data; 1455 if (currentAVSession !== undefined) { 1456 sessionId = currentAVSession.sessionId; 1457 } 1458 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1459 } 1460}); 1461 1462let aVCastController: avSession.AVCastController; 1463avSession.getAVCastController(sessionId).then((avcontroller: avSession.AVCastController) => { 1464 aVCastController = avcontroller; 1465 console.info('getAVCastController : SUCCESS'); 1466}).catch((err: BusinessError) => { 1467 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 1468}); 1469``` 1470 1471## avSession.startCasting<sup>10+</sup> 1472 1473startCasting(session: SessionToken, device: OutputDeviceInfo, callback: AsyncCallback\<void>): void 1474 1475Starts casting. This API uses an asynchronous callback to return the result. 1476 1477**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 1478 1479**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1480 1481**System API**: This is a system API. 1482 1483**Parameters** 1484 1485| Name | Type | Mandatory| Description | 1486| -------- | ------------------------------------- | ---- | ------------------------------------- | 1487| session | [SessionToken](#sessiontoken) | Yes | Session token. | 1488| device | [OutputDeviceInfo](#outputdeviceinfo10) | Yes | Device-related information.| 1489| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent and casting starts, **err** is **undefined**; otherwise, **err** is an error object.| 1490 1491**Error codes** 1492 1493For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1494 1495| ID| Error Message| 1496| -------- | ---------------------------------------- | 1497| 6600101 | Session service exception. | 1498| 6600108 | Device connecting failed. | 1499 1500**Example** 1501 1502```ts 1503import avSession from '@ohos.multimedia.avsession'; 1504import { BusinessError } from '@ohos.base'; 1505 1506let currentAVSession: avSession.AVSession | undefined = undefined; 1507let tag = "createNewSession"; 1508let context: Context = getContext(this); 1509let sessionId: string = ""; // Used as an input parameter of subsequent functions. 1510 1511avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1512 if (err) { 1513 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1514 } else { 1515 currentAVSession = data; 1516 if (currentAVSession !== undefined) { 1517 sessionId = currentAVSession.sessionId; 1518 } 1519 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1520 } 1521}); 1522 1523let myToken: avSession.SessionToken = { 1524 sessionId: sessionId, 1525} 1526let castDevice: avSession.OutputDeviceInfo | undefined = undefined; 1527avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { 1528 castDevice = device; 1529 console.info(`on deviceAvailable : ${device} `); 1530}); 1531if (castDevice !== undefined) { 1532 avSession.startCasting(myToken, castDevice, (err: BusinessError) => { 1533 if (err) { 1534 console.error(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); 1535 } else { 1536 console.info(`startCasting successfully`); 1537 } 1538 }); 1539} 1540``` 1541 1542## avSession.startCasting<sup>10+</sup> 1543 1544startCasting(session: SessionToken, device: OutputDeviceInfo): Promise\<void> 1545 1546Starts casting. This API uses a promise to return the result. 1547 1548**Required permissions**: ohos.permission.MANAGE_MEDIA_RESOURCES (available only to system applications) 1549 1550**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1551 1552**System API**: This is a system API. 1553 1554**Parameters** 1555 1556| Name | Type | Mandatory| Description | 1557| -------- | ------------------------------------- | ---- | ------------------------------------- | 1558| session | [SessionToken](#sessiontoken) | Yes | Session token. | 1559| device | [OutputDeviceInfo](#outputdeviceinfo10) | Yes | Device-related information.| 1560 1561**Return value** 1562 1563| Type | Description | 1564| -------------- | ----------------------------- | 1565| Promise\<void> | Promise used to return the result. If the command is sent and casting starts, no value is returned; otherwise, an error object is returned.| 1566 1567**Error codes** 1568 1569For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1570 1571| ID| Error Message| 1572| -------- | ---------------------------------------- | 1573| 6600101 | Session service exception. | 1574| 6600108 | Device connecting failed. | 1575 1576**Example** 1577 1578```ts 1579import avSession from '@ohos.multimedia.avsession'; 1580import { BusinessError } from '@ohos.base'; 1581 1582let currentAVSession: avSession.AVSession | undefined = undefined; 1583let tag = "createNewSession"; 1584let context: Context = getContext(this); 1585let sessionId: string = ""; // Used as an input parameter of subsequent functions. 1586 1587avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1588 if (err) { 1589 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1590 } else { 1591 currentAVSession = data; 1592 if (currentAVSession !== undefined) { 1593 sessionId = currentAVSession.sessionId; 1594 } 1595 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1596 } 1597}); 1598 1599let myToken: avSession.SessionToken = { 1600 sessionId: sessionId, 1601} 1602let castDevice: avSession.OutputDeviceInfo | undefined = undefined; 1603avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { 1604 castDevice = device; 1605 console.info(`on deviceAvailable : ${device} `); 1606}); 1607if (castDevice !== undefined) { 1608 avSession.startCasting(myToken, castDevice).then(() => { 1609 console.info(`startCasting successfully`); 1610 }).catch((err: BusinessError) => { 1611 console.error(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); 1612 }); 1613} 1614``` 1615 1616## avSession.stopCasting<sup>10+</sup> 1617 1618stopCasting(session: SessionToken, callback: AsyncCallback\<void>): void 1619 1620Stops castings. This API uses an asynchronous callback to return the result. 1621 1622**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1623 1624**System API**: This is a system API. 1625 1626**Parameters** 1627 1628| Name | Type | Mandatory| Description | 1629| -------- | ------------------------------------- | ---- | ------------------------------------- | 1630| session | [SessionToken](#sessiontoken) | Yes | Session token. | 1631| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If casting stops, **err** is **undefined**; otherwise, **err** is an error object.| 1632 1633**Error codes** 1634 1635For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1636 1637| ID| Error Message| 1638| -------- | ---------------------------------------- | 1639| 6600109 | The remote connection is not established. | 1640 1641**Example** 1642 1643```ts 1644import avSession from '@ohos.multimedia.avsession'; 1645import { BusinessError } from '@ohos.base'; 1646 1647let currentAVSession: avSession.AVSession | undefined = undefined; 1648let tag = "createNewSession"; 1649let context: Context = getContext(this); 1650let sessionId: string = ""; // Used as an input parameter of subsequent functions. 1651 1652avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1653 if (err) { 1654 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1655 } else { 1656 currentAVSession = data; 1657 if (currentAVSession !== undefined) { 1658 sessionId = currentAVSession.sessionId; 1659 } 1660 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1661 } 1662}); 1663 1664let myToken: avSession.SessionToken = { 1665 sessionId: sessionId, 1666} 1667avSession.stopCasting(myToken, (err: BusinessError) => { 1668 if (err) { 1669 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 1670 } else { 1671 console.info(`stopCasting successfully`); 1672 } 1673}); 1674``` 1675 1676## avSession.stopCasting<sup>10+</sup> 1677 1678stopCasting(session: SessionToken): Promise\<void> 1679 1680Stops castings. This API uses a promise to return the result. 1681 1682**System capability**: SystemCapability.Multimedia.AVSession.AVCast 1683 1684**System API**: This is a system API. 1685 1686**Parameters** 1687 1688| Name | Type | Mandatory| Description | 1689| -------- | ------------------------------------- | ---- | ------------------------------------- | 1690| session | [SessionToken](#sessiontoken) | Yes | Session token. | 1691 1692**Return value** 1693 1694| Type | Description | 1695| -------------- | ----------------------------- | 1696| Promise\<void> | Promise used to return the result. If casting stops, no value is returned; otherwise, an error object is returned.| 1697 1698**Error codes** 1699 1700For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1701 1702| ID| Error Message| 1703| -------- | ---------------------------------------- | 1704| 6600109 | The remote connection is not established. | 1705 1706**Example** 1707 1708```ts 1709import avSession from '@ohos.multimedia.avsession'; 1710import { BusinessError } from '@ohos.base'; 1711 1712let currentAVSession: avSession.AVSession | undefined = undefined; 1713let tag = "createNewSession"; 1714let context: Context = getContext(this); 1715let sessionId: string = ""; // Used as an input parameter of subsequent functions. 1716 1717avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1718 if (err) { 1719 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1720 } else { 1721 currentAVSession = data; 1722 if (currentAVSession !== undefined) { 1723 sessionId = currentAVSession.sessionId; 1724 } 1725 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1726 } 1727}); 1728 1729let myToken: avSession.SessionToken = { 1730 sessionId: sessionId, 1731} 1732avSession.stopCasting(myToken).then(() => { 1733 console.info(`stopCasting successfully`); 1734}).catch((err: BusinessError) => { 1735 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 1736}); 1737 1738 1739``` 1740 1741## AVSessionType<sup>10+<sup> 1742Enumerates the session types supported by the session. 1743 1744**System capability**: SystemCapability.Multimedia.AVSession.Core 1745 1746| Name | Type | Description| 1747| ----- | ------ | ---- | 1748| audio | string | Audio session.| 1749| video | string | Video session.| 1750 1751## AVSession<sup>10+</sup> 1752 1753An **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. 1754 1755### Attributes 1756 1757**System capability**: SystemCapability.Multimedia.AVSession.Core 1758 1759| Name | Type | Readable| Writable| Description | 1760| :-------- | :----- | :--- | :--- | :---------------------------- | 1761| sessionId | string | Yes | No | Unique session ID of the **AVSession** object.| 1762| sessionType<sup>10+</sup> | AVSessionType | Yes | No | AVSession type.| 1763 1764 1765**Example** 1766 1767```ts 1768import avSession from '@ohos.multimedia.avsession'; 1769 1770let sessionId: string = currentAVSession.sessionId; 1771let sessionType: avSession.AVSessionType = currentAVSession.sessionType; 1772``` 1773 1774### setAVMetadata<sup>10+</sup> 1775 1776setAVMetadata(data: AVMetadata): Promise\<void> 1777 1778Sets session metadata. This API uses a promise to return the result. 1779 1780**System capability**: SystemCapability.Multimedia.AVSession.Core 1781 1782**Parameters** 1783 1784| Name| Type | Mandatory| Description | 1785| ------ | ------------------------- | ---- | ------------ | 1786| data | [AVMetadata](#avmetadata10) | Yes | Session metadata.| 1787 1788**Return value** 1789 1790| Type | Description | 1791| -------------- | ----------------------------- | 1792| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 1793 1794**Error codes** 1795 1796For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1797 1798| ID| Error Message| 1799| -------- | ---------------------------------------- | 1800| 6600101 | Session service exception. | 1801| 6600102 | The session does not exist. | 1802 1803**Example** 1804 1805```ts 1806import avSession from '@ohos.multimedia.avsession'; 1807import { BusinessError } from '@ohos.base'; 1808 1809let metadata: avSession.AVMetadata = { 1810 assetId: "121278", 1811 title: "lose yourself", 1812 artist: "Eminem", 1813 author: "ST", 1814 album: "Slim shady", 1815 writer: "ST", 1816 composer: "ST", 1817 duration: 2222, 1818 mediaImage: "https://www.example.com/example.jpg", 1819 subtitle: "8 Mile", 1820 description: "Rap", 1821 lyric: "https://www.example.com/example.lrc", 1822 previousAssetId: "121277", 1823 nextAssetId: "121279", 1824}; 1825currentAVSession.setAVMetadata(metadata).then(() => { 1826 console.info(`SetAVMetadata successfully`); 1827}).catch((err: BusinessError) => { 1828 console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 1829}); 1830``` 1831 1832### setAVMetadata<sup>10+</sup> 1833 1834setAVMetadata(data: AVMetadata, callback: AsyncCallback\<void>): void 1835 1836Sets session metadata. This API uses an asynchronous callback to return the result. 1837 1838**System capability**: SystemCapability.Multimedia.AVSession.Core 1839 1840**Parameters** 1841 1842| Name | Type | Mandatory| Description | 1843| -------- | ------------------------- | ---- | ------------------------------------- | 1844| data | [AVMetadata](#avmetadata10) | Yes | Session metadata. | 1845| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1846 1847**Error codes** 1848 1849For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1850 1851| ID| Error Message| 1852| -------- | ---------------------------------------- | 1853| 6600101 | Session service exception. | 1854| 6600102 | The session does not exist. | 1855 1856**Example** 1857 1858```ts 1859import avSession from '@ohos.multimedia.avsession'; 1860import { BusinessError } from '@ohos.base'; 1861 1862let metadata: avSession.AVMetadata = { 1863 assetId: "121278", 1864 title: "lose yourself", 1865 artist: "Eminem", 1866 author: "ST", 1867 album: "Slim shady", 1868 writer: "ST", 1869 composer: "ST", 1870 duration: 2222, 1871 mediaImage: "https://www.example.com/example.jpg", 1872 subtitle: "8 Mile", 1873 description: "Rap", 1874 lyric: "https://www.example.com/example.lrc", 1875 previousAssetId: "121277", 1876 nextAssetId: "121279", 1877}; 1878currentAVSession.setAVMetadata(metadata, (err: BusinessError) => { 1879 if (err) { 1880 console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 1881 } else { 1882 console.info(`SetAVMetadata successfully`); 1883 } 1884}); 1885``` 1886 1887### setAVPlaybackState<sup>10+</sup> 1888 1889setAVPlaybackState(state: AVPlaybackState): Promise\<void> 1890 1891Sets information related to the session playback state. This API uses a promise to return the result. 1892 1893**System capability**: SystemCapability.Multimedia.AVSession.Core 1894 1895**Parameters** 1896 1897| Name| Type | Mandatory| Description | 1898| ------ | ----------------------------------- | ---- | ---------------------------------------------- | 1899| data | [AVPlaybackState](#avplaybackstate10) | Yes | Information related to the session playback state.| 1900 1901**Return value** 1902 1903| Type | Description | 1904| -------------- | ----------------------------- | 1905| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 1906 1907**Error codes** 1908 1909For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1910 1911| ID| Error Message| 1912| -------- | ---------------------------------------- | 1913| 6600101 | Session service exception. | 1914| 6600102 | The session does not exist. | 1915 1916**Example** 1917 1918```ts 1919import avSession from '@ohos.multimedia.avsession'; 1920import { BusinessError } from '@ohos.base'; 1921 1922let playbackState: avSession.AVPlaybackState = { 1923 state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, 1924 speed: 1.0, 1925 position:{elapsedTime:10, updateTime:(new Date()).getTime()}, 1926 bufferedTime:1000, 1927 loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, 1928 isFavorite:true, 1929}; 1930currentAVSession.setAVPlaybackState(playbackState).then(() => { 1931 console.info(`SetAVPlaybackState successfully`); 1932}).catch((err: BusinessError) => { 1933 console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 1934}); 1935``` 1936 1937### setAVPlaybackState<sup>10+</sup> 1938 1939setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\<void>): void 1940 1941Sets information related to the session playback state. This API uses an asynchronous callback to return the result. 1942 1943**System capability**: SystemCapability.Multimedia.AVSession.Core 1944 1945**Parameters** 1946 1947| Name | Type | Mandatory| Description | 1948| -------- | ----------------------------------- | ---- | ---------------------------------------------- | 1949| data | [AVPlaybackState](#avplaybackstate10) | Yes | Information related to the session playback state.| 1950| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1951 1952**Error codes** 1953 1954For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 1955 1956| ID| Error Message| 1957| -------- | ---------------------------------------- | 1958| 6600101 | Session service exception. | 1959| 6600102 | The session does not exist. | 1960 1961**Example** 1962 1963```ts 1964import avSession from '@ohos.multimedia.avsession'; 1965import { BusinessError } from '@ohos.base'; 1966 1967let PlaybackState: avSession.AVPlaybackState = { 1968 state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, 1969 speed: 1.0, 1970 position:{elapsedTime:10, updateTime:(new Date()).getTime()}, 1971 bufferedTime:1000, 1972 loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, 1973 isFavorite:true, 1974}; 1975currentAVSession.setAVPlaybackState(PlaybackState, (err: BusinessError) => { 1976 if (err) { 1977 console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 1978 } else { 1979 console.info(`SetAVPlaybackState successfully`); 1980 } 1981}); 1982``` 1983 1984### setLaunchAbility<sup>10+</sup> 1985 1986setLaunchAbility(ability: WantAgent): Promise\<void> 1987 1988Sets a launcher ability. This API uses a promise to return the result. 1989 1990**System capability**: SystemCapability.Multimedia.AVSession.Core 1991 1992**Parameters** 1993 1994| Name | Type | Mandatory| Description | 1995| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 1996| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | Yes | Application attributes, such as the bundle name, ability name, and deviceID.| 1997 1998**Return value** 1999 2000| Type | Description | 2001| -------------- | ----------------------------- | 2002| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 2003 2004**Error codes** 2005 2006For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2007 2008| ID| Error Message| 2009| -------- | ---------------------------------------- | 2010| 6600101 | Session service exception. | 2011| 6600102 | The session does not exist. | 2012 2013**Example** 2014 2015```ts 2016import wantAgent from '@ohos.app.ability.wantAgent'; 2017import { BusinessError } from '@ohos.base'; 2018 2019// WantAgentInfo object 2020let wantAgentInfo: wantAgent.WantAgentInfo = { 2021 wants: [ 2022 { 2023 deviceId: "deviceId", 2024 bundleName: "com.example.myapplication", 2025 abilityName: "EntryAbility", 2026 action: "action1", 2027 entities: ["entity1"], 2028 type: "MIMETYPE", 2029 uri: "key={true,true,false}", 2030 parameters: 2031 { 2032 mykey0: 2222, 2033 mykey1: [1, 2, 3], 2034 mykey2: "[1, 2, 3]", 2035 mykey3: "ssssssssssssssssssssssssss", 2036 mykey4: [false, true, false], 2037 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 2038 mykey6: true, 2039 } 2040 } 2041 ], 2042 operationType: wantAgent.OperationType.START_ABILITIES, 2043 requestCode: 0, 2044 wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 2045} 2046 2047wantAgent.getWantAgent(wantAgentInfo).then((agent) => { 2048 currentAVSession.setLaunchAbility(agent).then(() => { 2049 console.info(`SetLaunchAbility successfully`); 2050 }).catch((err: BusinessError) => { 2051 console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 2052 }); 2053}); 2054``` 2055 2056### setLaunchAbility<sup>10+</sup> 2057 2058setLaunchAbility(ability: WantAgent, callback: AsyncCallback\<void>): void 2059 2060Sets a launcher ability. This API uses an asynchronous callback to return the result. 2061 2062**System capability**: SystemCapability.Multimedia.AVSession.Core 2063 2064**Parameters** 2065 2066| Name | Type | Mandatory| Description | 2067| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 2068| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | Yes | Application attributes, such as the bundle name, ability name, and deviceID. | 2069| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2070 2071**Error codes** 2072 2073For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2074 2075| ID| Error Message| 2076| -------- | ---------------------------------------- | 2077| 6600101 | Session service exception. | 2078| 6600102 | The session does not exist. | 2079 2080**Example** 2081 2082```ts 2083import wantAgent from '@ohos.app.ability.wantAgent'; 2084import { BusinessError } from '@ohos.base'; 2085 2086// WantAgentInfo object 2087let wantAgentInfo: wantAgent.WantAgentInfo = { 2088 wants: [ 2089 { 2090 deviceId: "deviceId", 2091 bundleName: "com.example.myapplication", 2092 abilityName: "EntryAbility", 2093 action: "action1", 2094 entities: ["entity1"], 2095 type: "MIMETYPE", 2096 uri: "key={true,true,false}", 2097 parameters: 2098 { 2099 mykey0: 2222, 2100 mykey1: [1, 2, 3], 2101 mykey2: "[1, 2, 3]", 2102 mykey3: "ssssssssssssssssssssssssss", 2103 mykey4: [false, true, false], 2104 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 2105 mykey6: true, 2106 } 2107 } 2108 ], 2109 operationType: wantAgent.OperationType.START_ABILITIES, 2110 requestCode: 0, 2111 wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 2112} 2113 2114wantAgent.getWantAgent(wantAgentInfo).then((agent) => { 2115 currentAVSession.setLaunchAbility(agent, (err: BusinessError) => { 2116 if (err) { 2117 console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 2118 } else { 2119 console.info(`SetLaunchAbility successfully`); 2120 } 2121 }); 2122}); 2123``` 2124 2125### dispatchSessionEvent<sup>10+</sup> 2126 2127dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\<void> 2128 2129Dispatches 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. 2130 2131**System capability**: SystemCapability.Multimedia.AVSession.Core 2132 2133**Parameters** 2134 2135| Name | Type | Mandatory| Description | 2136| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 2137| event | string | Yes | Name of the session event.| 2138| args | {[key: string]: any} | Yes | Event content in key-value pair format.| 2139 2140> **NOTE** 2141> 2142> The **args** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want (Want)](./js-apis-app-ability-want.md). 2143 2144**Return value** 2145 2146| Type | Description | 2147| -------------- | ----------------------------- | 2148| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 2149 2150**Error codes** 2151 2152For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2153 2154| ID| Error Message| 2155| -------- | ---------------------------------------- | 2156| 6600101 | Session service exception. | 2157| 6600102 | The session does not exist. | 2158 2159**Example** 2160 2161```ts 2162import avSession from '@ohos.multimedia.avsession'; 2163import { BusinessError } from '@ohos.base'; 2164 2165let currentAVSession: avSession.AVSession | undefined = undefined; 2166let tag = "createNewSession"; 2167let context: Context = getContext(this); 2168 2169avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2170 if (err) { 2171 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2172 } else { 2173 currentAVSession = data; 2174 } 2175}); 2176let eventName = "dynamic_lyric"; 2177if (currentAVSession !== undefined) { 2178 (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}).then(() => { 2179 console.info(`dispatchSessionEvent successfully`); 2180 }).catch((err: BusinessError) => { 2181 console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); 2182 }) 2183} 2184``` 2185 2186### dispatchSessionEvent<sup>10+</sup> 2187 2188dispatchSessionEvent(event: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void 2189 2190Dispatches 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. 2191 2192**System capability**: SystemCapability.Multimedia.AVSession.Core 2193 2194**Parameters** 2195 2196| Name | Type | Mandatory| Description | 2197| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 2198| event | string | Yes | Name of the session event.| 2199| args | {[key: string]: any} | Yes | Event content in key-value pair format.| 2200| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2201 2202> **NOTE** 2203> 2204> The **args** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want (Want)](./js-apis-app-ability-want.md). 2205 2206**Error codes** 2207 2208For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2209 2210| ID| Error Message| 2211| -------- | ---------------------------------------- | 2212| 6600101 | Session service exception. | 2213| 6600102 | The session does not exist. | 2214 2215**Example** 2216 2217```ts 2218import avSession from '@ohos.multimedia.avsession'; 2219import { BusinessError } from '@ohos.base'; 2220 2221let currentAVSession: avSession.AVSession | undefined = undefined; 2222let tag = "createNewSession"; 2223let context: Context = getContext(this); 2224 2225avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2226 if (err) { 2227 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2228 } else { 2229 currentAVSession = data; 2230 } 2231}); 2232let eventName: string = "dynamic_lyric"; 2233if (currentAVSession !== undefined) { 2234 (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}, (err: BusinessError) => { 2235 if(err) { 2236 console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); 2237 } 2238 }) 2239} 2240``` 2241 2242### setAVQueueItems<sup>10+</sup> 2243 2244setAVQueueItems(items: Array\<AVQueueItem>): Promise\<void> 2245 2246Sets a playlist. This API uses a promise to return the result. 2247 2248**System capability**: SystemCapability.Multimedia.AVSession.Core 2249 2250**Parameters** 2251 2252| Name | Type | Mandatory| Description | 2253| ------ | ------------------------------------ | ---- | ---------------------------------- | 2254| items | Array<[AVQueueItem](#avqueueitem10)\> | Yes | Playlist to set.| 2255 2256**Return value** 2257 2258| Type | Description | 2259| -------------- | ----------------------------- | 2260| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 2261 2262**Error codes** 2263 2264For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2265 2266| ID| Error Message| 2267| -------- | ---------------------------------------- | 2268| 6600101 | Session service exception. | 2269| 6600102 | The session does not exist. | 2270 2271**Example** 2272 2273```ts 2274import image from '@ohos.multimedia.image'; 2275import resourceManager from '@ohos.resourceManager'; 2276import { BusinessError } from '@ohos.base'; 2277import avSession from '@ohos.multimedia.avsession'; 2278 2279let value: Uint8Array | undefined = undefined; 2280let imageSource: image.ImageSource | undefined = undefined; 2281resourceManager.getSystemResourceManager().getRawFileContent('IMAGE_URI').then((data) => { 2282 value = data; 2283}); 2284if (value !== undefined) { 2285 imageSource = image.createImageSource((value as Uint8Array).buffer); 2286} 2287let imagePixel: image.PixelMap | undefined = undefined; 2288if (imageSource !== undefined) { 2289 (imageSource as image.ImageSource).createPixelMap({desiredSize:{width: 150, height: 150}}).then((data) => { 2290 imagePixel = data; 2291 }).catch((err: BusinessError) => { 2292 console.error(`createPixelMap BusinessError: code: ${err.code}, message: ${err.message}`); 2293 }) 2294} 2295 2296let queueItemDescription_1: avSession.AVMediaDescription = { 2297 assetId: '001', 2298 title: 'music_name', 2299 subtitle: 'music_sub_name', 2300 description: 'music_description', 2301 mediaImage : imagePixel, 2302 extras: {extras:'any'} 2303}; 2304let queueItem_1: avSession.AVQueueItem = { 2305 itemId: 1, 2306 description: queueItemDescription_1 2307}; 2308let queueItemDescription_2: avSession.AVMediaDescription = { 2309 assetId: '002', 2310 title: 'music_name', 2311 subtitle: 'music_sub_name', 2312 description: 'music_description', 2313 mediaImage: imagePixel, 2314 extras: {extras:'any'} 2315}; 2316let queueItem_2: avSession.AVQueueItem = { 2317 itemId: 2, 2318 description: queueItemDescription_2 2319}; 2320let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; 2321currentAVSession.setAVQueueItems(queueItemsArray).then(() => { 2322 console.info(`SetAVQueueItems successfully`); 2323}).catch((err: BusinessError) => { 2324 console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 2325}); 2326``` 2327 2328### setAVQueueItems<sup>10+</sup> 2329 2330setAVQueueItems(items: Array\<AVQueueItem>, callback: AsyncCallback\<void>): void 2331 2332Sets a playlist. This API uses an asynchronous callback to return the result. 2333 2334**System capability**: SystemCapability.Multimedia.AVSession.Core 2335 2336**Parameters** 2337 2338| Name | Type | Mandatory| Description | 2339| -------- | ------------------------------------ | ---- | ----------------------------------------------------------- | 2340| items | Array<[AVQueueItem](#avqueueitem10)\> | Yes | Playlist to set. | 2341| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2342 2343**Error codes** 2344 2345For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2346 2347| ID| Error Message| 2348| -------- | ---------------------------------------- | 2349| 6600101 | Session service exception. | 2350| 6600102 | The session does not exist. | 2351 2352**Example** 2353 2354```ts 2355import image from '@ohos.multimedia.image'; 2356import resourceManager from '@ohos.resourceManager'; 2357import { BusinessError } from '@ohos.base'; 2358import avSession from '@ohos.multimedia.avsession'; 2359 2360let value: Uint8Array | undefined = undefined; 2361let imageSource: image.ImageSource | undefined = undefined; 2362resourceManager.getSystemResourceManager().getRawFileContent('IMAGE_URI').then((data) => { 2363 value = data; 2364}); 2365if (value !== undefined) { 2366 imageSource = image.createImageSource((value as Uint8Array).buffer); 2367} 2368let imagePixel: image.PixelMap | undefined = undefined; 2369if (imageSource !== undefined) { 2370 (imageSource as image.ImageSource).createPixelMap({desiredSize:{width: 150, height: 150}}).then((data) => { 2371 imagePixel = data; 2372 }).catch((err: BusinessError) => { 2373 console.error(`createPixelMap BusinessError: code: ${err.code}, message: ${err.message}`); 2374 }) 2375} 2376let queueItemDescription_1: avSession.AVMediaDescription = { 2377 assetId: '001', 2378 title: 'music_name', 2379 subtitle: 'music_sub_name', 2380 description: 'music_description', 2381 mediaImage : imagePixel, 2382 extras: {extras:'any'} 2383}; 2384let queueItem_1: avSession.AVQueueItem = { 2385 itemId: 1, 2386 description: queueItemDescription_1 2387}; 2388let queueItemDescription_2: avSession.AVMediaDescription = { 2389 assetId: '002', 2390 title: 'music_name', 2391 subtitle: 'music_sub_name', 2392 description: 'music_description', 2393 mediaImage: imagePixel, 2394 extras: {extras:'any'} 2395}; 2396let queueItem_2: avSession.AVQueueItem = { 2397 itemId: 2, 2398 description: queueItemDescription_2 2399}; 2400let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; 2401currentAVSession.setAVQueueItems(queueItemsArray, (err: BusinessError) => { 2402 if (err) { 2403 console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 2404 } else { 2405 console.info(`SetAVQueueItems successfully`); 2406 } 2407}); 2408``` 2409 2410### setAVQueueTitle<sup>10+</sup> 2411 2412setAVQueueTitle(title: string): Promise\<void> 2413 2414Sets a name for the playlist. This API uses a promise to return the result. 2415 2416**System capability**: SystemCapability.Multimedia.AVSession.Core 2417 2418**Parameters** 2419 2420| Name | Type | Mandatory| Description | 2421| ------ | ------ | ---- | -------------- | 2422| title | string | Yes | Name of the playlist.| 2423 2424**Return value** 2425 2426| Type | Description | 2427| -------------- | ----------------------------- | 2428| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 2429 2430**Error codes** 2431 2432For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2433 2434| ID| Error Message| 2435| -------- | ---------------------------------------- | 2436| 6600101 | Session service exception. | 2437| 6600102 | The session does not exist. | 2438 2439**Example** 2440 2441```ts 2442import { BusinessError } from '@ohos.base'; 2443 2444let queueTitle = 'QUEUE_TITLE'; 2445currentAVSession.setAVQueueTitle(queueTitle).then(() => { 2446 console.info(`SetAVQueueTitle successfully`); 2447}).catch((err: BusinessError) => { 2448 console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 2449}); 2450``` 2451 2452### setAVQueueTitle<sup>10+</sup> 2453 2454setAVQueueTitle(title: string, callback: AsyncCallback\<void>): void 2455 2456Sets a name for the playlist. This API uses an asynchronous callback to return the result. 2457 2458**System capability**: SystemCapability.Multimedia.AVSession.Core 2459 2460**Parameters** 2461 2462| Name | Type | Mandatory| Description | 2463| -------- | --------------------- | ---- | ----------------------------------------------------------- | 2464| title | string | Yes | Name of the playlist. | 2465| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2466 2467**Error codes** 2468 2469For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2470 2471| ID| Error Message| 2472| -------- | ---------------------------------------- | 2473| 6600101 | Session service exception. | 2474| 6600102 | The session does not exist. | 2475 2476**Example** 2477 2478```ts 2479import { BusinessError } from '@ohos.base'; 2480 2481let queueTitle = 'QUEUE_TITLE'; 2482currentAVSession.setAVQueueTitle(queueTitle, (err: BusinessError) => { 2483 if (err) { 2484 console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 2485 } else { 2486 console.error(`SetAVQueueTitle successfully`); 2487 } 2488}); 2489``` 2490 2491### setExtras<sup>10+</sup> 2492 2493setExtras(extras: {[key: string]: Object}): Promise\<void> 2494 2495Sets 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. 2496 2497**System capability**: SystemCapability.Multimedia.AVSession.Core 2498 2499**Parameters** 2500 2501| Name | Type | Mandatory| Description | 2502| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 2503| extras | {[key: string]: Object} | Yes | Key-value pairs of the custom media packet.| 2504 2505> **NOTE** 2506 2507> The **extras** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want (Want)](./js-apis-app-ability-want.md). 2508 2509**Return value** 2510 2511| Type | Description | 2512| -------------- | ----------------------------- | 2513| Promise\<void> | Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.| 2514 2515**Error codes** 2516 2517For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2518 2519| ID| Error Message| 2520| -------- | ---------------------------------------- | 2521| 6600101 | Session service exception. | 2522| 6600102 | The session does not exist. | 2523 2524**Example** 2525 2526```ts 2527import avSession from '@ohos.multimedia.avsession'; 2528import { BusinessError } from '@ohos.base'; 2529 2530let currentAVSession: avSession.AVSession | undefined = undefined; 2531let tag = "createNewSession"; 2532let context: Context = getContext(this); 2533 2534avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2535 if (err) { 2536 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2537 } else { 2538 currentAVSession = data; 2539 } 2540}); 2541if (currentAVSession !== undefined) { 2542 (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}).then(() => { 2543 console.info(`setExtras successfully`); 2544 }).catch((err: BusinessError) => { 2545 console.info(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); 2546 }) 2547} 2548``` 2549 2550### setExtras<sup>10+</sup> 2551 2552setExtras(extras: {[key: string]: Object}, callback: AsyncCallback\<void>): void 2553 2554Sets 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. 2555 2556**System capability**: SystemCapability.Multimedia.AVSession.Core 2557 2558**Parameters** 2559 2560| Name | Type | Mandatory| Description | 2561| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 2562| extras | {[key: string]: any} | Yes | Key-value pairs of the custom media packet.| 2563| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2564 2565> **NOTE** 2566> 2567> The **extras** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want (Want)](./js-apis-app-ability-want.md). 2568 2569**Error codes** 2570 2571For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2572 2573| ID| Error Message| 2574| -------- | ---------------------------------------- | 2575| 6600101 | Session service exception. | 2576| 6600102 | The session does not exist. | 2577 2578**Example** 2579 2580```ts 2581import avSession from '@ohos.multimedia.avsession'; 2582import { BusinessError } from '@ohos.base'; 2583 2584let currentAVSession: avSession.AVSession | undefined = undefined; 2585let tag = "createNewSession"; 2586let context: Context = getContext(this); 2587 2588avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2589 if (err) { 2590 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2591 } else { 2592 currentAVSession = data; 2593 } 2594}); 2595if (currentAVSession !== undefined) { 2596 (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}, (err: BusinessError) => { 2597 if(err) { 2598 console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); 2599 } 2600 }) 2601} 2602``` 2603 2604### getController<sup>10+</sup> 2605 2606getController(): Promise\<AVSessionController> 2607 2608Obtains the controller corresponding to this session. This API uses a promise to return the result. 2609 2610**System capability**: SystemCapability.Multimedia.AVSession.Core 2611 2612**Return value** 2613 2614| Type | Description | 2615| ---------------------------------------------------- | ----------------------------- | 2616| Promise<[AVSessionController](#avsessioncontroller10)> | Promise used to return the session controller.| 2617 2618**Error codes** 2619 2620For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2621 2622| ID| Error Message| 2623| -------- | ---------------------------------------- | 2624| 6600101 | Session service exception. | 2625| 6600102 | The session does not exist. | 2626 2627**Example** 2628 2629```ts 2630import { BusinessError } from '@ohos.base'; 2631 2632let avsessionController: avSession.AVSessionController; 2633currentAVSession.getController().then((avcontroller: avSession.AVSessionController) => { 2634 avsessionController = avcontroller; 2635 console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); 2636}).catch((err: BusinessError) => { 2637 console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); 2638}); 2639``` 2640 2641### getController<sup>10+</sup> 2642 2643getController(callback: AsyncCallback\<AVSessionController>): void 2644 2645Obtains the controller corresponding to this session. This API uses an asynchronous callback to return the result. 2646 2647**System capability**: SystemCapability.Multimedia.AVSession.Core 2648 2649**Parameters** 2650 2651| Name | Type | Mandatory| Description | 2652| -------- | ----------------------------------------------------------- | ---- | -------------------------- | 2653| callback | AsyncCallback<[AVSessionController](#avsessioncontroller10)\> | Yes | Callback used to return the session controller.| 2654 2655**Error codes** 2656 2657For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2658 2659| ID| Error Message| 2660| -------- | ---------------------------------------- | 2661| 6600101 | Session service exception. | 2662| 6600102 | The session does not exist. | 2663 2664**Example** 2665 2666```ts 2667import { BusinessError } from '@ohos.base'; 2668 2669let avsessionController: avSession.AVSessionController; 2670currentAVSession.getController((err: BusinessError, avcontroller: avSession.AVSessionController) => { 2671 if (err) { 2672 console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); 2673 } else { 2674 avsessionController = avcontroller; 2675 console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); 2676 } 2677}); 2678``` 2679 2680### getAVCastController<sup>10+</sup> 2681 2682getAVCastController(callback: AsyncCallback\<AVCastController>): void 2683 2684Obtains the cast controller when a casting connection is set up. This API uses an asynchronous callback to return the result. 2685 2686**System capability**: SystemCapability.Multimedia.AVSession.AVCast 2687 2688**Parameters** 2689 2690| Name | Type | Mandatory| Description | 2691| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2692| callback | AsyncCallback<[AVCastController](#avcastcontroller10)\> | Yes | Callback used to return the cast controller.| 2693 2694**Error codes** 2695 2696For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2697 2698| ID| Error Message | 2699| -------- |---------------------------------------| 2700| 6600102 | The session does not exist. | 2701| 6600110 | The remote connection does not exist. | 2702 2703**Example** 2704 2705```ts 2706import { BusinessError } from '@ohos.base'; 2707 2708let aVCastController: avSession.AVCastController; 2709currentAVSession.getAVCastController().then((avcontroller: avSession.AVCastController) => { 2710 aVCastController = avcontroller; 2711 console.info(`getAVCastController : SUCCESS : sessionid : ${aVCastController.sessionId}`); 2712}).catch((err: BusinessError) => { 2713 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 2714}); 2715``` 2716 2717### getAVCastController<sup>10+</sup> 2718 2719getAVCastController(): Promise\<AVCastController>; 2720 2721Obtains the cast controller when a casting connection is set up. This API uses an asynchronous callback to return the result. 2722 2723**System capability**: SystemCapability.Multimedia.AVSession.AVCast 2724 2725**Return value** 2726 2727| Type | Description | 2728| --------- | ------------------------------------------------------------ | 2729| Promise<[AVCastController](#avcastcontroller10)\> | Promise used to return the cast controller.| 2730 2731**Error codes** 2732 2733For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2734 2735| ID| Error Message| 2736| -------- | --------------------------------------- | 2737| 6600102 | The session does not exist. | 2738| 6600110 | The remote connection does not exist. | 2739 2740**Example** 2741 2742```ts 2743import { BusinessError } from '@ohos.base'; 2744 2745let aVCastController: avSession.AVCastController; 2746currentAVSession.getAVCastController((err: BusinessError, avcontroller: avSession.AVCastController) => { 2747 if (err) { 2748 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 2749 } else { 2750 aVCastController = avcontroller; 2751 console.info(`getAVCastController : SUCCESS : sessionid : ${aVCastController.sessionId}`); 2752 } 2753}); 2754``` 2755 2756### getOutputDevice<sup>10+</sup> 2757 2758getOutputDevice(): Promise\<OutputDeviceInfo> 2759 2760Obtains information about the output device for this session. This API uses a promise to return the result. 2761 2762**System capability**: SystemCapability.Multimedia.AVSession.Core 2763 2764**Return value** 2765 2766| Type | Description | 2767| ---------------------------------------------- | --------------------------------- | 2768| Promise<[OutputDeviceInfo](#outputdeviceinfo10)> | Promise used to return the output device information.| 2769 2770**Error codes** 2771 2772For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2773 2774| ID| Error Message| 2775| -------- | ---------------------------------------- | 2776| 6600101 | Session service exception. | 2777| 6600102 | The session does not exist. | 2778 2779**Example** 2780 2781```ts 2782import { BusinessError } from '@ohos.base'; 2783 2784currentAVSession.getOutputDevice().then((outputDeviceInfo: avSession.OutputDeviceInfo) => { 2785 console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); 2786}).catch((err: BusinessError) => { 2787 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 2788}) 2789``` 2790 2791### getOutputDevice<sup>10+</sup> 2792 2793getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void 2794 2795Obtains information about the output device for this session. This API uses an asynchronous callback to return the result. 2796 2797**System capability**: SystemCapability.Multimedia.AVSession.Core 2798 2799**Parameters** 2800 2801| Name | Type | Mandatory| Description | 2802| -------- | ----------------------------------------------------- | ---- | ------------------------------ | 2803| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | Yes | Callback used to return the information obtained.| 2804 2805**Error codes** 2806 2807For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2808 2809| ID| Error Message| 2810| -------- | ---------------------------------------- | 2811| 6600101 | Session service exception. | 2812| 6600102 | The session does not exist. | 2813 2814**Example** 2815 2816```ts 2817import { BusinessError } from '@ohos.base'; 2818 2819currentAVSession.getOutputDevice((err: BusinessError, outputDeviceInfo: avSession.OutputDeviceInfo) => { 2820 if (err) { 2821 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 2822 } else { 2823 console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); 2824 } 2825}); 2826``` 2827 2828### activate<sup>10+</sup> 2829 2830activate(): Promise\<void> 2831 2832Activates this session. A session can be used only after being activated. This API uses a promise to return the result. 2833 2834**System capability**: SystemCapability.Multimedia.AVSession.Core 2835 2836**Return value** 2837 2838| Type | Description | 2839| -------------- | ----------------------------- | 2840| Promise\<void> | Promise used to return the result. If the session is activated, no value is returned; otherwise, an error object is returned.| 2841 2842**Error codes** 2843 2844For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2845 2846| ID| Error Message| 2847| -------- | ---------------------------------------- | 2848| 6600101 | Session service exception. | 2849| 6600102 | The session does not exist. | 2850 2851**Example** 2852 2853```ts 2854import { BusinessError } from '@ohos.base'; 2855 2856currentAVSession.activate().then(() => { 2857 console.info(`Activate : SUCCESS `); 2858}).catch((err: BusinessError) => { 2859 console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); 2860}); 2861``` 2862 2863### activate<sup>10+</sup> 2864 2865activate(callback: AsyncCallback\<void>): void 2866 2867Activates this session. A session can be used only after being activated. This API uses an asynchronous callback to return the result. 2868 2869**System capability**: SystemCapability.Multimedia.AVSession.Core 2870 2871**Parameters** 2872 2873| Name | Type | Mandatory| Description | 2874| -------- | -------------------- | ---- | ---------- | 2875| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is activated, **err** is **undefined**; otherwise, **err** is an error object.| 2876 2877**Error codes** 2878 2879For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2880 2881| ID| Error Message| 2882| -------- | ---------------------------------------- | 2883| 6600101 | Session service exception. | 2884| 6600102 | The session does not exist. | 2885 2886**Example** 2887 2888```ts 2889import { BusinessError } from '@ohos.base'; 2890 2891currentAVSession.activate((err: BusinessError) => { 2892 if (err) { 2893 console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); 2894 } else { 2895 console.info(`Activate : SUCCESS `); 2896 } 2897}); 2898``` 2899 2900### deactivate<sup>10+</sup> 2901 2902deactivate(): Promise\<void> 2903 2904Deactivates this session. You can use [activate](#activate10) to activate the session again. This API uses a promise to return the result. 2905 2906**System capability**: SystemCapability.Multimedia.AVSession.Core 2907 2908**Return value** 2909 2910| Type | Description | 2911| -------------- | ----------------------------- | 2912| Promise\<void> | Promise used to return the result. If the session is deactivated, no value is returned; otherwise, an error object is returned.| 2913 2914**Error codes** 2915 2916For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2917 2918| ID| Error Message| 2919| -------- | ---------------------------------------- | 2920| 6600101 | Session service exception. | 2921| 6600102 | The session does not exist. | 2922 2923**Example** 2924 2925```ts 2926import { BusinessError } from '@ohos.base'; 2927 2928currentAVSession.deactivate().then(() => { 2929 console.info(`Deactivate : SUCCESS `); 2930}).catch((err: BusinessError) => { 2931 console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); 2932}); 2933``` 2934 2935### deactivate<sup>10+</sup> 2936 2937deactivate(callback: AsyncCallback\<void>): void 2938 2939Deactivates this session. This API uses an asynchronous callback to return the result. 2940 2941Deactivates this session. You can use [activate](#activate10) to activate the session again. 2942 2943**System capability**: SystemCapability.Multimedia.AVSession.Core 2944 2945**Parameters** 2946 2947| Name | Type | Mandatory| Description | 2948| -------- | -------------------- | ---- | ---------- | 2949| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is deactivated, **err** is **undefined**; otherwise, **err** is an error object.| 2950 2951**Error codes** 2952 2953For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2954 2955| ID| Error Message| 2956| -------- | ---------------------------------------- | 2957| 6600101 | Session service exception. | 2958| 6600102 | The session does not exist. | 2959 2960**Example** 2961 2962```ts 2963import { BusinessError } from '@ohos.base'; 2964 2965currentAVSession.deactivate((err: BusinessError) => { 2966 if (err) { 2967 console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); 2968 } else { 2969 console.info(`Deactivate : SUCCESS `); 2970 } 2971}); 2972``` 2973 2974### destroy<sup>10+</sup> 2975 2976destroy(): Promise\<void> 2977 2978Destroys this session. This API uses a promise to return the result. 2979 2980**System capability**: SystemCapability.Multimedia.AVSession.Core 2981 2982**Return value** 2983 2984| Type | Description | 2985| -------------- | ----------------------------- | 2986| Promise\<void> | Promise used to return the result. If the session is destroyed, no value is returned; otherwise, an error object is returned.| 2987 2988**Error codes** 2989 2990For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 2991 2992| ID| Error Message| 2993| -------- | ---------------------------------------- | 2994| 6600101 | Session service exception. | 2995| 6600102 | The session does not exist. | 2996 2997**Example** 2998 2999```ts 3000import { BusinessError } from '@ohos.base'; 3001 3002currentAVSession.destroy().then(() => { 3003 console.info(`Destroy : SUCCESS `); 3004}).catch((err: BusinessError) => { 3005 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 3006}); 3007``` 3008 3009### destroy<sup>10+</sup> 3010 3011destroy(callback: AsyncCallback\<void>): void 3012 3013Destroys this session. This API uses an asynchronous callback to return the result. 3014 3015**System capability**: SystemCapability.Multimedia.AVSession.Core 3016 3017**Parameters** 3018 3019| Name | Type | Mandatory| Description | 3020| -------- | -------------------- | ---- | ---------- | 3021| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the session is destroyed, **err** is **undefined**; otherwise, **err** is an error object.| 3022 3023**Error codes** 3024 3025For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3026 3027| ID| Error Message| 3028| -------- | ---------------------------------------- | 3029| 6600101 | Session service exception. | 3030| 6600102 | The session does not exist. | 3031 3032**Example** 3033 3034```ts 3035import { BusinessError } from '@ohos.base'; 3036 3037currentAVSession.destroy((err: BusinessError) => { 3038 if (err) { 3039 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 3040 } else { 3041 console.info(`Destroy : SUCCESS `); 3042 } 3043}); 3044``` 3045 3046### on('play')<sup>10+</sup> 3047 3048on(type: 'play', callback: () => void): void 3049 3050Subscribes to playback started events. 3051 3052**System capability**: SystemCapability.Multimedia.AVSession.Core 3053 3054**Parameters** 3055 3056| Name | Type | Mandatory| Description | 3057| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3058| type | string | Yes | Event type. The event **'play'** is triggered when the command for starting playback is sent to the session.| 3059| callback | callback: () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 3060 3061**Error codes** 3062 3063For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3064 3065| ID| Error Message| 3066| -------- | ---------------------------------------- | 3067| 6600101 | Session service exception. | 3068| 6600102 | The session does not exist. | 3069 3070**Example** 3071 3072```ts 3073currentAVSession.on('play', () => { 3074 console.info(`on play entry`); 3075}); 3076``` 3077 3078### on('pause')<sup>10+</sup> 3079 3080on(type: 'pause', callback: () => void): void 3081 3082Subscribes to playback paused events. 3083 3084**System capability**: SystemCapability.Multimedia.AVSession.Core 3085 3086**Parameters** 3087 3088| Name | Type | Mandatory| Description | 3089| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3090| type | string | Yes | Event type. The event **'pause'** is triggered when the command for pausing the playback is sent to the session.| 3091| callback | callback: () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 3092 3093**Error codes** 3094 3095For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3096 3097| ID| Error Message| 3098| -------- | ---------------------------------------- | 3099| 6600101 | Session service exception. | 3100| 6600102 | The session does not exist. | 3101 3102**Example** 3103 3104```ts 3105currentAVSession.on('pause', () => { 3106 console.info(`on pause entry`); 3107}); 3108``` 3109 3110### on('stop')<sup>10+</sup> 3111 3112on(type:'stop', callback: () => void): void 3113 3114Subscribes to playback stopped events. 3115 3116**System capability**: SystemCapability.Multimedia.AVSession.Core 3117 3118**Parameters** 3119 3120| Name | Type | Mandatory| Description | 3121| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3122| type | string | Yes | Event type. The event **'stop'** is triggered when the command for stopping the playback is sent to the session.| 3123| callback | callback: () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 3124 3125**Error codes** 3126 3127For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3128 3129| ID| Error Message| 3130| -------- | ---------------------------------------- | 3131| 6600101 | Session service exception. | 3132| 6600102 | The session does not exist. | 3133 3134**Example** 3135 3136```ts 3137currentAVSession.on('stop', () => { 3138 console.info(`on stop entry`); 3139}); 3140``` 3141 3142### on('playNext')<sup>10+</sup> 3143 3144on(type:'playNext', callback: () => void): void 3145 3146Subscribes to playNext command events. 3147 3148**System capability**: SystemCapability.Multimedia.AVSession.Core 3149 3150**Parameters** 3151 3152| Name | Type | Mandatory| Description | 3153| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3154| type | string | Yes | Event type. The event **'playNext'** is triggered when the command for playing the next item is sent to the session.| 3155| callback | callback: () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 3156 3157**Error codes** 3158 3159For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3160 3161| ID| Error Message| 3162| -------- | ---------------------------------------- | 3163| 6600101 | Session service exception. | 3164| 6600102 | The session does not exist. | 3165 3166**Example** 3167 3168```ts 3169currentAVSession.on('playNext', () => { 3170 console.info(`on playNext entry`); 3171}); 3172``` 3173 3174### on('playPrevious')<sup>10+</sup> 3175 3176on(type:'playPrevious', callback: () => void): void 3177 3178Subscribes to playPrevious command events. 3179 3180**System capability**: SystemCapability.Multimedia.AVSession.Core 3181 3182**Parameters** 3183 3184| Name | Type | Mandatory| Description | 3185| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3186| type | string | Yes | Event type. The event **'playPrevious'** is triggered when the command for playing the previous item sent to the session.| 3187| callback | callback: () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 3188 3189**Error codes** 3190 3191For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3192 3193| ID| Error Message| 3194| -------- | ---------------------------------------- | 3195| 6600101 | Session service exception. | 3196| 6600102 | The session does not exist. | 3197 3198**Example** 3199 3200```ts 3201currentAVSession.on('playPrevious', () => { 3202 console.info(`on playPrevious entry`); 3203}); 3204``` 3205 3206### on('fastForward')<sup>10+</sup> 3207 3208on(type: 'fastForward', callback: () => void): void 3209 3210Subscribes to fastForward command events. 3211 3212**System capability**: SystemCapability.Multimedia.AVSession.Core 3213 3214**Parameters** 3215 3216| Name | Type | Mandatory| Description | 3217| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3218| type | string | Yes | Event type. The event **'fastForward'** is triggered when the command for fast forwarding is sent to the session.| 3219| callback | callback: () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 3220 3221**Error codes** 3222 3223For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3224 3225| ID| Error Message| 3226| -------- | ---------------------------------------- | 3227| 6600101 | Session service exception. | 3228| 6600102 | The session does not exist. | 3229 3230**Example** 3231 3232```ts 3233currentAVSession.on('fastForward', () => { 3234 console.info(`on fastForward entry`); 3235}); 3236``` 3237 3238### on('rewind')<sup>10+</sup> 3239 3240on(type:'rewind', callback: () => void): void 3241 3242Subscribes to rewind command events. 3243 3244**System capability**: SystemCapability.Multimedia.AVSession.Core 3245 3246**Parameters** 3247 3248| Name | Type | Mandatory| Description | 3249| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3250| type | string | Yes | Event type. The event **'rewind'** is triggered when the command for rewinding is sent to the session.| 3251| callback | callback: () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 3252 3253**Error codes** 3254 3255For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3256 3257| ID| Error Message| 3258| -------- | ---------------------------------------- | 3259| 6600101 | Session service exception. | 3260| 6600102 | The session does not exist. | 3261 3262**Example** 3263 3264```ts 3265currentAVSession.on('rewind', () => { 3266 console.info(`on rewind entry`); 3267}); 3268``` 3269 3270### on('seek')<sup>10+</sup> 3271 3272on(type: 'seek', callback: (time: number) => void): void 3273 3274Subscribes to seek command events. 3275 3276**System capability**: SystemCapability.Multimedia.AVSession.Core 3277 3278**Parameters** 3279 3280| Name | Type | Mandatory| Description | 3281| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 3282| type | string | Yes | Event type. The event **'seek'** is triggered when the seek command is sent to the session.| 3283| callback | (time: number) => void | Yes | Callback used for subscription. The **time** parameter in the callback indicates the time to seek to, in milliseconds. | 3284 3285**Error codes** 3286 3287For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3288 3289| ID| Error Message| 3290| -------- | ---------------------------------------- | 3291| 6600101 | Session service exception. | 3292| 6600102 | The session does not exist. | 3293 3294**Example** 3295 3296```ts 3297currentAVSession.on('seek', (time: number) => { 3298 console.info(`on seek entry time : ${time}`); 3299}); 3300``` 3301 3302### on('setSpeed')<sup>10+</sup> 3303 3304on(type: 'setSpeed', callback: (speed: number) => void): void 3305 3306Subscribes to setSpeed command events. 3307 3308**System capability**: SystemCapability.Multimedia.AVSession.Core 3309 3310**Parameters** 3311 3312| Name | Type | Mandatory| Description | 3313| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 3314| type | string | Yes | Event type. The event **'setSpeed'** is triggered when the command for setting the playback speed is sent to the session.| 3315| callback | (speed: number) => void | Yes | Callback used for subscription. The **speed** parameter in the callback indicates the playback speed. | 3316 3317**Error codes** 3318 3319For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3320 3321| ID| Error Message| 3322| -------- | ---------------------------------------- | 3323| 6600101 | Session service exception. | 3324| 6600102 | The session does not exist. | 3325 3326**Example** 3327 3328```ts 3329currentAVSession.on('setSpeed', (speed: number) => { 3330 console.info(`on setSpeed speed : ${speed}`); 3331}); 3332``` 3333 3334### on('setLoopMode')<sup>10+</sup> 3335 3336on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void 3337 3338Subscribes to setLoopMode command events. 3339 3340**System capability**: SystemCapability.Multimedia.AVSession.Core 3341 3342**Parameters** 3343 3344| Name | Type | Mandatory| Description | 3345| -------- | ------------------------------------- | ---- | ---- | 3346| type | string | Yes | Event type. The event **'setLoopMode'** is triggered when the command for setting the loop mode is sent to the session.| 3347| callback | (mode: [LoopMode](#loopmode10)) => void | Yes | Callback used for subscription. The **mode** parameter in the callback indicates the loop mode. | 3348 3349**Error codes** 3350 3351For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3352 3353| ID| Error Message| 3354| -------- | ---------------------------------------- | 3355| 6600101 | Session service exception. | 3356| 6600102 | The session does not exist. | 3357 3358**Example** 3359 3360```ts 3361currentAVSession.on('setLoopMode', (mode: avSession.LoopMode) => { 3362 console.info(`on setLoopMode mode : ${mode}`); 3363}); 3364``` 3365 3366### on('toggleFavorite')<sup>10+</sup> 3367 3368on(type: 'toggleFavorite', callback: (assetId: string) => void): void 3369 3370Subscribes to toggleFavorite command events. 3371 3372**System capability**: SystemCapability.Multimedia.AVSession.Core 3373 3374**Parameters** 3375 3376| Name | Type | Mandatory| Description | 3377| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 3378| type | string | Yes | Event type. The event **'toggleFavorite'** is triggered when the command for favoriting the media asset is sent to the session.| 3379| callback | (assetId: string) => void | Yes | Callback used for subscription. The **assetId** parameter in the callback indicates the media asset ID. | 3380 3381**Error codes** 3382 3383For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3384 3385| ID| Error Message| 3386| -------- | ---------------------------------------- | 3387| 6600101 | Session service exception. | 3388| 6600102 | The session does not exist. | 3389 3390**Example** 3391 3392```ts 3393currentAVSession.on('toggleFavorite', (assetId: string) => { 3394 console.info(`on toggleFavorite mode : ${assetId}`); 3395}); 3396``` 3397 3398### on('skipToQueueItem')<sup>10+</sup> 3399 3400on(type: 'skipToQueueItem', callback: (itemId: number) => void): void 3401 3402Subscribes to the event that indicates an item in the playlist is selected. The session can play the selected item. 3403 3404**System capability**: SystemCapability.Multimedia.AVSession.Core 3405 3406**Parameters** 3407 3408| Name | Type | Mandatory| Description | 3409| -------- | ------------------------ | ---- | ---------------------------------------------------------------------------------------- | 3410| type | string | Yes | Event type. The event **'skipToQueueItem'** is triggered when an item in the playlist is selected.| 3411| callback | (itemId: number) => void | Yes | Callback used for subscription. The **itemId** parameter in the callback indicates the ID of the selected item. | 3412 3413**Error codes** 3414 3415For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3416 3417| ID| Error Message| 3418| -------- | ---------------------------------------- | 3419| 6600101 | Session service exception. | 3420| 6600102 | The session does not exist. | 3421 3422**Example** 3423 3424```ts 3425currentAVSession.on('skipToQueueItem', (itemId: number) => { 3426 console.info(`on skipToQueueItem id : ${itemId}`); 3427}); 3428``` 3429 3430### on('handleKeyEvent')<sup>10+</sup> 3431 3432on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void 3433 3434Subscribes to key events. 3435 3436**System capability**: SystemCapability.Multimedia.AVSession.Core 3437 3438**Parameters** 3439 3440| Name | Type | Mandatory| Description | 3441| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3442| type | string | Yes | Event type. The event **'handleKeyEvent'** is triggered when a key event is sent to the session.| 3443| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | Yes | Callback used for subscription. The **event** parameter in the callback indicates the key event. | 3444 3445**Error codes** 3446 3447For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3448 3449| ID| Error Message| 3450| -------- | ---------------------------------------- | 3451| 6600101 | Session service exception. | 3452| 6600102 | The session does not exist. | 3453 3454**Example** 3455 3456```ts 3457import keyEvent from '@ohos.multimodalInput.keyEvent'; 3458 3459currentAVSession.on('handleKeyEvent', (event: keyEvent.KeyEvent) => { 3460 console.info(`on handleKeyEvent event : ${event}`); 3461}); 3462 3463``` 3464 3465### on('outputDeviceChange')<sup>10+</sup> 3466 3467on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void 3468 3469Subscribes to output device change events. 3470 3471**System capability**: SystemCapability.Multimedia.AVSession.Core 3472 3473**Parameters** 3474 3475| Name | Type | Mandatory| Description | 3476| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3477| type | string | Yes | Event type. The event **'outputDeviceChange'** is triggered when the output device changes.| 3478| 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. | 3479 3480**Error codes** 3481 3482For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3483 3484| ID| Error Message| 3485| -------- | ---------------------------------------- | 3486| 6600101 | Session service exception. | 3487| 6600102 | The session does not exist. | 3488 3489**Example** 3490 3491```ts 3492currentAVSession.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { 3493 console.info(`on outputDeviceChange device : ${device}`); 3494}); 3495``` 3496 3497### on('commonCommand')<sup>10+</sup> 3498 3499on(type: 'commonCommand', callback: (command: string, args: {[key: string]: Object}) => void): void 3500 3501Subscribes to custom control command change events. 3502 3503**System capability**: SystemCapability.Multimedia.AVSession.Core 3504 3505**Parameters** 3506 3507| Name | Type | Mandatory| Description | 3508| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3509| type | string | Yes | Event type. The event **'commonCommand'** is triggered when a custom control command changes.| 3510| callback | (commonCommand: string, args: {[key:string]: Object}) => void | Yes | Callback used for subscription. The **commonCommand** 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 **sendCommand**. | 3511 3512**Error codes** 3513 3514For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3515 3516| ID| Error Message| 3517| -------- | ------------------------------ | 3518| 6600101 | Session service exception. | 3519| 6600102 | The session does not exist. | 3520 3521**Example** 3522 3523```ts 3524import { BusinessError } from '@ohos.base'; 3525import avSession from '@ohos.multimedia.avsession'; 3526let currentAVSession: avSession.AVSession | undefined = undefined; 3527let tag = "createNewSession"; 3528let context: Context = getContext(this); 3529 3530avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 3531 if (err) { 3532 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 3533 } else { 3534 currentAVSession = data; 3535 } 3536}); 3537if (currentAVSession !== undefined) { 3538 (currentAVSession as avSession.AVSession).on('commonCommand', (commonCommand, args) => { 3539 console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`); 3540 }); 3541} 3542``` 3543 3544### off('play')<sup>10+</sup> 3545 3546off(type: 'play', callback?: () => void): void 3547 3548Unsubscribes from playback started events. 3549 3550**System capability**: SystemCapability.Multimedia.AVSession.Core 3551 3552**Parameters** 3553 3554| Name | Type | Mandatory| Description | 3555| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3556| type | string | Yes | Event type, which is **'play'** in this case.| 3557| 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. | 3558 3559**Error codes** 3560 3561For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3562 3563| ID| Error Message| 3564| -------- | ---------------------------------------- | 3565| 6600101 | Session service exception. | 3566| 6600102 | The session does not exist. | 3567 3568**Example** 3569 3570```ts 3571currentAVSession.off('play'); 3572``` 3573 3574### off('pause')<sup>10+</sup> 3575 3576off(type: 'pause', callback?: () => void): void 3577 3578Unsubscribes from playback paused events. 3579 3580**System capability**: SystemCapability.Multimedia.AVSession.Core 3581 3582**Parameters** 3583 3584| Name | Type | Mandatory| Description | 3585| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3586| type | string | Yes | Event type, which is **'pause'** in this case.| 3587| 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.| 3588 3589**Error codes** 3590 3591For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3592 3593| ID| Error Message| 3594| -------- | ---------------------------------------- | 3595| 6600101 | Session service exception. | 3596| 6600102 | The session does not exist. | 3597 3598**Example** 3599 3600```ts 3601currentAVSession.off('pause'); 3602``` 3603 3604### off('stop')<sup>10+</sup> 3605 3606off(type: 'stop', callback?: () => void): void 3607 3608Unsubscribes from playback stopped events. 3609 3610**System capability**: SystemCapability.Multimedia.AVSession.Core 3611 3612**Parameters** 3613 3614| Name | Type | Mandatory| Description | 3615| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3616| type | string | Yes | Event type, which is **'stop'** in this case.| 3617| 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. | 3618 3619**Error codes** 3620 3621For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3622 3623| ID| Error Message| 3624| -------- | ---------------------------------------- | 3625| 6600101 | Session service exception. | 3626| 6600102 | The session does not exist. | 3627 3628**Example** 3629 3630```ts 3631currentAVSession.off('stop'); 3632``` 3633 3634### off('playNext')<sup>10+</sup> 3635 3636off(type: 'playNext', callback?: () => void): void 3637 3638Unsubscribes from playNext command events. 3639 3640**System capability**: SystemCapability.Multimedia.AVSession.Core 3641 3642**Parameters** 3643 3644| Name | Type | Mandatory| Description | 3645| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3646| type | string | Yes | Event type, which is **'playNext'** in this case.| 3647| 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. | 3648 3649**Error codes** 3650 3651For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3652 3653| ID| Error Message| 3654| -------- | ---------------------------------------- | 3655| 6600101 | Session service exception. | 3656| 6600102 | The session does not exist. | 3657 3658**Example** 3659 3660```ts 3661currentAVSession.off('playNext'); 3662``` 3663 3664### off('playPrevious')<sup>10+</sup> 3665 3666off(type: 'playPrevious', callback?: () => void): void 3667 3668Unsubscribes from playPrevious command events. 3669 3670**System capability**: SystemCapability.Multimedia.AVSession.Core 3671 3672**Parameters** 3673 3674| Name | Type | Mandatory| Description | 3675| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3676| type | string | Yes | Event type, which is **'playPrevious'** in this case.| 3677| 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. | 3678 3679**Error codes** 3680 3681For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3682 3683| ID| Error Message| 3684| -------- | ---------------------------------------- | 3685| 6600101 | Session service exception. | 3686| 6600102 | The session does not exist. | 3687 3688**Example** 3689 3690```ts 3691currentAVSession.off('playPrevious'); 3692``` 3693 3694### off('fastForward')<sup>10+</sup> 3695 3696off(type: 'fastForward', callback?: () => void): void 3697 3698Unsubscribes from fastForward command events. 3699 3700**System capability**: SystemCapability.Multimedia.AVSession.Core 3701 3702**Parameters** 3703 3704| Name | Type | Mandatory| Description | 3705| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3706| type | string | Yes | Event type, which is **'fastForward'** in this case.| 3707| 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. | 3708 3709**Error codes** 3710 3711For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3712 3713| ID| Error Message| 3714| -------- | ---------------------------------------- | 3715| 6600101 | Session service exception. | 3716| 6600102 | The session does not exist. | 3717 3718**Example** 3719 3720```ts 3721currentAVSession.off('fastForward'); 3722``` 3723 3724### off('rewind')<sup>10+</sup> 3725 3726off(type: 'rewind', callback?: () => void): void 3727 3728Unsubscribes from rewind command events. 3729 3730**System capability**: SystemCapability.Multimedia.AVSession.Core 3731 3732**Parameters** 3733 3734| Name | Type | Mandatory| Description | 3735| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3736| type | string | Yes | Event type, which is **'rewind'** in this case.| 3737| 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. | 3738 3739**Error codes** 3740 3741For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3742 3743| ID| Error Message| 3744| -------- | ---------------------------------------- | 3745| 6600101 | Session service exception. | 3746| 6600102 | The session does not exist. | 3747 3748**Example** 3749 3750```ts 3751currentAVSession.off('rewind'); 3752``` 3753 3754### off('seek')<sup>10+</sup> 3755 3756off(type: 'seek', callback?: (time: number) => void): void 3757 3758Unsubscribes from seek command events. 3759 3760**System capability**: SystemCapability.Multimedia.AVSession.Core 3761 3762**Parameters** 3763 3764| Name | Type | Mandatory| Description | 3765| -------- | ---------------------- | ---- | ----------------------------------------- | 3766| type | string | Yes | Event type, which is **'seek'** in this case. | 3767| 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. | 3768 3769**Error codes** 3770 3771For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3772 3773| ID| Error Message| 3774| -------- | ---------------------------------------- | 3775| 6600101 | Session service exception. | 3776| 6600102 | The session does not exist. | 3777 3778**Example** 3779 3780```ts 3781currentAVSession.off('seek'); 3782``` 3783 3784### off('setSpeed')<sup>10+</sup> 3785 3786off(type: 'setSpeed', callback?: (speed: number) => void): void 3787 3788Unsubscribes from setSpeed command events. 3789 3790**System capability**: SystemCapability.Multimedia.AVSession.Core 3791 3792**Parameters** 3793 3794| Name | Type | Mandatory| Description | 3795| -------- | ----------------------- | ---- | -------------------------------------------| 3796| type | string | Yes | Event type, which is **'setSpeed'** in this case. | 3797| 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. | 3798 3799**Error codes** 3800 3801For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3802 3803| ID| Error Message| 3804| -------- | ---------------------------------------- | 3805| 6600101 | Session service exception. | 3806| 6600102 | The session does not exist. | 3807 3808**Example** 3809 3810```ts 3811currentAVSession.off('setSpeed'); 3812``` 3813 3814### off('setLoopMode')<sup>10+</sup> 3815 3816off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void 3817 3818Unsubscribes from setSpeed command events. 3819 3820**System capability**: SystemCapability.Multimedia.AVSession.Core 3821 3822**Parameters** 3823 3824| Name | Type | Mandatory| Description | 3825| -------- | ------------------------------------- | ---- | ----- | 3826| type | string | Yes | Event type, which is **'setLoopMode'** in this case.| 3827| 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.| 3828 3829**Error codes** 3830 3831For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3832 3833| ID| Error Message| 3834| -------- | ---------------------------------------- | 3835| 6600101 | Session service exception. | 3836| 6600102 | The session does not exist. | 3837 3838**Example** 3839 3840```ts 3841currentAVSession.off('setLoopMode'); 3842``` 3843 3844### off('toggleFavorite')<sup>10+</sup> 3845 3846off(type: 'toggleFavorite', callback?: (assetId: string) => void): void 3847 3848Unsubscribes from toggleFavorite command events. 3849 3850**System capability**: SystemCapability.Multimedia.AVSession.Core 3851 3852**Parameters** 3853 3854| Name | Type | Mandatory| Description | 3855| -------- | ------------------------- | ---- | -------------------------------------------------------- | 3856| type | string | Yes | Event type, which is **'toggleFavorite'** in this case. | 3857| 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. | 3858 3859**Error codes** 3860 3861For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3862 3863| ID| Error Message| 3864| -------- | ---------------------------------------- | 3865| 6600101 | Session service exception. | 3866| 6600102 | The session does not exist. | 3867 3868**Example** 3869 3870```ts 3871currentAVSession.off('toggleFavorite'); 3872``` 3873 3874### off('skipToQueueItem')<sup>10+</sup> 3875 3876off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void 3877 3878Unsubscribes from the event that indicates an item in the playlist is selected. 3879 3880**System capability**: SystemCapability.Multimedia.AVSession.Core 3881 3882**Parameters** 3883 3884| Name | Type | Mandatory| Description | 3885| -------- | ------------------------ | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | 3886| type | string | Yes | Event type, which is **'skipToQueueItem'** in this case. | 3887| 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.| 3888 3889**Error codes** 3890 3891For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3892 3893| ID| Error Message| 3894| -------- | ---------------------------------------- | 3895| 6600101 | Session service exception. | 3896| 6600102 | The session does not exist. | 3897 3898**Example** 3899 3900```ts 3901currentAVSession.off('skipToQueueItem'); 3902``` 3903 3904### off('handleKeyEvent')<sup>10+</sup> 3905 3906off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void 3907 3908Unsubscribes from key events. 3909 3910**System capability**: SystemCapability.Multimedia.AVSession.Core 3911 3912**Parameters** 3913 3914| Name | Type | Mandatory| Description | 3915| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3916| type | string | Yes | Event type, which is **'handleKeyEvent'** in this case. | 3917| callback | (event: [KeyEvent](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. | 3918 3919**Error codes** 3920 3921For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3922 3923| ID| Error Message| 3924| -------- | ---------------------------------------- | 3925| 6600101 | Session service exception. | 3926| 6600102 | The session does not exist. | 3927 3928**Example** 3929 3930```ts 3931currentAVSession.off('handleKeyEvent'); 3932``` 3933 3934### off('outputDeviceChange')<sup>10+</sup> 3935 3936off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void 3937 3938Unsubscribes from playback device change events. 3939 3940**System capability**: SystemCapability.Multimedia.AVSession.Core 3941 3942**Parameters** 3943 3944| Name | Type | Mandatory| Description | 3945| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | 3946| type | string | Yes | Event type, which is **'outputDeviceChange'** in this case. | 3947| 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. | 3948 3949**Error codes** 3950 3951For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3952 3953| ID| Error Message| 3954| -------- | ---------------------------------------- | 3955| 6600101 | Session service exception. | 3956| 6600102 | The session does not exist. | 3957 3958**Example** 3959 3960```ts 3961currentAVSession.off('outputDeviceChange'); 3962``` 3963 3964 3965### off('commonCommand')<sup>10+</sup> 3966 3967off(type: 'commonCommand', callback?: (command: string, args: {[key:string]: Object}) => void): void 3968 3969Unsubscribes from custom control command change events. 3970 3971**System capability**: SystemCapability.Multimedia.AVSession.Core 3972 3973**Parameters** 3974 3975| Name | Type | Mandatory| Description | 3976| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 3977| type | string | Yes | Event type, which is **'commonCommand'** in this case. | 3978| 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. | 3979 3980**Error codes** 3981 3982For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 3983 3984| ID| Error Message| 3985| -------- | ---------------- | 3986| 6600101 | Session service exception. | 3987| 6600102 | The session does not exist. | 3988 3989**Example** 3990 3991```ts 3992currentAVSession.off('commonCommand'); 3993``` 3994 3995### stopCasting<sup>10+</sup> 3996 3997stopCasting(callback: AsyncCallback\<void>): void 3998 3999Stops castings. This API uses an asynchronous callback to return the result. 4000 4001**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4002 4003**Parameters** 4004 4005| Name | Type | Mandatory| Description | 4006| -------- | ------------------------------------- | ---- | ------------------------------------- | 4007| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object.| 4008 4009**Error codes** 4010 4011For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4012 4013| ID| Error Message| 4014| -------- | ---------------------------------------- | 4015| 6600109 | The remote connection is not established. | 4016 4017**Example** 4018 4019```ts 4020import { BusinessError } from '@ohos.base'; 4021 4022currentAVSession.stopCasting((err: BusinessError) => { 4023 if (err) { 4024 console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 4025 } else { 4026 console.info(`stopCasting successfully`); 4027 } 4028}); 4029``` 4030 4031### stopCasting<sup>10+</sup> 4032 4033stopCasting(): Promise\<void> 4034 4035Stops castings. This API uses a promise to return the result. 4036 4037**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4038 4039**Return value** 4040 4041| Type | Description | 4042| -------------- | ----------------------------- | 4043| Promise\<void> | Promise used to return the result. If casting stops, no value is returned; otherwise, an error object is returned.| 4044 4045**Error codes** 4046 4047For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4048 4049| ID| Error Message| 4050| -------- | ---------------------------------------- | 4051| 6600109 | The remote connection is not established. | 4052 4053**Example** 4054 4055```ts 4056import { BusinessError } from '@ohos.base'; 4057 4058currentAVSession.stopCasting().then(() => { 4059 console.info(`stopCasting successfully`); 4060}).catch((err: BusinessError) => { 4061 console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 4062}); 4063``` 4064 4065## AVCastControlCommandType<sup>10+</sup> 4066 4067Enumerates the commands that can be sent by a cast controller. 4068 4069**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4070 4071| Name | Type | Description | 4072| -------------- | ------ | ------------ | 4073| play | string | Play the media. | 4074| pause | string | Pause the playback. | 4075| stop | string | Stop the playback. | 4076| playNext | string | Play the next media asset. | 4077| playPrevious | string | Play the previous media asset. | 4078| fastForward | string | Fast-forward. | 4079| rewind | string | Rewind. | 4080| seek | numbder | Seek to a playback position.| 4081| setSpeed | number | Set the playback speed.| 4082| setLoopMode | string | Set the loop mode.| 4083| toggleFavorite | string | Favorite the media asset. | 4084| setVolume | number | Set the volume. | 4085 4086## AVCastControlCommand<sup>10+</sup> 4087 4088Defines the command that can be sent by a cast controller. 4089 4090**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4091 4092| Name | Type | Mandatory| Description | 4093| --------- | ------------------------------------------------- | ---- | -------------- | 4094| command | [AVCastControlCommandType](#avcastcontrolcommandtype10) | Yes | Command. | 4095| parameter | [LoopMode](#loopmode10) | string | number | No | Parameters carried in the command.| 4096 4097## AVCastController<sup>10+</sup> 4098 4099After 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. 4100 4101### setDisplaySurface<sup>10+</sup> 4102 4103setDisplaySurface(surfaceId: string): Promise\<void> 4104 4105Sets the surface ID for playback, which is used at the cast receiver (sink). This API uses a promise to return the result. 4106 4107**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4108 4109**System API**: This is a system API. 4110 4111**Return value** 4112 4113| Type | Description | 4114| --------------------------------------------- | --------------------------- | 4115| Promise\<void> | Promise used to return the result.| 4116 4117**Error codes** 4118 4119For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4120 4121| ID| Error Message| 4122| -------- | ---------------------------------------- | 4123| 6600109 | The remote connection is not established. | 4124 4125**Example** 4126 4127```ts 4128aVCastController.setDisplaySurface().then(() => { 4129 console.info(`setDisplaySurface : SUCCESS`); 4130}); 4131``` 4132 4133### setDisplaySurface<sup>10+</sup> 4134 4135setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void 4136 4137Sets the surface ID for playback, which is used at the cast receiver (sink). This API uses an asynchronous callback to return the result. 4138 4139**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4140 4141**System API**: This is a system API. 4142 4143**Parameters** 4144 4145| Name | Type | Mandatory| Description | 4146| -------- | --------------------------------------------------- | ---- | ---------------------------- | 4147| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4148 4149**Error codes** 4150 4151For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4152 4153| ID| Error Message| 4154| -------- | ---------------------------------------- | 4155| 6600109 | The remote connection is not established. | 4156 4157**Example** 4158 4159```ts 4160import { BusinessError } from '@ohos.base'; 4161 4162aVCastController.setDisplaySurface((err: BusinessError) => { 4163 if (err) { 4164 console.info(`setDisplaySurface BusinessError: code: ${err.code}, message: ${err.message}`); 4165 } else { 4166 console.info(`setDisplaySurface : SUCCESS`); 4167 } 4168}); 4169``` 4170 4171### getAVPlaybackState<sup>10+</sup> 4172 4173getAVPlaybackState(callback: AsyncCallback\<AVPlaybackState>): void 4174 4175Obtains the remote playback state. This API uses an asynchronous callback to return the result. 4176 4177**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4178 4179**Parameters** 4180 4181| Name | Type | Mandatory| Description | 4182| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4183| callback | AsyncCallback<[[AVPlaybackState](#avplaybackstate10)\> | Yes | Callback used to return the remote playback state.| 4184 4185**Error codes** 4186 4187For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4188 4189| ID| Error Message| 4190| -------- | ---------------------------------------- | 4191| 6600101 | Session service exception | 4192 4193**Example** 4194 4195```ts 4196import { BusinessError } from '@ohos.base'; 4197 4198aVCastController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { 4199 if (err) { 4200 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 4201 } else { 4202 console.info(`getAVPlaybackState : SUCCESS`); 4203 } 4204}); 4205``` 4206 4207### getAVPlaybackState<sup>10+</sup> 4208 4209getAVPlaybackState(): Promise\<AVPlaybackState>; 4210 4211Obtains the remote playback state. This API uses a promise to return the result. 4212 4213**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4214 4215**Return value** 4216 4217| Type | Description | 4218| --------- | ------------------------------------------------------------ | 4219| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise used to return the remote playback state.| 4220 4221**Error codes** 4222 4223For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4224 4225| ID| Error Message| 4226| -------- | ---------------------------------------- | 4227| 6600101 | Session service exception | 4228 4229**Example** 4230 4231```ts 4232import { BusinessError } from '@ohos.base'; 4233 4234aVCastController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { 4235 console.info(`getAVPlaybackState : SUCCESS`); 4236}).catch((err: BusinessError) => { 4237 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 4238}); 4239``` 4240 4241### sendControlCommand<sup>10+</sup> 4242 4243sendControlCommand(command: AVCastControlCommand): Promise\<void> 4244 4245Sends a control command to the session through the controller. This API uses a promise to return the result. 4246 4247 4248**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4249 4250**Parameters** 4251 4252| Name | Type | Mandatory| Description | 4253| ------- | ------------------------------------- | ---- | ------------------------------ | 4254| command | [AVCastControlCommand](#avcastcontrolcommand10) | Yes | Command to send.| 4255 4256**Return value** 4257 4258| Type | Description | 4259| -------------- | ----------------------------- | 4260| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned.| 4261 4262**Error codes** 4263 4264For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4265 4266| ID| Error Message| 4267| -------- | ---------------------------------------- | 4268| 6600101 | Session service exception. | 4269| 6600105 | Invalid session command. | 4270| 6600109 | The remote connection is not established. | 4271 4272**Example** 4273 4274```ts 4275import avSession from '@ohos.multimedia.avsession'; 4276import { BusinessError } from '@ohos.base'; 4277 4278let avCommand: avSession.AVCastControlCommand = {command:'play'}; 4279// let avCommand = {command:'pause'}; 4280// let avCommand = {command:'stop'}; 4281// let avCommand = {command:'playNext'}; 4282// let avCommand = {command:'playPrevious'}; 4283// let avCommand = {command:'fastForward'}; 4284// let avCommand = {command:'rewind'}; 4285// let avCommand = {command:'seek', parameter:10}; 4286aVCastController.sendControlCommand(avCommand).then(() => { 4287 console.info(`SendControlCommand successfully`); 4288}).catch((err: BusinessError) => { 4289 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 4290}); 4291``` 4292 4293### sendControlCommand<sup>10+</sup> 4294 4295sendControlCommand(command: AVCastControlCommand, callback: AsyncCallback\<void>): void 4296 4297Sends a control command to the session through the controller. This API uses an asynchronous callback to return the result. 4298 4299 4300**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4301 4302**Parameters** 4303 4304| Name | Type | Mandatory| Description | 4305| -------- | ------------------------------------- | ---- | ------------------------------ | 4306| command | [AVCastControlCommand](#avcastcontrolcommand10) | Yes | Command to send.| 4307| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 4308 4309**Error codes** 4310 4311For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4312 4313| ID| Error Message| 4314| -------- | ------------------------------- | 4315| 6600101 | Session service exception. | 4316| 6600105 | Invalid session command. | 4317| 6600109 | The remote connection is not established. | 4318 4319**Example** 4320 4321```ts 4322import avSession from '@ohos.multimedia.avsession'; 4323import { BusinessError } from '@ohos.base'; 4324 4325let avCommand: avSession.AVCastControlCommand = {command:'play'}; 4326// let avCommand = {command:'pause'}; 4327// let avCommand = {command:'stop'}; 4328// let avCommand = {command:'playNext'}; 4329// let avCommand = {command:'playPrevious'}; 4330// let avCommand = {command:'fastForward'}; 4331// let avCommand = {command:'rewind'}; 4332// let avCommand = {command:'seek', parameter:10}; 4333aVCastController.sendControlCommand(avCommand, (err: BusinessError) => { 4334 if (err) { 4335 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 4336 } else { 4337 console.info(`SendControlCommand successfully`); 4338 } 4339}); 4340``` 4341 4342### prepare<sup>10+</sup> 4343 4344prepare(item: AVQueueItem, callback: AsyncCallback\<void>): void 4345 4346Prepares 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. 4347 4348**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4349 4350**Parameters** 4351 4352| Name | Type | Mandatory| Description | 4353| ------- | ------------------------------------- | ---- | ------------------------------ | 4354| item | [AVQueueItem](#avqueueitem10) | Yes | Attributes of an item in the playlist.| 4355| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object.| 4356 4357**Error codes** 4358 4359For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4360 4361| ID| Error Message| 4362| -------- | ---------------------------------------- | 4363| 6600101 | Session service exception. | 4364| 6600109 | The remote connection is not established. | 4365 4366**Example** 4367 4368```ts 4369import avSession from '@ohos.multimedia.avsession'; 4370import { BusinessError } from '@ohos.base'; 4371 4372// Set playback parameters. 4373let playItem: avSession.AVQueueItem = { 4374 itemId: 0, 4375 description: { 4376 assetId: '12345', 4377 mediaType: 'AUDIO', 4378 mediaUri: 'http://resource1_address', 4379 mediaSize: 12345, 4380 startPosition: 0, 4381 duration: 0, 4382 artist: 'mysong', 4383 albumTitle: 'song1_title', 4384 albumCoverUri: "http://resource1_album_address", 4385 lyricUri: "http://resource1_lyric_address", 4386 appName: 'MyMusic' 4387 } 4388}; 4389// Prepare for playback. This operation triggers loading and buffering, but not the actual playback. 4390aVCastController.prepare(playItem, (err: BusinessError) => { 4391 if (err) { 4392 console.error(`prepare BusinessError: code: ${err.code}, message: ${err.message}`); 4393 } else { 4394 console.info(`prepare successfully`); 4395 } 4396}); 4397``` 4398 4399 4400### prepare<sup>10+</sup> 4401 4402prepare(item: AVQueueItem): Promise\<void> 4403 4404Prepares for the playback of a media asset, that is, loads and buffers a media asset. This API uses a promise to return the result. 4405 4406 4407**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4408 4409**Parameters** 4410 4411| Name | Type | Mandatory| Description | 4412| ------- | ------------------------------------- | ---- | ------------------------------ | 4413| item | [AVQueueItem](#avqueueitem10) | Yes | Attributes of an item in the playlist.| 4414 4415**Return value** 4416 4417| Type | Description | 4418| -------------- | ----------------------------- | 4419| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned.| 4420 4421**Error codes** 4422 4423For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4424 4425| ID| Error Message| 4426| -------- | ---------------------------------------- | 4427| 6600101 | Session service exception. | 4428| 6600109 | The remote connection is not established. | 4429 4430 4431**Example** 4432 4433```ts 4434import avSession from '@ohos.multimedia.avsession'; 4435import { BusinessError } from '@ohos.base'; 4436 4437// Set playback parameters. 4438let playItem: avSession.AVQueueItem = { 4439 itemId: 0, 4440 description: { 4441 assetId: '12345', 4442 mediaType: 'AUDIO', 4443 mediaUri: 'http://resource1_address', 4444 mediaSize: 12345, 4445 startPosition: 0, 4446 duration: 0, 4447 artist: 'mysong', 4448 albumTitle: 'song1_title', 4449 albumCoverUri: "http://resource1_album_address", 4450 lyricUri: "http://resource1_lyric_address", 4451 appName: 'MyMusic' 4452 } 4453}; 4454// Prepare for playback. This operation triggers loading and buffering, but not the actual playback. 4455aVCastController.prepare(playItem).then(() => { 4456 console.info(`prepare successfully`); 4457}).catch((err: BusinessError) => { 4458 console.error(`prepare BusinessError: code: ${err.code}, message: ${err.message}`); 4459}); 4460``` 4461 4462### start<sup>10+</sup> 4463 4464start(item: AVQueueItem, callback: AsyncCallback\<void>): void 4465 4466Prepares for the playback of a media asset. This API uses an asynchronous callback to return the result. 4467 4468**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4469 4470**Parameters** 4471 4472| Name | Type | Mandatory| Description | 4473| ------- | ------------------------------------- | ---- | ------------------------------ | 4474| item | [AVQueueItem](#avqueueitem10) | Yes | Attributes of an item in the playlist.| 4475| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object.| 4476 4477**Error codes** 4478 4479For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4480 4481| ID| Error Message| 4482| -------- | ---------------------------------------- | 4483| 6600101 | Session service exception. | 4484| 6600109 | The remote connection is not established. | 4485 4486**Example** 4487 4488```ts 4489import avSession from '@ohos.multimedia.avsession'; 4490import { BusinessError } from '@ohos.base'; 4491 4492// Set playback parameters. 4493let playItem: avSession.AVQueueItem = { 4494 itemId: 0, 4495 description: { 4496 assetId: '12345', 4497 mediaType: 'AUDIO', 4498 mediaUri: 'http://resource1_address', 4499 mediaSize: 12345, 4500 startPosition: 0, 4501 duration: 0, 4502 artist: 'mysong', 4503 albumTitle: 'song1_title', 4504 albumCoverUri: "http://resource1_album_address", 4505 lyricUri: "http://resource1_lyric_address", 4506 appName: 'MyMusic' 4507 } 4508}; 4509 4510// Start playback. 4511aVCastController.start(playItem, (err: BusinessError) => { 4512 if (err) { 4513 console.error(`start BusinessError: code: ${err.code}, message: ${err.message}`); 4514 } else { 4515 console.info(`start successfully`); 4516 } 4517}); 4518``` 4519 4520### start<sup>10+</sup> 4521 4522start(item: AVQueueItem): Promise\<void> 4523 4524Prepares for the playback of a media asset. This API uses a promise to return the result. 4525 4526 4527**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4528 4529**Parameters** 4530 4531| Name | Type | Mandatory| Description | 4532| ------- | ------------------------------------- | ---- | ------------------------------ | 4533| item | [AVQueueItem](#avqueueitem10) | Yes | Attributes of an item in the playlist.| 4534 4535**Return value** 4536 4537| Type | Description | 4538| -------------- | ----------------------------- | 4539| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned.| 4540 4541**Error codes** 4542 4543For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4544 4545| ID| Error Message| 4546| -------- | ---------------------------------------- | 4547| 6600101 | Session service exception. | 4548| 6600109 | The remote connection is not established. | 4549 4550 4551**Example** 4552 4553```ts 4554import avSession from '@ohos.multimedia.avsession'; 4555import { BusinessError } from '@ohos.base'; 4556 4557// Set playback parameters. 4558let playItem: avSession.AVQueueItem = { 4559 itemId: 0, 4560 description: { 4561 assetId: '12345', 4562 mediaType: 'AUDIO', 4563 mediaUri: 'http://resource1_address', 4564 mediaSize: 12345, 4565 startPosition: 0, 4566 duration: 0, 4567 artist: 'mysong', 4568 albumTitle: 'song1_title', 4569 albumCoverUri: "http://resource1_album_address", 4570 lyricUri: "http://resource1_lyric_address", 4571 appName: 'MyMusic' 4572 } 4573}; 4574// Start playback. 4575aVCastController.start(playItem).then(() => { 4576 console.info(`start successfully`); 4577}).catch((err: BusinessError) => { 4578 console.info(`start BusinessError: code: ${err.code}, message: ${err.message}`); 4579}); 4580``` 4581 4582### getCurrentItem<sup>10+</sup> 4583 4584getCurrentItem(callback: AsyncCallback\<AVQueueItem>): void 4585 4586Obtains the information about the media asset that is being played. This API uses an asynchronous callback to return the result. 4587 4588**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4589 4590**Parameters** 4591 4592| Name | Type | Mandatory| Description | 4593| -------- | ------------------------------------- | ---- | ------------------------------------- | 4594| 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.| 4595 4596**Error codes** 4597 4598For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4599 4600| ID| Error Message| 4601| -------- | ---------------------------------------- | 4602| 6600101 | Session service exception. | 4603 4604**Example** 4605 4606```ts 4607import { BusinessError } from '@ohos.base'; 4608 4609aVCastController.getCurrentItem((err: BusinessError, value: avSession.AVQueueItem) => { 4610 if (err) { 4611 console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); 4612 } else { 4613 console.info(`getCurrentItem successfully`); 4614 } 4615}); 4616``` 4617 4618### getCurrentItem<sup>10+</sup> 4619 4620getCurrentItem(): Promise\<AVQueueItem> 4621 4622Obtains the information about the media asset that is being played. This API uses a promise to return the result. 4623 4624**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4625 4626**Return value** 4627 4628| Type | Description | 4629| -------------- | ----------------------------- | 4630| Promise\<[AVQueueItem](#avqueueitem10)> | Promise used to return the media asset obtained. If the operation fails, an error object is returned.| 4631 4632**Error codes** 4633 4634For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4635 4636| ID| Error Message| 4637| -------- | ---------------------------------------- | 4638| 6600101 | Session service exception. | 4639 4640**Example** 4641 4642```ts 4643import { BusinessError } from '@ohos.base'; 4644 4645aVCastController.getCurrentItem().then((value: avSession.AVQueueItem) => { 4646 console.info(`getCurrentItem successfully`); 4647}).catch((err: BusinessError) => { 4648 console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); 4649}); 4650 4651``` 4652 4653### on('playbackStateChange')<sup>10+</sup> 4654 4655on(type: 'playbackStateChange', filter: Array\<keyof AVPlaybackState> | 'all', callback: (state: AVPlaybackState) => void): void 4656 4657Subscribes to playback state change events. 4658 4659**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4660 4661**Parameters** 4662 4663| Name | Type | Mandatory| Description | 4664| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4665| type | string | Yes | Event type. The event **'playbackStateChange'** is triggered when the playback state changes.| 4666| 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.| 4667| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | Yes | Callback used for subscription. The **state** parameter in the callback indicates the changed playback state. | 4668 4669**Error codes** 4670 4671For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4672 4673| ID| Error Message| 4674| -------- | ------------------------------ | 4675| 6600101 | Session service exception. | 4676 4677**Example** 4678 4679```ts 4680aVCastController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { 4681 console.info(`on playbackStateChange state : ${playbackState.state}`); 4682}); 4683 4684let playbackFilter = ['state', 'speed', 'loopMode']; 4685aVCastController.on('playbackStateChange', playbackFilter, (playbackState: avSession.AVPlaybackState) => { 4686 console.info(`on playbackStateChange state : ${playbackState.state}`); 4687}); 4688``` 4689 4690### off('playbackStateChange')<sup>10+</sup> 4691 4692off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void): void 4693 4694Unsubscribes from playback state change events. This API is called by the controller. 4695 4696**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4697 4698**Parameters** 4699 4700| Name | Type | Mandatory| Description | 4701| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4702| type | string | Yes | Event type, which is **'playbackStateChange'** in this case. | 4703| 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. | 4704 4705**Error codes** 4706 4707For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4708 4709| ID| Error Message| 4710| -------- | ---------------- | 4711| 6600101 | Session service exception. | 4712 4713**Example** 4714 4715```ts 4716aVCastController.off('playbackStateChange'); 4717``` 4718 4719### on('mediaItemChange')<sup>10+</sup> 4720 4721on(type: 'mediaItemChange', callback: Callback\<AVQueueItem>): void 4722 4723Subscribes to media asset change events. 4724 4725**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4726 4727**Parameters** 4728 4729| Name | Type | Mandatory| Description | 4730| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4731| type | string | Yes | Event type. The event **'mediaItemChange'** is triggered when the media content being played changes.| 4732| callback | (state: [AVQueueItem](#avqueueitem10)) => void | Yes | Callback used for subscription. **AVQueueItem** is the media asset that is being played. | 4733 4734**Error codes** 4735 4736For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4737 4738| ID| Error Message| 4739| -------- | ------------------------------ | 4740| 6600101 | Session service exception. | 4741 4742**Example** 4743 4744```ts 4745aVCastController.on('mediaItemChange', (item: avSession.AVQueueItem) => { 4746 console.info(`on mediaItemChange state : ${item.itemId}`); 4747}); 4748``` 4749 4750### off('mediaItemChange')<sup>10+</sup> 4751 4752off(type: 'mediaItemChange'): void 4753 4754Unsubscribes from media asset change events. 4755 4756**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4757 4758**Parameters** 4759 4760| Name | Type | Mandatory| Description | 4761| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4762| type | string | Yes | Event type, which is **'mediaItemChange'** in this case. | 4763 4764**Error codes** 4765 4766For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4767 4768| ID| Error Message| 4769| -------- | ---------------- | 4770| 6600101 | Session service exception. | 4771 4772**Example** 4773 4774```ts 4775aVCastController.off('mediaItemChange'); 4776``` 4777 4778### on('playNext')<sup>10+</sup> 4779 4780on(type: 'playNext', callback: Callback\<void>): void 4781 4782Subscribes to playNext command events. 4783 4784**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4785 4786**Parameters** 4787 4788| Name | Type | Mandatory| Description | 4789| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4790| type | string | Yes | Event type. The event **'playNext'** is triggered when the command for playing the next item is received.| 4791| callback | Callback\<void\> | Yes | Callback used to return the result. | 4792 4793**Error codes** 4794 4795For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4796 4797| ID| Error Message| 4798| -------- | ------------------------------ | 4799| 6600101 | Session service exception. | 4800 4801**Example** 4802 4803```ts 4804aVCastController.on('playNext', () => { 4805 console.info(`on playNext`); 4806}); 4807``` 4808 4809### off('playNext')<sup>10+</sup> 4810 4811off(type: 'playNext'): void 4812 4813Unsubscribes from playNext command events. 4814 4815**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4816 4817**Parameters** 4818 4819| Name | Type | Mandatory| Description | 4820| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4821| type | string | Yes | Event type, which is **'playNext'** in this case. | 4822 4823**Error codes** 4824 4825For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4826 4827| ID| Error Message| 4828| -------- | ---------------- | 4829| 6600101 | Session service exception. | 4830 4831**Example** 4832 4833```ts 4834aVCastController.off('playNext'); 4835``` 4836 4837### on('playPrevious')<sup>10+</sup> 4838 4839on(type: 'playPrevious', callback: Callback\<void>): void 4840 4841Subscribes to playPrevious command events. 4842 4843**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4844 4845**Parameters** 4846 4847| Name | Type | Mandatory| Description | 4848| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4849| type | string | Yes | Event type. The event **'playPrevious'** is triggered when the command for playing the previous event is received.| 4850| callback | Callback\<void\> | Yes | Callback used to return the result. | 4851 4852**Error codes** 4853 4854For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4855 4856| ID| Error Message| 4857| -------- | ------------------------------ | 4858| 6600101 | Session service exception. | 4859 4860**Example** 4861 4862```ts 4863aVCastController.on('playPrevious', () => { 4864 console.info(`on playPrevious`); 4865}); 4866``` 4867 4868### off('playPrevious')<sup>10+</sup> 4869 4870off(type: 'playPrevious'): void 4871 4872Unsubscribes from playPrevious command events. 4873 4874**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4875 4876**Parameters** 4877 4878| Name | Type | Mandatory| Description | 4879| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4880| type | string | Yes | Event type, which is **'playPrevious'** in this case. | 4881 4882**Error codes** 4883 4884For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4885 4886| ID| Error Message| 4887| -------- | ---------------- | 4888| 6600101 | Session service exception. | 4889 4890**Example** 4891 4892```ts 4893aVCastController.off('playPrevious'); 4894``` 4895 4896### on('seekDone')<sup>10+</sup> 4897 4898on(type: 'seekDone', callback: Callback\<number>): void 4899 4900Subscribes to seek done events. 4901 4902**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4903 4904**Parameters** 4905 4906| Name | Type | Mandatory| Description | 4907| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4908| type | string | Yes | Event type. The event **'seekDone'** is triggered when the seek operation is complete.| 4909| callback | Callback\<number\> | Yes | Callback used to return the position after the seek operation. | 4910 4911**Error codes** 4912 4913For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4914 4915| ID| Error Message| 4916| -------- | ------------------------------ | 4917| 6600101 | Session service exception. | 4918 4919**Example** 4920 4921```ts 4922aVCastController.on('seekDone', (pos: number) => { 4923 console.info(`on seekDone pos: ${pos} `); 4924}); 4925``` 4926 4927### off('seekDone')<sup>10+</sup> 4928 4929off(type: 'seekDone'): void 4930 4931Unsubscribes from the seek done events. 4932 4933**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4934 4935**Parameters** 4936 4937| Name | Type | Mandatory| Description | 4938| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4939| type | string | Yes | Event type, which is **'seekDone'** in this case. | 4940 4941**Error codes** 4942 4943For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4944 4945| ID| Error Message| 4946| -------- | ---------------- | 4947| 6600101 | Session service exception. | 4948 4949**Example** 4950 4951```ts 4952aVCastController.off('seekDone'); 4953``` 4954 4955### on('videoSizeChange')<sup>10+</sup> 4956 4957on(type: 'videoSizeChange', callback: (width:number, height:number) => void): void 4958 4959Subscribes to video size change events. 4960 4961**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4962 4963**System API**: This is a system API. 4964 4965**Parameters** 4966 4967| Name | Type | Mandatory| Description | 4968| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4969| type | string | Yes | Event type. The event **'videoSizeChange'** is triggered when the video size changes.| 4970| callback | (width:number, height:number) => void | Yes | Callback used to return the video width and height. | 4971 4972**Error codes** 4973 4974For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 4975 4976| ID| Error Message| 4977| -------- | ---------------- | 4978| 6600101 | Session service exception. | 4979 4980**Example** 4981 4982```ts 4983aVCastController.on('videoSizeChange', (width: number, height: number) => { 4984 console.info(`width : ${width} `); 4985 console.info(`height: ${height} `); 4986}); 4987``` 4988 4989### off('videoSizeChange')<sup>10+</sup> 4990 4991off(type: 'videoSizeChange'): void 4992 4993Unsubscribes from video size changes. 4994 4995**System capability**: SystemCapability.Multimedia.AVSession.AVCast 4996 4997**System API**: This is a system API. 4998 4999**Parameters** 5000 5001| Name | Type | Mandatory| Description | 5002| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 5003| type | string | Yes | Event type, which is **'videoSizeChange'** in this case. | 5004 5005**Error codes** 5006 5007For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5008 5009| ID| Error Message| 5010| -------- | ---------------- | 5011| 6600101 | Session service exception. | 5012 5013**Example** 5014 5015```ts 5016aVCastController.off('videoSizeChange'); 5017``` 5018 5019### on('error')<sup>10+</sup> 5020 5021on(type: 'error', callback: ErrorCallback): void 5022 5023Subscribes to remote AVPlayer errors. This event is used only for error prompt and does not require the user to stop playback control. 5024 5025**System capability**: SystemCapability.Multimedia.AVSession.AVCast 5026 5027**Parameters** 5028 5029| Name | Type | Mandatory| Description | 5030| -------- | -------- | ---- | ------------------------------------------------------------ | 5031| type | string | Yes | Event type, which is **'error'** in this case. This event can be triggered by both user operations and the system.| 5032| callback | function | Yes | Callback used to return the error code ID and error message.| 5033 5034**Error codes** 5035 5036For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 5037 5038| ID| Error Message | 5039| -------- | --------------------- | 5040| 5400101 | No memory. | 5041| 5400102 | Operation not allowed. | 5042| 5400103 | I/O error. | 5043| 5400104 | Time out. | 5044| 5400105 | Service died. | 5045| 5400106 | Unsupport format. | 5046| 6600101 | Session service exception. | 5047 5048**Example** 5049 5050```ts 5051import { BusinessError } from '@ohos.base' 5052 5053aVCastController.on('error', (error: BusinessError) => { 5054 console.error('error happened,and error message is :' + error.message) 5055 console.error('error happened,and error code is :' + error.code) 5056}) 5057``` 5058 5059### off('error')<sup>10+</sup> 5060 5061off(type: 'error'): void 5062 5063Unsubscribes from remote AVPlayer errors. 5064 5065**System capability**: SystemCapability.Multimedia.AVSession.AVCast 5066 5067**Parameters** 5068 5069| Name| Type | Mandatory| Description | 5070| ------ | ------ | ---- | ----------------------------------------- | 5071| type | string | Yes | Event type, which is **'error'** in this case.| 5072 5073**Error codes** 5074 5075For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 5076 5077| ID| Error Message | 5078| -------- | --------------------- | 5079| 5400101 | No memory. | 5080| 5400102 | Operation not allowed. | 5081| 5400103 | I/O error. | 5082| 5400104 | Time out. | 5083| 5400105 | Service died. | 5084| 5400106 | Unsupport format. | 5085| 6600101 | Session service exception. | 5086 5087**Example** 5088 5089```ts 5090aVCastController.off('error') 5091``` 5092 5093## ConnectionState<sup>10+</sup> 5094 5095Enumerates the connection states. 5096 5097**System capability**: SystemCapability.Multimedia.AVSession.Core 5098 5099| Name | Value | Description | 5100| --------------------------- | ---- | ----------- | 5101| STATE_CONNECTING | 0 | The device is connecting. | 5102| STATE_CONNECTED | 1 | The device is connected.| 5103| STATE_DISCONNECTED | 6 | The device is disconnected.| 5104 5105## AVMetadata<sup>10+</sup> 5106 5107Describes the media metadata. 5108 5109**System capability**: SystemCapability.Multimedia.AVSession.Core 5110 5111| Name | Type | Mandatory| Description | 5112| --------------- |-------------------------| ---- |---------------------------------------------------------------------| 5113| assetId | string | Yes | Media ID. | 5114| title | string | No | Title. | 5115| artist | string | No | Artist. | 5116| author | string | No | Author. | 5117| album | string | No | Album name. | 5118| writer | string | No | Writer. | 5119| composer | string | No | composer. | 5120| duration | number | No | Media duration, in ms. | 5121| mediaImage | image.PixelMap | string | No | Pixel map or image path (local path or network path) of the image. | 5122| publishDate | Date | No | Release date. | 5123| subtitle | string | No | Subtitle. | 5124| description | string | No | Media description. | 5125| lyric | string | No | Lyric file path (local path or network path).| 5126| previousAssetId | string | No | ID of the previous media asset. | 5127| nextAssetId | string | No | ID of the next media asset. | 5128 5129## AVMediaDescription<sup>10+</sup> 5130 5131Describes the attributes related to the media metadata in the playlist. 5132 5133**System capability**: SystemCapability.Multimedia.AVSession.Core 5134 5135| Name | Type | Mandatory | Description | 5136| ------------ | ----------------------- | ---- | ----------------------- | 5137| assetId | string | Yes | Media ID in the playlist. | 5138| title | string | No | Name of the media asset in the playlist. | 5139| subtitle | string | No | Subname of the media asset in the playlist. | 5140| description | string | No | Description of the media asset in the playlist. | 5141| mediaImage | image.PixelMap | No | Pixel map of the image of the media asset in the playlist.| 5142| extras | {[key: string]: any} | No | Additional fields of the media asset in the playlist. | 5143| mediaUri | string | No | URI of the media asset in the playlist. | 5144| mediaType | string | No | Type of the media asset in the playlist. | 5145| mediaSize | number | No | Size of the media asset in the playlist. | 5146| albumTitle | string | No | Album name of the media asset in the playlist. | 5147| albumCoverUri | string | No | URI of the album title of the media asset in the playlist. | 5148| lyricContent | string | No | Lyric content of the media asset in the playlist. | 5149| lyricUri | string | No | Lyric URI of the media asset in the playlist. | 5150| artist | string | No | Author of the lyric of the media asset in the playlist. | 5151| fdSrc | media.AVFileDescriptor | No | Handle to the local media file in the playlist. | 5152| duration | number | No | Playback duration of the media asset in the playlist. | 5153| startPosition | number | No | Start position for playing the media asset in the playlist. | 5154| creditsPosition | number | No | Position for playing the closing credits of the media asset in the playlist. | 5155| appName | string | No | Name of the application provided by the playlist. | 5156 5157## AVQueueItem<sup>10+</sup> 5158 5159Describes the attributes of an item in the playlist. 5160 5161**System capability**: SystemCapability.Multimedia.AVSession.Core 5162 5163| Name | Type | Mandatory| Description | 5164| ------------ | ------------------------------------------ | ---- | --------------------------- | 5165| itemId | number | Yes | ID of an item in the playlist. | 5166| description | [AVMediaDescription](#avmediadescription10) | Yes | Media metadata of the item in the playlist. | 5167 5168## AVPlaybackState<sup>10+</sup> 5169 5170Describes the information related to the media playback state. 5171 5172**System capability**: SystemCapability.Multimedia.AVSession.Core 5173 5174| Name | Type | Mandatory| Description | 5175| ------------ | ------------------------------------- | ---- | ------- | 5176| state | [PlaybackState](#playbackstate) | No | Playback state.| 5177| speed | number | No | Playback speed.| 5178| position | [PlaybackPosition](#playbackposition) | No | Playback position.| 5179| bufferedTime | number | No | Buffered time.| 5180| loopMode | [LoopMode](#loopmode10) | No | Loop mode.| 5181| isFavorite | boolean | No | Whether the media asset is favorited.| 5182| activeItemId<sup>10+</sup> | number | No | ID of the item that is being played.| 5183| volume<sup>10+</sup> | number | No | Media volume.| 5184| extras<sup>10+</sup> | {[key: string]: Object} | No | Custom media data.| 5185 5186## PlaybackPosition<sup>10+</sup> 5187 5188Describes the information related to the playback position. 5189 5190**System capability**: SystemCapability.Multimedia.AVSession.Core 5191 5192| Name | Type | Mandatory| Description | 5193| ----------- | ------ | ---- | ------------------ | 5194| elapsedTime | number | Yes | Elapsed time, in ms.| 5195| updateTime | number | Yes | Updated time, in ms.| 5196 5197## AVCastCategory<sup>10+</sup> 5198 5199Enumerates the cast categories. 5200 5201**System capability**: SystemCapability.Multimedia.AVSession.AVCast 5202 5203| Name | Value | Description | 5204| --------------------------- | ---- | ----------- | 5205| CATEGORY_LOCAL | 0 | Local playback. The sound is played from the local device or a connected Bluetooth headset by default. | 5206| CATEGORY_REMOTE | 1 | Remote playback. The sound or images are played from a remote device. | 5207 5208## DeviceType<sup>10+</sup> 5209 5210Enumerates the output device types. 5211 5212**System capability**: SystemCapability.Multimedia.AVSession.Core 5213 5214| Name | Value | Description | 5215| --------------------------- | ---- | ----------- | 5216| DEVICE_TYPE_LOCAL | 0 | Local device. | 5217| DEVICE_TYPE_BLUETOOTH | 10 | Bluetooth device. | 5218| DEVICE_TYPE_TV | 2 | TV.<br> **System capability**: SystemCapability.Multimedia.AVSession.AVCast| 5219| DEVICE_TYPE_SMART_SPEAKER | 3 | Speaker.<br> **System capability**: SystemCapability.Multimedia.AVSession.AVCast| 5220 5221## DeviceInfo<sup>10+</sup> 5222 5223Describes the information related to the output device. 5224 5225**System capability**: SystemCapability.Multimedia.AVSession.Core 5226 5227| Name | Type | Mandatory| Description | 5228| ---------- | -------------- | ---- | ---------------------- | 5229| castCategory | AVCastCategory | Yes | Cast category. | 5230| deviceId | string | Yes | ID of the output device. | 5231| deviceName | string | Yes | Name of the output device. | 5232| deviceType | DeviceType | Yes | Type of the output device. | 5233| ipAddress | string | No | IP address of the output device.<br>This is a system API.<br> **System capability**: SystemCapability.Multimedia.AVSession.AVCast | 5234| providerId | number | No | Vendor of the output device.<br>This is a system API.<br> **System capability**: SystemCapability.Multimedia.AVSession.AVCast | 5235 5236## OutputDeviceInfo<sup>10+</sup> 5237 5238Describes the information related to the output device. 5239 5240**System capability**: SystemCapability.Multimedia.AVSession.Core 5241 5242| Name | Type | Mandatory| Description | 5243| ---------- | -------------- | ---- | ---------------------- | 5244| devices | Array\<DeviceInfo\> | Yes | Output devices. | 5245 5246## LoopMode<sup>10+</sup> 5247 5248Enumerates the loop modes of media playback. 5249 5250**System capability**: SystemCapability.Multimedia.AVSession.Core 5251 5252| Name | Value | Description | 5253| ------------------ | ---- | -------- | 5254| LOOP_MODE_SEQUENCE | 0 | Sequential playback.| 5255| LOOP_MODE_SINGLE | 1 | Single loop.| 5256| LOOP_MODE_LIST | 2 | Playlist loop.| 5257| LOOP_MODE_SHUFFLE | 3 | Shuffle.| 5258 5259## PlaybackState<sup>10+</sup> 5260 5261Enumerates the media playback states. 5262 5263**System capability**: SystemCapability.Multimedia.AVSession.Core 5264 5265| Name | Value | Description | 5266| --------------------------- | ---- | ----------- | 5267| PLAYBACK_STATE_INITIAL | 0 | Initial. | 5268| PLAYBACK_STATE_PREPARE | 1 | Preparing. | 5269| PLAYBACK_STATE_PLAY | 2 | Playing. | 5270| PLAYBACK_STATE_PAUSE | 3 | Paused. | 5271| PLAYBACK_STATE_FAST_FORWARD | 4 | Fast-forwarding. | 5272| PLAYBACK_STATE_REWIND | 5 | Rewinding. | 5273| PLAYBACK_STATE_STOP | 6 | Stop the playback. | 5274| PLAYBACK_STATE_COMPLETED | 7 | Playback complete. | 5275| PLAYBACK_STATE_RELEASED | 8 | Released. | 5276| PLAYBACK_STATE_ERROR | 9 | Error. | 5277 5278## AVSessionDescriptor 5279 5280Declares the session descriptor. 5281 5282**System capability**: SystemCapability.Multimedia.AVSession.Manager 5283 5284**System API**: This is a system API. 5285 5286| Name | Type | Readable| Writable| Description | 5287| --------------| ---------------- |-----|-----|------| 5288| sessionId | string | Yes | No| Session ID. | 5289| type | [AVSessionType](#avsessiontype10) | Yes | No | Session type. | 5290| sessionTag | string | Yes | No | Custom session name. | 5291| elementName | [ElementName](js-apis-bundle-ElementName.md) | Yes | No | Information about the application to which the session belongs, including the bundle name and ability name.| 5292| isActive | boolean | Yes | No | Whether the session is activated. | 5293| isTopSession | boolean | Yes | No | Whether the session is the top session. | 5294| outputDevice | [OutputDeviceInfo](#outputdeviceinfo10) | Yes | No | Information about the output device. | 5295 5296## AVSessionController<sup>10+</sup> 5297 5298An AV session controller is created by calling [avSession.createController](#avsessioncreatecontroller). Through the controller, you can query the session ID, send commands and events to a session, and obtain session metadata and playback state information. 5299 5300### Attributes 5301 5302**System capability**: SystemCapability.Multimedia.AVSession.Core 5303 5304| Name | Type | Readable| Writable| Description | 5305| :-------- | :----- | :--- | :--- | :-------------------------------------- | 5306| sessionId | string | Yes | No | Unique session ID of the **AVSessionController** object.| 5307 5308 5309**Example** 5310 5311```ts 5312import { BusinessError } from '@ohos.base'; 5313 5314let AVSessionController: avSession.AVSessionController; 5315avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { 5316 AVSessionController = controller; 5317}).catch((err: BusinessError) => { 5318 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 5319}); 5320``` 5321 5322### getAVPlaybackState<sup>10+</sup> 5323 5324getAVPlaybackState(callback: AsyncCallback\<AVPlaybackState>): void 5325 5326Obtains the remote playback state. This API uses an asynchronous callback to return the result. 5327 5328**System capability**: SystemCapability.Multimedia.AVSession.Core 5329 5330**Parameters** 5331 5332| Name | Type | Mandatory| Description | 5333| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5334| callback | AsyncCallback<[[AVPlaybackState](#avplaybackstate10)\> | Yes | Callback used to return the remote playback state.| 5335 5336**Error codes** 5337 5338For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5339 5340| ID| Error Message| 5341| -------- | ---------------------------------------- | 5342| 6600101 | Session service exception. | 5343| 6600102 | The session does not exist. | 5344| 6600103 | The session controller does not exist. | 5345 5346**Example** 5347 5348```ts 5349import { BusinessError } from '@ohos.base'; 5350 5351avsessionController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { 5352 if (err) { 5353 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 5354 } else { 5355 console.info(`getAVPlaybackState : SUCCESS`); 5356 } 5357}); 5358``` 5359 5360### getAVPlaybackState<sup>10+</sup> 5361 5362getAVPlaybackState(): Promise\<AVPlaybackState>; 5363 5364Obtains the remote playback state. This API uses a promise to return the result. 5365 5366**System capability**: SystemCapability.Multimedia.AVSession.Core 5367 5368**Return value** 5369 5370| Type | Description | 5371| --------- | ------------------------------------------------------------ | 5372| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise used to return the remote playback state. | 5373 5374**Error codes** 5375 5376For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5377 5378| ID| Error Message| 5379| -------- | ---------------------------------------- | 5380| 6600101 | Session service exception. | 5381| 6600102 | The session does not exist. | 5382| 6600103 | The session controller does not exist. | 5383 5384**Example** 5385 5386```ts 5387import { BusinessError } from '@ohos.base'; 5388 5389avsessionController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { 5390 console.info(`getAVPlaybackState : SUCCESS`); 5391}).catch((err: BusinessError) => { 5392 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 5393}); 5394``` 5395 5396### getAVMetadata<sup>10+</sup> 5397 5398getAVMetadata(): Promise\<AVMetadata> 5399 5400Obtains the session metadata. This API uses a promise to return the result. 5401 5402**System capability**: SystemCapability.Multimedia.AVSession.Core 5403 5404**Return value** 5405 5406| Type | Description | 5407| ----------------------------------- | ----------------------------- | 5408| Promise<[AVMetadata](#avmetadata10)\> | Promise used to return the metadata obtained.| 5409 5410**Error codes** 5411 5412For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5413 5414| ID| Error Message| 5415| -------- | ---------------------------------------- | 5416| 6600101 | Session service exception. | 5417| 6600102 | The session does not exist. | 5418| 6600103 | The session controller does not exist. | 5419 5420**Example** 5421 5422```ts 5423import { BusinessError } from '@ohos.base'; 5424 5425avsessionController.getAVMetadata().then((metadata: avSession.AVMetadata) => { 5426 console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); 5427}).catch((err: BusinessError) => { 5428 console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 5429}); 5430``` 5431 5432### getAVMetadata<sup>10+</sup> 5433 5434getAVMetadata(callback: AsyncCallback\<AVMetadata>): void 5435 5436Obtains the session metadata. This API uses an asynchronous callback to return the result. 5437 5438**System capability**: SystemCapability.Multimedia.AVSession.Core 5439 5440**Parameters** 5441 5442| Name | Type | Mandatory| Description | 5443| -------- | ----------------------------------------- | ---- | -------------------------- | 5444| callback | AsyncCallback<[AVMetadata](#avmetadata10)\> | Yes | Callback used to return the metadata obtained.| 5445 5446**Error codes** 5447 5448For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5449 5450| ID| Error Message| 5451| -------- | ---------------------------------------- | 5452| 6600101 | Session service exception. | 5453| 6600102 | The session does not exist. | 5454| 6600103 | The session controller does not exist. | 5455 5456**Example** 5457 5458```ts 5459import { BusinessError } from '@ohos.base'; 5460 5461avsessionController.getAVMetadata((err: BusinessError, metadata: avSession.AVMetadata) => { 5462 if (err) { 5463 console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 5464 } else { 5465 console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); 5466 } 5467}); 5468``` 5469 5470### getAVQueueTitle<sup>10+</sup> 5471 5472getAVQueueTitle(): Promise\<string> 5473 5474Obtains the name of the playlist. This API uses a promise to return the result. 5475 5476**System capability**: SystemCapability.Multimedia.AVSession.Core 5477 5478**Return value** 5479 5480| Type | Description | 5481| ---------------- | ----------------------------- | 5482| Promise<string\> | Promise used to return the playlist name.| 5483 5484**Error codes** 5485 5486For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5487 5488| ID| Error Message| 5489| -------- | ---------------------------------------- | 5490| 6600101 | Session service exception. | 5491| 6600102 | The session does not exist. | 5492| 6600103 | The session controller does not exist. | 5493 5494**Example** 5495 5496```ts 5497import { BusinessError } from '@ohos.base'; 5498 5499avsessionController.getAVQueueTitle().then((title: string) => { 5500 console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); 5501}).catch((err: BusinessError) => { 5502 console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 5503}); 5504``` 5505 5506### getAVQueueTitle<sup>10+</sup> 5507 5508getAVQueueTitle(callback: AsyncCallback\<string>): void 5509 5510Obtains the name of the playlist. This API uses an asynchronous callback to return the result. 5511 5512**System capability**: SystemCapability.Multimedia.AVSession.Core 5513 5514**Parameters** 5515 5516| Name | Type | Mandatory| Description | 5517| -------- | ---------------------- | ---- | ------------------------- | 5518| callback | AsyncCallback<string\> | Yes | Callback used to return the playlist name.| 5519 5520**Error codes** 5521 5522For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5523 5524| ID| Error Message| 5525| -------- | ---------------------------------------- | 5526| 6600101 | Session service exception. | 5527| 6600102 | The session does not exist. | 5528| 6600103 | The session controller does not exist. | 5529 5530**Example** 5531 5532```ts 5533import { BusinessError } from '@ohos.base'; 5534 5535avsessionController.getAVQueueTitle((err: BusinessError, title: string) => { 5536 if (err) { 5537 console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 5538 } else { 5539 console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); 5540 } 5541}); 5542``` 5543 5544### getAVQueueItems<sup>10+</sup> 5545 5546getAVQueueItems(): Promise\<Array\<AVQueueItem>> 5547 5548Obtains the information related to the items in the queue. This API uses a promise to return the result. 5549 5550**System capability**: SystemCapability.Multimedia.AVSession.Core 5551 5552**Return value** 5553 5554| Type | Description | 5555| --------------------------------------------- | ----------------------------- | 5556| Promise<Array<[AVQueueItem](#avqueueitem10)\>\> | Promise used to return the items in the queue.| 5557 5558**Error codes** 5559 5560For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5561 5562| ID| Error Message| 5563| -------- | ---------------------------------------- | 5564| 6600101 | Session service exception. | 5565| 6600102 | The session does not exist. | 5566| 6600103 | The session controller does not exist. | 5567 5568**Example** 5569 5570```ts 5571import { BusinessError } from '@ohos.base'; 5572 5573avsessionController.getAVQueueItems().then((items: avSession.AVQueueItem[]) => { 5574 console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); 5575}).catch((err: BusinessError) => { 5576 console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 5577}); 5578``` 5579 5580### getAVQueueItems<sup>10+</sup> 5581 5582getAVQueueItems(callback: AsyncCallback\<Array\<AVQueueItem>>): void 5583 5584Obtains the information related to the items in the playlist. This API uses an asynchronous callback to return the result. 5585 5586**System capability**: SystemCapability.Multimedia.AVSession.Core 5587 5588**Parameters** 5589 5590| Name | Type | Mandatory| Description | 5591| -------- | --------------------------------------------------- | ---- | ------------------------- | 5592| callback | AsyncCallback<Array<[AVQueueItem](#avqueueitem10)\>\> | Yes | Callback used to return the items in the playlist.| 5593 5594**Error codes** 5595 5596For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5597 5598| ID| Error Message| 5599| -------- | ---------------------------------------- | 5600| 6600101 | Session service exception. | 5601| 6600102 | The session does not exist. | 5602| 6600103 | The session controller does not exist. | 5603 5604**Example** 5605 5606```ts 5607import { BusinessError } from '@ohos.base'; 5608 5609avsessionController.getAVQueueItems((err: BusinessError, items: avSession.AVQueueItem[]) => { 5610 if (err) { 5611 console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 5612 } else { 5613 console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); 5614 } 5615}); 5616``` 5617 5618### skipToQueueItem<sup>10+</sup> 5619 5620skipToQueueItem(itemId: number): Promise\<void> 5621 5622Sends 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. 5623 5624**System capability**: SystemCapability.Multimedia.AVSession.Core 5625 5626**Parameters** 5627 5628| Name | Type | Mandatory| Description | 5629| ------ | ------- | ---- | ------------------------------------------- | 5630| itemId | number | Yes | ID of an item in the playlist.| 5631 5632**Return value** 5633 5634| Type | Description | 5635| -------------- | --------------------------------------------------------------- | 5636| Promise\<void> | Promise used to return the result. If the item ID is sent, no value is returned; otherwise, an error object is returned.| 5637 5638**Error codes** 5639 5640For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5641 5642| ID| Error Message| 5643| -------- | ---------------------------------------- | 5644| 6600101 | Session service exception. | 5645| 6600102 | The session does not exist. | 5646| 6600103 | The session controller does not exist. | 5647 5648**Example** 5649 5650```ts 5651import { BusinessError } from '@ohos.base'; 5652 5653let queueItemId = 0; 5654avsessionController.skipToQueueItem(queueItemId).then(() => { 5655 console.info(`SkipToQueueItem successfully`); 5656}).catch((err: BusinessError) => { 5657 console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); 5658}); 5659``` 5660 5661### skipToQueueItem<sup>10+</sup> 5662 5663skipToQueueItem(itemId: number, callback: AsyncCallback\<void>): void 5664 5665Sends 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. 5666 5667**System capability**: SystemCapability.Multimedia.AVSession.Core 5668 5669**Parameters** 5670 5671| Name | Type | Mandatory| Description | 5672| -------- | --------------------- | ---- | ----------------------------------------------------------- | 5673| itemId | number | Yes | ID of an item in the playlist. | 5674| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the setting is successful, **err** is **undefined**; otherwise, **err** is an error object.| 5675 5676**Error codes** 5677 5678For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5679 5680| ID| Error Message| 5681| -------- | ---------------------------------------- | 5682| 6600101 | Session service exception. | 5683| 6600102 | The session does not exist. | 5684| 6600103 | The session controller does not exist. | 5685 5686**Example** 5687 5688```ts 5689import { BusinessError } from '@ohos.base'; 5690 5691let queueItemId = 0; 5692avsessionController.skipToQueueItem(queueItemId, (err: BusinessError) => { 5693 if (err) { 5694 console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); 5695 } else { 5696 console.info(`SkipToQueueItem successfully`); 5697 } 5698}); 5699``` 5700 5701### getOutputDevice<sup>10+</sup> 5702 5703getOutputDevice(): Promise\<OutputDeviceInfo> 5704 5705Obtains the output device information. This API uses a promise to return the result. 5706 5707**System capability**: SystemCapability.Multimedia.AVSession.Core 5708 5709**Return value** 5710 5711| Type | Description | 5712| ----------------------------------------------- | --------------------------------- | 5713| Promise<[OutputDeviceInfo](#outputdeviceinfo10)\> | Promise used to return the information obtained.| 5714 5715**Error codes** 5716 5717For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5718 5719| ID| Error Message| 5720| -------- | ---------------------------------------- | 5721| 600101 | Session service exception. | 5722| 600103 | The session controller does not exist. | 5723 5724**Example** 5725 5726```ts 5727import { BusinessError } from '@ohos.base'; 5728 5729avsessionController.getOutputDevice().then((deviceInfo: avSession.OutputDeviceInfo) => { 5730 console.info(`GetOutputDevice : SUCCESS`); 5731}).catch((err: BusinessError) => { 5732 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 5733}); 5734``` 5735 5736### getOutputDevice<sup>10+</sup> 5737 5738getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void 5739 5740Obtains the output device information. This API uses an asynchronous callback to return the result. 5741 5742**System capability**: SystemCapability.Multimedia.AVSession.Core 5743 5744**Parameters** 5745 5746| Name | Type | Mandatory| Description | 5747| -------- | ----------------------------------------------------- | ---- | ------------------------------ | 5748| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | Yes | Callback used to return the information obtained.| 5749 5750**Error codes** 5751 5752For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5753 5754| ID| Error Message| 5755| -------- | ---------------------------------------- | 5756| 600101 | Session service exception. | 5757| 600103 | The session controller does not exist. | 5758 5759**Example** 5760 5761```ts 5762import { BusinessError } from '@ohos.base'; 5763 5764avsessionController.getOutputDevice((err: BusinessError, deviceInfo: avSession.OutputDeviceInfo) => { 5765 if (err) { 5766 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 5767 } else { 5768 console.info(`GetOutputDevice : SUCCESS`); 5769 } 5770}); 5771``` 5772 5773### sendAVKeyEvent<sup>10+</sup> 5774 5775sendAVKeyEvent(event: KeyEvent): Promise\<void> 5776 5777Sends a key event to the session corresponding to this controller. This API uses a promise to return the result. 5778 5779**System capability**: SystemCapability.Multimedia.AVSession.Core 5780 5781**Parameters** 5782 5783| Name| Type | Mandatory| Description | 5784| ------ | ------------------------------------------------------------ | ---- | ---------- | 5785| event | [KeyEvent](js-apis-keyevent.md) | Yes | Key event.| 5786 5787**Error codes** 5788 5789For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5790 5791| ID| Error Message| 5792| -------- | ---------------------------------------- | 5793| 600101 | Session service exception. | 5794| 600102 | The session does not exist. | 5795| 600103 | The session controller does not exist. | 5796| 600105 | Invalid session command. | 5797| 600106 | The session is not activated. | 5798 5799**Return value** 5800 5801| Type | Description | 5802| -------------- | ----------------------------- | 5803| Promise\<void> | Promise used to return the result. If the event is sent, no value is returned; otherwise, an error object is returned.| 5804 5805**Example** 5806 5807```ts 5808import keyEvent from '@ohos.multimodalInput.keyEvent'; 5809import { BusinessError } from '@ohos.base'; 5810 5811let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; 5812let event: keyEvent.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}; 5813 5814avsessionController.sendAVKeyEvent(event).then(() => { 5815 console.info(`SendAVKeyEvent Successfully`); 5816}).catch((err: BusinessError) => { 5817 console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 5818}); 5819``` 5820 5821### sendAVKeyEvent<sup>10+</sup> 5822 5823sendAVKeyEvent(event: KeyEvent, callback: AsyncCallback\<void>): void 5824 5825Sends a key event to the session corresponding to this controller. This API uses an asynchronous callback to return the result. 5826 5827**System capability**: SystemCapability.Multimedia.AVSession.Core 5828 5829**Parameters** 5830 5831| Name | Type | Mandatory| Description | 5832| -------- | ------------------------------------------------------------ | ---- | ---------- | 5833| event | [KeyEvent](js-apis-keyevent.md) | Yes | Key event.| 5834| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the event is sent, **err** is **undefined**; otherwise, **err** is an error object.| 5835 5836**Error codes** 5837 5838For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5839 5840| ID| Error Message| 5841| -------- | ---------------------------------------- | 5842| 600101 | Session service exception. | 5843| 600102 | The session does not exist. | 5844| 600103 | The session controller does not exist. | 5845| 600105 | Invalid session command. | 5846| 600106 | The session is not activated. | 5847 5848**Example** 5849 5850```ts 5851import keyEvent from '@ohos.multimodalInput.keyEvent'; 5852import { BusinessError } from '@ohos.base'; 5853 5854let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; 5855let event: keyEvent.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}; 5856 5857avsessionController.sendAVKeyEvent(event, (err: BusinessError) => { 5858 if (err) { 5859 console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 5860 } else { 5861 console.info(`SendAVKeyEvent Successfully`); 5862 } 5863}); 5864``` 5865 5866### getLaunchAbility<sup>10+</sup> 5867 5868getLaunchAbility(): Promise\<WantAgent> 5869 5870Obtains the **WantAgent** object saved by the application in the session. This API uses a promise to return the result. 5871 5872**System capability**: SystemCapability.Multimedia.AVSession.Core 5873 5874**Return value** 5875 5876| Type | Description | 5877| ------------------------------------------------------- | ------------------------------------------------------------ | 5878| Promise<[WantAgent](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.| 5879 5880**Error codes** 5881 5882For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5883 5884| ID| Error Message| 5885| -------- | ---------------------------------------- | 5886| 6600101 | Session service exception. | 5887| 6600102 | The session does not exist. | 5888| 6600103 | The session controller does not exist. | 5889 5890**Example** 5891 5892```ts 5893import { BusinessError } from '@ohos.base'; 5894 5895avsessionController.getLaunchAbility().then((agent: object) => { 5896 console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); 5897}).catch((err: BusinessError) => { 5898 console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 5899}); 5900``` 5901 5902### getLaunchAbility<sup>10+</sup> 5903 5904getLaunchAbility(callback: AsyncCallback\<WantAgent>): void 5905 5906Obtains the **WantAgent** object saved by the application in the session. This API uses an asynchronous callback to return the result. 5907 5908**System capability**: SystemCapability.Multimedia.AVSession.Core 5909 5910**Parameters** 5911 5912| Name | Type | Mandatory| Description | 5913| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5914| callback | AsyncCallback<[WantAgent](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.| 5915 5916**Error codes** 5917 5918For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5919 5920| ID| Error Message| 5921| -------- | ---------------------------------------- | 5922| 6600101 | Session service exception. | 5923| 6600102 | The session does not exist. | 5924| 6600103 | The session controller does not exist. | 5925 5926**Example** 5927 5928```ts 5929import { BusinessError } from '@ohos.base'; 5930 5931avsessionController.getLaunchAbility((err: BusinessError, agent: object) => { 5932 if (err) { 5933 console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 5934 } else { 5935 console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); 5936 } 5937}); 5938``` 5939 5940### getRealPlaybackPositionSync<sup>10+</sup> 5941 5942getRealPlaybackPositionSync(): number 5943 5944Obtains the playback position. 5945 5946**System capability**: SystemCapability.Multimedia.AVSession.Core 5947 5948**Return value** 5949 5950| Type | Description | 5951| ------ | ------------------ | 5952| number | Playback position, in milliseconds.| 5953 5954**Error codes** 5955 5956For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5957 5958| ID| Error Message| 5959| -------- | ---------------------------------------- | 5960| 6600101 | Session service exception. | 5961| 6600103 | The session controller does not exist. | 5962 5963**Example** 5964 5965```ts 5966let time: number = avsessionController.getRealPlaybackPositionSync(); 5967``` 5968 5969### isActive<sup>10+</sup> 5970 5971isActive(): Promise\<boolean> 5972 5973Checks whether the session is activated. This API uses a promise to return the result. 5974 5975**System capability**: SystemCapability.Multimedia.AVSession.Core 5976 5977**Return value** 5978 5979| Type | Description | 5980| ----------------- | ------------------------------------------------------------ | 5981| Promise<boolean\> | Promise used to return the activation state. If the session is activated, **true** is returned; otherwise, **false** is returned.| 5982 5983**Error codes** 5984 5985For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 5986 5987| ID| Error Message| 5988| -------- | ---------------------------------------- | 5989| 6600101 | Session service exception. | 5990| 6600102 | The session does not exist. | 5991| 6600103 | The session controller does not exist. | 5992 5993**Example** 5994 5995```ts 5996import { BusinessError } from '@ohos.base'; 5997 5998avsessionController.isActive().then((isActive: boolean) => { 5999 console.info(`IsActive : SUCCESS : isactive : ${isActive}`); 6000}).catch((err: BusinessError) => { 6001 console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); 6002}); 6003``` 6004 6005### isActive<sup>10+</sup> 6006 6007isActive(callback: AsyncCallback\<boolean>): void 6008 6009Checks whether the session is activated. This API uses an asynchronous callback to return the result. 6010 6011**System capability**: SystemCapability.Multimedia.AVSession.Core 6012 6013**Parameters** 6014 6015| Name | Type | Mandatory| Description | 6016| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 6017| callback | AsyncCallback<boolean\> | Yes | Callback used to return the activation state. If the session is activated, **true** is returned; otherwise, **false** is returned.| 6018 6019**Error codes** 6020 6021For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6022 6023| ID| Error Message| 6024| -------- | ---------------------------------------- | 6025| 6600101 | Session service exception. | 6026| 6600102 | The session does not exist. | 6027| 6600103 | The session controller does not exist. | 6028 6029**Example** 6030 6031```ts 6032import { BusinessError } from '@ohos.base'; 6033 6034avsessionController.isActive((err: BusinessError, isActive: boolean) => { 6035 if (err) { 6036 console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); 6037 } else { 6038 console.info(`IsActive : SUCCESS : isactive : ${isActive}`); 6039 } 6040}); 6041``` 6042 6043### destroy<sup>10+</sup> 6044 6045destroy(): Promise\<void> 6046 6047Destroys this controller. A controller can no longer be used after being destroyed. This API uses a promise to return the result. 6048 6049**System capability**: SystemCapability.Multimedia.AVSession.Core 6050 6051**Return value** 6052 6053| Type | Description | 6054| -------------- | ----------------------------- | 6055| Promise\<void> | Promise used to return the result. If the controller is destroyed, no value is returned; otherwise, an error object is returned.| 6056 6057**Error codes** 6058 6059For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6060 6061| ID| Error Message| 6062| -------- | ---------------------------------------- | 6063| 6600101 | Session service exception. | 6064| 6600103 | The session controller does not exist. | 6065 6066**Example** 6067 6068```ts 6069import { BusinessError } from '@ohos.base'; 6070 6071avsessionController.destroy().then(() => { 6072 console.info(`Destroy : SUCCESS `); 6073}).catch((err: BusinessError) => { 6074 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 6075}); 6076``` 6077 6078### destroy<sup>10+</sup> 6079 6080destroy(callback: AsyncCallback\<void>): void 6081 6082Destroys this controller. A controller can no longer be used after being destroyed. This API uses an asynchronous callback to return the result. 6083 6084**System capability**: SystemCapability.Multimedia.AVSession.Core 6085 6086**Parameters** 6087 6088| Name | Type | Mandatory| Description | 6089| -------- | -------------------- | ---- | ---------- | 6090| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the controller is destroyed, **err** is **undefined**; otherwise, **err** is an error object.| 6091 6092**Error codes** 6093 6094For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6095 6096| ID| Error Message| 6097| -------- | ---------------------------------------- | 6098| 6600101 | Session service exception. | 6099| 6600103 | The session controller does not exist. | 6100 6101**Example** 6102 6103```ts 6104import { BusinessError } from '@ohos.base'; 6105 6106avsessionController.destroy((err: BusinessError) => { 6107 if (err) { 6108 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 6109 } else { 6110 console.info(`Destroy : SUCCESS `); 6111 } 6112}); 6113``` 6114 6115### getValidCommands<sup>10+</sup> 6116 6117getValidCommands(): Promise\<Array\<AVControlCommandType>> 6118 6119Obtains valid commands supported by the session. This API uses a promise to return the result. 6120 6121**System capability**: SystemCapability.Multimedia.AVSession.Core 6122 6123**Return value** 6124 6125| Type | Description | 6126| ------------------------------------------------------------ | --------------------------------- | 6127| Promise<Array<[AVControlCommandType](#avcontrolcommandtype10)\>\> | Promise used to return a set of valid commands.| 6128 6129**Error codes** 6130 6131For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6132 6133| ID| Error Message| 6134| -------- | ---------------------------------------- | 6135| 6600101 | Session service exception. | 6136| 6600102 | The session does not exist. | 6137| 6600103 | The session controller does not exist. | 6138 6139**Example** 6140 6141```ts 6142import { BusinessError } from '@ohos.base'; 6143 6144avsessionController.getValidCommands.then((validCommands: avSession.AVControlCommandType[]) => { 6145 console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); 6146}).catch((err: BusinessError) => { 6147 console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 6148}); 6149``` 6150 6151### getValidCommands<sup>10+</sup> 6152 6153getValidCommands(callback: AsyncCallback\<Array\<AVControlCommandType>>): void 6154 6155Obtains valid commands supported by the session. This API uses an asynchronous callback to return the result. 6156 6157**System capability**: SystemCapability.Multimedia.AVSession.Core 6158 6159**Parameters** 6160 6161| Name | Type | Mandatory| Description | 6162| -------- | ------------------------------------------------------------ | ---- | ------------------------------ | 6163| callback | AsyncCallback\<Array\<[AVControlCommandType](#avcontrolcommandtype10)\>\> | Yes | Callback used to return a set of valid commands.| 6164 6165**Error codes** 6166 6167For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6168 6169| ID| Error Message| 6170| -------- | ---------------------------------------- | 6171| 6600101 | Session service exception. | 6172| 6600102 | The session does not exist. | 6173| 6600103 | The session controller does not exist. | 6174 6175**Example** 6176 6177```ts 6178import { BusinessError } from '@ohos.base'; 6179 6180avsessionController.getValidCommands((err: BusinessError, validCommands: avSession.AVControlCommandType[]) => { 6181 if (err) { 6182 console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 6183 } else { 6184 console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); 6185 } 6186}); 6187``` 6188 6189### sendControlCommand<sup>10+</sup> 6190 6191sendControlCommand(command: AVControlCommand): Promise\<void> 6192 6193Sends a control command to the session through the controller. This API uses a promise to return the result. 6194 6195> **NOTE** 6196> 6197> 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. 6198 6199**System capability**: SystemCapability.Multimedia.AVSession.Core 6200 6201**Parameters** 6202 6203| Name | Type | Mandatory| Description | 6204| ------- | ------------------------------------- | ---- | ------------------------------ | 6205| command | [AVControlCommand](#avcontrolcommand10) | Yes | Command to send.| 6206 6207**Return value** 6208 6209| Type | Description | 6210| -------------- | ----------------------------- | 6211| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned.| 6212 6213**Error codes** 6214 6215For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6216 6217| ID| Error Message| 6218| -------- | ---------------------------------------- | 6219| 6600101 | Session service exception. | 6220| 6600102 | The session does not exist. | 6221| 6600103 | The session controller does not exist. | 6222| 6600105 | Invalid session command. | 6223| 6600106 | The session is not activated. | 6224| 6600107 | Too many commands or events. | 6225 6226**Example** 6227 6228```ts 6229import avSession from '@ohos.multimedia.avsession'; 6230import { BusinessError } from '@ohos.base'; 6231 6232let avCommand: avSession.AVControlCommand = {command:'play'}; 6233// let avCommand = {command:'pause'}; 6234// let avCommand = {command:'stop'}; 6235// let avCommand = {command:'playNext'}; 6236// let avCommand = {command:'playPrevious'}; 6237// let avCommand = {command:'fastForward'}; 6238// let avCommand = {command:'rewind'}; 6239// let avCommand = {command:'seek', parameter:10}; 6240// let avCommand = {command:'setSpeed', parameter:2.6}; 6241// let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; 6242// let avCommand = {command:'toggleFavorite', parameter:"false"}; 6243avsessionController.sendControlCommand(avCommand).then(() => { 6244 console.info(`SendControlCommand successfully`); 6245}).catch((err: BusinessError) => { 6246 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6247}); 6248``` 6249 6250### sendControlCommand<sup>10+</sup> 6251 6252sendControlCommand(command: AVControlCommand, callback: AsyncCallback\<void>): void 6253 6254Sends a control command to the session through the controller. This API uses an asynchronous callback to return the result. 6255 6256> **NOTE** 6257> 6258> 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. 6259 6260**System capability**: SystemCapability.Multimedia.AVSession.Core 6261 6262**Parameters** 6263 6264| Name | Type | Mandatory| Description | 6265| -------- | ------------------------------------- | ---- | ------------------------------ | 6266| command | [AVControlCommand](#avcontrolcommand10) | Yes | Command to send.| 6267| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 6268 6269**Error codes** 6270 6271For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6272 6273| ID| Error Message| 6274| -------- | ------------------------------- | 6275| 6600101 | Session service exception. | 6276| 6600102 | The session does not exist. | 6277| 6600103 | The session controller does not exist. | 6278| 6600105 | Invalid session command. | 6279| 6600106 | The session is not activated. | 6280| 6600107 | Too many commands or events. | 6281 6282**Example** 6283 6284```ts 6285import avSession from '@ohos.multimedia.avsession'; 6286import { BusinessError } from '@ohos.base'; 6287 6288let avCommand: avSession.AVControlCommand = {command:'play'}; 6289// let avCommand = {command:'pause'}; 6290// let avCommand = {command:'stop'}; 6291// let avCommand = {command:'playNext'}; 6292// let avCommand = {command:'playPrevious'}; 6293// let avCommand = {command:'fastForward'}; 6294// let avCommand = {command:'rewind'}; 6295// let avCommand = {command:'seek', parameter:10}; 6296// let avCommand = {command:'setSpeed', parameter:2.6}; 6297// let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; 6298// let avCommand = {command:'toggleFavorite', parameter:"false"}; 6299avsessionController.sendControlCommand(avCommand, (err: BusinessError) => { 6300 if (err) { 6301 console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6302 } else { 6303 console.error(`SendControlCommand successfully`); 6304 } 6305}); 6306``` 6307 6308### sendCommonCommand<sup>10+</sup> 6309 6310sendCommonCommand(command: string, args: {[key: string]: Object}): Promise\<void> 6311 6312Sends a custom control command to the session through the controller. This API uses a promise to return the result. 6313 6314**System capability**: SystemCapability.Multimedia.AVSession.Core 6315 6316**Parameters** 6317 6318| Name | Type | Mandatory| Description | 6319| ------- | ------------------------------------- | ---- | ------------------------------ | 6320| command | string | Yes | Name of the custom control command.| 6321| args | {[key: string]: any} | Yes | Parameters in key-value pair format carried in the custom control command.| 6322 6323> **NOTE** 6324> 6325> The **args** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want (Want)](./js-apis-app-ability-want.md). 6326 6327**Return value** 6328 6329| Type | Description | 6330| -------------- | ----------------------------- | 6331| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned.| 6332 6333**Error codes** 6334 6335For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6336 6337| ID| Error Message| 6338| -------- | ---------------------------------------- | 6339| 6600101 | Session service exception. | 6340| 6600102 | The session does not exist. | 6341| 6600103 | The session controller does not exist. | 6342| 6600105 | Invalid session command. | 6343| 6600106 | The session is not activated. | 6344| 6600107 | Too many commands or events. | 6345 6346**Example** 6347 6348```ts 6349import avSession from '@ohos.multimedia.avsession'; 6350import { BusinessError } from '@ohos.base'; 6351 6352let avSessionController: avSession.AVSessionController | undefined = undefined; 6353let currentAVSession: avSession.AVSession | undefined = undefined; 6354let tag = "createNewSession"; 6355let context: Context = getContext(this); 6356let sessionId: string = ""; 6357avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6358 if (err) { 6359 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6360 } else { 6361 currentAVSession = data; 6362 } 6363}); 6364if (currentAVSession !== undefined) { 6365 sessionId = (currentAVSession as avSession.AVSession).sessionId; 6366 avSession.createController(sessionId).then((controller: avSession.AVSessionController) => { 6367 avSessionController = controller; 6368 }).catch((err: BusinessError) => { 6369 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6370 }); 6371} 6372 6373let commandName = "my_command"; 6374if (avSessionController !== undefined) { 6375 (avSessionController as avSession.AVSessionController).sendCommonCommand(commandName, {command : "This is my command"}).then(() => { 6376 console.info(`SendCommonCommand successfully`); 6377 }).catch((err: BusinessError) => { 6378 console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6379 }) 6380} 6381``` 6382 6383### sendCommonCommand<sup>10+</sup> 6384 6385sendCommonCommand(command: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void 6386 6387Sends a custom control command to the session through the controller. This API uses an asynchronous callback to return the result. 6388 6389**System capability**: SystemCapability.Multimedia.AVSession.Core 6390 6391**Parameters** 6392 6393| Name | Type | Mandatory| Description | 6394| ------- | ------------------------------------- | ---- | ------------------------------ | 6395| command | string | Yes | Name of the custom control command.| 6396| args | {[key: string]: any} | Yes | Parameters in key-value pair format carried in the custom control command.| 6397| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the command is sent, **err** is **undefined**; otherwise, **err** is an error object. | 6398 6399> **NOTE** 6400> The **args** parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see [@ohos.app.ability.Want (Want)](./js-apis-app-ability-want.md). 6401 6402**Error codes** 6403 6404For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6405 6406| ID| Error Message| 6407| -------- | ------------------------------- | 6408| 6600101 | Session service exception. | 6409| 6600102 | The session does not exist. | 6410| 6600103 | The session controller does not exist. | 6411| 6600105 | Invalid session command. | 6412| 6600106 | The session is not activated. | 6413| 6600107 | Too many commands or events. | 6414 6415**Example** 6416 6417```ts 6418import avSession from '@ohos.multimedia.avsession'; 6419import { BusinessError } from '@ohos.base'; 6420let avSessionController: avSession.AVSessionController | undefined = undefined; 6421let currentAVSession: avSession.AVSession | undefined = undefined; 6422let tag = "createNewSession"; 6423let context: Context = getContext(this); 6424 6425avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6426 if (err) { 6427 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6428 } else { 6429 currentAVSession = data; 6430 } 6431}); 6432if (currentAVSession !== undefined) { 6433 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6434 avSessionController = controller; 6435 }).catch((err: BusinessError) => { 6436 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6437 }); 6438} 6439 6440let commandName = "my_command"; 6441if (avSessionController !== undefined) { 6442 (avSessionController as avSession.AVSessionController).sendCommonCommand(commandName, {command : "This is my command"}, (err: BusinessError) => { 6443 if(err) { 6444 console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6445 } 6446 }) 6447} 6448``` 6449 6450### getExtras<sup>10+</sup> 6451 6452getExtras(): Promise\<{[key: string]: Object}> 6453 6454Obtains the custom media packet set by the provider. This API uses a promise to return the result. 6455 6456**System capability**: SystemCapability.Multimedia.AVSession.Core 6457 6458**Return value** 6459 6460| Type | Description | 6461| ----------------------------------- | ----------------------------- | 6462| 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**.| 6463 6464**Error codes** 6465 6466For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6467 6468| ID| Error Message| 6469| -------- | ---------------------------------------- | 6470| 6600101 | Session service exception. | 6471| 6600102 | The session does not exist. | 6472| 6600103 | The session controller does not exist. | 6473| 6600105 | Invalid session command. | 6474| 6600107 | Too many commands or events. | 6475 6476**Example** 6477 6478```ts 6479import avSession from '@ohos.multimedia.avsession'; 6480import { BusinessError } from '@ohos.base'; 6481 6482let avSessionController: avSession.AVSessionController | undefined = undefined; 6483let currentAVSession: avSession.AVSession | undefined = undefined; 6484let tag = "createNewSession"; 6485let context: Context = getContext(this); 6486 6487avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6488 if (err) { 6489 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6490 } else { 6491 currentAVSession = data; 6492 } 6493}); 6494if (currentAVSession !== undefined) { 6495 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6496 avSessionController = controller; 6497 }).catch((err: BusinessError) => { 6498 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6499 }); 6500} 6501 6502if (avSessionController !== undefined) { 6503 (avSessionController as avSession.AVSessionController).getExtras().then((extras) => { 6504 console.info(`getExtras : SUCCESS : ${extras}`); 6505 }).catch((err: BusinessError) => { 6506 console.info(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); 6507 }); 6508} 6509``` 6510 6511### getExtras<sup>10+</sup> 6512 6513getExtras(callback: AsyncCallback\<{[key: string]: Object}>): void 6514 6515Obtains the custom media packet set by the provider. This API uses an asynchronous callback to return the result. 6516 6517**System capability**: SystemCapability.Multimedia.AVSession.Core 6518 6519**Parameters** 6520 6521| Name | Type | Mandatory| Description | 6522| -------- | ----------------------------------------- | ---- | -------------------------- | 6523| 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**.| 6524 6525**Error codes** 6526 6527For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6528 6529| ID| Error Message| 6530| -------- | ---------------------------------------- | 6531| 6600101 | Session service exception. | 6532| 6600102 | The session does not exist. | 6533| 6600103 | The session controller does not exist. | 6534| 6600105 | Invalid session command. | 6535| 6600107 | Too many commands or events. | 6536 6537**Example** 6538 6539```ts 6540import avSession from '@ohos.multimedia.avsession'; 6541import { BusinessError } from '@ohos.base'; 6542 6543let avSessionController: avSession.AVSessionController | undefined = undefined; 6544let currentAVSession: avSession.AVSession | undefined = undefined; 6545let tag = "createNewSession"; 6546let context: Context = getContext(this); 6547 6548avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6549 if (err) { 6550 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6551 } else { 6552 currentAVSession = data; 6553 } 6554}); 6555if (currentAVSession !== undefined) { 6556 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6557 avSessionController = controller; 6558 }).catch((err: BusinessError) => { 6559 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6560 }); 6561} 6562 6563if (avSessionController !== undefined) { 6564 (avSessionController as avSession.AVSessionController).getExtras((err, extras) => { 6565 if (err) { 6566 console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); 6567 } else { 6568 console.info(`getExtras : SUCCESS : ${extras}`); 6569 } 6570 }); 6571} 6572``` 6573 6574### on('metadataChange')<sup>10+</sup> 6575 6576on(type: 'metadataChange', filter: Array\<keyof AVMetadata> | 'all', callback: (data: AVMetadata) => void) 6577 6578Subscribes to metadata change events. 6579 6580**System capability**: SystemCapability.Multimedia.AVSession.Core 6581 6582**Parameters** 6583 6584| Name | Type | Mandatory| Description | 6585| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6586| type | string | Yes | Event type. The event **'metadataChange'** is triggered when the session metadata changes.| 6587| 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.| 6588| callback | (data: [AVMetadata](#avmetadata10)) => void | Yes | Callback used for subscription. The **data** parameter in the callback indicates the changed metadata. | 6589 6590**Error codes** 6591 6592For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6593 6594| ID| Error Message| 6595| -------- | ------------------------------ | 6596| 6600101 | Session service exception. | 6597| 6600103 | The session controller does not exist. | 6598 6599**Example** 6600 6601```ts 6602avsessionController.on('metadataChange', 'all', (metadata: avSession.AVMetadata) => { 6603 console.info(`on metadataChange assetId : ${metadata.assetId}`); 6604}); 6605 6606avsessionController.on('metadataChange', ['assetId', 'title', 'description'], (metadata: avSession.AVMetadata) => { 6607 console.info(`on metadataChange assetId : ${metadata.assetId}`); 6608}); 6609 6610``` 6611 6612### off('metadataChange')<sup>10+</sup> 6613 6614off(type: 'metadataChange', callback?: (data: AVMetadata) => void) 6615 6616Unsubscribes from metadata change events. This API is called by the controller. 6617 6618**System capability**: SystemCapability.Multimedia.AVSession.Core 6619 6620**Parameters** 6621 6622| Name | Type | Mandatory| Description | 6623| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------ | 6624| type | string | Yes | Event type, which is **'metadataChange'** in this case. | 6625| 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. | 6626 6627**Error codes** 6628 6629For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6630 6631| ID| Error Message| 6632| -------- | ---------------- | 6633| 6600101 | Session service exception. | 6634| 6600103 | The session controller does not exist. | 6635 6636**Example** 6637 6638```ts 6639avsessionController.off('metadataChange'); 6640``` 6641 6642### on('playbackStateChange')<sup>10+</sup> 6643 6644on(type: 'playbackStateChange', filter: Array\<keyof AVPlaybackState> | 'all', callback: (state: AVPlaybackState) => void) 6645 6646Subscribes to playback state change events. 6647 6648**System capability**: SystemCapability.Multimedia.AVSession.Core 6649 6650**Parameters** 6651 6652| Name | Type | Mandatory| Description | 6653| --------| -----------|-----|------------| 6654| type | string | Yes | Event type. The event **'playbackStateChange'** is triggered when the playback state changes.| 6655| 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.| 6656| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | Yes | Callback used for subscription. The **state** parameter in the callback indicates the changed playback state.| 6657 6658**Error codes** 6659 6660For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6661 6662| ID| Error Message| 6663| -------- | ------------------------------ | 6664| 6600101 | Session service exception. | 6665| 6600103 | The session controller does not exist. | 6666 6667**Example** 6668 6669```ts 6670avsessionController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { 6671 console.info(`on playbackStateChange state : ${playbackState.state}`); 6672}); 6673 6674avsessionController.on('playbackStateChange', ['state', 'speed', 'loopMode'], (playbackState: avSession.AVPlaybackState) => { 6675 console.info(`on playbackStateChange state : ${playbackState.state}`); 6676}); 6677``` 6678 6679### off('playbackStateChange')<sup>10+</sup> 6680 6681off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void) 6682 6683Unsubscribes from playback state change events. This API is called by the controller. 6684 6685**System capability**: SystemCapability.Multimedia.AVSession.Core 6686 6687**Parameters** 6688 6689| Name | Type | Mandatory| Description | 6690| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 6691| type | string | Yes | Event type, which is **'playbackStateChange'** in this case. | 6692| 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. | 6693 6694**Error codes** 6695 6696For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6697 6698| ID| Error Message| 6699| -------- | ---------------- | 6700| 6600101 | Session service exception. | 6701| 6600103 | The session controller does not exist. | 6702 6703**Example** 6704 6705```ts 6706avsessionController.off('playbackStateChange'); 6707``` 6708 6709### on('sessionDestroy')<sup>10+</sup> 6710 6711on(type: 'sessionDestroy', callback: () => void) 6712 6713Subscribes to session destruction events. 6714 6715**System capability**: SystemCapability.Multimedia.AVSession.Core 6716 6717**Parameters** 6718 6719| Name | Type | Mandatory| Description | 6720| -------- | ---------- | ---- | ------------------------------------------------------------ | 6721| type | string | Yes | Event type. The event **'sessionDestroy'** is triggered when a session is destroyed.| 6722| callback | () => void | Yes | Callback used for subscription. If the subscription is successful, **err** is **undefined**; otherwise, **err** is an error object. | 6723 6724**Error codes** 6725 6726For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6727 6728| ID| Error Message| 6729| -------- | ------------------------------ | 6730| 6600101 | Session service exception. | 6731| 6600103 | The session controller does not exist. | 6732 6733**Example** 6734 6735```ts 6736avsessionController.on('sessionDestroy', () => { 6737 console.info(`on sessionDestroy : SUCCESS `); 6738}); 6739``` 6740 6741### off('sessionDestroy')<sup>10+</sup> 6742 6743off(type: 'sessionDestroy', callback?: () => void) 6744 6745Unsubscribes from session destruction events. This API is called by the controller. 6746 6747**System capability**: SystemCapability.Multimedia.AVSession.Core 6748 6749**Parameters** 6750 6751| Name | Type | Mandatory| Description | 6752| -------- | ---------- | ---- | ----------------------------------------------------- | 6753| type | string | Yes | Event type, which is **'sessionDestroy'** in this case. | 6754| 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. | 6755 6756**Error codes** 6757 6758For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6759 6760| ID| Error Message| 6761| -------- | ---------------- | 6762| 6600101 | Session service exception. | 6763| 6600103 | The session controller does not exist. | 6764 6765**Example** 6766 6767```ts 6768avsessionController.off('sessionDestroy'); 6769``` 6770 6771### on('activeStateChange')<sup>10+</sup> 6772 6773on(type: 'activeStateChange', callback: (isActive: boolean) => void) 6774 6775Subscribes to session activation state change events. 6776 6777**System capability**: SystemCapability.Multimedia.AVSession.Core 6778 6779**Parameters** 6780 6781| Name | Type | Mandatory| Description | 6782| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 6783| type | string | Yes | Event type. The event **'activeStateChange'** is triggered when the activation state of the session changes.| 6784| 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. | 6785 6786**Error codes** 6787 6788For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6789 6790| ID| Error Message| 6791| -------- | ----------------------------- | 6792| 6600101 | Session service exception. | 6793| 6600103 |The session controller does not exist. | 6794 6795**Example** 6796 6797```ts 6798avsessionController.on('activeStateChange', (isActive: boolean) => { 6799 console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`); 6800}); 6801``` 6802 6803### off('activeStateChange')<sup>10+</sup> 6804 6805off(type: 'activeStateChange', callback?: (isActive: boolean) => void) 6806 6807Unsubscribes from session activation state change events. This API is called by the controller. 6808 6809**System capability**: SystemCapability.Multimedia.AVSession.Core 6810 6811**Parameters** 6812 6813| Name | Type | Mandatory| Description | 6814| -------- | --------------------------- | ---- | ----------------------------------------------------- | 6815| type | string | Yes | Event type, which is **'activeStateChange'** in this case. | 6816| 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. | 6817 6818**Error codes** 6819 6820For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6821 6822| ID| Error Message| 6823| -------- | ---------------- | 6824| 6600101 | Session service exception. | 6825| 6600103 | The session controller does not exist. | 6826 6827**Example** 6828 6829```ts 6830avsessionController.off('activeStateChange'); 6831``` 6832 6833### on('validCommandChange')<sup>10+</sup> 6834 6835on(type: 'validCommandChange', callback: (commands: Array\<AVControlCommandType>) => void) 6836 6837Subscribes to valid command change events. 6838 6839**System capability**: SystemCapability.Multimedia.AVSession.Core 6840 6841**Parameters** 6842 6843| Name | Type | Mandatory| Description | 6844| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6845| type | string | Yes | Event type. The event **'validCommandChange'** is triggered when the valid commands supported by the session changes.| 6846| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | Yes | Callback used for subscription. The **commands** parameter in the callback is a set of valid commands. | 6847 6848**Error codes** 6849 6850For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6851 6852| ID| Error Message| 6853| -------- | ------------------------------ | 6854| 6600101 | Session service exception. | 6855| 6600103 | The session controller does not exist. | 6856 6857**Example** 6858 6859```ts 6860avsessionController.on('validCommandChange', (validCommands: avSession.AVControlCommandType[]) => { 6861 console.info(`validCommandChange : SUCCESS : size : ${validCommands.length}`); 6862 console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); 6863}); 6864``` 6865 6866### off('validCommandChange')<sup>10+</sup> 6867 6868off(type: 'validCommandChange', callback?: (commands: Array\<AVControlCommandType>) => void) 6869 6870Unsubscribes from valid command change events. This API is called by the controller. 6871 6872**System capability**: SystemCapability.Multimedia.AVSession.Core 6873 6874**Parameters** 6875 6876| Name | Type | Mandatory| Description | 6877| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | 6878| type | string | Yes | Event type, which is **'validCommandChange'** in this case. | 6879| 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. | 6880 6881**Error codes** 6882 6883For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6884 6885| ID| Error Message | 6886| -------- | ---------------- | 6887| 6600101 | Session service exception. | 6888| 6600103 | The session controller does not exist. | 6889 6890**Example** 6891 6892```ts 6893avsessionController.off('validCommandChange'); 6894``` 6895 6896### on('outputDeviceChange')<sup>10+</sup> 6897 6898on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void 6899 6900Subscribes to output device change events. 6901 6902**System capability**: SystemCapability.Multimedia.AVSession.Core 6903 6904**Parameters** 6905 6906| Name | Type | Mandatory| Description | 6907| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 6908| type | string | Yes | Event type. The event **'outputDeviceChange'** is triggered when the output device changes.| 6909| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | Yes | Callback used for subscription. The **device** parameter in the callback indicates the output device information. | 6910 6911**Error codes** 6912 6913For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6914 6915| ID| Error Message| 6916| -------- | ----------------------- | 6917| 6600101 | Session service exception. | 6918| 6600103 | The session controller does not exist. | 6919 6920**Example** 6921 6922```ts 6923avsessionController.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { 6924 console.info(`on outputDeviceChange state: ${state}, device : ${device}`); 6925}); 6926``` 6927 6928### off('outputDeviceChange')<sup>10+</sup> 6929 6930off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void 6931 6932Unsubscribes from output device change events. This API is called by the controller. 6933 6934**System capability**: SystemCapability.Multimedia.AVSession.Core 6935 6936**Parameters** 6937 6938| Name | Type | Mandatory| Description | 6939| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | 6940| type | string | Yes | Event type, which is **'outputDeviceChange'** in this case. | 6941| 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. | 6942 6943**Error codes** 6944 6945For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6946 6947| ID | Error Message | 6948| -------- | ---------------- | 6949| 6600101 | Session service exception. | 6950| 6600103 | The session controller does not exist. | 6951 6952**Example** 6953 6954```ts 6955avsessionController.off('outputDeviceChange'); 6956``` 6957 6958### on('sessionEvent')<sup>10+</sup> 6959 6960on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key:string]: Object}) => void): void 6961 6962Subscribes to session event change events. This API is called by the controller. 6963 6964**System capability**: SystemCapability.Multimedia.AVSession.Core 6965 6966**Parameters** 6967 6968| Name | Type | Mandatory| Description | 6969| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6970| type | string | Yes | Event type. The event **'sessionEvent'** is triggered when the session event changes.| 6971| 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. | 6972 6973**Error codes** 6974 6975For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 6976 6977| ID| Error Message| 6978| -------- | ------------------------------ | 6979| 6600101 | Session service exception. | 6980| 6600103 | The session controller does not exist. | 6981 6982**Example** 6983 6984```ts 6985import avSession from '@ohos.multimedia.avsession'; 6986import { BusinessError } from '@ohos.base'; 6987 6988let avSessionController: avSession.AVSessionController | undefined = undefined; 6989let currentAVSession: avSession.AVSession | undefined = undefined; 6990let tag = "createNewSession"; 6991let context: Context = getContext(this); 6992 6993avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6994 if (err) { 6995 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6996 } else { 6997 currentAVSession = data; 6998 } 6999}); 7000if (currentAVSession !== undefined) { 7001 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 7002 avSessionController = controller; 7003 }).catch((err: BusinessError) => { 7004 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 7005 }); 7006} 7007 7008if (avSessionController !== undefined) { 7009 (avSessionController as avSession.AVSessionController).on('sessionEvent', (sessionEvent, args) => { 7010 console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`); 7011 }); 7012} 7013``` 7014 7015### off('sessionEvent')<sup>10+</sup> 7016 7017off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key:string]: Object}) => void): void 7018 7019Unsubscribes from session event change events. This API is called by the controller. 7020 7021**System capability**: SystemCapability.Multimedia.AVSession.Core 7022 7023**Parameters** 7024 7025| Name | Type | Mandatory| Description | 7026| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 7027| type | string | Yes | Event type, which is **'sessionEvent'** in this case. | 7028| 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. | 7029 7030**Error codes** 7031 7032For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 7033 7034| ID| Error Message| 7035| -------- | ---------------- | 7036| 6600101 | Session service exception. | 7037| 6600103 | The session controller does not exist. | 7038 7039**Example** 7040 7041```ts 7042avsessionController.off('sessionEvent'); 7043``` 7044 7045### on('queueItemsChange')<sup>10+</sup> 7046 7047on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void 7048 7049Subscribes to playlist item change events. This API is called by the controller. 7050 7051**System capability**: SystemCapability.Multimedia.AVSession.Core 7052 7053**Parameters** 7054 7055| Name | Type | Mandatory| Description | 7056| -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------------------------------- | 7057| type | string | Yes | Event type. The event **'queueItemsChange'** is triggered when one or more items in the playlist changes.| 7058| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | Yes | Callback used for subscription. The **items** parameter in the callback indicates the changed items in the playlist. | 7059 7060**Error codes** 7061 7062For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 7063 7064| ID| Error Message| 7065| -------- | ------------------------------ | 7066| 6600101 | Session service exception. | 7067| 6600103 | The session controller does not exist. | 7068 7069**Example** 7070 7071```ts 7072avsessionController.on('queueItemsChange', (items: avSession.AVQueueItem[]) => { 7073 console.info(`OnQueueItemsChange, items length is ${items.length}`); 7074}); 7075``` 7076 7077### off('queueItemsChange')<sup>10+</sup> 7078 7079off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void 7080 7081Unsubscribes from playback item change events. This API is called by the controller. 7082 7083**System capability**: SystemCapability.Multimedia.AVSession.Core 7084 7085**Parameters** 7086 7087| Name | Type | Mandatory| Description | 7088| -------- | ---------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------- | 7089| type | string | Yes | Event type, which is **'queueItemsChange'** in this case. | 7090| 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.| 7091 7092**Error codes** 7093 7094For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 7095 7096| ID| Error Message| 7097| -------- | ---------------- | 7098| 6600101 | Session service exception. | 7099| 6600103 | The session controller does not exist. | 7100 7101**Example** 7102 7103```ts 7104avsessionController.off('queueItemsChange'); 7105``` 7106 7107### on('queueTitleChange')<sup>10+</sup> 7108 7109on(type: 'queueTitleChange', callback: (title: string) => void): void 7110 7111Subscribes to playlist name change events. This API is called by the controller. 7112 7113**System capability**: SystemCapability.Multimedia.AVSession.Core 7114 7115**Parameters** 7116 7117| Name | Type | Mandatory| Description | 7118| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------- | 7119| type | string | Yes | Event type. The event **'queueTitleChange'** is triggered when the playlist name changes.| 7120| callback | (title: string) => void | Yes | Callback used for subscription. The **title** parameter in the callback indicates the changed playlist name. | 7121 7122**Error codes** 7123 7124For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 7125 7126| ID| Error Message| 7127| -------- | ------------------------------ | 7128| 6600101 | Session service exception. | 7129| 6600103 | The session controller does not exist. | 7130 7131**Example** 7132 7133```ts 7134avsessionController.on('queueTitleChange', (title: string) => { 7135 console.info(`queueTitleChange, title is ${title}`); 7136}); 7137``` 7138 7139### off('queueTitleChange')<sup>10+</sup> 7140 7141off(type: 'queueTitleChange', callback?: (title: string) => void): void 7142 7143Unsubscribes from playlist name change events. This API is called by the controller. 7144 7145**System capability**: SystemCapability.Multimedia.AVSession.Core 7146 7147**Parameters** 7148 7149| Name | Type | Mandatory| Description | 7150| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | 7151| type | string | Yes | Event type, which is **'queueTitleChange'** in this case. | 7152| 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.| 7153 7154**Error codes** 7155 7156For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 7157 7158| ID| Error Message| 7159| -------- | ---------------- | 7160| 6600101 | Session service exception. | 7161| 6600103 | The session controller does not exist. | 7162 7163**Example** 7164 7165```ts 7166avsessionController.off('queueTitleChange'); 7167``` 7168 7169### on('extrasChange')<sup>10+</sup> 7170 7171on(type: 'extrasChange', callback: (extras: {[key:string]: Object}) => void): void 7172 7173Subscribes to custom media packet change events. This API is called by the controller. 7174 7175**System capability**: SystemCapability.Multimedia.AVSession.Core 7176 7177**Parameters** 7178 7179| Name | Type | Mandatory| Description | 7180| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7181| type | string | Yes | Event type. The event **'extrasChange'** is triggered when the provider sets a custom media packet.| 7182| 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**. | 7183 7184**Error codes** 7185 7186For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 7187 7188| ID| Error Message| 7189| -------- | ------------------------------ | 7190| 6600101 | Session service exception. | 7191| 6600103 | The session controller does not exist. | 7192 7193**Example** 7194 7195```ts 7196import avSession from '@ohos.multimedia.avsession'; 7197import { BusinessError } from '@ohos.base'; 7198 7199let avSessionController: avSession.AVSessionController | undefined = undefined; 7200let currentAVSession: avSession.AVSession | undefined = undefined; 7201let tag = "createNewSession"; 7202let context: Context = getContext(this); 7203 7204avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 7205 if (err) { 7206 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 7207 } else { 7208 currentAVSession = data; 7209 } 7210}); 7211if (currentAVSession !== undefined) { 7212 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 7213 avSessionController = controller; 7214 }).catch((err: BusinessError) => { 7215 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 7216 }); 7217} 7218 7219if (avSessionController !== undefined) { 7220 (avSessionController as avSession.AVSessionController).on('extrasChange', (extras) => { 7221 console.info(`Caught extrasChange event,the new extra is: ${JSON.stringify(extras)}`); 7222 }); 7223} 7224``` 7225 7226### off('extrasChange')<sup>10+</sup> 7227 7228off(type: 'extrasChange', callback?: (extras: {[key:string]: Object}) => void): void 7229 7230Unsubscribes from custom media packet change events. This API is called by the controller. 7231 7232**System capability**: SystemCapability.Multimedia.AVSession.Core 7233 7234**Parameters** 7235 7236| Name | Type | Mandatory| Description | 7237| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | 7238| type | string | Yes | Event type, which is **'extrasChange'** in this case. | 7239| 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.| 7240 7241**Error codes** 7242 7243For details about the error codes, see [AVSession Management Error Codes](../errorcodes/errorcode-avsession.md). 7244 7245| ID| Error Message| 7246| -------- | ---------------- | 7247| 6600101 | Session service exception. | 7248| 6600103 | The session controller does not exist. | 7249 7250**Example** 7251 7252```ts 7253avsessionController.off('extrasChange'); 7254``` 7255 7256## AVControlCommandType<sup>10+</sup> 7257 7258Enumerates the commands that can be sent to a session. 7259 7260**System capability**: SystemCapability.Multimedia.AVSession.Core 7261 7262| Name | Type | Description | 7263| -------------- | ------ | ------------ | 7264| play | string | Play the media. | 7265| pause | string | Pause the playback. | 7266| stop | string | Stop the playback. | 7267| playNext | string | Play the next media asset. | 7268| playPrevious | string | Play the previous media asset. | 7269| fastForward | string | Fast-forward. | 7270| rewind | string | Rewind. | 7271| seek | string | Seek to a playback position.| 7272| setSpeed | string | Set the playback speed.| 7273| setLoopMode | string | Set the loop mode.| 7274| toggleFavorite | string | Favorite the media asset. | 7275 7276## AVControlCommand<sup>10+</sup> 7277 7278Describes the command that can be sent to the session. 7279 7280**System capability**: SystemCapability.Multimedia.AVSession.Core 7281 7282| Name | Type | Mandatory| Description | 7283| --------- | ------------------------------------------------- | ---- | -------------- | 7284| command | [AVControlCommandType](#avcontrolcommandtype10) | Yes | Command. | 7285| parameter | [LoopMode](#loopmode10) | string | number | No | Parameters carried in the command.| 7286 7287## AVSessionErrorCode<sup>10+</sup> 7288 7289Enumerates the error codes used in the media session. 7290 7291**System capability**: SystemCapability.Multimedia.AVSession.Core 7292 7293| Name | Value | Description | 7294| -------------------------------------- | ------- | ------------------------------- | 7295| ERR_CODE_SERVICE_EXCEPTION | 6600101 | Session service exception. | 7296| ERR_CODE_SESSION_NOT_EXIST | 6600102 | The session does not exist. | 7297| ERR_CODE_CONTROLLER_NOT_EXIST | 6600103 | The session controller does not exist. | 7298| ERR_CODE_REMOTE_CONNECTION_ERR | 6600104 | The remote session connection failed. | 7299| ERR_CODE_COMMAND_INVALID | 6600105 | Invalid session command. | 7300| ERR_CODE_SESSION_INACTIVE | 6600106 | The session is not activated. | 7301| ERR_CODE_MESSAGE_OVERLOAD | 6600107 | Too many commands or events. | 7302| ERR_CODE_DEVICE_CONNECTION_FAILED | 6600108 | Device connecting failed. | 7303| ERR_CODE_REMOTE_CONNECTION_NOT_EXIST | 6600109 | The remote connection is not established. | 7304