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