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