1# @ohos.multimedia.avsession (媒体会话管理) 2 3媒体会话管理提供媒体播控相关功能的接口,目的是让应用接入播控中心。 4 5该模块提供以下媒体会话相关的常用功能: 6 7- [AVSession](#avsession10) : 会话,可用于设置元数据、播放状态信息等操作。 8- [AVSessionController](#avsessioncontroller10): 会话控制器,可用于查看会话ID,完成对会话发送命令及事件,获取会话元数据、播放状态信息等操作。 9- [AVCastController](#avcastcontroller10): 投播控制器,可用于投播场景下,完成播放控制、远端播放状态监听、远端播放状态信息获取等操作。 10 11> **说明:** 12> 13> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## 导入模块 16 17```ts 18import avSession from '@ohos.multimedia.avsession'; 19``` 20 21## avSession.createAVSession<sup>10+</sup> 22 23createAVSession(context: Context, tag: string, type: AVSessionType): Promise\<AVSession> 24 25创建会话对象,一个Ability只能存在一个会话,重复创建会失败,结果通过Promise异步回调方式返回。 26 27**系统能力:** SystemCapability.Multimedia.AVSession.Core 28 29**参数:** 30 31| 参数名 | 类型 | 必填 | 说明 | 32| ------ | ------------------------------- | ---- | ------------------------------ | 33| context| [Context](js-apis-inner-app-context.md) | 是| 应用上下文,提供获取应用程序环境信息的能力。 | 34| tag | string | 是 | 会话的自定义名称。 | 35| type | [AVSessionType](#avsessiontype10) | 是 | 会话类型,当前支持音频和视频。 | 36 37**返回值:** 38 39| 类型 | 说明 | 40| --------------------------------- | ------------------------------------------------------------ | 41| Promise<[AVSession](#avsession10)\> | Promise对象。回调返回会话实例对象,可用于获取会话ID,以及设置元数据、播放状态,发送按键事件等操作。| 42 43**错误码:** 44 45以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 46 47| 错误码ID | 错误信息 | 48| -------- | ---------------------------------------- | 49| 6600101 | Session service exception. | 50 51**示例:** 52 53```ts 54import { BusinessError } from '@ohos.base'; 55 56let currentAVSession: avSession.AVSession; 57let tag = "createNewSession"; 58let context: Context = getContext(this); 59let sessionId: string; //供后续函数入参使用 60 61avSession.createAVSession(context, tag, "audio").then((data: avSession.AVSession) => { 62 currentAVSession = data; 63 sessionId = currentAVSession.sessionId; 64 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 65}).catch((err: BusinessError) => { 66 console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 67}); 68``` 69 70## avSession.createAVSession<sup>10+</sup> 71 72createAVSession(context: Context, tag: string, type: AVSessionType, callback: AsyncCallback\<AVSession>): void 73 74创建会话对象,一个Ability只能存在一个会话,重复创建会失败,结果通过callback异步回调方式返回。 75 76**系统能力:** SystemCapability.Multimedia.AVSession.Core 77 78**参数:** 79 80| 参数名 | 类型 | 必填 | 说明 | 81| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 82| context| [Context](js-apis-inner-app-context.md) | 是| 应用上下文,提供获取应用程序环境信息的能力。 | 83| tag | string | 是 | 会话的自定义名称。 | 84| type | [AVSessionType](#avsessiontype10) | 是 | 会话类型,当前支持音频和视频。 | 85| callback | AsyncCallback<[AVSession](#avsession10)\> | 是 | 回调函数。回调返回会话实例对象,可用于获取会话ID,以及设置元数据、播放状态,发送按键事件等操作。 | 86 87**错误码:** 88 89以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 90 91| 错误码ID | 错误信息 | 92| -------- | ---------------------------------------- | 93| 6600101 | Session service exception. | 94 95**示例:** 96 97```ts 98import { BusinessError } from '@ohos.base'; 99 100let currentAVSession: avSession.AVSession; 101let tag = "createNewSession"; 102let context: Context = getContext(this); 103let sessionId: string; //供后续函数入参使用 104 105avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 106 if (err) { 107 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 108 } else { 109 currentAVSession = data; 110 sessionId = currentAVSession.sessionId; 111 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 112 } 113}); 114``` 115 116## avSession.getAllSessionDescriptors 117 118getAllSessionDescriptors(): Promise\<Array\<Readonly\<AVSessionDescriptor>>> 119 120获取所有会话的相关描述。结果通过Promise异步回调方式返回。 121 122**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 123 124**系统能力:** SystemCapability.Multimedia.AVSession.Manager 125 126**系统接口:** 该接口为系统接口。 127 128**返回值:** 129 130| 类型 | 说明 | 131| ------------------------------------------------------------ | --------------------------------------------- | 132| Promise\<Array\<Readonly\<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | Promise对象。返回所有会话描述的只读对象。 | 133 134**错误码:** 135 136以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 137 138| 错误码ID | 错误信息 | 139| -------- | ---------------------------------------- | 140| 6600101 | Session service exception. | 141 142**示例:** 143 144```ts 145import { BusinessError } from '@ohos.base'; 146 147avSession.getAllSessionDescriptors().then((descriptors: avSession.AVSessionDescriptor[]) => { 148 console.info(`getAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); 149 if (descriptors.length > 0 ) { 150 console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); 151 console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); 152 console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); 153 } 154}).catch((err: BusinessError) => { 155 console.error(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); 156}); 157``` 158 159## avSession.getAllSessionDescriptors 160 161getAllSessionDescriptors(callback: AsyncCallback\<Array\<Readonly\<AVSessionDescriptor>>>): void 162 163获取所有会话的相关描述。结果通过callback异步回调方式返回。 164 165**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES 166 167**系统能力:** SystemCapability.Multimedia.AVSession.Manager 168 169**系统接口:** 该接口为系统接口。 170 171**参数:** 172 173| 参数名 | 类型 | 必填 | 说明 | 174| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 175| callback | AsyncCallback<Array<Readonly<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | 是 | 回调函数。返回所有会话描述的只读对象。 | 176 177**错误码:** 178 179以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 180 181| 错误码ID | 错误信息 | 182| -------- | ---------------------------------------- | 183| 6600101 |Session service exception. | 184 185**示例:** 186 187```ts 188import { BusinessError } from '@ohos.base'; 189 190avSession.getAllSessionDescriptors((err: BusinessError, descriptors: avSession.AVSessionDescriptor[]) => { 191 if (err) { 192 console.error(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); 193 } else { 194 console.info(`GetAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); 195 if (descriptors.length > 0 ) { 196 console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); 197 console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); 198 console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); 199 } 200 } 201}); 202``` 203 204## avSession.getHistoricalSessionDescriptors<sup>10+</sup> 205 206getHistoricalSessionDescriptors(maxSize?: number): Promise\<Array\<Readonly\<AVSessionDescriptor>>> 207 208获取所有已被销毁的会话相关描述。结果通过Promise异步回调方式返回。 209 210**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES 211 212**系统能力:** SystemCapability.Multimedia.AVSession.Manager 213 214**系统接口:** 该接口为系统接口。 215 216**参数:** 217 218| 参数名 | 类型 | 必填 | 说明 | 219| -------- | ------ | ---- | -----------------------------------------------------------------| 220| maxSize | number | 否 | 指定获取描述符数量的最大值,可选范围是0-10,不填则取默认值,默认值为3。| 221 222**返回值:** 223 224| 类型 | 说明 | 225| --------------------------------------------------------------------------- | -------------------------------------- | 226| Promise\<Array\<Readonly\<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | Promise对象。返回所有会话描述的只读对象。 | 227 228**错误码:** 229 230以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 231 232| 错误码ID | 错误信息 | 233| -------- | ---------------------------------------- | 234| 6600101 | Session service exception. | 235 236**示例:** 237 238```ts 239import { BusinessError } from '@ohos.base'; 240 241avSession.getHistoricalSessionDescriptors().then((descriptors: avSession.AVSessionDescriptor[]) => { 242 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); 243 if (descriptors.length > 0 ) { 244 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); 245 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); 246 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); 247 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`); 248 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`); 249 } 250}).catch((err: BusinessError) => { 251 console.error(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); 252}); 253``` 254 255## avSession.getHistoricalSessionDescriptors<sup>10+</sup> 256 257getHistoricalSessionDescriptors(maxSize: number, callback: AsyncCallback\<Array\<Readonly\<AVSessionDescriptor>>>): void 258 259获取所有已被销毁的会话相关描述。结果通过Promise异步回调方式返回。 260 261**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES。 262 263**系统能力:** SystemCapability.Multimedia.AVSession.Manager 264 265**系统接口:** 该接口为系统接口。 266 267**参数:** 268 269| 参数名 | 类型 | 必填 | 说明 | 270| -------- | ------------------------------------------------------------------------------ | ---- | -----------------------------------------------------------------| 271| maxSize | number | 是 | 指定获取描述符数量的最大值,可选范围是0-10,不填则取默认值,默认值为3。| 272| callback | AsyncCallback<Array<Readonly<[AVSessionDescriptor](#avsessiondescriptor)\>\>\> | 是 | 回调函数。返回所有会话描述的只读对象。 | 273 274**错误码:** 275 276以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 277 278| 错误码ID | 错误信息 | 279| -------- | ---------------------------------------- | 280| 6600101 |Session service exception. | 281 282**示例:** 283 284```ts 285import { BusinessError } from '@ohos.base'; 286 287avSession.getHistoricalSessionDescriptors(1, (err: BusinessError, descriptors: avSession.AVSessionDescriptor[]) => { 288 if (err) { 289 console.error(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); 290 } else { 291 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); 292 if (descriptors.length > 0 ) { 293 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); 294 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); 295 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); 296 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`); 297 console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`); 298 } 299 } 300}); 301``` 302 303## avSession.createController 304 305createController(sessionId: string): Promise\<AVSessionController> 306 307根据会话ID创建会话控制器,可以创建多个会话控制器。结果通过Promise异步回调方式返回。 308 309**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 310 311**系统能力:** SystemCapability.Multimedia.AVSession.Manager 312 313**系统接口:** 该接口为系统接口。 314 315**参数:** 316 317| 参数名 | 类型 | 必填 | 说明 | 318| --------- | ------ | ---- | -------- | 319| sessionId | string | 是 | 会话ID,如果提供 'default',系统将创建一个默认控制器,用于控制系统默认会话。 | 320 321**返回值:** 322 323| 类型 | 说明 | 324| ----------------------------------------------------- | ------------------------------------------------------------ | 325| Promise<[AVSessionController](#avsessioncontroller10)\> | Promise对象。返回会话控制器实例,可查看会话ID,<br>并完成对会话发送命令及事件,获取元数据、播放状态信息等操作。| 326 327**错误码:** 328 329以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 330 331| 错误码ID | 错误信息 | 332| -------- | ---------------------------------------- | 333| 6600101 | Session service exception. | 334| 6600102 | The session does not exist. | 335 336**示例:** 337 338```ts 339import { BusinessError } from '@ohos.base'; 340 341let currentAVcontroller: avSession.AVSessionController | undefined = undefined; 342currentAvSession.createController(sessionId).then((avcontroller: avSession.AVSessionController) => { 343 currentAVcontroller = avcontroller; 344 console.info('CreateController : SUCCESS '); 345}).catch((err: BusinessError) => { 346 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 347}); 348``` 349 350## avSession.createController 351 352createController(sessionId: string, callback: AsyncCallback\<AVSessionController>): void 353 354根据会话ID创建会话控制器,可以创建多个会话控制器。结果通过callback异步回调方式返回。 355 356**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 357 358**系统能力:** SystemCapability.Multimedia.AVSession.Manager 359 360**系统接口:** 该接口为系统接口。 361 362**参数:** 363 364| 参数名 | 类型 | 必填 | 说明 | 365| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 366| sessionId | string | 是 | 会话ID,如果提供 'default',系统将创建一个默认控制器,用于控制系统默认会话。 | 367| callback | AsyncCallback<[AVSessionController](#avsessioncontroller10)\> | 是 | 回调函数。返回会话控制器实例,可查看会话ID,<br>并完成对会话发送命令及事件,获取元数据、播放状态信息等操作。 | 368 369**错误码:** 370 371以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 372 373| 错误码ID | 错误信息 | 374| -------- | ---------------------------------------- | 375| 6600101 | Session service exception. | 376| 6600102 | The session does not exist. | 377 378**示例:** 379 380```ts 381import { BusinessError } from '@ohos.base'; 382 383let currentAVcontroller: avSession.AVSessionController | undefined = undefined; 384currentAvSession.createController(sessionId, (err: BusinessError, avcontroller: avSession.AVSessionController) => { 385 if (err) { 386 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 387 } else { 388 currentAVcontroller = avcontroller; 389 console.info('CreateController : SUCCESS '); 390 } 391}); 392``` 393 394## avSession.castAudio 395 396castAudio(session: SessionToken | 'all', audioDevices: Array<audio.AudioDeviceDescriptor>): Promise\<void> 397 398投播会话到指定设备列表。结果通过Promise异步回调方式返回。 399 400调用此接口之前,需要导入`ohos.multimedia.audio`模块获取AudioDeviceDescriptor的相关描述。 401 402**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 403 404**系统能力:** SystemCapability.Multimedia.AVSession.Manager 405 406**系统接口:** 该接口为系统接口。 407 408**参数:** 409 410| 参数名 | 类型 | 必填 | 说明 | 411| ------------ | -------------- |------|------| 412| session | [SessionToken](#sessiontoken) | 'all' | 是 | 会话令牌。SessionToken表示单个token;字符串`'all'`指所有token。 | 413| audioDevices | Array\<[audio.AudioDeviceDescriptor](js-apis-audio.md#audiodevicedescriptor)\> | 是 | 媒体设备列表。 | 414 415**返回值:** 416 417| 类型 | 说明 | 418| -------------- | ----------------------------- | 419| Promise\<void> | Promise对象。当投播成功,无返回结果,否则返回错误对象。 | 420 421**错误码:** 422 423以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 424 425| 错误码ID | 错误信息 | 426| -------- | ---------------------------------------- | 427| 6600101 | Session service exception. | 428| 6600102 | The session does not exist. | 429| 6600104 | The remote session connection failed. | 430 431**示例:** 432 433```ts 434import audio from '@ohos.multimedia.audio'; 435import { BusinessError } from '@ohos.base'; 436 437let audioManager = audio.getAudioManager(); 438let audioRoutingManager = audioManager.getRoutingManager(); 439let audioDevices: audio.AudioDeviceDescriptors | undefined = undefined; 440audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => { 441 audioDevices = data; 442 console.info(`Promise returned to indicate that the device list is obtained.`); 443}).catch((err: BusinessError) => { 444 console.error(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`); 445}); 446 447if (audioDevices !== undefined) { 448 avSession.castAudio('all', audioDevices as audio.AudioDeviceDescriptors).then(() => { 449 console.info(`CreateController : SUCCESS`); 450 }).catch((err: BusinessError) => { 451 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 452 }); 453} 454``` 455 456## avSession.castAudio 457 458castAudio(session: SessionToken | 'all', audioDevices: Array<audio.AudioDeviceDescriptor>, callback: AsyncCallback\<void>): void 459 460投播会话到指定设备列表。结果通过callback异步回调方式返回。 461 462需要导入`ohos.multimedia.audio`模块获取AudioDeviceDescriptor的相关描述。 463 464**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 465 466**系统能力:** SystemCapability.Multimedia.AVSession.Manager 467 468**系统接口:** 该接口为系统接口。 469 470**参数:** 471 472| 参数名 | 类型 | 必填 | 说明 | 473| ------------ |--------------------------------------------| ---- | ------------------------------------------------------------ | 474| session | [SessionToken](#sessiontoken) | 'all' | 是 | 会话令牌。SessionToken表示单个token;字符串`'all'`指所有token。 | 475| audioDevices | Array\<[audio.AudioDeviceDescriptor](js-apis-audio.md#audiodevicedescriptor)\> | 是 | 媒体设备列表。 | 476| callback | AsyncCallback\<void> | 是 | 回调函数。当投播成功,err为undefined,否则返回错误对象。 | 477 478**错误码:** 479 480以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 481 482| 错误码ID | 错误信息 | 483| -------- | ---------------------------------------- | 484| 6600101 | Session service exception. | 485| 6600102 | The session does not exist. | 486| 6600104 | The remote session connection failed. | 487 488**示例:** 489 490```ts 491import audio from '@ohos.multimedia.audio'; 492import { BusinessError } from '@ohos.base'; 493 494let audioManager = audio.getAudioManager(); 495let audioRoutingManager = audioManager.getRoutingManager(); 496let audioDevices: audio.AudioDeviceDescriptors | undefined = undefined; 497audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => { 498 audioDevices = data; 499 console.info(`Promise returned to indicate that the device list is obtained.`); 500}).catch((err: BusinessError) => { 501 console.error(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`); 502}); 503 504if (audioDevices !== undefined) { 505 avSession.castAudio('all', audioDevices as audio.AudioDeviceDescriptors, (err: BusinessError) => { 506 if (err) { 507 console.error(`CastAudio BusinessError: code: ${err.code}, message: ${err.message}`); 508 } else { 509 console.info(`CastAudio : SUCCESS `); 510 } 511 }); 512} 513``` 514 515## SessionToken 516 517会话令牌的信息。 518 519**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 520 521**系统能力:** SystemCapability.Multimedia.AVSession.Manager 522 523**系统接口:** 该接口为系统接口。 524 525| 名称 | 类型 | 必填 | 说明 | 526| :-------- | :----- | :--- | :----------- | 527| sessionId | string | 是 | 会话ID | 528| pid | number | 否 | 会话的进程ID | 529| uid | number | 否 | 用户ID | 530 531## avSession.on('sessionCreate') 532 533on(type: 'sessionCreate', callback: (session: AVSessionDescriptor) => void): void 534 535会话的创建监听事件。 536 537**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 538 539**系统能力:** SystemCapability.Multimedia.AVSession.Manager 540 541**系统接口:** 该接口为系统接口。 542 543**参数:** 544 545| 参数名 | 类型 | 必填 | 说明 | 546| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 547| type | string | 是 | 事件回调类型,支持的事件是'sessionCreate'`:会话创建事件,检测到会话创建时触发。| 548| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | 是 | 回调函数。参数为会话相关描述。 | 549 550**错误码:** 551 552以下错误码的详细介绍请参见[ohos.multimedia.avsession(多媒体会话)错误码](../errorcodes/errorcode-avsession.md)。 553 554| 错误码ID | 错误信息 | 555| -------- | ---------------------------------------- | 556| 6600101 | Session service exception. | 557 558**示例:** 559 560```ts 561avSession.on('sessionCreate', (descriptor: avSession.AVSessionDescriptor) => { 562 console.info(`on sessionCreate : isActive : ${descriptor.isActive}`); 563 console.info(`on sessionCreate : type : ${descriptor.type}`); 564 console.info(`on sessionCreate : sessionTag : ${descriptor.sessionTag}`); 565}); 566 567``` 568 569## avSession.on('sessionDestroy') 570 571on(type: 'sessionDestroy', callback: (session: AVSessionDescriptor) => void): void 572 573会话的销毁监听事件。 574 575**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 576 577**系统能力:** SystemCapability.Multimedia.AVSession.Manager 578 579**系统接口:** 该接口为系统接口。 580 581**参数:** 582 583| 参数名 | 类型 | 必填 | 说明 | 584| -------- | ---------------| ---- | ------------------------------------------------------------ | 585| type | string | 是 | 事件回调类型,支持的事件包括是`'sessionDestroy'`:会话销毁事件,检测到会话销毁时触发。| 586| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | 是 | 回调函数。参数为会话相关描述。 | 587 588**错误码:** 589 590以下错误码的详细介绍请参见[ohos.multimedia.avsession(多媒体会话)错误码](../errorcodes/errorcode-avsession.md)。 591 592| 错误码ID | 错误信息 | 593| -------- | ---------------------------------------- | 594| 6600101 | Session service exception. | 595 596**示例:** 597 598```ts 599avSession.on('sessionDestroy', (descriptor: avSession.AVSessionDescriptor) => { 600 console.info(`on sessionDestroy : isActive : ${descriptor.isActive}`); 601 console.info(`on sessionDestroy : type : ${descriptor.type}`); 602 console.info(`on sessionDestroy : sessionTag : ${descriptor.sessionTag}`); 603}); 604``` 605 606## avSession.on('topSessionChange') 607 608on(type: 'topSessionChange', callback: (session: AVSessionDescriptor) => void): void 609 610最新会话变更的监听事件。 611 612**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 613 614**系统能力:** SystemCapability.Multimedia.AVSession.Manager 615 616**系统接口:** 该接口为系统接口。 617 618**参数:** 619 620| 参数名 | 类型 | 必填 | 说明 | 621| -------- | --------------------| ---- | ------------------------------------------------------------ | 622| type | string | 是 | 事件回调类型,支持的事件包括是 `'topSessionChange'`:最新会话的变化事件,检测到最新的会话改变时触发。| 623| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | 是 | 回调函数。参数为会话相关描述。 | 624 625**错误码:** 626 627以下错误码的详细介绍请参见[ohos.multimedia.avsession(多媒体会话)错误码](../errorcodes/errorcode-avsession.md)。 628 629| 错误码ID | 错误信息 | 630| -------- | ---------------------------------------- | 631| 6600101 | Session service exception. | 632 633**示例:** 634 635```ts 636avSession.on('topSessionChange', (descriptor: avSession.AVSessionDescriptor) => { 637 console.info(`on topSessionChange : isActive : ${descriptor.isActive}`); 638 console.info(`on topSessionChange : type : ${descriptor.type}`); 639 console.info(`on topSessionChange : sessionTag : ${descriptor.sessionTag}`); 640}); 641``` 642 643## avSession.off('sessionCreate') 644 645off(type: 'sessionCreate', callback?: (session: AVSessionDescriptor) => void): void 646 647取消会话创建事件监听,取消后,不再进行该事件的监听。 648 649**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 650 651**系统能力:** SystemCapability.Multimedia.AVSession.Manager 652 653**系统接口:** 该接口为系统接口。 654 655**参数:** 656 657| 参数名 | 类型 | 必填 | 说明 | 658| -------- | ----------| ---- | ----------| 659| type | string | 是 | 事件回调类型,支持的事件为:`'sessionCreate'`。| 660| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为会话相关描述,为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 661 662**错误码:** 663 664以下错误码的详细介绍请参见[ohos.multimedia.avsession(多媒体会话)错误码](../errorcodes/errorcode-avsession.md)。 665 666| 错误码ID | 错误信息 | 667| -------- | ---------------------------------------- | 668| 6600101 | Session service exception. | 669 670**示例:** 671 672```ts 673avSession.off('sessionCreate'); 674``` 675 676## avSession.off('sessionDestroy') 677 678off(type: 'sessionDestroy', callback?: (session: AVSessionDescriptor) => void): void 679 680取消会话销毁事件监听,取消后,不再进行该事件的监听。 681 682**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 683 684**系统能力:** SystemCapability.Multimedia.AVSession.Manager 685 686**系统接口:** 该接口为系统接口。 687 688**参数:** 689 690| 参数名 | 类型 | 必填 | 说明 | 691| -------- | -----------| ---- | -------------------------| 692| type | string | 是 | 事件回调类型,支持的事件为`'sessionDestroy'`。| 693| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为会话相关描述,为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。| 694 695**错误码:** 696 697以下错误码的详细介绍请参见[ohos.multimedia.avsession(多媒体会话)错误码](../errorcodes/errorcode-avsession.md)。 698 699| 错误码ID | 错误信息 | 700| -------- | ---------------------------------------- | 701| 6600101 | Session service exception. | 702 703**示例:** 704 705```ts 706avSession.off('sessionDestroy'); 707``` 708 709## avSession.off('topSessionChange') 710 711off(type: 'topSessionChange', callback?: (session: AVSessionDescriptor) => void): void 712 713取消最新会话变更事件监听,取消后,不再进行该事件的监听。 714 715**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 716 717**系统能力:** SystemCapability.Multimedia.AVSession.Manager 718 719**系统接口:** 该接口为系统接口。 720 721**参数:** 722 723| 参数名 | 类型 | 必填 | 说明 | 724| -------- | -----------------| ---- | ---------------------------- | 725| type | string | 是 | 事件回调类型,支持的事件为`'topSessionChange'`。| 726| callback | (session: [AVSessionDescriptor](#avsessiondescriptor)) => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为会话相关描述,为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 727 728**错误码:** 729 730以下错误码的详细介绍请参见[ohos.multimedia.avsession(多媒体会话)错误码](../errorcodes/errorcode-avsession.md)。 731 732| 错误码ID | 错误信息 | 733| -------- | ---------------------------------------- | 734| 6600101 | Session service exception. | 735 736**示例:** 737 738```ts 739avSession.off('topSessionChange'); 740``` 741 742## avSession.on('sessionServiceDie') 743 744on(type: 'sessionServiceDie', callback: () => void): void 745 746监听会话的服务死亡事件。通知应用清理资源。 747 748**系统能力:** SystemCapability.Multimedia.AVSession.Core 749 750**系统接口:** 该接口为系统接口 751 752**参数:** 753 754| 参数名 | 类型 | 必填 | 说明 | 755| -------- | -------------------- | ---- | ------------------------------------------------------------ | 756| type | string | 是 | 事件回调类型,支持事件`'sessionServiceDie'`:会话服务死亡事件,检测到会话的服务死亡时触发。 | 757| callback | callback: () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则返回错误对象。 | 758 759**错误码:** 760 761以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 762 763| 错误码ID | 错误信息 | 764| -------- | ---------------------------------------- | 765| 6600101 | Session service exception. | 766 767**示例:** 768 769```ts 770avSession.on('sessionServiceDie', () => { 771 console.info(`on sessionServiceDie : session is Died `); 772}); 773``` 774 775## avSession.off('sessionServiceDie') 776 777off(type: 'sessionServiceDie', callback?: () => void): void 778 779取消会话服务死亡监听,取消后,不再进行服务死亡监听。 780 781**系统能力:** SystemCapability.Multimedia.AVSession.Core 782 783**系统接口:** 该接口为系统接口 784 785**参数:** 786 787| 参数名 | 类型 | 必填 | 说明 | 788| ------ | ---------------------- | ---- | ------------------------------------------------------- | 789| type | string | 是 | 事件回调类型,支持事件`'sessionServiceDie'`:会话服务死亡事件。| 790| callback | callback: () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的服务死亡监听。 | 791 792**错误码:** 793 794以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 795 796| 错误码ID | 错误信息 | 797| -------- | ---------------------------------------- | 798| 6600101 | Session service exception. | 799 800**示例:** 801 802```ts 803avSession.off('sessionServiceDie'); 804``` 805 806## avSession.sendSystemAVKeyEvent 807 808sendSystemAVKeyEvent(event: KeyEvent, callback: AsyncCallback\<void>): void 809 810发送按键事件给置顶会话。结果通过callback异步回调方式返回。 811 812**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 813 814**系统能力:** SystemCapability.Multimedia.AVSession.Manager 815 816**系统接口:** 该接口为系统接口。 817 818**参数:** 819 820| 参数名 | 类型 | 必填 | 说明 | 821| -------- | ------------------------------------------------------------ | ---- | ------------------------------------- | 822| event | [KeyEvent](js-apis-keyevent.md) | 是 | 按键事件。 | 823| callback | AsyncCallback\<void> | 是 | 回调函数。当事件发送成功,err为undefined,否则返回错误对象。 | 824 825**错误码:** 826 827以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 828 829| 错误码ID | 错误信息 | 830| -------- | ---------------------------------------- | 831| 6600101 | Session service exception. | 832| 6600105 | Invalid session command. | 833 834**示例:** 835 836```ts 837import keyEvent from '@ohos.multimodalInput.keyEvent'; 838import { BusinessError } from '@ohos.base'; 839 840let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; 841let event: keyEvent.KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; 842 843avSession.sendSystemAVKeyEvent(event, (err: BusinessError) => { 844 if (err) { 845 console.error(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 846 } else { 847 console.info(`SendSystemAVKeyEvent : SUCCESS `); 848 } 849}); 850``` 851 852## avSession.sendSystemAVKeyEvent 853 854sendSystemAVKeyEvent(event: KeyEvent): Promise\<void> 855 856发送按键事件给置顶会话。结果通过Promise异步回调方式返回。 857 858**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 859 860**系统能力:** SystemCapability.Multimedia.AVSession.Manager 861 862**系统接口:** 该接口为系统接口。 863 864**参数:** 865 866| 参数名 | 类型 | 必填 | 说明 | 867| ------ | ------------------------------- | ---- | ---------- | 868| event | [KeyEvent](js-apis-keyevent.md) | 是 | 按键事件。 | 869 870**返回值:** 871 872| 类型 | 说明 | 873| -------------- | ----------------------------- | 874| Promise\<void> | Promise对象。当事件发送成功,无返回结果,否则返回错误对象。 | 875 876**错误码:** 877 878以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 879 880| 错误码ID | 错误信息 | 881| -------- | ---------------------------------------- | 882| 6600101 | Session service exception. | 883| 6600105 | Invalid session command. | 884 885**示例:** 886 887```ts 888import keyEvent from '@ohos.multimodalInput.keyEvent'; 889import { BusinessError } from '@ohos.base'; 890 891let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; 892let event: keyEvent.KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; 893 894avSession.sendSystemAVKeyEvent(event).then(() => { 895 console.info(`SendSystemAVKeyEvent Successfully`); 896}).catch((err: BusinessError) => { 897 console.error(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 898}); 899``` 900 901## avSession.sendSystemControlCommand 902 903sendSystemControlCommand(command: AVControlCommand, callback: AsyncCallback\<void>): void 904 905发送控制命令给置顶会话。结果通过callback异步回调方式返回。 906 907**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 908 909**系统能力:** SystemCapability.Multimedia.AVSession.Manager 910 911**系统接口:** 该接口为系统接口。 912 913**参数:** 914 915| 参数名 | 类型 | 必填 | 说明 | 916| -------- | ------------------------------------- | ---- | ------------------------------------- | 917| command | [AVControlCommand](#avcontrolcommand10) | 是 | AVSession的相关命令和命令相关参数。 | 918| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 919 920**错误码:** 921 922以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 923 924| 错误码ID | 错误信息 | 925| -------- | ---------------------------------------- | 926| 6600101 | Session service exception. | 927| 6600105 | Invalid session command. | 928| 6600107 | Too many commands or events. | 929 930**示例:** 931 932```ts 933import avSession from '@ohos.multimedia.avsession'; 934 935let cmd : avSession.AVControlCommandType = 'play'; 936// let cmd : avSession.AVControlCommandType = 'pause'; 937// let cmd : avSession.AVControlCommandType = 'stop'; 938// let cmd : avSession.AVControlCommandType = 'playNext'; 939// let cmd : avSession.AVControlCommandType = 'playPrevious'; 940// let cmd : avSession.AVControlCommandType = 'fastForward'; 941// let cmd : avSession.AVControlCommandType = 'rewind'; 942let avcommand: avSession.AVControlCommand = {command:cmd}; 943// let cmd : avSession.AVControlCommandType = 'seek'; 944// let avcommand = {command:cmd, parameter:10}; 945// let cmd : avSession.AVControlCommandType = 'setSpeed'; 946// let avcommand = {command:cmd, parameter:2.6}; 947// let cmd : avSession.AVControlCommandType = 'setLoopMode'; 948// let avcommand = {command:cmd, parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; 949// let cmd : avSession.AVControlCommandType = 'toggleFavorite'; 950// let avcommand = {command:cmd, parameter:"false"}; 951avSession.sendSystemControlCommand(avcommand, (err) => { 952 if (err) { 953 console.error(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 954 } else { 955 console.info(`sendSystemControlCommand successfully`); 956 } 957}); 958``` 959 960## avSession.sendSystemControlCommand 961 962sendSystemControlCommand(command: AVControlCommand): Promise\<void> 963 964发送控制命令给置顶会话。结果通过Promise异步回调方式返回。 965 966**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 967 968**系统能力:** SystemCapability.Multimedia.AVSession.Manager 969 970**系统接口:** 该接口为系统接口。 971 972**参数:** 973 974| 参数名 | 类型 | 必填 | 说明 | 975| ------- | ------------------------------------- | ---- | ----------------------------------- | 976| command | [AVControlCommand](#avcontrolcommand10) | 是 | AVSession的相关命令和命令相关参数。 | 977 978**返回值:** 979 980| 类型 | 说明 | 981| -------------- | ----------------------------- | 982| Promise\<void> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | 983 984**错误码:** 985 986以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 987 988| 错误码ID | 错误信息 | 989| -------- | ---------------------------------------- | 990| 6600101 | Session service exception. | 991| 6600105 | Invalid session command. | 992| 6600107 | Too many commands or events. | 993 994**示例:** 995 996```ts 997import avSession from '@ohos.multimedia.avsession'; 998import { BusinessError } from '@ohos.base'; 999 1000let cmd : avSession.AVControlCommandType = 'play'; 1001// let cmd : avSession.AVControlCommandType = 'pause'; 1002// let cmd : avSession.AVControlCommandType = 'stop'; 1003// let cmd : avSession.AVControlCommandType = 'playNext'; 1004// let cmd : avSession.AVControlCommandType = 'playPrevious'; 1005// let cmd : avSession.AVControlCommandType = 'fastForward'; 1006// let cmd : avSession.AVControlCommandType = 'rewind'; 1007let avcommand: avSession.AVControlCommand = {command:cmd}; 1008// let cmd : avSession.AVControlCommandType = 'seek'; 1009// let avcommand = {command:cmd, parameter:10}; 1010// let cmd : avSession.AVControlCommandType = 'setSpeed'; 1011// let avcommand = {command:cmd, parameter:2.6}; 1012// let cmd : avSession.AVControlCommandType = 'setLoopMode'; 1013// let avcommand = {command:cmd, parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; 1014// let cmd : avSession.AVControlCommandType = 'toggleFavorite'; 1015// let avcommand = {command:cmd, parameter:"false"}; 1016avSession.sendSystemControlCommand(avcommand).then(() => { 1017 console.info(`SendSystemControlCommand successfully`); 1018}).catch((err: BusinessError) => { 1019 console.error(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 1020}); 1021``` 1022 1023## ProtocolType<sup>10+</sup> 1024 1025远端设备支持的协议类型。 1026 1027**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1028 1029**系统接口:** 该接口为系统接口。 1030 1031| 名称 | 值 | 说明 | 1032| --------------------------- | ---- | ----------- | 1033| TYPE_LOCAL | 0 | 本地设备,包括设备本身的内置扬声器或音频插孔、A2DP 设备。 <br> **系统接口:** 该接口为系统接口。 | 1034| TYPE_CAST_PLUS_MIRROR | 1 | Cast+的镜像模式 <br> **系统接口:** 该接口为系统接口。| 1035| TYPE_CAST_PLUS_STREAM | 2 | Cast+的Stream模式。表示媒体正在其他设备上展示。 | 1036 1037## avSession.startCastDeviceDiscovery<sup>10+</sup> 1038 1039startCastDeviceDiscovery(callback: AsyncCallback\<void>): void 1040 1041开始设备搜索发现。结果通过callback异步回调方式返回。 1042 1043**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1044 1045**系统接口:** 该接口为系统接口。 1046 1047**参数:** 1048 1049| 参数名 | 类型 | 必填 | 说明 | 1050| -------- | ------------------------------------- | ---- | ------------------------------------- | 1051| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功并开始搜索,err为undefined,否则返回错误对象。 | 1052 1053 1054**示例:** 1055 1056```ts 1057import { BusinessError } from '@ohos.base'; 1058 1059avSession.startCastDeviceDiscovery((err: BusinessError) => { 1060 if (err) { 1061 console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1062 } else { 1063 console.info(`startCastDeviceDiscovery successfully`); 1064 } 1065}); 1066``` 1067 1068## avSession.startCastDeviceDiscovery<sup>10+</sup> 1069 1070startCastDeviceDiscovery(filter: number, callback: AsyncCallback\<void>): void 1071 1072指定过滤条件,开始设备搜索发现。结果通过callback异步回调方式返回。 1073 1074**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1075 1076**系统接口:** 该接口为系统接口。 1077 1078**参数:** 1079 1080| 参数名 | 类型 | 必填 | 说明 | 1081| -------- | ------------------------------------- | ---- | ------------------------------------- | 1082| filter | number | 是 | 进行设备发现的过滤条件,由ProtocolType的组合而成 | 1083| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功并开始搜索,err为undefined,否则返回错误对象。 | 1084 1085 1086**示例:** 1087 1088```ts 1089import { BusinessError } from '@ohos.base'; 1090 1091let filter = 2; 1092avSession.startCastDeviceDiscovery(filter, (err: BusinessError) => { 1093 if (err) { 1094 console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1095 } else { 1096 console.info(`startCastDeviceDiscovery successfully`); 1097 } 1098}); 1099``` 1100 1101## avSession.startCastDeviceDiscovery<sup>10+</sup> 1102 1103startCastDeviceDiscovery(filter?: number): Promise\<void> 1104 1105开始设备搜索发现。结果通过Promise异步回调方式返回。 1106 1107**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1108 1109**系统接口:** 该接口为系统接口。 1110 1111**参数:** 1112 1113| 参数名 | 类型 | 必填 | 说明 | 1114| -------- | ------------------------------------- | ---- | ------------------------------------- | 1115| filter | number | 否 | 进行设备发现的过滤条件,由ProtocolType的组合而成 | 1116 1117**返回值:** 1118 1119| 类型 | 说明 | 1120| -------------- | ----------------------------- | 1121| Promise\<void> | Promise对象。当命令发送成功并开始搜索,无返回结果,否则返回错误对象。 | 1122 1123**示例:** 1124 1125```ts 1126import { BusinessError } from '@ohos.base'; 1127 1128let filter = 2; 1129avSession.startCastDeviceDiscovery(filter).then(() => { 1130 console.info(`startCastDeviceDiscovery successfully`); 1131}).catch((err: BusinessError) => { 1132 console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1133}); 1134``` 1135 1136## avSession.stopCastDeviceDiscovery<sup>10+</sup> 1137 1138stopCastDeviceDiscovery(callback: AsyncCallback\<void>): void 1139 1140结束设备搜索发现。结果通过callback异步回调方式返回。 1141 1142**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1143 1144**系统接口:** 该接口为系统接口。 1145 1146**参数:** 1147 1148| 参数名 | 类型 | 必填 | 说明 | 1149| -------- | ------------------------------------- | ---- | ------------------------------------- | 1150| callback | AsyncCallback\<void> | 是 | 回调函数。当成功停止搜索,err为undefined,否则返回错误对象。 | 1151 1152 1153**示例:** 1154 1155```ts 1156import { BusinessError } from '@ohos.base'; 1157 1158avSession.stopCastDeviceDiscovery((err: BusinessError) => { 1159 if (err) { 1160 console.error(`stopCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1161 } else { 1162 console.info(`stopCastDeviceDiscovery successfully`); 1163 } 1164}); 1165``` 1166 1167## avSession.stopCastDeviceDiscovery<sup>10+</sup> 1168 1169stopCastDeviceDiscovery(): Promise\<void> 1170 1171结束设备搜索发现。结果通过Promise异步回调方式返回。 1172 1173**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1174 1175**系统接口:** 该接口为系统接口。 1176 1177**返回值:** 1178 1179| 类型 | 说明 | 1180| -------------- | ----------------------------- | 1181| Promise\<void> | Promise对象。当成功停止搜索,无返回结果,否则返回错误对象。 | 1182 1183**示例:** 1184 1185```ts 1186import { BusinessError } from '@ohos.base'; 1187 1188avSession.stopCastDeviceDiscovery().then(() => { 1189 console.info(`stopCastDeviceDiscovery successfully`); 1190}).catch((err: BusinessError) => { 1191 console.error(`stopCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); 1192}); 1193``` 1194 1195## avSession.setDiscoverable<sup>10+</sup> 1196 1197setDiscoverable(enable: boolean, callback: AsyncCallback\<void>): void 1198 1199设置设备是否可被发现,用于投播接收端。结果通过callback异步回调方式返回。 1200 1201**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1202 1203**系统接口:** 该接口为系统接口。 1204 1205**参数:** 1206 1207| 参数名 | 类型 | 必填 | 说明 | 1208| -------- | ------------------------------------- | ---- | ------------------------------------- | 1209| enable | boolean | 是 | 是否允许本设备被发现. true: 允许被发现, false:不允许被发现 | 1210| callback | AsyncCallback\<void> | 是 | 回调函数。当设置成功,err为undefined,否则返回错误对象。 | 1211 1212 1213**示例:** 1214 1215```ts 1216import { BusinessError } from '@ohos.base'; 1217 1218avSession.setDiscoverable(true, (err: BusinessError) => { 1219 if (err) { 1220 console.error(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); 1221 } else { 1222 console.info(`setDiscoverable successfully`); 1223 } 1224}); 1225``` 1226 1227## avSession.setDiscoverable<sup>10+</sup> 1228 1229setDiscoverable(enable: boolean): Promise\<void> 1230 1231设置设备是否可被发现,用于投播接收端。结果通过Promise异步回调方式返回。 1232 1233**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1234 1235**系统接口:** 该接口为系统接口。 1236 1237**参数:** 1238 1239| 参数名 | 类型 | 必填 | 说明 | 1240| -------- | ------------------------------------- | ---- | ------------------------------------- | 1241| enable | boolean | 是 | 是否允许本设备被发现. true: 允许被发现, false:不允许被发现 | 1242 1243**返回值:** 1244 1245| 类型 | 说明 | 1246| -------------- | ----------------------------- | 1247| Promise\<void> | Promise对象。当设置成功,无返回结果,否则返回错误对象。 | 1248 1249**示例:** 1250 1251```ts 1252import { BusinessError } from '@ohos.base'; 1253 1254avSession.setDiscoverable(true).then(() => { 1255 console.info(`setDiscoverable successfully`); 1256}).catch((err: BusinessError) => { 1257 console.error(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); 1258}); 1259``` 1260 1261## avSession.on('deviceAvailable')<sup>10+</sup> 1262 1263on(type: 'deviceAvailable', callback: (device: OutputDeviceInfo) => void): void 1264 1265设备发现回调监听。 1266 1267**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1268 1269**系统接口:** 该接口为系统接口 1270 1271**参数:** 1272 1273| 参数名 | 类型 | 必填 | 说明 | 1274| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1275| type | string | 是 | 事件回调类型,支持事件`'deviceAvailable'`,有设备被发现时触发回调。 | 1276| callback | (device: OutputDeviceInfo) => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则返回错误对象。 | 1277 1278**示例:** 1279 1280```ts 1281import avSession from '@ohos.multimedia.avsession'; 1282 1283let castDevice: avSession.OutputDeviceInfo; 1284avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { 1285 castDevice = device; 1286 console.info(`on deviceAvailable : ${device} `); 1287}); 1288``` 1289 1290## avSession.off('deviceAvailable')<sup>10+</sup> 1291 1292off(type: 'deviceAvailable', callback?: (device: OutputDeviceInfo) => void): void 1293 1294取消设备发现回调的监听。 1295 1296**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1297 1298**系统接口:** 该接口为系统接口 1299 1300**参数:** 1301 1302| 参数名 | 类型 | 必填 | 说明 | 1303| ------ | ---------------------- | ---- | ------------------------------------------------------- | 1304| type | string | 是 | 事件回调类型,支持事件`'deviceAvailable'`:设备发现回调。| 1305| callback | function | 否 | 用于返回设备信息。| 1306 1307**示例:** 1308 1309```ts 1310avSession.off('deviceAvailable'); 1311``` 1312 1313## avSession.getAVCastController<sup>10+</sup> 1314 1315getAVCastController(sessionId: string, callback: AsyncCallback\<AVCastController>): void 1316 1317设备建立连接后,获取投播控制器。结果通过callback异步回调方式返回。 1318 1319此功能在本端和远端都可以使用,通过该接口可以获取一个相同的控制器,进行投播音频的播放控制。 1320 1321**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1322 1323**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES 1324 1325**系统接口:** 该接口为系统接口 1326 1327**参数:** 1328 1329| 参数名 | 类型 | 必填 | 说明 | 1330| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 1331| sessionId | string | 是 |用于指定要获取的投播控制器的sessionId | 1332| callback | AsyncCallback<[AVCastController](#avcastcontroller10)\> | 是 | 回调函数,返回投播控制器实例。 | 1333 1334**错误码:** 1335 1336以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1337 1338| 错误码ID | 错误信息 | 1339| -------- | ---------------------------------------- | 1340| 6600101 | Session service exception | 1341| 6600102 | session does not exist | 1342 1343**示例:** 1344 1345```ts 1346import { BusinessError } from '@ohos.base'; 1347 1348let currentAVSession: avSession.AVSession | undefined = undefined; 1349let tag = "createNewSession"; 1350let context: Context = getContext(this); 1351let sessionId: string = ""; //供后续函数入参使用 1352 1353avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1354 if (err) { 1355 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1356 } else { 1357 currentAVSession = data; 1358 if (currentAVSession !== undefined) { 1359 sessionId = currentAVSession.sessionId; 1360 } 1361 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1362 } 1363}); 1364 1365let aVCastController: avSession.AVCastController; 1366avSession.getAVCastController(sessionId , (err: BusinessError, avcontroller: avSession.AVCastController) => { 1367 if (err) { 1368 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 1369 } else { 1370 aVCastController = avcontroller; 1371 console.info('getAVCastController : SUCCESS '); 1372 } 1373}); 1374``` 1375 1376## avSession.getAVCastController<sup>10+</sup> 1377 1378getAVCastController(sessionId: string): Promise\<AVCastController> 1379 1380设备建立连接后,获取投播控制器。结果通过Promise方式返回。 1381 1382此功能在本端和远端都可以使用,通过该接口可以获取一个相同的控制器,进行投播音频的播放控制。 1383 1384**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1385 1386**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES 1387 1388**系统接口:** 该接口为系统接口 1389 1390**参数:** 1391 1392| 参数名 | 类型 | 必填 | 说明 | 1393| --------- | ------------------------- | ---- | ------------------------------------------------------------ | 1394| sessionId | string | 是 |用于指定要获取的投播控制器的sessionId | 1395 1396**返回值:** 1397 1398| 类型 | 说明 | 1399| --------- | ------------------------------------------------------------ | 1400| Promise<[AVCastController](#avcastcontroller10)\> | Promise对象。返回投播控制器实例。 | 1401 1402**错误码:** 1403 1404以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1405 1406| 错误码ID | 错误信息 | 1407| -------- | ---------------------------------------- | 1408| 6600101 | server exception | 1409| 6600102 | The session does not exist | 1410 1411**示例:** 1412 1413```ts 1414import { BusinessError } from '@ohos.base'; 1415 1416let currentAVSession: avSession.AVSession | undefined = undefined; 1417let tag = "createNewSession"; 1418let context: Context = getContext(this); 1419let sessionId: string = ""; //供后续函数入参使用 1420 1421avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1422 if (err) { 1423 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1424 } else { 1425 currentAVSession = data; 1426 if (currentAVSession !== undefined) { 1427 sessionId = currentAVSession.sessionId; 1428 } 1429 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1430 } 1431}); 1432 1433let aVCastController: avSession.AVCastController; 1434avSession.getAVCastController(sessionId).then((avcontroller: avSession.AVCastController) => { 1435 aVCastController = avcontroller; 1436 console.info('getAVCastController : SUCCESS'); 1437}).catch((err: BusinessError) => { 1438 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 1439}); 1440``` 1441 1442## avSession.startCasting<sup>10+</sup> 1443 1444startCasting(session: SessionToken, device: OutputDeviceInfo, callback: AsyncCallback\<void>): void 1445 1446启动投播。结果通过callback异步回调方式返回。 1447 1448**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 1449 1450**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1451 1452**系统接口:** 该接口为系统接口。 1453 1454**参数:** 1455 1456| 参数名 | 类型 | 必填 | 说明 | 1457| -------- | ------------------------------------- | ---- | ------------------------------------- | 1458| session | [SessionToken](#sessiontoken) | 是 | 会话令牌。SessionToken表示单个token。 | 1459| device | [OutputDeviceInfo](#outputdeviceinfo10) | 是 | 设备相关信息 | 1460| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功并启动投播,err为undefined,否则返回错误对象。 | 1461 1462**错误码:** 1463 1464以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1465 1466| 错误码ID | 错误信息 | 1467| -------- | ---------------------------------------- | 1468| 6600101 | Session service exception. | 1469| 6600108 | Device connecting failed. | 1470 1471**示例:** 1472 1473```ts 1474import avSession from '@ohos.multimedia.avsession'; 1475import { BusinessError } from '@ohos.base'; 1476 1477let currentAVSession: avSession.AVSession | undefined = undefined; 1478let tag = "createNewSession"; 1479let context: Context = getContext(this); 1480let sessionId: string = ""; //供后续函数入参使用 1481 1482avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1483 if (err) { 1484 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1485 } else { 1486 currentAVSession = data; 1487 if (currentAVSession !== undefined) { 1488 sessionId = currentAVSession.sessionId; 1489 } 1490 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1491 } 1492}); 1493 1494let myToken: avSession.SessionToken = { 1495 sessionId: sessionId, 1496} 1497let castDevice: avSession.OutputDeviceInfo | undefined = undefined; 1498avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { 1499 castDevice = device; 1500 console.info(`on deviceAvailable : ${device} `); 1501}); 1502if (castDevice !== undefined) { 1503 avSession.startCasting(myToken, castDevice, (err: BusinessError) => { 1504 if (err) { 1505 console.error(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); 1506 } else { 1507 console.info(`startCasting successfully`); 1508 } 1509 }); 1510} 1511``` 1512 1513## avSession.startCasting<sup>10+</sup> 1514 1515startCasting(session: SessionToken, device: OutputDeviceInfo): Promise\<void> 1516 1517启动投播。结果通过Promise异步回调方式返回。 1518 1519**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 1520 1521**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1522 1523**系统接口:** 该接口为系统接口。 1524 1525**参数:** 1526 1527| 参数名 | 类型 | 必填 | 说明 | 1528| -------- | ------------------------------------- | ---- | ------------------------------------- | 1529| session | [SessionToken](#sessiontoken) | 是 | 会话令牌。SessionToken表示单个token。 | 1530| device | [OutputDeviceInfo](#outputdeviceinfo10) | 是 | 设备相关信息 | 1531 1532**返回值:** 1533 1534| 类型 | 说明 | 1535| -------------- | ----------------------------- | 1536| Promise\<void> | Promise对象。当命令发送成功并启动投播,无返回结果,否则返回错误对象。 | 1537 1538**错误码:** 1539 1540以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1541 1542| 错误码ID | 错误信息 | 1543| -------- | ---------------------------------------- | 1544| 6600101 | Session service exception. | 1545| 6600108 | Device connecting failed. | 1546 1547**示例:** 1548 1549```ts 1550import avSession from '@ohos.multimedia.avsession'; 1551import { BusinessError } from '@ohos.base'; 1552 1553let currentAVSession: avSession.AVSession | undefined = undefined; 1554let tag = "createNewSession"; 1555let context: Context = getContext(this); 1556let sessionId: string = ""; //供后续函数入参使用 1557 1558avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1559 if (err) { 1560 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1561 } else { 1562 currentAVSession = data; 1563 if (currentAVSession !== undefined) { 1564 sessionId = currentAVSession.sessionId; 1565 } 1566 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1567 } 1568}); 1569 1570let myToken: avSession.SessionToken = { 1571 sessionId: sessionId, 1572} 1573let castDevice: avSession.OutputDeviceInfo | undefined = undefined; 1574avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { 1575 castDevice = device; 1576 console.info(`on deviceAvailable : ${device} `); 1577}); 1578if (castDevice !== undefined) { 1579 avSession.startCasting(myToken, castDevice).then(() => { 1580 console.info(`startCasting successfully`); 1581 }).catch((err: BusinessError) => { 1582 console.error(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); 1583 }); 1584} 1585``` 1586 1587## avSession.stopCasting<sup>10+</sup> 1588 1589stopCasting(session: SessionToken, callback: AsyncCallback\<void>): void 1590 1591结束投播。结果通过callback异步回调方式返回。 1592 1593**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1594 1595**系统接口:** 该接口为系统接口。 1596 1597**参数:** 1598 1599| 参数名 | 类型 | 必填 | 说明 | 1600| -------- | ------------------------------------- | ---- | ------------------------------------- | 1601| session | [SessionToken](#sessiontoken) | 是 | 会话令牌。SessionToken表示单个token。 | 1602| callback | AsyncCallback\<void> | 是 | 回调函数。当成功结束投播,err为undefined,否则返回错误对象。 | 1603 1604**错误码:** 1605 1606以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1607 1608| 错误码ID | 错误信息 | 1609| -------- | ---------------------------------------- | 1610| 6600109 | The remote connection is not established. | 1611 1612**示例:** 1613 1614```ts 1615import avSession from '@ohos.multimedia.avsession'; 1616import { BusinessError } from '@ohos.base'; 1617 1618let currentAVSession: avSession.AVSession | undefined = undefined; 1619let tag = "createNewSession"; 1620let context: Context = getContext(this); 1621let sessionId: string = ""; //供后续函数入参使用 1622 1623avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1624 if (err) { 1625 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1626 } else { 1627 currentAVSession = data; 1628 if (currentAVSession !== undefined) { 1629 sessionId = currentAVSession.sessionId; 1630 } 1631 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1632 } 1633}); 1634 1635let myToken: avSession.SessionToken = { 1636 sessionId: sessionId, 1637} 1638avSession.stopCasting(myToken, (err: BusinessError) => { 1639 if (err) { 1640 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 1641 } else { 1642 console.info(`stopCasting successfully`); 1643 } 1644}); 1645``` 1646 1647## avSession.stopCasting<sup>10+</sup> 1648 1649stopCasting(session: SessionToken): Promise\<void> 1650 1651结束投播。结果通过Promise异步回调方式返回。 1652 1653**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 1654 1655**系统接口:** 该接口为系统接口。 1656 1657**参数:** 1658 1659| 参数名 | 类型 | 必填 | 说明 | 1660| -------- | ------------------------------------- | ---- | ------------------------------------- | 1661| session | [SessionToken](#sessiontoken) | 是 | 会话令牌。SessionToken表示单个token。 | 1662 1663**返回值:** 1664 1665| 类型 | 说明 | 1666| -------------- | ----------------------------- | 1667| Promise\<void> | Promise对象。当成功结束投播,无返回结果,否则返回错误对象。 | 1668 1669**错误码:** 1670 1671错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1672 1673| 错误码ID | 错误信息 | 1674| -------- | ---------------------------------------- | 1675| 6600109 | The remote connection is not established. | 1676 1677**示例:** 1678 1679```ts 1680import avSession from '@ohos.multimedia.avsession'; 1681import { BusinessError } from '@ohos.base'; 1682 1683let currentAVSession: avSession.AVSession | undefined = undefined; 1684let tag = "createNewSession"; 1685let context: Context = getContext(this); 1686let sessionId: string = ""; //供后续函数入参使用 1687 1688avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 1689 if (err) { 1690 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 1691 } else { 1692 currentAVSession = data; 1693 if (currentAVSession !== undefined) { 1694 sessionId = currentAVSession.sessionId; 1695 } 1696 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 1697 } 1698}); 1699 1700let myToken: avSession.SessionToken = { 1701 sessionId: sessionId, 1702} 1703avSession.stopCasting(myToken).then(() => { 1704 console.info(`stopCasting successfully`); 1705}).catch((err: BusinessError) => { 1706 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 1707}); 1708 1709 1710``` 1711 1712## AVSessionType<sup>10+<sup> 1713当前会话支持的会话类型。 1714 1715**系统能力:** SystemCapability.Multimedia.AVSession.Core 1716 1717| 名称 | 类型 | 说明 | 1718| ----- | ------ | ---- | 1719| audio | string | 音频 | 1720| video | string | 视频 | 1721 1722## AVSession<sup>10+</sup> 1723 1724调用[avSession.createAVSession](#avsessioncreateavsession10)后,返回会话的实例,可以获得会话ID,完成设置元数据,播放状态信息等操作。 1725 1726### 属性 1727 1728**系统能力:** SystemCapability.Multimedia.AVSession.Core 1729 1730| 名称 | 类型 | 可读 | 可写 | 说明 | 1731| :-------- | :----- | :--- | :--- | :---------------------------- | 1732| sessionId | string | 是 | 否 | AVSession对象唯一的会话标识。 | 1733| sessionType<sup>10+</sup> | AVSessionType | 是 | 否 | AVSession会话类型。 | 1734 1735 1736**示例:** 1737 1738```ts 1739import avSession from '@ohos.multimedia.avsession'; 1740 1741let sessionId: string = currentAVSession.sessionId; 1742let sessionType: avSession.AVSessionType = currentAVSession.sessionType; 1743``` 1744 1745### setAVMetadata<sup>10+</sup> 1746 1747setAVMetadata(data: AVMetadata): Promise\<void> 1748 1749设置会话元数据。结果通过Promise异步回调方式返回。 1750 1751**系统能力:** SystemCapability.Multimedia.AVSession.Core 1752 1753**参数:** 1754 1755| 参数名 | 类型 | 必填 | 说明 | 1756| ------ | ------------------------- | ---- | ------------ | 1757| data | [AVMetadata](#avmetadata10) | 是 | 会话元数据。 | 1758 1759**返回值:** 1760 1761| 类型 | 说明 | 1762| -------------- | ----------------------------- | 1763| Promise\<void> | Promise对象。当元数据设置成功,无返回结果,否则返回错误对象。 | 1764 1765**错误码:** 1766 1767以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1768 1769| 错误码ID | 错误信息 | 1770| -------- | ---------------------------------------- | 1771| 6600101 | Session service exception. | 1772| 6600102 | The session does not exist. | 1773 1774**示例:** 1775 1776```ts 1777import avSession from '@ohos.multimedia.avsession'; 1778import { BusinessError } from '@ohos.base'; 1779 1780let metadata: avSession.AVMetadata = { 1781 assetId: "121278", 1782 title: "lose yourself", 1783 artist: "Eminem", 1784 author: "ST", 1785 album: "Slim shady", 1786 writer: "ST", 1787 composer: "ST", 1788 duration: 2222, 1789 mediaImage: "https://www.example.com/example.jpg", 1790 subtitle: "8 Mile", 1791 description: "Rap", 1792 lyric: "https://www.example.com/example.lrc", 1793 previousAssetId: "121277", 1794 nextAssetId: "121279", 1795}; 1796currentAVSession.setAVMetadata(metadata).then(() => { 1797 console.info(`SetAVMetadata successfully`); 1798}).catch((err: BusinessError) => { 1799 console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 1800}); 1801``` 1802 1803### setAVMetadata<sup>10+</sup> 1804 1805setAVMetadata(data: AVMetadata, callback: AsyncCallback\<void>): void 1806 1807设置会话元数据。结果通过callback异步回调方式返回。 1808 1809**系统能力:** SystemCapability.Multimedia.AVSession.Core 1810 1811**参数:** 1812 1813| 参数名 | 类型 | 必填 | 说明 | 1814| -------- | ------------------------- | ---- | ------------------------------------- | 1815| data | [AVMetadata](#avmetadata10) | 是 | 会话元数据。 | 1816| callback | AsyncCallback\<void> | 是 | 回调函数。当元数据设置成功,err为undefined,否则返回错误对象。 | 1817 1818**错误码:** 1819 1820以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1821 1822| 错误码ID | 错误信息 | 1823| -------- | ---------------------------------------- | 1824| 6600101 | Session service exception. | 1825| 6600102 | The session does not exist. | 1826 1827**示例:** 1828 1829```ts 1830import avSession from '@ohos.multimedia.avsession'; 1831import { BusinessError } from '@ohos.base'; 1832 1833let metadata: avSession.AVMetadata = { 1834 assetId: "121278", 1835 title: "lose yourself", 1836 artist: "Eminem", 1837 author: "ST", 1838 album: "Slim shady", 1839 writer: "ST", 1840 composer: "ST", 1841 duration: 2222, 1842 mediaImage: "https://www.example.com/example.jpg", 1843 subtitle: "8 Mile", 1844 description: "Rap", 1845 lyric: "https://www.example.com/example.lrc", 1846 previousAssetId: "121277", 1847 nextAssetId: "121279", 1848}; 1849currentAVSession.setAVMetadata(metadata, (err: BusinessError) => { 1850 if (err) { 1851 console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 1852 } else { 1853 console.info(`SetAVMetadata successfully`); 1854 } 1855}); 1856``` 1857 1858### setAVPlaybackState<sup>10+</sup> 1859 1860setAVPlaybackState(state: AVPlaybackState): Promise\<void> 1861 1862设置会话播放状态。结果通过Promise异步回调方式返回。 1863 1864**系统能力:** SystemCapability.Multimedia.AVSession.Core 1865 1866**参数:** 1867 1868| 参数名 | 类型 | 必填 | 说明 | 1869| ------ | ----------------------------------- | ---- | ---------------------------------------------- | 1870| state | [AVPlaybackState](#avplaybackstate10) | 是 | 会话播放状态,包括状态、倍数、循环模式等信息。 | 1871 1872**返回值:** 1873 1874| 类型 | 说明 | 1875| -------------- | ----------------------------- | 1876| Promise\<void> | Promise对象。当播放状态设置成功,无返回结果,否则返回错误对象。 | 1877 1878**错误码:** 1879 1880以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1881 1882| 错误码ID | 错误信息 | 1883| -------- | ---------------------------------------- | 1884| 6600101 | Session service exception. | 1885| 6600102 | The session does not exist. | 1886 1887**示例:** 1888 1889```ts 1890import avSession from '@ohos.multimedia.avsession'; 1891import { BusinessError } from '@ohos.base'; 1892 1893let playbackState: avSession.AVPlaybackState = { 1894 state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, 1895 speed: 1.0, 1896 position:{elapsedTime:10, updateTime:(new Date()).getTime()}, 1897 bufferedTime:1000, 1898 loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, 1899 isFavorite:true, 1900}; 1901currentAVSession.setAVPlaybackState(playbackState).then(() => { 1902 console.info(`SetAVPlaybackState successfully`); 1903}).catch((err: BusinessError) => { 1904 console.error(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 1905}); 1906``` 1907 1908### setAVPlaybackState<sup>10+</sup> 1909 1910setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\<void>): void 1911 1912设置会话播放状态。结果通过callback异步回调方式返回。 1913 1914**系统能力:** SystemCapability.Multimedia.AVSession.Core 1915 1916**参数:** 1917 1918| 参数名 | 类型 | 必填 | 说明 | 1919| -------- | ----------------------------------- | ---- | ---------------------------------------------- | 1920| state | [AVPlaybackState](#avplaybackstate10) | 是 | 会话播放状态,包括状态、倍数、循环模式等信息。 | 1921| callback | AsyncCallback\<void> | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | 1922 1923**错误码:** 1924 1925以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1926 1927| 错误码ID | 错误信息 | 1928| -------- | ---------------------------------------- | 1929| 6600101 | Session service exception. | 1930| 6600102 | The session does not exist. | 1931 1932**示例:** 1933 1934```ts 1935import avSession from '@ohos.multimedia.avsession'; 1936import { BusinessError } from '@ohos.base'; 1937 1938let PlaybackState: avSession.AVPlaybackState = { 1939 state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, 1940 speed: 1.0, 1941 position:{elapsedTime:10, updateTime:(new Date()).getTime()}, 1942 bufferedTime:1000, 1943 loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, 1944 isFavorite:true, 1945}; 1946currentAVSession.setAVPlaybackState(PlaybackState, (err: BusinessError) => { 1947 if (err) { 1948 console.error(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 1949 } else { 1950 console.info(`SetAVPlaybackState successfully`); 1951 } 1952}); 1953``` 1954 1955### setLaunchAbility<sup>10+</sup> 1956 1957setLaunchAbility(ability: WantAgent): Promise\<void> 1958 1959设置一个WantAgent用于拉起会话的Ability。结果通过Promise异步回调方式返回。 1960 1961**系统能力:** SystemCapability.Multimedia.AVSession.Core 1962 1963**参数:** 1964 1965| 参数名 | 类型 | 必填 | 说明 | 1966| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 1967| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | 是 | 应用的相关属性信息,如bundleName,abilityName,deviceId等。 | 1968 1969**返回值:** 1970 1971| 类型 | 说明 | 1972| -------------- | ----------------------------- | 1973| Promise\<void> | Promise对象。当Ability设置成功,无返回结果,否则返回错误对象。 | 1974 1975**错误码:** 1976 1977以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 1978 1979| 错误码ID | 错误信息 | 1980| -------- | ---------------------------------------- | 1981| 6600101 | Session service exception. | 1982| 6600102 | The session does not exist. | 1983 1984**示例:** 1985 1986```ts 1987import wantAgent from '@ohos.app.ability.wantAgent'; 1988import { BusinessError } from '@ohos.base'; 1989 1990//WantAgentInfo对象 1991let wantAgentInfo: wantAgent.WantAgentInfo = { 1992 wants: [ 1993 { 1994 deviceId: "deviceId", 1995 bundleName: "com.example.myapplication", 1996 abilityName: "EntryAbility", 1997 action: "action1", 1998 entities: ["entity1"], 1999 type: "MIMETYPE", 2000 uri: "key={true,true,false}", 2001 parameters: 2002 { 2003 mykey0: 2222, 2004 mykey1: [1, 2, 3], 2005 mykey2: "[1, 2, 3]", 2006 mykey3: "ssssssssssssssssssssssssss", 2007 mykey4: [false, true, false], 2008 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 2009 mykey6: true, 2010 } 2011 } 2012 ], 2013 operationType: wantAgent.OperationType.START_ABILITIES, 2014 requestCode: 0, 2015 wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 2016} 2017 2018wantAgent.getWantAgent(wantAgentInfo).then((agent) => { 2019 currentAVSession.setLaunchAbility(agent).then(() => { 2020 console.info(`SetLaunchAbility successfully`); 2021 }).catch((err: BusinessError) => { 2022 console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 2023 }); 2024}); 2025``` 2026 2027### setLaunchAbility<sup>10+</sup> 2028 2029setLaunchAbility(ability: WantAgent, callback: AsyncCallback\<void>): void 2030 2031设置一个WantAgent用于拉起会话的Ability。结果通过callback异步回调方式返回。 2032 2033**系统能力:** SystemCapability.Multimedia.AVSession.Core 2034 2035**参数:** 2036 2037| 参数名 | 类型 | 必填 | 说明 | 2038| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 2039| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | 是 | 应用的相关属性信息,如bundleName,abilityName,deviceId等。 | 2040| callback | AsyncCallback\<void> | 是 | 回调函数。当Ability设置成功,err为undefined,否则返回错误对象。 | 2041 2042**错误码:** 2043 2044以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2045 2046| 错误码ID | 错误信息 | 2047| -------- | ---------------------------------------- | 2048| 6600101 | Session service exception. | 2049| 6600102 | The session does not exist. | 2050 2051**示例:** 2052 2053```ts 2054import wantAgent from '@ohos.app.ability.wantAgent'; 2055import { BusinessError } from '@ohos.base'; 2056 2057//WantAgentInfo对象 2058let wantAgentInfo: wantAgent.WantAgentInfo = { 2059 wants: [ 2060 { 2061 deviceId: "deviceId", 2062 bundleName: "com.example.myapplication", 2063 abilityName: "EntryAbility", 2064 action: "action1", 2065 entities: ["entity1"], 2066 type: "MIMETYPE", 2067 uri: "key={true,true,false}", 2068 parameters: 2069 { 2070 mykey0: 2222, 2071 mykey1: [1, 2, 3], 2072 mykey2: "[1, 2, 3]", 2073 mykey3: "ssssssssssssssssssssssssss", 2074 mykey4: [false, true, false], 2075 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 2076 mykey6: true, 2077 } 2078 } 2079 ], 2080 operationType: wantAgent.OperationType.START_ABILITIES, 2081 requestCode: 0, 2082 wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 2083} 2084 2085wantAgent.getWantAgent(wantAgentInfo).then((agent) => { 2086 currentAVSession.setLaunchAbility(agent, (err: BusinessError) => { 2087 if (err) { 2088 console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 2089 } else { 2090 console.info(`SetLaunchAbility successfully`); 2091 } 2092 }); 2093}); 2094``` 2095 2096### dispatchSessionEvent<sup>10+</sup> 2097 2098dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\<void> 2099 2100媒体提供方设置一个会话内自定义事件,包括事件名和键值对形式的事件内容, 结果通过Promise异步回调方式返回。 2101 2102**系统能力:** SystemCapability.Multimedia.AVSession.Core 2103 2104**参数:** 2105 2106| 参数名 | 类型 | 必填 | 说明 | 2107| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 2108| event | string | 是 | 需要设置的会话事件的名称 | 2109| args | {[key: string]: Object} | 是 | 需要传递的会话事件键值对 | 2110 2111> **说明:** 2112> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 2113 2114**返回值:** 2115 2116| 类型 | 说明 | 2117| -------------- | ----------------------------- | 2118| Promise\<void> | Promise对象。当事件设置成功,无返回结果,否则返回错误对象。 | 2119 2120**错误码:** 2121 2122以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2123 2124| 错误码ID | 错误信息 | 2125| -------- | ---------------------------------------- | 2126| 6600101 | Session service exception. | 2127| 6600102 | The session does not exist. | 2128 2129**示例:** 2130 2131```ts 2132import avSession from '@ohos.multimedia.avsession'; 2133import { BusinessError } from '@ohos.base'; 2134 2135let currentAVSession: avSession.AVSession | undefined = undefined; 2136let tag = "createNewSession"; 2137let context: Context = getContext(this); 2138 2139avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2140 if (err) { 2141 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2142 } else { 2143 currentAVSession = data; 2144 } 2145}); 2146let eventName = "dynamic_lyric"; 2147if (currentAVSession !== undefined) { 2148 (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}).then(() => { 2149 console.info(`dispatchSessionEvent successfully`); 2150 }).catch((err: BusinessError) => { 2151 console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); 2152 }) 2153} 2154``` 2155 2156### dispatchSessionEvent<sup>10+</sup> 2157 2158dispatchSessionEvent(event: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void 2159 2160媒体提供方设置一个会话内自定义事件,包括事件名和键值对形式的事件内容, 结果通过callback异步回调方式返回。 2161 2162**系统能力:** SystemCapability.Multimedia.AVSession.Core 2163 2164**参数:** 2165 2166| 参数名 | 类型 | 必填 | 说明 | 2167| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 2168| event | string | 是 | 需要设置的会话事件的名称 | 2169| args | {[key: string]: Object} | 是 | 需要传递的会话事件键值对 | 2170| callback | AsyncCallback\<void> | 是 | 回调函数。当会话事件设置成功,err为undefined,否则返回错误对象。 | 2171 2172> **说明:** 2173 2174> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 2175 2176**错误码:** 2177 2178以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2179 2180| 错误码ID | 错误信息 | 2181| -------- | ---------------------------------------- | 2182| 6600101 | Session service exception. | 2183| 6600102 | The session does not exist. | 2184 2185**示例:** 2186 2187```ts 2188import avSession from '@ohos.multimedia.avsession'; 2189import { BusinessError } from '@ohos.base'; 2190 2191let currentAVSession: avSession.AVSession | undefined = undefined; 2192let tag = "createNewSession"; 2193let context: Context = getContext(this); 2194 2195avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2196 if (err) { 2197 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2198 } else { 2199 currentAVSession = data; 2200 } 2201}); 2202let eventName: string = "dynamic_lyric"; 2203if (currentAVSession !== undefined) { 2204 (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}, (err: BusinessError) => { 2205 if (err) { 2206 console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); 2207 } 2208 }) 2209} 2210``` 2211 2212### setAVQueueItems<sup>10+</sup> 2213 2214setAVQueueItems(items: Array\<AVQueueItem>): Promise\<void> 2215 2216设置媒体播放列表。结果通过Promise异步回调方式返回。 2217 2218**系统能力:** SystemCapability.Multimedia.AVSession.Core 2219 2220**参数:** 2221 2222| 参数名 | 类型 | 必填 | 说明 | 2223| ------ | ------------------------------------ | ---- | ---------------------------------- | 2224| items | Array<[AVQueueItem](#avqueueitem10)\> | 是 | 播放列表单项的队列,用以表示播放列表。 | 2225 2226**返回值:** 2227 2228| 类型 | 说明 | 2229| -------------- | ----------------------------- | 2230| Promise\<void> | Promise对象。当播放列表设置成功,无返回结果,否则返回错误对象。 | 2231 2232**错误码:** 2233 2234以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2235 2236| 错误码ID | 错误信息 | 2237| -------- | ---------------------------------------- | 2238| 6600101 | Session service exception. | 2239| 6600102 | The session does not exist. | 2240 2241**示例:** 2242 2243```ts 2244import image from '@ohos.multimedia.image'; 2245import resourceManager from '@ohos.resourceManager'; 2246import { BusinessError } from '@ohos.base'; 2247import avSession from '@ohos.multimedia.avsession'; 2248 2249async function setAVQueueItems() { 2250 let value = await resourceManager.getSystemResourceManager().getRawFileContent('IMAGE_URI'); 2251 let imageSource= await image.createImageSource(value.buffer); 2252 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 2253 let queueItemDescription_1: avSession.AVMediaDescription = { 2254 assetId: '001', 2255 title: 'music_name', 2256 subtitle: 'music_sub_name', 2257 description: 'music_description', 2258 mediaImage : imagePixel, 2259 extras: {extras:'any'} 2260 }; 2261 let queueItem_1: avSession.AVQueueItem = { 2262 itemId: 1, 2263 description: queueItemDescription_1 2264 }; 2265 let queueItemDescription_2: avSession.AVMediaDescription = { 2266 assetId: '002', 2267 title: 'music_name', 2268 subtitle: 'music_sub_name', 2269 description: 'music_description', 2270 mediaImage: imagePixel, 2271 extras: {extras:'any'} 2272 }; 2273 let queueItem_2: avSession.AVQueueItem = { 2274 itemId: 2, 2275 description: queueItemDescription_2 2276 }; 2277 let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; 2278 currentAVSession.setAVQueueItems(queueItemsArray).then(() => { 2279 console.info(`SetAVQueueItems successfully`); 2280 }).catch((err: BusinessError) => { 2281 console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 2282 }); 2283} 2284``` 2285 2286### setAVQueueItems<sup>10+</sup> 2287 2288setAVQueueItems(items: Array\<AVQueueItem>, callback: AsyncCallback\<void>): void 2289 2290设置媒体播放列表。结果通过callback异步回调方式返回。 2291 2292**系统能力:** SystemCapability.Multimedia.AVSession.Core 2293 2294**参数:** 2295 2296| 参数名 | 类型 | 必填 | 说明 | 2297| -------- | ------------------------------------ | ---- | ----------------------------------------------------------- | 2298| items | Array<[AVQueueItem](#avqueueitem10)\> | 是 | 播放列表单项的队列,用以表示播放列表。 | 2299| callback | AsyncCallback\<void> | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | 2300 2301**错误码:** 2302 2303以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2304 2305| 错误码ID | 错误信息 | 2306| -------- | ---------------------------------------- | 2307| 6600101 | Session service exception. | 2308| 6600102 | The session does not exist. | 2309 2310**示例:** 2311 2312```ts 2313import image from '@ohos.multimedia.image'; 2314import resourceManager from '@ohos.resourceManager'; 2315import { BusinessError } from '@ohos.base'; 2316import avSession from '@ohos.multimedia.avsession'; 2317 2318async function setAVQueueItems() { 2319 let value = await resourceManager.getSystemResourceManager().getRawFileContent('IMAGE_URI'); 2320 let imageSource= await image.createImageSource(value.buffer); 2321 let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); 2322 let queueItemDescription_1: avSession.AVMediaDescription = { 2323 assetId: '001', 2324 title: 'music_name', 2325 subtitle: 'music_sub_name', 2326 description: 'music_description', 2327 mediaImage : imagePixel, 2328 extras: {extras:'any'} 2329 }; 2330 let queueItem_1: avSession.AVQueueItem = { 2331 itemId: 1, 2332 description: queueItemDescription_1 2333 }; 2334 let queueItemDescription_2: avSession.AVMediaDescription = { 2335 assetId: '002', 2336 title: 'music_name', 2337 subtitle: 'music_sub_name', 2338 description: 'music_description', 2339 mediaImage: imagePixel, 2340 extras: {extras:'any'} 2341 }; 2342 let queueItem_2: avSession.AVQueueItem = { 2343 itemId: 2, 2344 description: queueItemDescription_2 2345 }; 2346 let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; 2347 currentAVSession.setAVQueueItems(queueItemsArray, (err: BusinessError) => { 2348 if (err) { 2349 console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 2350 } else { 2351 console.info(`SetAVQueueItems successfully`); 2352 } 2353 }); 2354} 2355``` 2356 2357### setAVQueueTitle<sup>10+</sup> 2358 2359setAVQueueTitle(title: string): Promise\<void> 2360 2361设置媒体播放列表名称。结果通过Promise异步回调方式返回。 2362 2363**系统能力:** SystemCapability.Multimedia.AVSession.Core 2364 2365**参数:** 2366 2367| 参数名 | 类型 | 必填 | 说明 | 2368| ------ | ------ | ---- | -------------- | 2369| title | string | 是 | 播放列表的名称。 | 2370 2371**返回值:** 2372 2373| 类型 | 说明 | 2374| -------------- | ----------------------------- | 2375| Promise\<void> | Promise对象。当播放列表设置成功,无返回结果,否则返回错误对象。 | 2376 2377**错误码:** 2378 2379以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2380 2381| 错误码ID | 错误信息 | 2382| -------- | ---------------------------------------- | 2383| 6600101 | Session service exception. | 2384| 6600102 | The session does not exist. | 2385 2386**示例:** 2387 2388```ts 2389import { BusinessError } from '@ohos.base'; 2390 2391let queueTitle = 'QUEUE_TITLE'; 2392currentAVSession.setAVQueueTitle(queueTitle).then(() => { 2393 console.info(`SetAVQueueTitle successfully`); 2394}).catch((err: BusinessError) => { 2395 console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 2396}); 2397``` 2398 2399### setAVQueueTitle<sup>10+</sup> 2400 2401setAVQueueTitle(title: string, callback: AsyncCallback\<void>): void 2402 2403设置媒体播放列表名称。结果通过callback异步回调方式返回。 2404 2405**系统能力:** SystemCapability.Multimedia.AVSession.Core 2406 2407**参数:** 2408 2409| 参数名 | 类型 | 必填 | 说明 | 2410| -------- | --------------------- | ---- | ----------------------------------------------------------- | 2411| title | string | 是 | 播放列表名称字段。 | 2412| callback | AsyncCallback\<void> | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | 2413 2414**错误码:** 2415 2416以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2417 2418| 错误码ID | 错误信息 | 2419| -------- | ---------------------------------------- | 2420| 6600101 | Session service exception. | 2421| 6600102 | The session does not exist. | 2422 2423**示例:** 2424 2425```ts 2426import { BusinessError } from '@ohos.base'; 2427 2428let queueTitle = 'QUEUE_TITLE'; 2429currentAVSession.setAVQueueTitle(queueTitle, (err: BusinessError) => { 2430 if (err) { 2431 console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 2432 } else { 2433 console.info(`SetAVQueueTitle successfully`); 2434 } 2435}); 2436``` 2437 2438### setExtras<sup>10+</sup> 2439 2440setExtras(extras: {[key: string]: Object}): Promise\<void> 2441 2442媒体提供方设置键值对形式的自定义媒体数据包, 结果通过Promise异步回调方式返回。 2443 2444**系统能力:** SystemCapability.Multimedia.AVSession.Core 2445 2446**参数:** 2447 2448| 参数名 | 类型 | 必填 | 说明 | 2449| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 2450| extras | {[key: string]: Object} | 是 | 需要传递的自定义媒体数据包键值对 | 2451 2452> **说明:** 2453 2454> 参数extras支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 2455 2456**返回值:** 2457 2458| 类型 | 说明 | 2459| -------------- | ----------------------------- | 2460| Promise\<void> | Promise对象。当自定义媒体数据包设置成功,无返回结果,否则返回错误对象。 | 2461 2462**错误码:** 2463 2464以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2465 2466| 错误码ID | 错误信息 | 2467| -------- | ---------------------------------------- | 2468| 6600101 | Session service exception. | 2469| 6600102 | The session does not exist. | 2470 2471**示例:** 2472 2473```ts 2474import avSession from '@ohos.multimedia.avsession'; 2475import { BusinessError } from '@ohos.base'; 2476 2477let currentAVSession: avSession.AVSession | undefined = undefined; 2478let tag = "createNewSession"; 2479let context: Context = getContext(this); 2480 2481avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2482 if (err) { 2483 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2484 } else { 2485 currentAVSession = data; 2486 } 2487}); 2488if (currentAVSession !== undefined) { 2489 (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}).then(() => { 2490 console.info(`setExtras successfully`); 2491 }).catch((err: BusinessError) => { 2492 console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); 2493 }) 2494} 2495``` 2496 2497### setExtras<sup>10+</sup> 2498 2499setExtras(extras: {[key: string]: Object}, callback: AsyncCallback\<void>): void 2500 2501媒体提供方设置键值对形式的自定义媒体数据包, 结果通过callback异步回调方式返回。 2502 2503**系统能力:** SystemCapability.Multimedia.AVSession.Core 2504 2505**参数:** 2506 2507| 参数名 | 类型 | 必填 | 说明 | 2508| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 2509| extras | {[key: string]: Object} | 是 | 需要传递的自定义媒体数据包键值对 | 2510| callback | AsyncCallback\<void> | 是 | 回调函数。当自定义媒体数据包设置成功,err为undefined,否则返回错误对象。 | 2511 2512> **说明:** 2513 2514> 参数extras支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 2515 2516**错误码:** 2517 2518以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2519 2520| 错误码ID | 错误信息 | 2521| -------- | ---------------------------------------- | 2522| 6600101 | Session service exception. | 2523| 6600102 | The session does not exist. | 2524 2525**示例:** 2526 2527```ts 2528import avSession from '@ohos.multimedia.avsession'; 2529import { BusinessError } from '@ohos.base'; 2530 2531let currentAVSession: avSession.AVSession | undefined = undefined; 2532let tag = "createNewSession"; 2533let context: Context = getContext(this); 2534 2535avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 2536 if (err) { 2537 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 2538 } else { 2539 currentAVSession = data; 2540 } 2541}); 2542if (currentAVSession !== undefined) { 2543 (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}, (err: BusinessError) => { 2544 if (err) { 2545 console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); 2546 } 2547 }) 2548} 2549``` 2550 2551### getController<sup>10+</sup> 2552 2553getController(): Promise\<AVSessionController> 2554 2555获取本会话对应的控制器。结果通过Promise异步回调方式返回。 2556 2557**系统能力:** SystemCapability.Multimedia.AVSession.Core 2558 2559**返回值:** 2560 2561| 类型 | 说明 | 2562| ---------------------------------------------------- | ----------------------------- | 2563| Promise<[AVSessionController](#avsessioncontroller10)> | Promise对象。返回会话控制器。 | 2564 2565**错误码:** 2566 2567以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2568 2569| 错误码ID | 错误信息 | 2570| -------- | ---------------------------------------- | 2571| 6600101 | Session service exception. | 2572| 6600102 | The session does not exist. | 2573 2574**示例:** 2575 2576```ts 2577import { BusinessError } from '@ohos.base'; 2578 2579let avsessionController: avSession.AVSessionController; 2580currentAVSession.getController().then((avcontroller: avSession.AVSessionController) => { 2581 avsessionController = avcontroller; 2582 console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); 2583}).catch((err: BusinessError) => { 2584 console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); 2585}); 2586``` 2587 2588### getController<sup>10+</sup> 2589 2590getController(callback: AsyncCallback\<AVSessionController>): void 2591 2592获取本会话相应的控制器。结果通过callback异步回调方式返回。 2593 2594**系统能力:** SystemCapability.Multimedia.AVSession.Core 2595 2596**参数:** 2597 2598| 参数名 | 类型 | 必填 | 说明 | 2599| -------- | ----------------------------------------------------------- | ---- | -------------------------- | 2600| callback | AsyncCallback<[AVSessionController](#avsessioncontroller10)\> | 是 | 回调函数。返回会话控制器。 | 2601 2602**错误码:** 2603 2604以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2605 2606| 错误码ID | 错误信息 | 2607| -------- | ---------------------------------------- | 2608| 6600101 | Session service exception. | 2609| 6600102 | The session does not exist. | 2610 2611**示例:** 2612 2613```ts 2614import { BusinessError } from '@ohos.base'; 2615 2616let avsessionController: avSession.AVSessionController; 2617currentAVSession.getController((err: BusinessError, avcontroller: avSession.AVSessionController) => { 2618 if (err) { 2619 console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); 2620 } else { 2621 avsessionController = avcontroller; 2622 console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); 2623 } 2624}); 2625``` 2626 2627### getAVCastController<sup>10+</sup> 2628 2629getAVCastController(callback: AsyncCallback\<AVCastController>): void 2630 2631设备建立连接后,获取投播控制器。结果通过callback异步回调方式返回。如果 avsession 未处于投播状态,则控制器将返回 null。 2632 2633**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 2634 2635**参数:** 2636 2637| 参数名 | 类型 | 必填 | 说明 | 2638| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 2639| callback | AsyncCallback<[AVCastController](#avcastcontroller10)\> | 是 | 回调函数,返回投播控制器实例。 | 2640 2641**错误码:** 2642 2643以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2644 2645| 错误码ID | 错误信息 | 2646| -------- |---------------------------------------| 2647| 6600101 | Session service exception. | 2648| 6600102 | The session does not exist. | 2649| 6600109 | The remote connection does not exist. | 2650 2651**示例:** 2652 2653```ts 2654import { BusinessError } from '@ohos.base'; 2655 2656let aVCastController: avSession.AVCastController; 2657currentAVSession.getAVCastController().then((avcontroller: avSession.AVCastController) => { 2658 aVCastController = avcontroller; 2659 console.info(`getAVCastController : SUCCESS`); 2660}).catch((err: BusinessError) => { 2661 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 2662}); 2663``` 2664 2665### getAVCastController<sup>10+</sup> 2666 2667getAVCastController(): Promise\<AVCastController> 2668 2669设备建立连接后,获取投播控制器。结果通过callback异步回调方式返回。如果 avsession 未处于投播状态,则控制器将返回 null。 2670 2671**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 2672 2673**返回值:** 2674 2675| 类型 | 说明 | 2676| --------- | ------------------------------------------------------------ | 2677| Promise<[AVCastController](#avcastcontroller10)\> | Promise对象。返回投播控制器实例。 | 2678 2679**错误码:** 2680 2681以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2682 2683| 错误码ID | 错误信息 | 2684| -------- | --------------------------------------- | 2685| 6600101 | Session service exception. | 2686| 6600102 | The session does not exist. | 2687| 6600109 | The remote connection does not exist. | 2688 2689**示例:** 2690 2691```ts 2692import { BusinessError } from '@ohos.base'; 2693 2694let aVCastController: avSession.AVCastController; 2695currentAVSession.getAVCastController((err: BusinessError, avcontroller: avSession.AVCastController) => { 2696 if (err) { 2697 console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); 2698 } else { 2699 aVCastController = avcontroller; 2700 console.info(`getAVCastController : SUCCESS`); 2701 } 2702}); 2703``` 2704 2705### getOutputDevice<sup>10+</sup> 2706 2707getOutputDevice(): Promise\<OutputDeviceInfo> 2708 2709通过会话获取播放设备信息。结果通过Promise异步回调方式返回。 2710 2711**系统能力:** SystemCapability.Multimedia.AVSession.Core 2712 2713**返回值:** 2714 2715| 类型 | 说明 | 2716| ---------------------------------------------- | --------------------------------- | 2717| Promise<[OutputDeviceInfo](#outputdeviceinfo10)> | Promise对象。返回播放设备信息。 | 2718 2719**错误码:** 2720 2721以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2722 2723| 错误码ID | 错误信息 | 2724| -------- | ---------------------------------------- | 2725| 6600101 | Session service exception. | 2726| 6600102 | The session does not exist. | 2727 2728**示例:** 2729 2730```ts 2731import { BusinessError } from '@ohos.base'; 2732 2733currentAVSession.getOutputDevice().then((outputDeviceInfo: avSession.OutputDeviceInfo) => { 2734 console.info(`GetOutputDevice : SUCCESS : devices length : ${outputDeviceInfo.devices.length}`); 2735}).catch((err: BusinessError) => { 2736 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 2737}) 2738``` 2739 2740### getOutputDevice<sup>10+</sup> 2741 2742getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void 2743 2744通过会话获取播放设备相关信息。结果通过callback异步回调方式返回。 2745 2746**系统能力:** SystemCapability.Multimedia.AVSession.Core 2747 2748**参数:** 2749 2750| 参数名 | 类型 | 必填 | 说明 | 2751| -------- | ----------------------------------------------------- | ---- | ------------------------------ | 2752| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | 是 | 回调函数,返回播放设备信息。 | 2753 2754**错误码:** 2755 2756以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2757 2758| 错误码ID | 错误信息 | 2759| -------- | ---------------------------------------- | 2760| 6600101 | Session service exception. | 2761| 6600102 | The session does not exist. | 2762 2763**示例:** 2764 2765```ts 2766import { BusinessError } from '@ohos.base'; 2767 2768currentAVSession.getOutputDevice((err: BusinessError, outputDeviceInfo: avSession.OutputDeviceInfo) => { 2769 if (err) { 2770 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 2771 } else { 2772 console.info(`GetOutputDevice : SUCCESS : devices length : ${outputDeviceInfo.devices.length}`); 2773 } 2774}); 2775``` 2776 2777### activate<sup>10+</sup> 2778 2779activate(): Promise\<void> 2780 2781激活会话,激活后可正常使用会话。结果通过Promise异步回调方式返回。 2782 2783**系统能力:** SystemCapability.Multimedia.AVSession.Core 2784 2785**返回值:** 2786 2787| 类型 | 说明 | 2788| -------------- | ----------------------------- | 2789| Promise\<void> | Promise对象。当会话激活成功,无返回结果,否则返回错误对象。 | 2790 2791**错误码:** 2792 2793以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2794 2795| 错误码ID | 错误信息 | 2796| -------- | ---------------------------------------- | 2797| 6600101 | Session service exception. | 2798| 6600102 | The session does not exist. | 2799 2800**示例:** 2801 2802```ts 2803import { BusinessError } from '@ohos.base'; 2804 2805currentAVSession.activate().then(() => { 2806 console.info(`Activate : SUCCESS `); 2807}).catch((err: BusinessError) => { 2808 console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); 2809}); 2810``` 2811 2812### activate<sup>10+</sup> 2813 2814activate(callback: AsyncCallback\<void>): void 2815 2816激活会话,激活后可正常使用会话。结果通过callback异步回调方式返回。 2817 2818**系统能力:** SystemCapability.Multimedia.AVSession.Core 2819 2820**参数:** 2821 2822| 参数名 | 类型 | 必填 | 说明 | 2823| -------- | -------------------- | ---- | ---------- | 2824| callback | AsyncCallback\<void> | 是 | 回调函数。当会话激活成功,err为undefined,否则返回错误对象。 | 2825 2826**错误码:** 2827 2828以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2829 2830| 错误码ID | 错误信息 | 2831| -------- | ---------------------------------------- | 2832| 6600101 | Session service exception. | 2833| 6600102 | The session does not exist. | 2834 2835**示例:** 2836 2837```ts 2838import { BusinessError } from '@ohos.base'; 2839 2840currentAVSession.activate((err: BusinessError) => { 2841 if (err) { 2842 console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); 2843 } else { 2844 console.info(`Activate : SUCCESS `); 2845 } 2846}); 2847``` 2848 2849### deactivate<sup>10+</sup> 2850 2851deactivate(): Promise\<void> 2852 2853禁用当前会话的功能,可通过[activate](#activate10)恢复。结果通过Promise异步回调方式返回。 2854 2855**系统能力:** SystemCapability.Multimedia.AVSession.Core 2856 2857**返回值:** 2858 2859| 类型 | 说明 | 2860| -------------- | ----------------------------- | 2861| Promise\<void> | Promise对象。当禁用会话成功,无返回结果,否则返回错误对象。| 2862 2863**错误码:** 2864 2865以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2866 2867| 错误码ID | 错误信息 | 2868| -------- | ---------------------------------------- | 2869| 6600101 | Session service exception. | 2870| 6600102 | The session does not exist. | 2871 2872**示例:** 2873 2874```ts 2875import { BusinessError } from '@ohos.base'; 2876 2877currentAVSession.deactivate().then(() => { 2878 console.info(`Deactivate : SUCCESS `); 2879}).catch((err: BusinessError) => { 2880 console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); 2881}); 2882``` 2883 2884### deactivate<sup>10+</sup> 2885 2886deactivate(callback: AsyncCallback\<void>): void 2887 2888禁用当前会话。结果通过callback异步回调方式返回。 2889 2890禁用当前会话的功能,可通过[activate](#activate10)恢复。 2891 2892**系统能力:** SystemCapability.Multimedia.AVSession.Core 2893 2894**参数:** 2895 2896| 参数名 | 类型 | 必填 | 说明 | 2897| -------- | -------------------- | ---- | ---------- | 2898| callback | AsyncCallback\<void> | 是 | 回调函数。当禁用会话成功,err为undefined,否则返回错误对象。| 2899 2900**错误码:** 2901 2902以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2903 2904| 错误码ID | 错误信息 | 2905| -------- | ---------------------------------------- | 2906| 6600101 | Session service exception. | 2907| 6600102 | The session does not exist. | 2908 2909**示例:** 2910 2911```ts 2912import { BusinessError } from '@ohos.base'; 2913 2914currentAVSession.deactivate((err: BusinessError) => { 2915 if (err) { 2916 console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); 2917 } else { 2918 console.info(`Deactivate : SUCCESS `); 2919 } 2920}); 2921``` 2922 2923### destroy<sup>10+</sup> 2924 2925destroy(): Promise\<void> 2926 2927销毁当前会话,使当前会话完全失效。结果通过Promise异步回调方式返回。 2928 2929**系统能力:** SystemCapability.Multimedia.AVSession.Core 2930 2931**返回值:** 2932 2933| 类型 | 说明 | 2934| -------------- | ----------------------------- | 2935| Promise\<void> | Promise对象。当会话销毁成功,无返回结果,否则返回错误对象。 | 2936 2937**错误码:** 2938 2939以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2940 2941| 错误码ID | 错误信息 | 2942| -------- | ---------------------------------------- | 2943| 6600101 | Session service exception. | 2944| 6600102 | The session does not exist. | 2945 2946**示例:** 2947 2948```ts 2949import { BusinessError } from '@ohos.base'; 2950 2951currentAVSession.destroy().then(() => { 2952 console.info(`Destroy : SUCCESS `); 2953}).catch((err: BusinessError) => { 2954 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 2955}); 2956``` 2957 2958### destroy<sup>10+</sup> 2959 2960destroy(callback: AsyncCallback\<void>): void 2961 2962销毁当前会话,使当前会话完全失效。结果通过callback异步回调方式返回。 2963 2964**系统能力:** SystemCapability.Multimedia.AVSession.Core 2965 2966**参数:** 2967 2968| 参数名 | 类型 | 必填 | 说明 | 2969| -------- | -------------------- | ---- | ---------- | 2970| callback | AsyncCallback\<void> | 是 | 回调函数。当会话销毁成功,err为undefined,否则返回错误对象。 | 2971 2972**错误码:** 2973 2974以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 2975 2976| 错误码ID | 错误信息 | 2977| -------- | ---------------------------------------- | 2978| 6600101 | Session service exception. | 2979| 6600102 | The session does not exist. | 2980 2981**示例:** 2982 2983```ts 2984import { BusinessError } from '@ohos.base'; 2985 2986currentAVSession.destroy((err: BusinessError) => { 2987 if (err) { 2988 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 2989 } else { 2990 console.info(`Destroy : SUCCESS `); 2991 } 2992}); 2993``` 2994 2995### on('play')<sup>10+</sup> 2996 2997on(type: 'play', callback: () => void): void 2998 2999设置播放命令监听事件。注册该监听,说明应用支持播放指令。 3000 3001每个播放命令仅支持注册一个回调,如果注册新的回调,将替换前一个回调。 3002 3003**系统能力:** SystemCapability.Multimedia.AVSession.Core 3004 3005**参数:** 3006 3007| 参数名 | 类型 | 必填 | 说明 | 3008| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3009| type | string | 是 | 事件回调类型,支持的事件为`'play'`当播放命令被发送到会话时,触发该事件回调。 | 3010| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | 3011 3012**错误码:** 3013 3014以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3015 3016| 错误码ID | 错误信息 | 3017| -------- | ---------------------------------------- | 3018| 6600101 | Session service exception. | 3019| 6600102 | The session does not exist. | 3020 3021**示例:** 3022 3023```ts 3024currentAVSession.on('play', () => { 3025 console.info(`on play entry`); 3026}); 3027``` 3028 3029### on('pause')<sup>10+</sup> 3030 3031on(type: 'pause', callback: () => void): void 3032 3033设置暂停命令监听事件。注册该监听,说明应用支持暂停指令。 3034 3035每个播放命令仅支持注册一个回调,如果注册新的回调,将替换前一个回调。 3036 3037**系统能力:** SystemCapability.Multimedia.AVSession.Core 3038 3039**参数:** 3040 3041| 参数名 | 类型 | 必填 | 说明 | 3042| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3043| type | string | 是 | 事件回调类型,支持的事件为`'pause'`,当暂停命令被发送到会话时,触发该事件回调。 | 3044| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | 3045 3046**错误码:** 3047 3048以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3049 3050| 错误码ID | 错误信息 | 3051| -------- | ---------------------------------------- | 3052| 6600101 | Session service exception. | 3053| 6600102 | The session does not exist. | 3054 3055**示例:** 3056 3057```ts 3058currentAVSession.on('pause', () => { 3059 console.info(`on pause entry`); 3060}); 3061``` 3062 3063### on('stop')<sup>10+</sup> 3064 3065on(type:'stop', callback: () => void): void 3066 3067设置停止命令监听事件。注册该监听,说明应用支持停止指令。 3068 3069每个播放命令仅支持注册一个回调,如果注册新的回调,将替换前一个回调。 3070 3071**系统能力:** SystemCapability.Multimedia.AVSession.Core 3072 3073**参数:** 3074 3075| 参数名 | 类型 | 必填 | 说明 | 3076| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3077| type | string | 是 | 事件回调类型,支持的事件是`'stop'`,当停止命令被发送到会话时,触发该事件回调。 | 3078| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | 3079 3080**错误码:** 3081 3082以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3083 3084| 错误码ID | 错误信息 | 3085| -------- | ---------------------------------------- | 3086| 6600101 | Session service exception. | 3087| 6600102 | The session does not exist. | 3088 3089**示例:** 3090 3091```ts 3092currentAVSession.on('stop', () => { 3093 console.info(`on stop entry`); 3094}); 3095``` 3096 3097### on('playNext')<sup>10+</sup> 3098 3099on(type:'playNext', callback: () => void): void 3100 3101设置播放下一首命令监听事件。注册该监听,说明应用支持下一首指令。 3102 3103每个播放命令仅支持注册一个回调,如果注册新的回调,将替换前一个回调。 3104 3105**系统能力:** SystemCapability.Multimedia.AVSession.Core 3106 3107**参数:** 3108 3109| 参数名 | 类型 | 必填 | 说明 | 3110| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3111| type | string | 是 | 事件回调类型,支持的事件是`'playNext'`,当播放下一首命令被发送到会话时,触发该事件回调。 | 3112| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | 3113 3114**错误码:** 3115 3116以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3117 3118| 错误码ID | 错误信息 | 3119| -------- | ---------------------------------------- | 3120| 6600101 | Session service exception. | 3121| 6600102 | The session does not exist. | 3122 3123**示例:** 3124 3125```ts 3126currentAVSession.on('playNext', () => { 3127 console.info(`on playNext entry`); 3128}); 3129``` 3130 3131### on('playPrevious')<sup>10+</sup> 3132 3133on(type:'playPrevious', callback: () => void): void 3134 3135设置播放上一首命令监听事件。注册该监听,说明应用支持上一首指令。 3136 3137每个播放命令仅支持注册一个回调,如果注册新的回调,将替换前一个回调。 3138 3139**系统能力:** SystemCapability.Multimedia.AVSession.Core 3140 3141**参数:** 3142 3143| 参数名 | 类型 | 必填 | 说明 | 3144| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3145| type | string | 是 | 事件回调类型,支持的事件是`'playPrevious'`当播放上一首命令被发送到会话时,触发该事件回调。 | 3146| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | 3147 3148**错误码:** 3149 3150以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3151 3152| 错误码ID | 错误信息 | 3153| -------- | ---------------------------------------- | 3154| 6600101 | Session service exception. | 3155| 6600102 | The session does not exist. | 3156 3157**示例:** 3158 3159```ts 3160currentAVSession.on('playPrevious', () => { 3161 console.info(`on playPrevious entry`); 3162}); 3163``` 3164 3165### on('fastForward')<sup>10+</sup> 3166 3167on(type: 'fastForward', callback: (time?: number) => void): void 3168 3169设置快进命令监听事件。注册该监听,说明应用支持快进指令。 3170 3171每个播放命令仅支持注册一个回调,如果注册新的回调,将替换前一个回调。 3172 3173**系统能力:** SystemCapability.Multimedia.AVSession.Core 3174 3175**参数:** 3176 3177| 参数名 | 类型 | 必填 | 说明 | 3178| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3179| type | string | 是 | 事件回调类型,支持的事件是 `'fastForward'`,当快进命令被发送到会话时,触发该事件回调。 | 3180| callback | (time?: number) => void | 是 | 回调函数。回调函数。参数time是时间节点,单位为秒。 | 3181 3182**错误码:** 3183以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3184 3185| 错误码ID | 错误信息 | 3186| -------- | ---------------------------------------- | 3187| 6600101 | Session service exception. | 3188| 6600102 | The session does not exist. | 3189 3190**示例:** 3191 3192```ts 3193currentAVSession.on('fastForward', () => { 3194 console.info(`on fastForward entry`); 3195}); 3196``` 3197 3198### on('rewind')<sup>10+</sup> 3199 3200on(type:'rewind', callback: (time?: number) => void): void 3201 3202设置快退命令监听事件。 3203 3204**系统能力:** SystemCapability.Multimedia.AVSession.Core 3205 3206**参数:** 3207 3208| 参数名 | 类型 | 必填 | 说明 | 3209| -------- | -------------------- | ---- | ------------------------------------------------------------ | 3210| type | string | 是 | 事件回调类型,支持的事件是`'rewind'`,当快退命令被发送到会话时,触发该事件回调。 | 3211| callback | (time?: number) => void | 是 | 回调函数。参数time是时间节点,单位为秒。 | 3212 3213**错误码:** 3214以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3215 3216| 错误码ID | 错误信息 | 3217| -------- | ---------------------------------------- | 3218| 6600101 | Session service exception. | 3219| 6600102 | The session does not exist. | 3220 3221**示例:** 3222 3223```ts 3224currentAVSession.on('rewind', () => { 3225 console.info(`on rewind entry`); 3226}); 3227``` 3228 3229### on('seek')<sup>10+</sup> 3230 3231on(type: 'seek', callback: (time: number) => void): void 3232 3233设置跳转节点监听事件。 3234 3235**系统能力:** SystemCapability.Multimedia.AVSession.Core 3236 3237**参数:** 3238 3239| 参数名 | 类型 | 必填 | 说明 | 3240| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 3241| type | string | 是 | 事件回调类型,支持事件`'seek'`:当跳转节点命令被发送到会话时,触发该事件。 | 3242| callback | (time: number) => void | 是 | 回调函数。参数time是时间节点,单位为毫秒。 | 3243 3244**错误码:** 3245 3246以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3247 3248| 错误码ID | 错误信息 | 3249| -------- | ---------------------------------------- | 3250| 6600101 | Session service exception. | 3251| 6600102 | The session does not exist. | 3252 3253**示例:** 3254 3255```ts 3256currentAVSession.on('seek', (time: number) => { 3257 console.info(`on seek entry time : ${time}`); 3258}); 3259``` 3260 3261### on('setSpeed')<sup>10+</sup> 3262 3263on(type: 'setSpeed', callback: (speed: number) => void): void 3264 3265设置播放速率的监听事件。 3266 3267**系统能力:** SystemCapability.Multimedia.AVSession.Core 3268 3269**参数:** 3270 3271| 参数名 | 类型 | 必填 | 说明 | 3272| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 3273| type | string | 是 | 事件回调类型,支持事件`'setSpeed'`:当设置播放速率的命令被发送到会话时,触发该事件。 | 3274| callback | (speed: number) => void | 是 | 回调函数。参数speed是播放倍速。 | 3275 3276**错误码:** 3277 3278以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3279 3280| 错误码ID | 错误信息 | 3281| -------- | ---------------------------------------- | 3282| 6600101 | Session service exception. | 3283| 6600102 | The session does not exist. | 3284 3285**示例:** 3286 3287```ts 3288currentAVSession.on('setSpeed', (speed: number) => { 3289 console.info(`on setSpeed speed : ${speed}`); 3290}); 3291``` 3292 3293### on('setLoopMode')<sup>10+</sup> 3294 3295on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void 3296 3297设置循环模式的监听事件。 3298 3299**系统能力:** SystemCapability.Multimedia.AVSession.Core 3300 3301**参数:** 3302 3303| 参数名 | 类型 | 必填 | 说明 | 3304| -------- | ------------------------------------- | ---- | ---- | 3305| type | string | 是 | 事件回调类型,支持事件`'setLoopMode'`:当设置循环模式的命令被发送到会话时,触发该事件。 | 3306| callback | (mode: [LoopMode](#loopmode10)) => void | 是 | 回调函数。参数mode是循环模式。 | 3307 3308**错误码:** 3309 3310以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3311 3312| 错误码ID | 错误信息 | 3313| -------- | ---------------------------------------- | 3314| 6600101 | Session service exception. | 3315| 6600102 | The session does not exist. | 3316 3317**示例:** 3318 3319```ts 3320currentAVSession.on('setLoopMode', (mode: avSession.LoopMode) => { 3321 console.info(`on setLoopMode mode : ${mode}`); 3322}); 3323``` 3324 3325### on('toggleFavorite')<sup>10+</sup> 3326 3327on(type: 'toggleFavorite', callback: (assetId: string) => void): void 3328 3329设置是否收藏的监听事件 3330 3331**系统能力:** SystemCapability.Multimedia.AVSession.Core 3332 3333**参数:** 3334 3335| 参数名 | 类型 | 必填 | 说明 | 3336| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 3337| type | string | 是 | 事件回调类型,支持事件`'toggleFavorite'`:当是否收藏的命令被发送到会话时,触发该事件。 | 3338| callback | (assetId: string) => void | 是 | 回调函数。参数assetId是媒体ID。 | 3339 3340**错误码:** 3341 3342以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3343 3344| 错误码ID | 错误信息 | 3345| -------- | ---------------------------------------- | 3346| 6600101 | Session service exception. | 3347| 6600102 | The session does not exist. | 3348 3349**示例:** 3350 3351```ts 3352currentAVSession.on('toggleFavorite', (assetId: string) => { 3353 console.info(`on toggleFavorite mode : ${assetId}`); 3354}); 3355``` 3356 3357### on('skipToQueueItem')<sup>10+</sup> 3358 3359on(type: 'skipToQueueItem', callback: (itemId: number) => void): void 3360 3361设置播放列表其中某项被选中的监听事件,session端可以选择对这个单项歌曲进行播放。 3362 3363**系统能力:** SystemCapability.Multimedia.AVSession.Core 3364 3365**参数:** 3366 3367| 参数名 | 类型 | 必填 | 说明 | 3368| -------- | ------------------------ | ---- | ---------------------------------------------------------------------------------------- | 3369| type | string | 是 | 事件回调类型,支持事件`'skipToQueueItem'`:当播放列表选中单项的命令被发送到会话时,触发该事件。 | 3370| callback | (itemId: number) => void | 是 | 回调函数。参数itemId是选中的播放列表项的ID。 | 3371 3372**错误码:** 3373 3374以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3375 3376| 错误码ID | 错误信息 | 3377| -------- | ---------------------------------------- | 3378| 6600101 | Session service exception. | 3379| 6600102 | The session does not exist. | 3380 3381**示例:** 3382 3383```ts 3384currentAVSession.on('skipToQueueItem', (itemId: number) => { 3385 console.info(`on skipToQueueItem id : ${itemId}`); 3386}); 3387``` 3388 3389### on('handleKeyEvent')<sup>10+</sup> 3390 3391on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void 3392 3393设置按键事件的监听 3394 3395**系统能力:** SystemCapability.Multimedia.AVSession.Core 3396 3397**参数:** 3398 3399| 参数名 | 类型 | 必填 | 说明 | 3400| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3401| type | string | 是 | 事件回调类型,支持事件`'handleKeyEvent'`:当按键事件被发送到会话时,触发该事件。 | 3402| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | 是 | 回调函数。参数event是按键事件。 | 3403 3404**错误码:** 3405 3406以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3407 3408| 错误码ID | 错误信息 | 3409| -------- | ---------------------------------------- | 3410| 6600101 | Session service exception. | 3411| 6600102 | The session does not exist. | 3412 3413**示例:** 3414 3415```ts 3416import keyEvent from '@ohos.multimodalInput.keyEvent'; 3417 3418currentAVSession.on('handleKeyEvent', (event: keyEvent.KeyEvent) => { 3419 console.info(`on handleKeyEvent event : ${event}`); 3420}); 3421 3422``` 3423 3424### on('outputDeviceChange')<sup>10+</sup> 3425 3426on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void 3427 3428设置播放设备变化的监听事件。 3429 3430**系统能力:** SystemCapability.Multimedia.AVSession.Core 3431 3432**参数:** 3433 3434| 参数名 | 类型 | 必填 | 说明 | 3435| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3436| type | string | 是 | 事件回调类型,支持事件`'outputDeviceChange'`:当播放设备变化时,触发该事件。 | 3437| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 是 | 回调函数,参数device是设备相关信息。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3438 3439**错误码:** 3440 3441以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/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('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { 3452 console.info(`on outputDeviceChange device : ${device}`); 3453}); 3454``` 3455 3456### on('commonCommand')<sup>10+</sup> 3457 3458on(type: 'commonCommand', callback: (command: string, args: {[key: string]: Object}) => void): void 3459 3460设置自定义控制命令变化的监听器。 3461 3462**系统能力:** SystemCapability.Multimedia.AVSession.Core 3463 3464**参数:** 3465 3466| 参数名 | 类型 | 必填 | 说明 | 3467| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3468| type | string | 是 | 事件回调类型,支持事件`'commonCommand'`:当自定义控制命令变化时,触发该事件。 | 3469| callback | (command: string, args: {[key:string]: Object}) => void | 是 | 回调函数,commonCommand为变化的自定义控制命令名,args为自定义控制命令的参数,参数内容与[sendCommonCommand](#sendcommoncommand10)方法设置的参数内容完全一致。 | 3470 3471**错误码:** 3472 3473以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3474 3475| 错误码ID | 错误信息 | 3476| -------- | ------------------------------ | 3477| 6600101 | Session service exception. | 3478| 6600102 | The session does not exist. | 3479 3480**示例:** 3481 3482```ts 3483import { BusinessError } from '@ohos.base'; 3484import avSession from '@ohos.multimedia.avsession'; 3485let currentAVSession: avSession.AVSession | undefined = undefined; 3486let tag = "createNewSession"; 3487let context: Context = getContext(this); 3488 3489avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 3490 if (err) { 3491 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 3492 } else { 3493 currentAVSession = data; 3494 } 3495}); 3496if (currentAVSession !== undefined) { 3497 (currentAVSession as avSession.AVSession).on('commonCommand', (commonCommand, args) => { 3498 console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`); 3499 }); 3500} 3501``` 3502 3503### off('play')<sup>10+</sup> 3504 3505off(type: 'play', callback?: () => void): void 3506 3507取消会话播放事件监听,关闭后,不再进行该事件回调。 3508 3509取消回调时,需要更新支持的命令列表。 3510 3511**系统能力:** SystemCapability.Multimedia.AVSession.Core 3512 3513**参数:** 3514 3515| 参数名 | 类型 | 必填 | 说明 | 3516| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3517| type | string | 是 | 关闭对应的监听事件,支持的事件是`'play'`| 3518| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3519 3520**错误码:** 3521 3522以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3523 3524| 错误码ID | 错误信息 | 3525| -------- | ---------------------------------------- | 3526| 6600101 | Session service exception. | 3527| 6600102 | The session does not exist. | 3528 3529**示例:** 3530 3531```ts 3532currentAVSession.off('play'); 3533``` 3534 3535### off('pause')<sup>10+</sup> 3536 3537off(type: 'pause', callback?: () => void): void 3538 3539取消会话暂停事件监听,关闭后,不再进行该事件回调。 3540 3541取消回调时,需要更新支持的命令列表。 3542 3543**系统能力:** SystemCapability.Multimedia.AVSession.Core 3544 3545**参数:** 3546 3547| 参数名 | 类型 | 必填 | 说明 | 3548| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3549| type | string | 是 | 关闭对应的监听事件,支持的事件是`'pause'`。 | 3550| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3551 3552**错误码:** 3553 3554以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3555 3556| 错误码ID | 错误信息 | 3557| -------- | ---------------------------------------- | 3558| 6600101 | Session service exception. | 3559| 6600102 | The session does not exist. | 3560 3561**示例:** 3562 3563```ts 3564currentAVSession.off('pause'); 3565``` 3566 3567### off('stop')<sup>10+</sup> 3568 3569off(type: 'stop', callback?: () => void): void 3570 3571取消会话停止事件监听,关闭后,不再进行该事件回调。 3572 3573取消回调时,需要更新支持的命令列表。 3574 3575**系统能力:** SystemCapability.Multimedia.AVSession.Core 3576 3577**参数:** 3578 3579| 参数名 | 类型 | 必填 | 说明 | 3580| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3581| type | string | 是 | 关闭对应的监听事件,支持的事件是`'stop'`。 | 3582| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3583 3584**错误码:** 3585 3586以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3587 3588| 错误码ID | 错误信息 | 3589| -------- | ---------------------------------------- | 3590| 6600101 | Session service exception. | 3591| 6600102 | The session does not exist. | 3592 3593**示例:** 3594 3595```ts 3596currentAVSession.off('stop'); 3597``` 3598 3599### off('playNext')<sup>10+</sup> 3600 3601off(type: 'playNext', callback?: () => void): void 3602 3603取消会话播放下一首事件监听,关闭后,不再进行该事件回调。 3604 3605取消回调时,需要更新支持的命令列表。 3606 3607**系统能力:** SystemCapability.Multimedia.AVSession.Core 3608 3609**参数:** 3610 3611| 参数名 | 类型 | 必填 | 说明 | 3612| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3613| type | string | 是 | 关闭对应的监听事件,支持的事件是 `'playNext'`。 | 3614| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3615 3616**错误码:** 3617 3618以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3619 3620| 错误码ID | 错误信息 | 3621| -------- | ---------------------------------------- | 3622| 6600101 | Session service exception. | 3623| 6600102 | The session does not exist. | 3624 3625**示例:** 3626 3627```ts 3628currentAVSession.off('playNext'); 3629``` 3630 3631### off('playPrevious')<sup>10+</sup> 3632 3633off(type: 'playPrevious', callback?: () => void): void 3634 3635取消会话播放上一首事件监听,关闭后,不再进行该事件回调。 3636 3637取消回调时,需要更新支持的命令列表。 3638 3639**系统能力:** SystemCapability.Multimedia.AVSession.Core 3640 3641**参数:** 3642 3643| 参数名 | 类型 | 必填 | 说明 | 3644| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3645| type | string | 是 | 关闭对应的监听事件,支持的事件是`'playPrevious'`。 | 3646| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3647 3648**错误码:** 3649 3650以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3651 3652| 错误码ID | 错误信息 | 3653| -------- | ---------------------------------------- | 3654| 6600101 | Session service exception. | 3655| 6600102 | The session does not exist. | 3656 3657**示例:** 3658 3659```ts 3660currentAVSession.off('playPrevious'); 3661``` 3662 3663### off('fastForward')<sup>10+</sup> 3664 3665off(type: 'fastForward', callback?: () => void): void 3666 3667取消会话快进事件监听,关闭后,不再进行该事件回调。 3668 3669取消回调时,需要更新支持的命令列表。 3670 3671**系统能力:** SystemCapability.Multimedia.AVSession.Core 3672 3673**参数:** 3674 3675| 参数名 | 类型 | 必填 | 说明 | 3676| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3677| type | string | 是 | 关闭对应的监听事件,支持的事件是`'fastForward'`。 | 3678| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3679 3680**错误码:** 3681 3682以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3683 3684| 错误码ID | 错误信息 | 3685| -------- | ---------------------------------------- | 3686| 6600101 | Session service exception. | 3687| 6600102 | The session does not exist. | 3688 3689**示例:** 3690 3691```ts 3692currentAVSession.off('fastForward'); 3693``` 3694 3695### off('rewind')<sup>10+</sup> 3696 3697off(type: 'rewind', callback?: () => void): void 3698 3699取消会话快退事件监听,关闭后,不再进行该事件回调。 3700 3701**系统能力:** SystemCapability.Multimedia.AVSession.Core 3702 3703**参数:** 3704 3705| 参数名 | 类型 | 必填 | 说明 | 3706| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | 3707| type | string | 是 | 关闭对应的监听事件,支持的事件是`'rewind'`。 | 3708| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3709 3710**错误码:** 3711 3712以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3713 3714| 错误码ID | 错误信息 | 3715| -------- | ---------------------------------------- | 3716| 6600101 | Session service exception. | 3717| 6600102 | The session does not exist. | 3718 3719**示例:** 3720 3721```ts 3722currentAVSession.off('rewind'); 3723``` 3724 3725### off('seek')<sup>10+</sup> 3726 3727off(type: 'seek', callback?: (time: number) => void): void 3728 3729取消监听跳转节点事件。 3730 3731**系统能力:** SystemCapability.Multimedia.AVSession.Core 3732 3733**参数:** 3734 3735| 参数名 | 类型 | 必填 | 说明 | 3736| -------- | ---------------------- | ---- | ----------------------------------------- | 3737| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'seek'`。 | 3738| callback | (time: number) => void | 否 | 回调函数,参数time是时间节点,单位为毫秒。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3739 3740**错误码:** 3741 3742以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3743 3744| 错误码ID | 错误信息 | 3745| -------- | ---------------------------------------- | 3746| 6600101 | Session service exception. | 3747| 6600102 | The session does not exist. | 3748 3749**示例:** 3750 3751```ts 3752currentAVSession.off('seek'); 3753``` 3754 3755### off('setSpeed')<sup>10+</sup> 3756 3757off(type: 'setSpeed', callback?: (speed: number) => void): void 3758 3759取消监听播放速率变化事件。 3760 3761**系统能力:** SystemCapability.Multimedia.AVSession.Core 3762 3763**参数:** 3764 3765| 参数名 | 类型 | 必填 | 说明 | 3766| -------- | ----------------------- | ---- | -------------------------------------------| 3767| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'setSpeed'`。 | 3768| callback | (speed: number) => void | 否 | 回调函数,参数speed是播放倍速。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3769 3770**错误码:** 3771 3772以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3773 3774| 错误码ID | 错误信息 | 3775| -------- | ---------------------------------------- | 3776| 6600101 | Session service exception. | 3777| 6600102 | The session does not exist. | 3778 3779**示例:** 3780 3781```ts 3782currentAVSession.off('setSpeed'); 3783``` 3784 3785### off('setLoopMode')<sup>10+</sup> 3786 3787off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void 3788 3789取消监听循环模式变化事件。 3790 3791**系统能力:** SystemCapability.Multimedia.AVSession.Core 3792 3793**参数:** 3794 3795| 参数名 | 类型 | 必填 | 说明 | 3796| -------- | ------------------------------------- | ---- | ----- | 3797| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'setLoopMode'`。| 3798| callback | (mode: [LoopMode](#loopmode10)) => void | 否 | 回调函数,参数mode是循环模式。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3799 3800**错误码:** 3801 3802以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3803 3804| 错误码ID | 错误信息 | 3805| -------- | ---------------------------------------- | 3806| 6600101 | Session service exception. | 3807| 6600102 | The session does not exist. | 3808 3809**示例:** 3810 3811```ts 3812currentAVSession.off('setLoopMode'); 3813``` 3814 3815### off('toggleFavorite')<sup>10+</sup> 3816 3817off(type: 'toggleFavorite', callback?: (assetId: string) => void): void 3818 3819取消监听是否收藏的事件 3820 3821**系统能力:** SystemCapability.Multimedia.AVSession.Core 3822 3823**参数:** 3824 3825| 参数名 | 类型 | 必填 | 说明 | 3826| -------- | ------------------------- | ---- | -------------------------------------------------------- | 3827| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'toggleFavorite'`。 | 3828| callback | (assetId: string) => void | 否 | 回调函数,参数assetId是媒体ID。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3829 3830**错误码:** 3831 3832以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3833 3834| 错误码ID | 错误信息 | 3835| -------- | ---------------------------------------- | 3836| 6600101 | Session service exception. | 3837| 6600102 | The session does not exist. | 3838 3839**示例:** 3840 3841```ts 3842currentAVSession.off('toggleFavorite'); 3843``` 3844 3845### off('skipToQueueItem')<sup>10+</sup> 3846 3847off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void 3848 3849取消监听播放列表单项选中的事件 3850 3851**系统能力:** SystemCapability.Multimedia.AVSession.Core 3852 3853**参数:** 3854 3855| 参数名 | 类型 | 必填 | 说明 | 3856| -------- | ------------------------ | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | 3857| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'skipToQueueItem'`。 | 3858| callback | (itemId: number) => void | 否 | 回调函数,参数itemId是播放列表单项ID。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3859 3860**错误码:** 3861 3862以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3863 3864| 错误码ID | 错误信息 | 3865| -------- | ---------------------------------------- | 3866| 6600101 | Session service exception. | 3867| 6600102 | The session does not exist. | 3868 3869**示例:** 3870 3871```ts 3872currentAVSession.off('skipToQueueItem'); 3873``` 3874 3875### off('handleKeyEvent')<sup>10+</sup> 3876 3877off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void 3878 3879取消监听按键事件。 3880 3881**系统能力:** SystemCapability.Multimedia.AVSession.Core 3882 3883**参数:** 3884 3885| 参数名 | 类型 | 必填 | 说明 | 3886| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 3887| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'handleKeyEvent'`。 | 3888| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | 否 | 回调函数,参数event是按键事件。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3889 3890**错误码:** 3891 3892以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3893 3894| 错误码ID | 错误信息 | 3895| -------- | ---------------------------------------- | 3896| 6600101 | Session service exception. | 3897| 6600102 | The session does not exist. | 3898 3899**示例:** 3900 3901```ts 3902currentAVSession.off('handleKeyEvent'); 3903``` 3904 3905### off('outputDeviceChange')<sup>10+</sup> 3906 3907off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void 3908 3909取消监听播放设备变化的事件。 3910 3911**系统能力:** SystemCapability.Multimedia.AVSession.Core 3912 3913**参数:** 3914 3915| 参数名 | 类型 | 必填 | 说明 | 3916| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | 3917| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'outputDeviceChange'`。 | 3918| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 否 | 回调函数,参数device是设备相关信息。<br>当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 3919 3920**错误码:** 3921 3922以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3923 3924| 错误码ID | 错误信息 | 3925| -------- | ---------------------------------------- | 3926| 6600101 | Session service exception. | 3927| 6600102 | The session does not exist. | 3928 3929**示例:** 3930 3931```ts 3932currentAVSession.off('outputDeviceChange'); 3933``` 3934 3935 3936### off('commonCommand')<sup>10+</sup> 3937 3938off(type: 'commonCommand', callback?: (command: string, args: {[key:string]: Object}) => void): void 3939 3940取消监听自定义控制命令的变化。 3941 3942**系统能力:** SystemCapability.Multimedia.AVSession.Core 3943 3944**参数:** 3945 3946| 参数名 | 类型 | 必填 | 说明 | 3947| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 3948| type | string | 是 | 取消对应的监听事件,支持事件`'commonCommand'`。 | 3949| callback | (command: string, args: {[key:string]: Object}) => void | 否 | 回调函数,参数command是变化的自定义控制命令名,args为自定义控制命令的参数。<br>该参数为可选参数,若不填写该参数,则认为取消所有对command事件的监听。 | 3950 3951**错误码:** 3952 3953以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3954 3955| 错误码ID | 错误信息 | 3956| -------- | ---------------- | 3957| 6600101 | Session service exception. | 3958| 6600102 | The session does not exist. | 3959 3960**示例:** 3961 3962```ts 3963currentAVSession.off('commonCommand'); 3964``` 3965 3966### stopCasting<sup>10+</sup> 3967 3968stopCasting(callback: AsyncCallback\<void>): void 3969 3970结束投播。结果通过callback异步回调方式返回。 3971 3972**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 3973 3974**参数:** 3975 3976| 参数名 | 类型 | 必填 | 说明 | 3977| -------- | ------------------------------------- | ---- | ------------------------------------- | 3978| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 3979 3980**错误码:** 3981 3982以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 3983 3984| 错误码ID | 错误信息 | 3985| -------- | ---------------------------------------- | 3986| 6600109 | The remote connection is not established. | 3987 3988**示例:** 3989 3990```ts 3991import { BusinessError } from '@ohos.base'; 3992 3993currentAVSession.stopCasting((err: BusinessError) => { 3994 if (err) { 3995 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 3996 } else { 3997 console.info(`stopCasting successfully`); 3998 } 3999}); 4000``` 4001 4002### stopCasting<sup>10+</sup> 4003 4004stopCasting(): Promise\<void> 4005 4006结束投播。结果通过Promise异步回调方式返回。 4007 4008**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4009 4010**返回值:** 4011 4012| 类型 | 说明 | 4013| -------------- | ----------------------------- | 4014| Promise\<void> | Promise对象。当成功结束投播,无返回结果,否则返回错误对象。 | 4015 4016**错误码:** 4017 4018以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4019 4020| 错误码ID | 错误信息 | 4021| -------- | ---------------------------------------- | 4022| 6600109 | The remote connection is not established. | 4023 4024**示例:** 4025 4026```ts 4027import { BusinessError } from '@ohos.base'; 4028 4029currentAVSession.stopCasting().then(() => { 4030 console.info(`stopCasting successfully`); 4031}).catch((err: BusinessError) => { 4032 console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); 4033}); 4034``` 4035 4036## AVCastControlCommandType<sup>10+</sup> 4037 4038投播控制器可传递的命令。 4039 4040**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4041 4042| 名称 | 类型 | 说明 | 4043| -------------- | ------ | ------------ | 4044| play | string | 播放 | 4045| pause | string | 暂停 | 4046| stop | string | 停止 | 4047| playNext | string | 下一首 | 4048| playPrevious | string | 上一首 | 4049| fastForward | string | 快进 | 4050| rewind | string | 快退 | 4051| seek | number | 跳转某一节点 | 4052| setSpeed | number | 设置播放倍速 | 4053| setLoopMode | string | 设置循环模式 | 4054| toggleFavorite | string | 是否收藏 | 4055| setVolume | number | 设置音量 | 4056 4057## AVCastControlCommand<sup>10+</sup> 4058 4059投播控制器接受的命令的对象描述。 4060 4061**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4062 4063| 名称 | 类型 | 必填 | 说明 | 4064| --------- | ------------------------------------------------- | ---- | -------------- | 4065| command | [AVCastControlCommandType](#avcastcontrolcommandtype10) | 是 | 命令 | 4066| parameter | [LoopMode](#loopmode10) | string | number | [media.PlaybackSpeed](./js-apis-media.md#playbackspeed8) | 否 | 命令对应的参数 | 4067 4068## AVCastController<sup>10+</sup> 4069 4070在投播建立后,调用[avSession.getAVCastController](#getavcastcontroller10)后,返回会话控制器实例。控制器可查看会话ID,并可完成对会话发送命令及事件,获取会话元数据,播放状态信息等操作。 4071 4072### setDisplaySurface<sup>10+</sup> 4073 4074setDisplaySurface(surfaceId: string): Promise\<void> 4075 4076设置播放的surfaceId,在投播sink端使用。结果通过Promise异步回调方式返回。 4077 4078**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4079 4080**系统接口:** 该接口为系统接口。 4081 4082**返回值:** 4083 4084| 类型 | 说明 | 4085| --------------------------------------------- | --------------------------- | 4086| Promise\<void> | Promise对象。返回设置结果。 | 4087 4088**错误码:** 4089 4090以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4091 4092| 错误码ID | 错误信息 | 4093| -------- | ---------------------------------------- | 4094| 6600109 | The remote connection is not established. | 4095 4096**示例:** 4097 4098```ts 4099import media from '@ohos.multimedia.media'; 4100let surfaceID: string = ''; 4101media.createAVRecorder().then((avRecorder) => { 4102 avRecorder.getInputSurface((err: BusinessError, surfaceId: string) => { 4103 if (err == null) { 4104 console.info('getInputSurface success'); 4105 surfaceID = surfaceId; 4106 } else { 4107 console.error('getInputSurface failed and error is ' + err.message); 4108 } 4109 }); 4110}) 4111aVCastController.setDisplaySurface(surfaceID).then(() => { 4112 console.info(`setDisplaySurface : SUCCESS`); 4113}); 4114``` 4115 4116### setDisplaySurface<sup>10+</sup> 4117 4118setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void 4119 4120设置播放的surfaceId,在投播sink端使用。结果通过callback异步回调方式返回。 4121 4122**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4123 4124**系统接口:** 该接口为系统接口。 4125 4126**参数:** 4127 4128| 参数名 | 类型 | 必填 | 说明 | 4129| -------- | --------------------------------------------------- | ---- | ---------------------------- | 4130| callback | AsyncCallback\<void> | 是 | 回调函数,返回当前设置结果。 | 4131| surfaceId | string | 是 | 设置播放的surfaceId。 | 4132 4133**错误码:** 4134 4135以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4136 4137| 错误码ID | 错误信息 | 4138| -------- | ---------------------------------------- | 4139| 6600109 | The remote connection is not established. | 4140 4141**示例:** 4142 4143```ts 4144import { BusinessError } from '@ohos.base'; 4145import media from '@ohos.multimedia.media'; 4146let surfaceID: string = ''; 4147media.createAVRecorder().then((avRecorder) => { 4148 avRecorder.getInputSurface((err: BusinessError, surfaceId: string) => { 4149 if (err == null) { 4150 console.info('getInputSurface success'); 4151 surfaceID = surfaceId; 4152 } else { 4153 console.error('getInputSurface failed and error is ' + err.message); 4154 } 4155 }); 4156}) 4157aVCastController.setDisplaySurface(surfaceID, (err: BusinessError) => { 4158 if (err) { 4159 console.error(`setDisplaySurface BusinessError: code: ${err.code}, message: ${err.message}`); 4160 } else { 4161 console.info(`setDisplaySurface : SUCCESS`); 4162 } 4163}); 4164``` 4165 4166### getAVPlaybackState<sup>10+</sup> 4167 4168getAVPlaybackState(callback: AsyncCallback\<AVPlaybackState>): void 4169 4170获取当前的远端播放状态。结果通过callback异步回调方式返回。 4171 4172**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4173 4174**参数:** 4175 4176| 参数名 | 类型 | 必填 | 说明 | 4177| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 4178| callback | AsyncCallback<[[AVPlaybackState](#avplaybackstate10)\> | 是 | 回调函数,返回远端播放状态。 | 4179 4180**错误码:** 4181 4182以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4183 4184| 错误码ID | 错误信息 | 4185| -------- | ---------------------------------------- | 4186| 6600101 | Session service exception | 4187 4188**示例:** 4189 4190```ts 4191import { BusinessError } from '@ohos.base'; 4192 4193aVCastController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { 4194 if (err) { 4195 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 4196 } else { 4197 console.info(`getAVPlaybackState : SUCCESS`); 4198 } 4199}); 4200``` 4201 4202### getAVPlaybackState<sup>10+</sup> 4203 4204getAVPlaybackState(): Promise\<AVPlaybackState> 4205 4206获取当前的远端播放状态。结果通过Promise异步回调方式返回。 4207 4208**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4209 4210**返回值:** 4211 4212| 类型 | 说明 | 4213| --------- | ------------------------------------------------------------ | 4214| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise对象。返回远端播放状态。。 | 4215 4216**错误码:** 4217 4218以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4219 4220| 错误码ID | 错误信息 | 4221| -------- | ---------------------------------------- | 4222| 6600101 | Session service exception | 4223 4224**示例:** 4225 4226```ts 4227import { BusinessError } from '@ohos.base'; 4228 4229aVCastController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { 4230 console.info(`getAVPlaybackState : SUCCESS`); 4231}).catch((err: BusinessError) => { 4232 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 4233}); 4234``` 4235 4236### sendControlCommand<sup>10+</sup> 4237 4238sendControlCommand(command: AVCastControlCommand): Promise\<void> 4239 4240通过控制器发送命令到其对应的会话。结果通过Promise异步回调方式返回。 4241 4242 4243**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4244 4245**参数:** 4246 4247| 参数名 | 类型 | 必填 | 说明 | 4248| ------- | ------------------------------------- | ---- | ------------------------------ | 4249| command | [AVCastControlCommand](#avcastcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | 4250 4251**返回值:** 4252 4253| 类型 | 说明 | 4254| -------------- | ----------------------------- | 4255| Promise\<void> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | 4256 4257**错误码:** 4258 4259以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4260 4261| 错误码ID | 错误信息 | 4262| -------- | ---------------------------------------- | 4263| 6600101 | Session service exception. | 4264| 6600105 | Invalid session command. | 4265| 6600109 | The remote connection is not established. | 4266 4267**示例:** 4268 4269```ts 4270import avSession from '@ohos.multimedia.avsession'; 4271import { BusinessError } from '@ohos.base'; 4272 4273let avCommand: avSession.AVCastControlCommand = {command:'play'}; 4274aVCastController.sendControlCommand(avCommand).then(() => { 4275 console.info(`SendControlCommand successfully`); 4276}).catch((err: BusinessError) => { 4277 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 4278}); 4279``` 4280 4281### sendControlCommand<sup>10+</sup> 4282 4283sendControlCommand(command: AVCastControlCommand, callback: AsyncCallback\<void>): void 4284 4285通过会话控制器发送命令到其对应的会话。结果通过callback异步回调方式返回。 4286 4287 4288**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4289 4290**参数:** 4291 4292| 参数名 | 类型 | 必填 | 说明 | 4293| -------- | ------------------------------------- | ---- | ------------------------------ | 4294| command | [AVCastControlCommand](#avcastcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | 4295| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 4296 4297**错误码:** 4298 4299以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4300 4301| 错误码ID | 错误信息 | 4302| -------- | ------------------------------- | 4303| 6600101 | Session service exception. | 4304| 6600105 | Invalid session command. | 4305| 6600109 | The remote connection is not established. | 4306 4307**示例:** 4308 4309```ts 4310import avSession from '@ohos.multimedia.avsession'; 4311import { BusinessError } from '@ohos.base'; 4312 4313let avCommand: avSession.AVCastControlCommand = {command:'play'}; 4314aVCastController.sendControlCommand(avCommand, (err: BusinessError) => { 4315 if (err) { 4316 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 4317 } else { 4318 console.info(`SendControlCommand successfully`); 4319 } 4320}); 4321``` 4322 4323### prepare<sup>10+</sup> 4324 4325prepare(item: AVQueueItem, callback: AsyncCallback\<void>): void 4326 4327准备播放媒体资源,即进行播放资源的加载和缓冲。结果通过callback异步回调方式返回。 4328 4329**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4330 4331**参数:** 4332 4333| 参数名 | 类型 | 必填 | 说明 | 4334| ------- | ------------------------------------- | ---- | ------------------------------ | 4335| item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | 4336| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 4337 4338**错误码:** 4339 4340以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4341 4342| 错误码ID | 错误信息 | 4343| -------- | ---------------------------------------- | 4344| 6600101 | Session service exception. | 4345| 6600109 | The remote connection is not established. | 4346 4347**示例:** 4348 4349```ts 4350import avSession from '@ohos.multimedia.avsession'; 4351import { BusinessError } from '@ohos.base'; 4352 4353// 设置播放参数,开始播放 4354let playItem: avSession.AVQueueItem = { 4355 itemId: 0, 4356 description: { 4357 assetId: '12345', 4358 mediaType: 'AUDIO', 4359 mediaUri: 'http://resource1_address', 4360 mediaSize: 12345, 4361 startPosition: 0, 4362 duration: 0, 4363 artist: 'mysong', 4364 albumTitle: 'song1_title', 4365 albumCoverUri: "http://resource1_album_address", 4366 lyricUri: "http://resource1_lyric_address", 4367 appName: 'MyMusic' 4368 } 4369}; 4370// 准备播放,这个不会触发真正的播放,会进行加载和缓冲 4371aVCastController.prepare(playItem, (err: BusinessError) => { 4372 if (err) { 4373 console.error(`prepare BusinessError: code: ${err.code}, message: ${err.message}`); 4374 } else { 4375 console.info(`prepare successfully`); 4376 } 4377}); 4378``` 4379 4380 4381### prepare<sup>10+</sup> 4382 4383prepare(item: AVQueueItem): Promise\<void> 4384 4385准备播放媒体资源,即进行播放资源的加载和缓冲。结果通过Promise异步回调方式返回。 4386 4387 4388**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4389 4390**参数:** 4391 4392| 参数名 | 类型 | 必填 | 说明 | 4393| ------- | ------------------------------------- | ---- | ------------------------------ | 4394| item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | 4395 4396**返回值:** 4397 4398| 类型 | 说明 | 4399| -------------- | ----------------------------- | 4400| Promise\<void> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | 4401 4402**错误码:** 4403 4404以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4405 4406| 错误码ID | 错误信息 | 4407| -------- | ---------------------------------------- | 4408| 6600101 | Session service exception. | 4409| 6600109 | The remote connection is not established. | 4410 4411 4412**示例:** 4413 4414```ts 4415import avSession from '@ohos.multimedia.avsession'; 4416import { BusinessError } from '@ohos.base'; 4417 4418// 设置播放参数,开始播放 4419let playItem: avSession.AVQueueItem = { 4420 itemId: 0, 4421 description: { 4422 assetId: '12345', 4423 mediaType: 'AUDIO', 4424 mediaUri: 'http://resource1_address', 4425 mediaSize: 12345, 4426 startPosition: 0, 4427 duration: 0, 4428 artist: 'mysong', 4429 albumTitle: 'song1_title', 4430 albumCoverUri: "http://resource1_album_address", 4431 lyricUri: "http://resource1_lyric_address", 4432 appName: 'MyMusic' 4433 } 4434}; 4435// 准备播放,这个不会触发真正的播放,会进行加载和缓冲 4436aVCastController.prepare(playItem).then(() => { 4437 console.info(`prepare successfully`); 4438}).catch((err: BusinessError) => { 4439 console.error(`prepare BusinessError: code: ${err.code}, message: ${err.message}`); 4440}); 4441``` 4442 4443### start<sup>10+</sup> 4444 4445start(item: AVQueueItem, callback: AsyncCallback\<void>): void 4446 4447启动播放某个媒体资源。结果通过callback异步回调方式返回。 4448 4449**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4450 4451**参数:** 4452 4453| 参数名 | 类型 | 必填 | 说明 | 4454| ------- | ------------------------------------- | ---- | ------------------------------ | 4455| item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | 4456| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 4457 4458**错误码:** 4459 4460以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4461 4462| 错误码ID | 错误信息 | 4463| -------- | ---------------------------------------- | 4464| 6600101 | Session service exception. | 4465| 6600109 | The remote connection is not established. | 4466 4467**示例:** 4468 4469```ts 4470import avSession from '@ohos.multimedia.avsession'; 4471import { BusinessError } from '@ohos.base'; 4472 4473// 设置播放参数,开始播放 4474let playItem: avSession.AVQueueItem = { 4475 itemId: 0, 4476 description: { 4477 assetId: '12345', 4478 mediaType: 'AUDIO', 4479 mediaUri: 'http://resource1_address', 4480 mediaSize: 12345, 4481 startPosition: 0, 4482 duration: 0, 4483 artist: 'mysong', 4484 albumTitle: 'song1_title', 4485 albumCoverUri: "http://resource1_album_address", 4486 lyricUri: "http://resource1_lyric_address", 4487 appName: 'MyMusic' 4488 } 4489}; 4490 4491// 启动播放 4492aVCastController.start(playItem, (err: BusinessError) => { 4493 if (err) { 4494 console.error(`start BusinessError: code: ${err.code}, message: ${err.message}`); 4495 } else { 4496 console.info(`start successfully`); 4497 } 4498}); 4499``` 4500 4501### start<sup>10+</sup> 4502 4503start(item: AVQueueItem): Promise\<void> 4504 4505启动播放某个媒体资源。结果通过Promise异步回调方式返回。 4506 4507 4508**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4509 4510**参数:** 4511 4512| 参数名 | 类型 | 必填 | 说明 | 4513| ------- | ------------------------------------- | ---- | ------------------------------ | 4514| item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | 4515 4516**返回值:** 4517 4518| 类型 | 说明 | 4519| -------------- | ----------------------------- | 4520| Promise\<void> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | 4521 4522**错误码:** 4523 4524以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4525 4526| 错误码ID | 错误信息 | 4527| -------- | ---------------------------------------- | 4528| 6600101 | Session service exception. | 4529| 6600109 | The remote connection is not established. | 4530 4531 4532**示例:** 4533 4534```ts 4535import avSession from '@ohos.multimedia.avsession'; 4536import { BusinessError } from '@ohos.base'; 4537 4538// 设置播放参数,开始播放 4539let playItem: avSession.AVQueueItem = { 4540 itemId: 0, 4541 description: { 4542 assetId: '12345', 4543 mediaType: 'AUDIO', 4544 mediaUri: 'http://resource1_address', 4545 mediaSize: 12345, 4546 startPosition: 0, 4547 duration: 0, 4548 artist: 'mysong', 4549 albumTitle: 'song1_title', 4550 albumCoverUri: "http://resource1_album_address", 4551 lyricUri: "http://resource1_lyric_address", 4552 appName: 'MyMusic' 4553 } 4554}; 4555// 启动播放 4556aVCastController.start(playItem).then(() => { 4557 console.info(`start successfully`); 4558}).catch((err: BusinessError) => { 4559 console.error(`start BusinessError: code: ${err.code}, message: ${err.message}`); 4560}); 4561``` 4562 4563### getCurrentItem<sup>10+</sup> 4564 4565getCurrentItem(callback: AsyncCallback\<AVQueueItem>): void 4566 4567获取当前投播的资源信息。结果通过callback异步回调方式返回。 4568 4569**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4570 4571**参数:** 4572 4573| 参数名 | 类型 | 必填 | 说明 | 4574| -------- | ------------------------------------- | ---- | ------------------------------------- | 4575| callback | AsyncCallback\<[AVQueueItem](#avqueueitem10)> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 4576 4577**错误码:** 4578 4579以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4580 4581| 错误码ID | 错误信息 | 4582| -------- | ---------------------------------------- | 4583| 6600101 | Session service exception. | 4584 4585**示例:** 4586 4587```ts 4588import { BusinessError } from '@ohos.base'; 4589 4590aVCastController.getCurrentItem((err: BusinessError, value: avSession.AVQueueItem) => { 4591 if (err) { 4592 console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); 4593 } else { 4594 console.info(`getCurrentItem successfully`); 4595 } 4596}); 4597``` 4598 4599### getCurrentItem<sup>10+</sup> 4600 4601getCurrentItem(): Promise\<AVQueueItem> 4602 4603获取当前投播的资源信息。结果通过Promise异步回调方式返回。 4604 4605**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4606 4607**返回值:** 4608 4609| 类型 | 说明 | 4610| -------------- | ----------------------------- | 4611| Promise\<[AVQueueItem](#avqueueitem10)> | Promise对象,返回当前的播放资源,否则返回错误对象。 | 4612 4613**错误码:** 4614 4615以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4616 4617| 错误码ID | 错误信息 | 4618| -------- | ---------------------------------------- | 4619| 6600101 | Session service exception. | 4620 4621**示例:** 4622 4623```ts 4624import { BusinessError } from '@ohos.base'; 4625 4626aVCastController.getCurrentItem().then((value: avSession.AVQueueItem) => { 4627 console.info(`getCurrentItem successfully`); 4628}).catch((err: BusinessError) => { 4629 console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); 4630}); 4631 4632``` 4633 4634### on('playbackStateChange')<sup>10+</sup> 4635 4636on(type: 'playbackStateChange', filter: Array\<keyof AVPlaybackState> | 'all', callback: (state: AVPlaybackState) => void): void 4637 4638设置播放状态变化的监听事件。 4639 4640**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4641 4642**参数:** 4643 4644| 参数名 | 类型 | 必填 | 说明 | 4645| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4646| type | string | 是 | 事件回调类型,支持事件`'playbackStateChange'`:当播放状态变化时,触发该事件。 | 4647| filter | Array\<keyof [AVPlaybackState](#avplaybackstate10)\> | 'all' | 是 | 'all' 表示关注播放状态所有字段变化;Array<keyof [AVPlaybackState](#avplaybackstate10)\> 表示关注Array中的字段变化。 | 4648| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | 是 | 回调函数,参数state是变化后的播放状态。 | 4649 4650**错误码:** 4651 4652以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4653 4654| 错误码ID | 错误信息 | 4655| -------- | ------------------------------ | 4656| 6600101 | Session service exception. | 4657 4658**示例:** 4659 4660```ts 4661aVCastController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { 4662 console.info(`on playbackStateChange state : ${playbackState.state}`); 4663}); 4664 4665let playbackFilter = ['state', 'speed', 'loopMode']; 4666aVCastController.on('playbackStateChange', playbackFilter, (playbackState: avSession.AVPlaybackState) => { 4667 console.info(`on playbackStateChange state : ${playbackState.state}`); 4668}); 4669``` 4670 4671### off('playbackStateChange')<sup>10+</sup> 4672 4673off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void): void 4674 4675媒体控制器取消监听播放状态变化的事件。 4676 4677**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4678 4679**参数:** 4680 4681| 参数名 | 类型 | 必填 | 说明 | 4682| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4683| type | string | 是 | 取消对应的监听事件,支持事件`'playbackStateChange'`。 | 4684| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | 否 | 回调函数,参数state是变化后的播放状态。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 4685 4686**错误码:** 4687 4688以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4689 4690| 错误码ID | 错误信息 | 4691| -------- | ---------------- | 4692| 6600101 | Session service exception. | 4693 4694**示例:** 4695 4696```ts 4697aVCastController.off('playbackStateChange'); 4698``` 4699 4700### on('mediaItemChange')<sup>10+</sup> 4701 4702on(type: 'mediaItemChange', callback: Callback\<AVQueueItem>): void 4703 4704设置投播当前播放媒体内容的监听事件。 4705 4706**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4707 4708**参数:** 4709 4710| 参数名 | 类型 | 必填 | 说明 | 4711| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4712| type | string | 是 | 事件回调类型,支持事件`'mediaItemChange'`:当播放的媒体内容变化时,触发该事件。 | 4713| callback | (callback: [AVQueueItem](#avqueueitem10)) => void | 是 | 回调函数,参数AVQueueItem是当前正在播放的媒体内容。 | 4714 4715**错误码:** 4716 4717以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4718 4719| 错误码ID | 错误信息 | 4720| -------- | ------------------------------ | 4721| 6600101 | Session service exception. | 4722 4723**示例:** 4724 4725```ts 4726aVCastController.on('mediaItemChange', (item: avSession.AVQueueItem) => { 4727 console.info(`on mediaItemChange state : ${item.itemId}`); 4728}); 4729``` 4730 4731### off('mediaItemChange')<sup>10+</sup> 4732 4733off(type: 'mediaItemChange'): void 4734 4735取消设置投播当前播放媒体内容的监听事件。 4736 4737**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4738 4739**参数:** 4740 4741| 参数名 | 类型 | 必填 | 说明 | 4742| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4743| type | string | 是 | 取消对应的监听事件,支持事件`'mediaItemChange'`。 | 4744 4745**错误码:** 4746 4747以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4748 4749| 错误码ID | 错误信息 | 4750| -------- | ---------------- | 4751| 6600101 | Session service exception. | 4752 4753**示例:** 4754 4755```ts 4756aVCastController.off('mediaItemChange'); 4757``` 4758 4759### on('playNext')<sup>10+</sup> 4760 4761on(type: 'playNext', callback: Callback\<void>): void 4762 4763设置播放下一首资源的监听事件。 4764 4765**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4766 4767**参数:** 4768 4769| 参数名 | 类型 | 必填 | 说明 | 4770| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4771| type | string | 是 | 事件回调类型,支持事件`'playNext'`:当播放下一首状态变化时,触发该事件。 | 4772| callback | Callback\<void\> | 是 | 回调函数 | 4773 4774**错误码:** 4775 4776以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4777 4778| 错误码ID | 错误信息 | 4779| -------- | ------------------------------ | 4780| 6600101 | Session service exception. | 4781 4782**示例:** 4783 4784```ts 4785aVCastController.on('playNext', () => { 4786 console.info(`on playNext`); 4787}); 4788``` 4789 4790### off('playNext')<sup>10+</sup> 4791 4792off(type: 'playNext'): void 4793 4794取消设置播放下一首资源的监听事件。 4795 4796**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4797 4798**参数:** 4799 4800| 参数名 | 类型 | 必填 | 说明 | 4801| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4802| type | string | 是 | 取消对应的监听事件,支持事件`'playNext'`。 | 4803 4804**错误码:** 4805 4806以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4807 4808| 错误码ID | 错误信息 | 4809| -------- | ---------------- | 4810| 6600101 | Session service exception. | 4811 4812**示例:** 4813 4814```ts 4815aVCastController.off('playNext'); 4816``` 4817 4818### on('playPrevious')<sup>10+</sup> 4819 4820on(type: 'playPrevious', callback: Callback\<void>): void 4821 4822设置播放上一首资源的监听事件。 4823 4824**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4825 4826**参数:** 4827 4828| 参数名 | 类型 | 必填 | 说明 | 4829| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4830| type | string | 是 | 事件回调类型,支持事件`'playPrevious'`:当播放上一首状态变化时,触发该事件。 | 4831| callback | Callback\<void\> | 是 | 回调函数 | 4832 4833**错误码:** 4834 4835以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4836 4837| 错误码ID | 错误信息 | 4838| -------- | ------------------------------ | 4839| 6600101 | Session service exception. | 4840 4841**示例:** 4842 4843```ts 4844aVCastController.on('playPrevious', () => { 4845 console.info(`on playPrevious`); 4846}); 4847``` 4848 4849### off('playPrevious')<sup>10+</sup> 4850 4851off(type: 'playPrevious'): void 4852 4853取消设置播放上一首资源的监听事件。 4854 4855**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4856 4857**参数:** 4858 4859| 参数名 | 类型 | 必填 | 说明 | 4860| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4861| type | string | 是 | 取消对应的监听事件,支持事件`'playPrevious'`。 | 4862 4863**错误码:** 4864 4865以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4866 4867| 错误码ID | 错误信息 | 4868| -------- | ---------------- | 4869| 6600101 | Session service exception. | 4870 4871**示例:** 4872 4873```ts 4874aVCastController.off('playPrevious'); 4875``` 4876 4877### on('seekDone')<sup>10+</sup> 4878 4879on(type: 'seekDone', callback: Callback\<number>): void 4880 4881设置seek结束的监听事件。 4882 4883**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4884 4885**参数:** 4886 4887| 参数名 | 类型 | 必填 | 说明 | 4888| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4889| type | string | 是 | 事件回调类型,支持事件`'seekDone'`:当seek结束时,触发该事件。 | 4890| callback | Callback\<number\> | 是 | 回调函数,返回seek后播放的位置 | 4891 4892**错误码:** 4893 4894以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4895 4896| 错误码ID | 错误信息 | 4897| -------- | ------------------------------ | 4898| 6600101 | Session service exception. | 4899 4900**示例:** 4901 4902```ts 4903aVCastController.on('seekDone', (pos: number) => { 4904 console.info(`on seekDone pos:${pos} `); 4905}); 4906``` 4907 4908### off('seekDone')<sup>10+</sup> 4909 4910off(type: 'seekDone'): void 4911 4912取消设置seek结束的监听事件。 4913 4914**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4915 4916**参数:** 4917 4918| 参数名 | 类型 | 必填 | 说明 | 4919| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4920| type | string | 是 | 取消对应的监听事件,支持事件`'seekDone'`。 | 4921 4922**错误码:** 4923 4924以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4925 4926| 错误码ID | 错误信息 | 4927| -------- | ---------------- | 4928| 6600101 | Session service exception. | 4929 4930**示例:** 4931 4932```ts 4933aVCastController.off('seekDone'); 4934``` 4935 4936### on('videoSizeChange')<sup>10+</sup> 4937 4938on(type: 'videoSizeChange', callback: (width:number, height:number) => void): void 4939 4940设置video尺寸更改监听事件。 4941 4942**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4943 4944**系统接口:** 该接口为系统接口 4945 4946**参数:** 4947 4948| 参数名 | 类型 | 必填 | 说明 | 4949| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4950| type | string | 是 | 事件回调类型,支持事件`'videoSizeChange'`:当video尺寸更改时,触发该事件。 | 4951| callback | (width:number, height:number) => void | 是 | 回调函数,返回video的宽度和高度 | 4952 4953**错误码:** 4954 4955以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4956 4957| 错误码ID | 错误信息 | 4958| -------- | ---------------- | 4959| 6600101 | Session service exception. | 4960 4961**示例:** 4962 4963```ts 4964aVCastController.on('videoSizeChange', (width: number, height: number) => { 4965 console.info(`width :${width} `); 4966 console.info(`height:${height} `); 4967}); 4968``` 4969 4970### off('videoSizeChange')<sup>10+</sup> 4971 4972off(type: 'videoSizeChange'): void 4973 4974取消设置video尺寸更改监听事件。 4975 4976**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 4977 4978**系统接口:** 该接口为系统接口 4979 4980**参数:** 4981 4982| 参数名 | 类型 | 必填 | 说明 | 4983| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 4984| type | string | 是 | 取消对应的监听事件,支持事件`'videoSizeChange'`。 | 4985 4986**错误码:** 4987 4988以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 4989 4990| 错误码ID | 错误信息 | 4991| -------- | ---------------- | 4992| 6600101 | Session service exception. | 4993 4994**示例:** 4995 4996```ts 4997aVCastController.off('videoSizeChange'); 4998``` 4999 5000### on('error')<sup>10+</sup> 5001 5002on(type: 'error', callback: ErrorCallback): void 5003 5004监听远端播放器的错误事件,该事件仅用于错误提示,不需要用户停止播控动作。 5005 5006**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 5007 5008**参数:** 5009 5010| 参数名 | 类型 | 必填 | 说明 | 5011| -------- | -------- | ---- | ------------------------------------------------------------ | 5012| type | string | 是 | 错误事件回调类型,支持的事件:'error',用户操作和系统都会触发此事件。 | 5013| callback | ErrorCallback | 是 | 错误事件回调方法:远端播放过程中发生的错误,会提供错误码ID和错误信息。 | 5014 5015**错误码:** 5016 5017以下错误码的详细介绍请参见[媒体服务错误码](../errorcodes/errorcode-media.md)。 5018 5019| 错误码ID | 错误信息 | 5020| -------- | --------------------- | 5021| 5400101 | No memory. | 5022| 5400102 | Operation not allowed. | 5023| 5400103 | I/O error. | 5024| 5400104 | Time out. | 5025| 5400105 | Service died. | 5026| 5400106 | Unsupport format. | 5027| 6600101 | Session service exception. | 5028 5029**示例:** 5030 5031```ts 5032import { BusinessError } from '@ohos.base' 5033 5034aVCastController.on('error', (error: BusinessError) => { 5035 console.info('error happened,and error message is :' + error.message) 5036 console.info('error happened,and error code is :' + error.code) 5037}) 5038``` 5039 5040### off('error')<sup>10+</sup> 5041 5042off(type: 'error'): void 5043 5044取消监听播放的错误事件。 5045 5046**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 5047 5048**参数:** 5049 5050| 参数名 | 类型 | 必填 | 说明 | 5051| ------ | ------ | ---- | ----------------------------------------- | 5052| type | string | 是 | 错误事件回调类型,取消注册的事件:'error' | 5053 5054**错误码:** 5055 5056以下错误码的详细介绍请参见[媒体服务错误码](../errorcodes/errorcode-media.md)。 5057 5058| 错误码ID | 错误信息 | 5059| -------- | --------------------- | 5060| 5400101 | No memory. | 5061| 5400102 | Operation not allowed. | 5062| 5400103 | I/O error. | 5063| 5400104 | Time out. | 5064| 5400105 | Service died. | 5065| 5400106 | Unsupport format. | 5066| 6600101 | Session service exception. | 5067 5068**示例:** 5069 5070```ts 5071aVCastController.off('error') 5072``` 5073 5074## ConnectionState<sup>10+</sup> 5075 5076连接状态枚举。 5077 5078**系统能力:** SystemCapability.Multimedia.AVSession.Core 5079 5080| 名称 | 值 | 说明 | 5081| --------------------------- | ---- | ----------- | 5082| STATE_CONNECTING | 0 | 设备连接中 | 5083| STATE_CONNECTED | 1 | 设备连接成功 | 5084| STATE_DISCONNECTED | 6 | 设备断开连接 | 5085 5086## AVMetadata<sup>10+</sup> 5087 5088媒体元数据的相关属性。 5089 5090**系统能力:** SystemCapability.Multimedia.AVSession.Core 5091 5092| 名称 | 类型 | 必填 | 说明 | 5093| --------------- |-------------------------| ---- |---------------------------------------------------------------------| 5094| assetId | string | 是 | 媒体ID。 | 5095| title | string | 否 | 标题。 | 5096| artist | string | 否 | 艺术家。 | 5097| author | string | 否 | 专辑作者。 | 5098| album | string | 否 | 专辑名称。 | 5099| writer | string | 否 | 词作者。 | 5100| composer | string | 否 | 作曲者。 | 5101| duration | number | 否 | 媒体时长,单位毫秒(ms)。 | 5102| mediaImage | image.PixelMap | string | 否 | 图片的像素数据或者图片路径地址(本地路径或网络路径)。 | 5103| publishDate | Date | 否 | 发行日期。 | 5104| subtitle | string | 否 | 子标题。 | 5105| description | string | 否 | 媒体描述。 | 5106| lyric | string | 否 | 歌词文件路径地址(本地路径或网络路径) | 5107| previousAssetId | string | 否 | 上一首媒体ID。 | 5108| nextAssetId | string | 否 | 下一首媒体ID。 | 5109 5110## AVMediaDescription<sup>10+</sup> 5111 5112播放列表媒体元数据的相关属性。 5113 5114**系统能力:** SystemCapability.Multimedia.AVSession.Core 5115 5116| 名称 | 类型 | 必填 | 说明 | 5117| ------------ | ----------------------- | ---- | ----------------------- | 5118| assetId | string | 是 | 播放列表媒体ID。 | 5119| title | string | 否 | 播放列表媒体标题。 | 5120| subtitle | string | 否 | 播放列表媒体子标题。 | 5121| description | string | 否 | 播放列表媒体描述的文本。 | 5122| mediaImage | image.PixelMap | 否 | 播放列表媒体图片像素数据。 | 5123| extras | {[key: string]: any} | 否 | 播放列表媒体额外字段。 | 5124| mediaUri | string | 否 | 播放列表媒体URI。 | 5125| mediaType | string | 否 | 播放列表媒体类型。 | 5126| mediaSize | number | 否 | 播放列表媒体的大小。 | 5127| albumTitle | string | 否 | 播放列表媒体专辑标题。 | 5128| albumCoverUri | string | 否 | 播放列表媒体专辑标题URI。 | 5129| lyricContent | string | 否 | 播放列表媒体歌词内容。 | 5130| lyricUri | string | 否 | 播放列表媒体歌词URI。 | 5131| artist | string | 否 | 播放列表媒体专辑作者。 | 5132| fdSrc | media.AVFileDescriptor | 否 | 播放列表媒体本地文件的句柄。 | 5133| duration | number | 否 | 播放列表媒体播放时长。 | 5134| startPosition | number | 否 | 播放列表媒体起始播放位置。 | 5135| creditsPosition | number | 否 | 播放列表媒体的片尾播放位置。 | 5136| appName | string | 否 | 播放列表提供的应用的名字。 | 5137 5138## AVQueueItem<sup>10+</sup> 5139 5140播放列表中单项的相关属性。 5141 5142**系统能力:** SystemCapability.Multimedia.AVSession.Core 5143 5144| 名称 | 类型 | 必填 | 说明 | 5145| ------------ | ------------------------------------------ | ---- | --------------------------- | 5146| itemId | number | 是 | 播放列表中单项的ID。 | 5147| description | [AVMediaDescription](#avmediadescription10) | 否 | 播放列表中单项的媒体元数据。 | 5148 5149## AVPlaybackState<sup>10+</sup> 5150 5151媒体播放状态的相关属性。 5152 5153**系统能力:** SystemCapability.Multimedia.AVSession.Core 5154 5155| 名称 | 类型 | 必填 | 说明 | 5156| ------------ | ------------------------------------- | ---- | ------- | 5157| state | [PlaybackState](#playbackstate10) | 否 | 播放状态 | 5158| speed | number | 否 | 播放倍速 | 5159| position | [PlaybackPosition](#playbackposition10) | 否 | 播放位置 | 5160| bufferedTime | number | 否 | 缓冲时间 | 5161| loopMode | [LoopMode](#loopmode10) | 否 | 循环模式 | 5162| isFavorite | boolean | 否 | 是否收藏 | 5163| activeItemId<sup>10+</sup> | number | 否 | 正在播放的媒体Id | 5164| volume<sup>10+</sup> | number | 否 | 正在播放的媒体音量 | 5165| extras<sup>10+</sup> | {[key: string]: Object} | 否 | 自定义媒体数据 | 5166 5167## PlaybackPosition<sup>10+</sup> 5168 5169媒体播放位置的相关属性。 5170 5171**系统能力:** SystemCapability.Multimedia.AVSession.Core 5172 5173| 名称 | 类型 | 必填 | 说明 | 5174| ----------- | ------ | ---- | ------------------ | 5175| elapsedTime | number | 是 | 已用时间,单位毫秒(ms)。 | 5176| updateTime | number | 是 | 更新时间,单位毫秒(ms)。 | 5177 5178## AVCastCategory<sup>10+</sup> 5179 5180投播的类别枚举。 5181 5182**系统能力:** SystemCapability.Multimedia.AVSession.AVCast 5183 5184| 名称 | 值 | 说明 | 5185| --------------------------- | ---- | ----------- | 5186| CATEGORY_LOCAL | 0 | 本地播放,默认播放设备,声音从本机或者连接的蓝牙耳机设备出声。 | 5187| CATEGORY_REMOTE | 1 | 远端播放,远端播放设备,声音从其他设备发出声音或者画面。 | 5188 5189## DeviceType<sup>10+</sup> 5190 5191播放设备的类型枚举。 5192 5193**系统能力:** SystemCapability.Multimedia.AVSession.Core 5194 5195| 名称 | 值 | 说明 | 5196| --------------------------- | ---- | ----------- | 5197| DEVICE_TYPE_LOCAL | 0 | 本地播放类型 | 5198| DEVICE_TYPE_BLUETOOTH | 10 | 蓝牙设备 | 5199| DEVICE_TYPE_TV | 2 | 电视 <br> **系统能力:** SystemCapability.Multimedia.AVSession.AVCast | 5200| DEVICE_TYPE_SMART_SPEAKER | 3 | 音箱设备 <br> **系统能力:** SystemCapability.Multimedia.AVSession.AVCast | 5201 5202## DeviceInfo<sup>10+</sup> 5203 5204播放设备的相关信息。 5205 5206**系统能力:** SystemCapability.Multimedia.AVSession.Core 5207 5208| 名称 | 类型 | 必填 | 说明 | 5209| ---------- | -------------- | ---- | ---------------------- | 5210| castCategory | AVCastCategory | 是 | 投播的类别。 | 5211| deviceId | string | 是 | 播放设备的ID。 | 5212| deviceName | string | 是 | 播放设备的名称。 | 5213| deviceType | DeviceType | 是 | 播放设备的类型。 | 5214| ipAddress | string | 否 | 播放设备的ip地址。<br/>此接口为系统接口。<br> **系统能力:** SystemCapability.Multimedia.AVSession.AVCast | 5215| providerId | number | 否 | 播放设备提供商。<br/>此接口为系统接口。<br> **系统能力:** SystemCapability.Multimedia.AVSession.AVCast | 5216 5217## OutputDeviceInfo<sup>10+</sup> 5218 5219播放设备的相关信息。 5220 5221**系统能力:** SystemCapability.Multimedia.AVSession.Core 5222 5223| 名称 | 类型 | 必填 | 说明 | 5224| ---------- | -------------- | ---- | ---------------------- | 5225| devices | Array\<DeviceInfo\> | 是 | 播放设备的集合。 | 5226 5227## LoopMode<sup>10+</sup> 5228 5229表示媒体播放循环模式的枚举。 5230 5231**系统能力:** SystemCapability.Multimedia.AVSession.Core 5232 5233| 名称 | 值 | 说明 | 5234| ------------------ | ---- | -------- | 5235| LOOP_MODE_SEQUENCE | 0 | 顺序播放 | 5236| LOOP_MODE_SINGLE | 1 | 单曲循环 | 5237| LOOP_MODE_LIST | 2 | 表单循环 | 5238| LOOP_MODE_SHUFFLE | 3 | 随机播放 | 5239 5240## PlaybackState<sup>10+</sup> 5241 5242表示媒体播放状态的枚举。 5243 5244**系统能力:** SystemCapability.Multimedia.AVSession.Core 5245 5246| 名称 | 值 | 说明 | 5247| --------------------------- | ---- | ----------- | 5248| PLAYBACK_STATE_INITIAL | 0 | 初始状态 | 5249| PLAYBACK_STATE_PREPARE | 1 | 播放准备状态 | 5250| PLAYBACK_STATE_PLAY | 2 | 正在播放 | 5251| PLAYBACK_STATE_PAUSE | 3 | 暂停 | 5252| PLAYBACK_STATE_FAST_FORWARD | 4 | 快进 | 5253| PLAYBACK_STATE_REWIND | 5 | 快退 | 5254| PLAYBACK_STATE_STOP | 6 | 停止 | 5255| PLAYBACK_STATE_COMPLETED | 7 | 播放完成 | 5256| PLAYBACK_STATE_RELEASED | 8 | 释放 | 5257| PLAYBACK_STATE_ERROR | 9 | 错误 | 5258 5259## AVSessionDescriptor 5260 5261会话的相关描述信息。 5262 5263**系统能力:** SystemCapability.Multimedia.AVSession.Manager 5264 5265**系统接口:** 该接口为系统接口。 5266 5267| 名称 | 类型 | 说明 | 5268| --------------| ---------------- |------| 5269| sessionId | string | 会话ID | 5270| type | [AVSessionType](#avsessiontype10) | 会话类型 | 5271| sessionTag | string | 会话的自定义名称 | 5272| elementName | [ElementName](js-apis-bundle-ElementName.md) | 会话所属应用的信息(包含bundleName、abilityName等) | 5273| isActive | boolean | 会话是否被激活 | 5274| isTopSession | boolean | 会话是否为最新的会话 | 5275| outputDevice | [OutputDeviceInfo](#outputdeviceinfo10) | 分布式设备相关信息 | 5276 5277## AVSessionController<sup>10+</sup> 5278 5279调用[avSession.createController](#avsessioncreatecontroller)后,返回会话控制器实例。控制器可查看会话ID,并可完成对会话发送命令及事件,获取会话元数据,播放状态信息等操作。 5280 5281### 属性 5282 5283**系统能力:** SystemCapability.Multimedia.AVSession.Core 5284 5285| 名称 | 类型 | 可读 | 可写 | 说明 | 5286| :-------- | :----- | :--- | :--- | :-------------------------------------- | 5287| sessionId | string | 是 | 否 | AVSessionController对象唯一的会话标识。 | 5288 5289 5290**示例:** 5291 5292```ts 5293import { BusinessError } from '@ohos.base'; 5294 5295let AVSessionController: avSession.AVSessionController; 5296avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { 5297 AVSessionController = controller; 5298}).catch((err: BusinessError) => { 5299 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 5300}); 5301``` 5302 5303### getAVPlaybackState<sup>10+</sup> 5304 5305getAVPlaybackState(callback: AsyncCallback\<AVPlaybackState>): void 5306 5307获取当前的远端播放状态。结果通过callback异步回调方式返回。 5308 5309**系统能力:** SystemCapability.Multimedia.AVSession.Core 5310 5311**参数:** 5312 5313| 参数名 | 类型 | 必填 | 说明 | 5314| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 5315| callback | AsyncCallback<[[AVPlaybackState](#avplaybackstate10)\> | 是 | 回调函数,返回远端播放状态。 | 5316 5317**错误码:** 5318 5319以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5320 5321| 错误码ID | 错误信息 | 5322| -------- | ---------------------------------------- | 5323| 6600101 | Session service exception. | 5324| 6600102 | The session does not exist. | 5325| 6600103 | The session controller does not exist. | 5326 5327**示例:** 5328 5329```ts 5330import { BusinessError } from '@ohos.base'; 5331 5332avsessionController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { 5333 if (err) { 5334 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 5335 } else { 5336 console.info(`getAVPlaybackState : SUCCESS`); 5337 } 5338}); 5339``` 5340 5341### getAVPlaybackState<sup>10+</sup> 5342 5343getAVPlaybackState(): Promise\<AVPlaybackState> 5344 5345获取当前的远端播放状态。结果通过Promise异步回调方式返回。 5346 5347**系统能力:** SystemCapability.Multimedia.AVSession.Core 5348 5349**返回值:** 5350 5351| 类型 | 说明 | 5352| --------- | ------------------------------------------------------------ | 5353| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise对象。返回远端播放状态。 | 5354 5355**错误码:** 5356 5357以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5358 5359| 错误码ID | 错误信息 | 5360| -------- | ---------------------------------------- | 5361| 6600101 | Session service exception. | 5362| 6600102 | The session does not exist. | 5363| 6600103 | The session controller does not exist. | 5364 5365**示例:** 5366 5367```ts 5368import { BusinessError } from '@ohos.base'; 5369 5370avsessionController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { 5371 console.info(`getAVPlaybackState : SUCCESS`); 5372}).catch((err: BusinessError) => { 5373 console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); 5374}); 5375``` 5376 5377### getAVMetadata<sup>10+</sup> 5378 5379getAVMetadata(): Promise\<AVMetadata> 5380 5381获取会话元数据。结果通过Promise异步回调方式返回。 5382 5383**系统能力:** SystemCapability.Multimedia.AVSession.Core 5384 5385**返回值:** 5386 5387| 类型 | 说明 | 5388| ----------------------------------- | ----------------------------- | 5389| Promise<[AVMetadata](#avmetadata10)\> | Promise对象,返回会话元数据。 | 5390 5391**错误码:** 5392 5393以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5394 5395| 错误码ID | 错误信息 | 5396| -------- | ---------------------------------------- | 5397| 6600101 | Session service exception. | 5398| 6600102 | The session does not exist. | 5399| 6600103 | The session controller does not exist. | 5400 5401**示例:** 5402 5403```ts 5404import { BusinessError } from '@ohos.base'; 5405 5406avsessionController.getAVMetadata().then((metadata: avSession.AVMetadata) => { 5407 console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); 5408}).catch((err: BusinessError) => { 5409 console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 5410}); 5411``` 5412 5413### getAVMetadata<sup>10+</sup> 5414 5415getAVMetadata(callback: AsyncCallback\<AVMetadata>): void 5416 5417获取会话元数据。结果通过callback异步回调方式返回。 5418 5419**系统能力:** SystemCapability.Multimedia.AVSession.Core 5420 5421**参数:** 5422 5423| 参数名 | 类型 | 必填 | 说明 | 5424| -------- | ----------------------------------------- | ---- | -------------------------- | 5425| callback | AsyncCallback<[AVMetadata](#avmetadata10)\> | 是 | 回调函数,返回会话元数据。 | 5426 5427**错误码:** 5428 5429以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5430 5431| 错误码ID | 错误信息 | 5432| -------- | ---------------------------------------- | 5433| 6600101 | Session service exception. | 5434| 6600102 | The session does not exist. | 5435| 6600103 | The session controller does not exist. | 5436 5437**示例:** 5438 5439```ts 5440import { BusinessError } from '@ohos.base'; 5441 5442avsessionController.getAVMetadata((err: BusinessError, metadata: avSession.AVMetadata) => { 5443 if (err) { 5444 console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); 5445 } else { 5446 console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); 5447 } 5448}); 5449``` 5450 5451### getAVQueueTitle<sup>10+</sup> 5452 5453getAVQueueTitle(): Promise\<string> 5454 5455获取当前会话播放列表的名称。结果通过Promise异步回调方式返回。 5456 5457**系统能力:** SystemCapability.Multimedia.AVSession.Core 5458 5459**返回值:** 5460 5461| 类型 | 说明 | 5462| ---------------- | ----------------------------- | 5463| Promise<string\> | Promise对象。返回播放列表名称。 | 5464 5465**错误码:** 5466 5467以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5468 5469| 错误码ID | 错误信息 | 5470| -------- | ---------------------------------------- | 5471| 6600101 | Session service exception. | 5472| 6600102 | The session does not exist. | 5473| 6600103 | The session controller does not exist. | 5474 5475**示例:** 5476 5477```ts 5478import { BusinessError } from '@ohos.base'; 5479 5480avsessionController.getAVQueueTitle().then((title: string) => { 5481 console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); 5482}).catch((err: BusinessError) => { 5483 console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 5484}); 5485``` 5486 5487### getAVQueueTitle<sup>10+</sup> 5488 5489getAVQueueTitle(callback: AsyncCallback\<string>): void 5490 5491获取当前播放列表的名称。结果通过callback异步回调方式返回。 5492 5493**系统能力:** SystemCapability.Multimedia.AVSession.Core 5494 5495**参数:** 5496 5497| 参数名 | 类型 | 必填 | 说明 | 5498| -------- | ---------------------- | ---- | ------------------------- | 5499| callback | AsyncCallback<string\> | 是 | 回调函数,返回播放列表名称。 | 5500 5501**错误码:** 5502 5503以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5504 5505| 错误码ID | 错误信息 | 5506| -------- | ---------------------------------------- | 5507| 6600101 | Session service exception. | 5508| 6600102 | The session does not exist. | 5509| 6600103 | The session controller does not exist. | 5510 5511**示例:** 5512 5513```ts 5514import { BusinessError } from '@ohos.base'; 5515 5516avsessionController.getAVQueueTitle((err: BusinessError, title: string) => { 5517 if (err) { 5518 console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); 5519 } else { 5520 console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); 5521 } 5522}); 5523``` 5524 5525### getAVQueueItems<sup>10+</sup> 5526 5527getAVQueueItems(): Promise\<Array\<AVQueueItem>> 5528 5529获取当前会话播放列表相关信息。结果通过Promise异步回调方式返回。 5530 5531**系统能力:** SystemCapability.Multimedia.AVSession.Core 5532 5533**返回值:** 5534 5535| 类型 | 说明 | 5536| --------------------------------------------- | ----------------------------- | 5537| Promise<Array<[AVQueueItem](#avqueueitem10)\>\> | Promise对象。返回播放列表队列。 | 5538 5539**错误码:** 5540 5541以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5542 5543| 错误码ID | 错误信息 | 5544| -------- | ---------------------------------------- | 5545| 6600101 | Session service exception. | 5546| 6600102 | The session does not exist. | 5547| 6600103 | The session controller does not exist. | 5548 5549**示例:** 5550 5551```ts 5552import { BusinessError } from '@ohos.base'; 5553 5554avsessionController.getAVQueueItems().then((items: avSession.AVQueueItem[]) => { 5555 console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); 5556}).catch((err: BusinessError) => { 5557 console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 5558}); 5559``` 5560 5561### getAVQueueItems<sup>10+</sup> 5562 5563getAVQueueItems(callback: AsyncCallback\<Array\<AVQueueItem>>): void 5564 5565获取当前播放列表相关信息。结果通过callback异步回调方式返回。 5566 5567**系统能力:** SystemCapability.Multimedia.AVSession.Core 5568 5569**参数:** 5570 5571| 参数名 | 类型 | 必填 | 说明 | 5572| -------- | --------------------------------------------------- | ---- | ------------------------- | 5573| callback | AsyncCallback<Array<[AVQueueItem](#avqueueitem10)\>\> | 是 | 回调函数,返回播放列表队列。 | 5574 5575**错误码:** 5576 5577以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5578 5579| 错误码ID | 错误信息 | 5580| -------- | ---------------------------------------- | 5581| 6600101 | Session service exception. | 5582| 6600102 | The session does not exist. | 5583| 6600103 | The session controller does not exist. | 5584 5585**示例:** 5586 5587```ts 5588import { BusinessError } from '@ohos.base'; 5589 5590avsessionController.getAVQueueItems((err: BusinessError, items: avSession.AVQueueItem[]) => { 5591 if (err) { 5592 console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); 5593 } else { 5594 console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); 5595 } 5596}); 5597``` 5598 5599### skipToQueueItem<sup>10+</sup> 5600 5601skipToQueueItem(itemId: number): Promise\<void> 5602 5603设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过Promise异步回调方式返回。 5604 5605**系统能力:** SystemCapability.Multimedia.AVSession.Core 5606 5607**参数:** 5608 5609| 参数名 | 类型 | 必填 | 说明 | 5610| ------ | ------- | ---- | ------------------------------------------- | 5611| itemId | number | 是 | 播放列表单项的ID值,用以表示选中的播放列表单项。 | 5612 5613**返回值:** 5614 5615| 类型 | 说明 | 5616| -------------- | --------------------------------------------------------------- | 5617| Promise\<void> | Promise对象。当播放列表单项ID设置成功,无返回结果,否则返回错误对象。 | 5618 5619**错误码:** 5620 5621以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5622 5623| 错误码ID | 错误信息 | 5624| -------- | ---------------------------------------- | 5625| 6600101 | Session service exception. | 5626| 6600102 | The session does not exist. | 5627| 6600103 | The session controller does not exist. | 5628 5629**示例:** 5630 5631```ts 5632import { BusinessError } from '@ohos.base'; 5633 5634let queueItemId = 0; 5635avsessionController.skipToQueueItem(queueItemId).then(() => { 5636 console.info(`SkipToQueueItem successfully`); 5637}).catch((err: BusinessError) => { 5638 console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); 5639}); 5640``` 5641 5642### skipToQueueItem<sup>10+</sup> 5643 5644skipToQueueItem(itemId: number, callback: AsyncCallback\<void>): void 5645 5646设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过callback异步回调方式返回。 5647 5648**系统能力:** SystemCapability.Multimedia.AVSession.Core 5649 5650**参数:** 5651 5652| 参数名 | 类型 | 必填 | 说明 | 5653| -------- | --------------------- | ---- | ----------------------------------------------------------- | 5654| itemId | number | 是 | 播放列表单项的ID值,用以表示选中的播放列表单项。 | 5655| callback | AsyncCallback\<void> | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | 5656 5657**错误码:** 5658 5659以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5660 5661| 错误码ID | 错误信息 | 5662| -------- | ---------------------------------------- | 5663| 6600101 | Session service exception. | 5664| 6600102 | The session does not exist. | 5665| 6600103 | The session controller does not exist. | 5666 5667**示例:** 5668 5669```ts 5670import { BusinessError } from '@ohos.base'; 5671 5672let queueItemId = 0; 5673avsessionController.skipToQueueItem(queueItemId, (err: BusinessError) => { 5674 if (err) { 5675 console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); 5676 } else { 5677 console.info(`SkipToQueueItem successfully`); 5678 } 5679}); 5680``` 5681 5682### getOutputDevice<sup>10+</sup> 5683 5684getOutputDevice(): Promise\<OutputDeviceInfo> 5685 5686获取播放设备信息。结果通过Promise异步回调方式返回。 5687 5688**系统能力:** SystemCapability.Multimedia.AVSession.Core 5689 5690**返回值:** 5691 5692| 类型 | 说明 | 5693| ----------------------------------------------- | --------------------------------- | 5694| Promise<[OutputDeviceInfo](#outputdeviceinfo10)\> | Promise对象,返回播放设备信息。 | 5695 5696**错误码:** 5697 5698以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5699 5700| 错误码ID | 错误信息 | 5701| -------- | ---------------------------------------- | 5702| 6600101 | Session service exception. | 5703| 6600103 | The session controller does not exist. | 5704 5705**示例:** 5706 5707```ts 5708import { BusinessError } from '@ohos.base'; 5709 5710avsessionController.getOutputDevice().then((deviceInfo: avSession.OutputDeviceInfo) => { 5711 console.info(`GetOutputDevice : SUCCESS`); 5712}).catch((err: BusinessError) => { 5713 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 5714}); 5715``` 5716 5717### getOutputDevice<sup>10+</sup> 5718 5719getOutputDevice(callback: AsyncCallback\<OutputDeviceInfo>): void 5720 5721获取播放设备信息。结果通过callback异步回调方式返回。 5722 5723**系统能力:** SystemCapability.Multimedia.AVSession.Core 5724 5725**参数:** 5726 5727| 参数名 | 类型 | 必填 | 说明 | 5728| -------- | ----------------------------------------------------- | ---- | ------------------------------ | 5729| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | 是 | 回调函数,返回播放设备信息。 | 5730 5731**错误码:** 5732 5733以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5734 5735| 错误码ID | 错误信息 | 5736| -------- | ---------------------------------------- | 5737| 6600101 | Session service exception. | 5738| 6600103 | The session controller does not exist. | 5739 5740**示例:** 5741 5742```ts 5743import { BusinessError } from '@ohos.base'; 5744 5745avsessionController.getOutputDevice((err: BusinessError, deviceInfo: avSession.OutputDeviceInfo) => { 5746 if (err) { 5747 console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); 5748 } else { 5749 console.info(`GetOutputDevice : SUCCESS`); 5750 } 5751}); 5752``` 5753 5754### sendAVKeyEvent<sup>10+</sup> 5755 5756sendAVKeyEvent(event: KeyEvent): Promise\<void> 5757 5758发送按键事件到控制器对应的会话。结果通过Promise异步回调方式返回。 5759 5760**系统能力:** SystemCapability.Multimedia.AVSession.Core 5761 5762**参数:** 5763 5764| 参数名 | 类型 | 必填 | 说明 | 5765| ------ | ------------------------------------------------------------ | ---- | ---------- | 5766| event | [KeyEvent](js-apis-keyevent.md) | 是 | 按键事件。 | 5767 5768**错误码:** 5769 5770以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5771 5772| 错误码ID | 错误信息 | 5773| -------- | ---------------------------------------- | 5774| 6600101 | Session service exception. | 5775| 6600102 | The session does not exist. | 5776| 6600103 | The session controller does not exist. | 5777| 6600105 | Invalid session command. | 5778| 6600106 | The session is not activated. | 5779 5780**返回值:** 5781 5782| 类型 | 说明 | 5783| -------------- | ----------------------------- | 5784| Promise\<void> | Promise对象。当事件发送成功,无返回结果,否则返回错误对象。 | 5785 5786**示例:** 5787 5788```ts 5789import keyEvent from '@ohos.multimodalInput.keyEvent'; 5790import { BusinessError } from '@ohos.base'; 5791 5792let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; 5793let event: keyEvent.KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; 5794 5795avsessionController.sendAVKeyEvent(event).then(() => { 5796 console.info(`SendAVKeyEvent Successfully`); 5797}).catch((err: BusinessError) => { 5798 console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 5799}); 5800``` 5801 5802### sendAVKeyEvent<sup>10+</sup> 5803 5804sendAVKeyEvent(event: KeyEvent, callback: AsyncCallback\<void>): void 5805 5806发送按键事件到会话。结果通过callback异步回调方式返回。 5807 5808**系统能力:** SystemCapability.Multimedia.AVSession.Core 5809 5810**参数:** 5811 5812| 参数名 | 类型 | 必填 | 说明 | 5813| -------- | ------------------------------------------------------------ | ---- | ---------- | 5814| event | [KeyEvent](js-apis-keyevent.md) | 是 | 按键事件。 | 5815| callback | AsyncCallback\<void> | 是 | 回调函数。当事件发送成功,err为undefined,否则返回错误对象。 | 5816 5817**错误码:** 5818 5819以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5820 5821| 错误码ID | 错误信息 | 5822| -------- | ---------------------------------------- | 5823| 6600101 | Session service exception. | 5824| 6600102 | The session does not exist. | 5825| 6600103 | The session controller does not exist. | 5826| 6600105 | Invalid session command. | 5827| 6600106 | The session is not activated. | 5828 5829**示例:** 5830 5831```ts 5832import keyEvent from '@ohos.multimodalInput.keyEvent'; 5833import { BusinessError } from '@ohos.base'; 5834 5835let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; 5836let event: keyEvent.KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; 5837 5838avsessionController.sendAVKeyEvent(event, (err: BusinessError) => { 5839 if (err) { 5840 console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); 5841 } else { 5842 console.info(`SendAVKeyEvent Successfully`); 5843 } 5844}); 5845``` 5846 5847### getLaunchAbility<sup>10+</sup> 5848 5849getLaunchAbility(): Promise\<WantAgent> 5850 5851获取应用在会话中保存的WantAgent对象。结果通过Promise异步回调方式返回。 5852 5853**系统能力:** SystemCapability.Multimedia.AVSession.Core 5854 5855**返回值:** 5856 5857| 类型 | 说明 | 5858| ------------------------------------------------------- | ------------------------------------------------------------ | 5859| Promise<[WantAgent](js-apis-app-ability-wantAgent.md)\> | Promise对象,返回在[setLaunchAbility](#setlaunchability10)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 | 5860 5861**错误码:** 5862 5863以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5864 5865| 错误码ID | 错误信息 | 5866| -------- | ---------------------------------------- | 5867| 6600101 | Session service exception. | 5868| 6600102 | The session does not exist. | 5869| 6600103 | The session controller does not exist. | 5870 5871**示例:** 5872 5873```ts 5874import { BusinessError } from '@ohos.base'; 5875 5876avsessionController.getLaunchAbility().then((agent: object) => { 5877 console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); 5878}).catch((err: BusinessError) => { 5879 console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 5880}); 5881``` 5882 5883### getLaunchAbility<sup>10+</sup> 5884 5885getLaunchAbility(callback: AsyncCallback\<WantAgent>): void 5886 5887获取应用在会话中保存的WantAgent对象。结果通过callback异步回调方式返回。 5888 5889**系统能力:** SystemCapability.Multimedia.AVSession.Core 5890 5891**参数:** 5892 5893| 参数名 | 类型 | 必填 | 说明 | 5894| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5895| callback | AsyncCallback<[WantAgent](js-apis-app-ability-wantAgent.md)\> | 是 | 回调函数。返回在[setLaunchAbility](#setlaunchability10)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 | 5896 5897**错误码:** 5898 5899以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5900 5901| 错误码ID | 错误信息 | 5902| -------- | ---------------------------------------- | 5903| 6600101 | Session service exception. | 5904| 6600102 | The session does not exist. | 5905| 6600103 | The session controller does not exist. | 5906 5907**示例:** 5908 5909```ts 5910import { BusinessError } from '@ohos.base'; 5911 5912avsessionController.getLaunchAbility((err: BusinessError, agent: object) => { 5913 if (err) { 5914 console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); 5915 } else { 5916 console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); 5917 } 5918}); 5919``` 5920 5921### getRealPlaybackPositionSync<sup>10+</sup> 5922 5923getRealPlaybackPositionSync(): number 5924 5925获取当前播放位置。 5926 5927**系统能力:** SystemCapability.Multimedia.AVSession.Core 5928 5929**返回值:** 5930 5931| 类型 | 说明 | 5932| ------ | ------------------ | 5933| number | 时间节点,毫秒数。 | 5934 5935**错误码:** 5936 5937以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5938 5939| 错误码ID | 错误信息 | 5940| -------- | ---------------------------------------- | 5941| 6600101 | Session service exception. | 5942| 6600103 | The session controller does not exist. | 5943 5944**示例:** 5945 5946```ts 5947let time: number = avsessionController.getRealPlaybackPositionSync(); 5948``` 5949 5950### isActive<sup>10+</sup> 5951 5952isActive(): Promise\<boolean> 5953 5954获取会话是否被激活。结果通过Promise异步回调方式返回。 5955 5956**系统能力:** SystemCapability.Multimedia.AVSession.Core 5957 5958**返回值:** 5959 5960| 类型 | 说明 | 5961| ----------------- | ------------------------------------------------------------ | 5962| Promise<boolean\> | Promise对象,返回会话是否为激活状态,true表示被激活,false表示禁用。 | 5963 5964**错误码:** 5965 5966以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 5967 5968| 错误码ID | 错误信息 | 5969| -------- | ---------------------------------------- | 5970| 6600101 | Session service exception. | 5971| 6600102 | The session does not exist. | 5972| 6600103 | The session controller does not exist. | 5973 5974**示例:** 5975 5976```ts 5977import { BusinessError } from '@ohos.base'; 5978 5979avsessionController.isActive().then((isActive: boolean) => { 5980 console.info(`IsActive : SUCCESS : isactive : ${isActive}`); 5981}).catch((err: BusinessError) => { 5982 console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); 5983}); 5984``` 5985 5986### isActive<sup>10+</sup> 5987 5988isActive(callback: AsyncCallback\<boolean>): void 5989 5990判断会话是否被激活。结果通过callback异步回调方式返回。 5991 5992**系统能力:** SystemCapability.Multimedia.AVSession.Core 5993 5994**参数:** 5995 5996| 参数名 | 类型 | 必填 | 说明 | 5997| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 5998| callback | AsyncCallback<boolean\> | 是 | 回调函数,返回会话是否为激活状态,true表示被激活,false表示禁用。 | 5999 6000**错误码:** 6001 6002以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6003 6004| 错误码ID | 错误信息 | 6005| -------- | ---------------------------------------- | 6006| 6600101 | Session service exception. | 6007| 6600102 | The session does not exist. | 6008| 6600103 | The session controller does not exist. | 6009 6010**示例:** 6011 6012```ts 6013import { BusinessError } from '@ohos.base'; 6014 6015avsessionController.isActive((err: BusinessError, isActive: boolean) => { 6016 if (err) { 6017 console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); 6018 } else { 6019 console.info(`IsActive : SUCCESS : isactive : ${isActive}`); 6020 } 6021}); 6022``` 6023 6024### destroy<sup>10+</sup> 6025 6026destroy(): Promise\<void> 6027 6028销毁当前控制器,销毁后当前控制器不可再用。结果通过Promise异步回调方式返回。 6029 6030**系统能力:** SystemCapability.Multimedia.AVSession.Core 6031 6032**返回值:** 6033 6034| 类型 | 说明 | 6035| -------------- | ----------------------------- | 6036| Promise\<void> | Promise对象。当控制器销毁成功,无返回结果,否则返回错误对象。 | 6037 6038**错误码:** 6039 6040以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6041 6042| 错误码ID | 错误信息 | 6043| -------- | ---------------------------------------- | 6044| 6600101 | Session service exception. | 6045| 6600103 | The session controller does not exist. | 6046 6047**示例:** 6048 6049```ts 6050import { BusinessError } from '@ohos.base'; 6051 6052avsessionController.destroy().then(() => { 6053 console.info(`Destroy : SUCCESS `); 6054}).catch((err: BusinessError) => { 6055 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 6056}); 6057``` 6058 6059### destroy<sup>10+</sup> 6060 6061destroy(callback: AsyncCallback\<void>): void 6062 6063销毁当前控制器,销毁后当前控制器不可再用。结果通过callback异步回调方式返回。 6064 6065**系统能力:** SystemCapability.Multimedia.AVSession.Core 6066 6067**参数:** 6068 6069| 参数名 | 类型 | 必填 | 说明 | 6070| -------- | -------------------- | ---- | ---------- | 6071| callback | AsyncCallback\<void> | 是 | 回调函数。当控制器销毁成功,err为undefined,否则返回错误对象。 | 6072 6073**错误码:** 6074 6075以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6076 6077| 错误码ID | 错误信息 | 6078| -------- | ---------------------------------------- | 6079| 6600101 | Session service exception. | 6080| 6600103 | The session controller does not exist. | 6081 6082**示例:** 6083 6084```ts 6085import { BusinessError } from '@ohos.base'; 6086 6087avsessionController.destroy((err: BusinessError) => { 6088 if (err) { 6089 console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); 6090 } else { 6091 console.info(`Destroy : SUCCESS `); 6092 } 6093}); 6094``` 6095 6096### getValidCommands<sup>10+</sup> 6097 6098getValidCommands(): Promise\<Array\<AVControlCommandType>> 6099 6100获取会话支持的有效命令。结果通过Promise异步回调方式返回。 6101 6102**系统能力:** SystemCapability.Multimedia.AVSession.Core 6103 6104**返回值:** 6105 6106| 类型 | 说明 | 6107| ------------------------------------------------------------ | --------------------------------- | 6108| Promise<Array<[AVControlCommandType](#avcontrolcommandtype10)\>\> | Promise对象。返回有效命令的集合。 | 6109 6110**错误码:** 6111 6112以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6113 6114| 错误码ID | 错误信息 | 6115| -------- | ---------------------------------------- | 6116| 6600101 | Session service exception. | 6117| 6600102 | The session does not exist. | 6118| 6600103 | The session controller does not exist. | 6119 6120**示例:** 6121 6122```ts 6123import { BusinessError } from '@ohos.base'; 6124 6125avsessionController.getValidCommands().then((validCommands: avSession.AVControlCommandType[]) => { 6126 console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); 6127}).catch((err: BusinessError) => { 6128 console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 6129}); 6130``` 6131 6132### getValidCommands<sup>10+</sup> 6133 6134getValidCommands(callback: AsyncCallback\<Array\<AVControlCommandType>>): void 6135 6136获取会话支持的有效命令。结果通过callback异步回调方式返回。 6137 6138**系统能力:** SystemCapability.Multimedia.AVSession.Core 6139 6140**参数:** 6141 6142| 参数名 | 类型 | 必填 | 说明 | 6143| -------- | ------------------------------------------------------------ | ---- | ------------------------------ | 6144| callback | AsyncCallback\<Array\<[AVControlCommandType](#avcontrolcommandtype10)\>\> | 是 | 回调函数,返回有效命令的集合。 | 6145 6146**错误码:** 6147 6148以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6149 6150| 错误码ID | 错误信息 | 6151| -------- | ---------------------------------------- | 6152| 6600101 | Session service exception. | 6153| 6600102 | The session does not exist. | 6154| 6600103 | The session controller does not exist. | 6155 6156**示例:** 6157 6158```ts 6159import { BusinessError } from '@ohos.base'; 6160 6161avsessionController.getValidCommands((err: BusinessError, validCommands: avSession.AVControlCommandType[]) => { 6162 if (err) { 6163 console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); 6164 } else { 6165 console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); 6166 } 6167}); 6168``` 6169 6170### sendControlCommand<sup>10+</sup> 6171 6172sendControlCommand(command: AVControlCommand): Promise\<void> 6173 6174通过控制器发送命令到其对应的会话。结果通过Promise异步回调方式返回。 6175 6176> **说明:** 6177> 6178> 媒体控制方在使用sendControlCommand命令前,需要确保控制对应的媒体会话注册了对应的监听,注册媒体会话相关监听的方法请参见接口[on'play'](#onplay10)、[on'pause'](#onpause10)等。 6179 6180**系统能力:** SystemCapability.Multimedia.AVSession.Core 6181 6182**参数:** 6183 6184| 参数名 | 类型 | 必填 | 说明 | 6185| ------- | ------------------------------------- | ---- | ------------------------------ | 6186| command | [AVControlCommand](#avcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | 6187 6188**返回值:** 6189 6190| 类型 | 说明 | 6191| -------------- | ----------------------------- | 6192| Promise\<void> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | 6193 6194**错误码:** 6195 6196以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6197 6198| 错误码ID | 错误信息 | 6199| -------- | ---------------------------------------- | 6200| 6600101 | Session service exception. | 6201| 6600102 | The session does not exist. | 6202| 6600103 | The session controller does not exist. | 6203| 6600105 | Invalid session command. | 6204| 6600106 | The session is not activated. | 6205| 6600107 | Too many commands or events. | 6206 6207**示例:** 6208 6209```ts 6210import avSession from '@ohos.multimedia.avsession'; 6211import { BusinessError } from '@ohos.base'; 6212 6213let avCommand: avSession.AVControlCommand = {command:'play'}; 6214avsessionController.sendControlCommand(avCommand).then(() => { 6215 console.info(`SendControlCommand successfully`); 6216}).catch((err: BusinessError) => { 6217 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6218}); 6219``` 6220 6221### sendControlCommand<sup>10+</sup> 6222 6223sendControlCommand(command: AVControlCommand, callback: AsyncCallback\<void>): void 6224 6225通过会话控制器发送命令到其对应的会话。结果通过callback异步回调方式返回。 6226 6227> **说明:** 6228> 6229> 媒体控制方在使用sendControlCommand命令前,需要确保控制对应的媒体会话注册了对应的监听,注册媒体会话相关监听的方法请参见接口[on'play'](#onplay10)、[on'pause'](#onpause10)等。 6230 6231**系统能力:** SystemCapability.Multimedia.AVSession.Core 6232 6233**参数:** 6234 6235| 参数名 | 类型 | 必填 | 说明 | 6236| -------- | ------------------------------------- | ---- | ------------------------------ | 6237| command | [AVControlCommand](#avcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | 6238| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 6239 6240**错误码:** 6241 6242以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6243 6244| 错误码ID | 错误信息 | 6245| -------- | ------------------------------- | 6246| 6600101 | Session service exception. | 6247| 6600102 | The session does not exist. | 6248| 6600103 | The session controller does not exist. | 6249| 6600105 | Invalid session command. | 6250| 6600106 | The session is not activated. | 6251| 6600107 | Too many commands or events. | 6252 6253**示例:** 6254 6255```ts 6256import avSession from '@ohos.multimedia.avsession'; 6257import { BusinessError } from '@ohos.base'; 6258 6259let avCommand: avSession.AVControlCommand = {command:'play'}; 6260avsessionController.sendControlCommand(avCommand, (err: BusinessError) => { 6261 if (err) { 6262 console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6263 } else { 6264 console.info(`SendControlCommand successfully`); 6265 } 6266}); 6267``` 6268 6269### sendCommonCommand<sup>10+</sup> 6270 6271sendCommonCommand(command: string, args: {[key: string]: Object}): Promise\<void> 6272 6273通过会话控制器发送自定义控制命令到其对应的会话。结果通过Promise异步回调方式返回。 6274 6275**系统能力:** SystemCapability.Multimedia.AVSession.Core 6276 6277**参数:** 6278 6279| 参数名 | 类型 | 必填 | 说明 | 6280| ------- | ------------------------------------- | ---- | ------------------------------ | 6281| command | string | 是 | 需要设置的自定义控制命令的名称 | 6282| args | {[key: string]: Object} | 是 | 需要传递的控制命令键值对 | 6283 6284> **说明:** 6285> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 6286 6287**返回值:** 6288 6289| 类型 | 说明 | 6290| -------------- | ----------------------------- | 6291| Promise\<void> | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | 6292 6293**错误码:** 6294 6295以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6296 6297| 错误码ID | 错误信息 | 6298| -------- | ---------------------------------------- | 6299| 6600101 | Session service exception. | 6300| 6600102 | The session does not exist. | 6301| 6600103 | The session controller does not exist. | 6302| 6600105 | Invalid session command. | 6303| 6600106 | The session is not activated. | 6304| 6600107 | Too many commands or events. | 6305 6306**示例:** 6307 6308```ts 6309import avSession from '@ohos.multimedia.avsession'; 6310import { BusinessError } from '@ohos.base'; 6311 6312let avSessionController: avSession.AVSessionController | undefined = undefined; 6313let currentAVSession: avSession.AVSession | undefined = undefined; 6314let tag = "createNewSession"; 6315let context: Context = getContext(this); 6316let sessionId: string = ""; 6317avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6318 if (err) { 6319 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6320 } else { 6321 currentAVSession = data; 6322 } 6323}); 6324if (currentAVSession !== undefined) { 6325 sessionId = (currentAVSession as avSession.AVSession).sessionId; 6326 avSession.createController(sessionId).then((controller: avSession.AVSessionController) => { 6327 avSessionController = controller; 6328 }).catch((err: BusinessError) => { 6329 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6330 }); 6331} 6332 6333let commandName = "my_command"; 6334if (avSessionController !== undefined) { 6335 (avSessionController as avSession.AVSessionController).sendCommonCommand(commandName, {command : "This is my command"}).then(() => { 6336 console.info(`SendCommonCommand successfully`); 6337 }).catch((err: BusinessError) => { 6338 console.error(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6339 }) 6340} 6341``` 6342 6343### sendCommonCommand<sup>10+</sup> 6344 6345sendCommonCommand(command: string, args: {[key: string]: Object}, callback: AsyncCallback\<void>): void 6346 6347通过会话控制器发送自定义命令到其对应的会话。结果通过callback异步回调方式返回。 6348 6349**系统能力:** SystemCapability.Multimedia.AVSession.Core 6350 6351**参数:** 6352 6353| 参数名 | 类型 | 必填 | 说明 | 6354| ------- | ------------------------------------- | ---- | ------------------------------ | 6355| command | string | 是 | 需要设置的自定义控制命令的名称 | 6356| args | {[key: string]: Object} | 是 | 需要传递的控制命令键值对 | 6357| callback | AsyncCallback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | 6358 6359> **说明:** 6360> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 6361 6362**错误码:** 6363 6364以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6365 6366| 错误码ID | 错误信息 | 6367| -------- | ------------------------------- | 6368| 6600101 | Session service exception. | 6369| 6600102 | The session does not exist. | 6370| 6600103 | The session controller does not exist. | 6371| 6600105 | Invalid session command. | 6372| 6600106 | The session is not activated. | 6373| 6600107 | Too many commands or events. | 6374 6375**示例:** 6376 6377```ts 6378import avSession from '@ohos.multimedia.avsession'; 6379import { BusinessError } from '@ohos.base'; 6380let avSessionController: avSession.AVSessionController | undefined = undefined; 6381let currentAVSession: avSession.AVSession | undefined = undefined; 6382let tag = "createNewSession"; 6383let context: Context = getContext(this); 6384 6385avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6386 if (err) { 6387 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6388 } else { 6389 currentAVSession = data; 6390 } 6391}); 6392if (currentAVSession !== undefined) { 6393 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6394 avSessionController = controller; 6395 }).catch((err: BusinessError) => { 6396 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6397 }); 6398} 6399 6400let commandName = "my_command"; 6401if (avSessionController !== undefined) { 6402 (avSessionController as avSession.AVSessionController).sendCommonCommand(commandName, {command : "This is my command"}, (err: BusinessError) => { 6403 if (err) { 6404 console.error(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); 6405 } 6406 }) 6407} 6408``` 6409 6410### getExtras<sup>10+</sup> 6411 6412getExtras(): Promise\<{[key: string]: Object}> 6413 6414获取媒体提供方设置的自定义媒体数据包。结果通过Promise异步回调方式返回。 6415 6416**系统能力:** SystemCapability.Multimedia.AVSession.Core 6417 6418**返回值:** 6419 6420| 类型 | 说明 | 6421| ----------------------------------- | ----------------------------- | 6422| Promise<{[key: string]: Object}\> | Promise对象,返回媒体提供方设置的自定义媒体数据包,数据包的内容与setExtras设置的内容完全一致。 | 6423 6424**错误码:** 6425 6426以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6427 6428| 错误码ID | 错误信息 | 6429| -------- | ---------------------------------------- | 6430| 6600101 | Session service exception. | 6431| 6600102 | The session does not exist. | 6432| 6600103 | The session controller does not exist. | 6433| 6600105 | Invalid session command. | 6434| 6600107 | Too many commands or events. | 6435 6436**示例:** 6437 6438```ts 6439import avSession from '@ohos.multimedia.avsession'; 6440import { BusinessError } from '@ohos.base'; 6441 6442let avSessionController: avSession.AVSessionController | undefined = undefined; 6443let currentAVSession: avSession.AVSession | undefined = undefined; 6444let tag = "createNewSession"; 6445let context: Context = getContext(this); 6446 6447avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6448 if (err) { 6449 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6450 } else { 6451 currentAVSession = data; 6452 } 6453}); 6454if (currentAVSession !== undefined) { 6455 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6456 avSessionController = controller; 6457 }).catch((err: BusinessError) => { 6458 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6459 }); 6460} 6461 6462if (avSessionController !== undefined) { 6463 (avSessionController as avSession.AVSessionController).getExtras().then((extras) => { 6464 console.info(`getExtras : SUCCESS : ${extras}`); 6465 }).catch((err: BusinessError) => { 6466 console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); 6467 }); 6468} 6469``` 6470 6471### getExtras<sup>10+</sup> 6472 6473getExtras(callback: AsyncCallback\<{[key: string]: Object}>): void 6474 6475获取媒体提供方设置的自定义媒体数据包,结果通过callback异步回调方式返回。 6476 6477**系统能力:** SystemCapability.Multimedia.AVSession.Core 6478 6479**参数:** 6480 6481| 参数名 | 类型 | 必填 | 说明 | 6482| -------- | ----------------------------------------- | ---- | -------------------------- | 6483| callback | AsyncCallback<{[key: string]: Object}\> | 是 | 回调函数,返回媒体提供方设置的自定义媒体数据包,数据包的内容与setExtras设置的内容完全一致。 | 6484 6485**错误码:** 6486 6487以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6488 6489| 错误码ID | 错误信息 | 6490| -------- | ---------------------------------------- | 6491| 6600101 | Session service exception. | 6492| 6600102 | The session does not exist. | 6493| 6600103 | The session controller does not exist. | 6494| 6600105 | Invalid session command. | 6495| 6600107 | Too many commands or events. | 6496 6497**示例:** 6498 6499```ts 6500import avSession from '@ohos.multimedia.avsession'; 6501import { BusinessError } from '@ohos.base'; 6502 6503let avSessionController: avSession.AVSessionController | undefined = undefined; 6504let currentAVSession: avSession.AVSession | undefined = undefined; 6505let tag = "createNewSession"; 6506let context: Context = getContext(this); 6507 6508avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6509 if (err) { 6510 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6511 } else { 6512 currentAVSession = data; 6513 } 6514}); 6515if (currentAVSession !== undefined) { 6516 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6517 avSessionController = controller; 6518 }).catch((err: BusinessError) => { 6519 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6520 }); 6521} 6522 6523if (avSessionController !== undefined) { 6524 (avSessionController as avSession.AVSessionController).getExtras((err, extras) => { 6525 if (err) { 6526 console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); 6527 } else { 6528 console.info(`getExtras : SUCCESS : ${extras}`); 6529 } 6530 }); 6531} 6532``` 6533 6534### on('metadataChange')<sup>10+</sup> 6535 6536on(type: 'metadataChange', filter: Array\<keyof AVMetadata> | 'all', callback: (data: AVMetadata) => void) 6537 6538设置元数据变化的监听事件。 6539 6540**系统能力:** SystemCapability.Multimedia.AVSession.Core 6541 6542**参数:** 6543 6544| 参数名 | 类型 | 必填 | 说明 | 6545| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6546| type | string | 是 | 事件回调类型,支持事件`'metadataChange'`:当元数据变化时,触发该事件。 | 6547| filter | Array\<keyof [AVMetadata](#avmetadata10)\> | 'all' | 是 | 'all' 表示关注元数据所有字段变化;Array<keyof [AVMetadata](#avmetadata10)\> 表示关注Array中的字段变化。 | 6548| callback | (data: [AVMetadata](#avmetadata10)) => void | 是 | 回调函数,参数data是变化后的元数据。 | 6549 6550**错误码:** 6551 6552以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6553 6554| 错误码ID | 错误信息 | 6555| -------- | ------------------------------ | 6556| 6600101 | Session service exception. | 6557| 6600103 | The session controller does not exist. | 6558 6559**示例:** 6560 6561```ts 6562avsessionController.on('metadataChange', 'all', (metadata: avSession.AVMetadata) => { 6563 console.info(`on metadataChange assetId : ${metadata.assetId}`); 6564}); 6565 6566avsessionController.on('metadataChange', ['assetId', 'title', 'description'], (metadata: avSession.AVMetadata) => { 6567 console.info(`on metadataChange assetId : ${metadata.assetId}`); 6568}); 6569 6570``` 6571 6572### off('metadataChange')<sup>10+</sup> 6573 6574off(type: 'metadataChange', callback?: (data: AVMetadata) => void) 6575 6576媒体控制器取消监听元数据变化的事件。 6577 6578**系统能力:** SystemCapability.Multimedia.AVSession.Core 6579 6580**参数:** 6581 6582| 参数名 | 类型 | 必填 | 说明 | 6583| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------ | 6584| type | string | 是 | 取消对应的监听事件,支持事件`'metadataChange'`。 | 6585| callback | (data: [AVMetadata](#avmetadata10)) => void | 否 | 回调函数,参数data是变化后的元数据。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 6586 6587**错误码:** 6588 6589以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6590 6591| 错误码ID | 错误信息 | 6592| -------- | ---------------- | 6593| 6600101 | Session service exception. | 6594| 6600103 | The session controller does not exist. | 6595 6596**示例:** 6597 6598```ts 6599avsessionController.off('metadataChange'); 6600``` 6601 6602### on('playbackStateChange')<sup>10+</sup> 6603 6604on(type: 'playbackStateChange', filter: Array\<keyof AVPlaybackState> | 'all', callback: (state: AVPlaybackState) => void) 6605 6606设置播放状态变化的监听事件。 6607 6608**系统能力:** SystemCapability.Multimedia.AVSession.Core 6609 6610**参数:** 6611 6612| 参数名 | 类型 | 必填 | 说明 | 6613| --------| -----------|-----|------------| 6614| type | string | 是 | 事件回调类型,支持事件`'playbackStateChange'`:当播放状态变化时,触发该事件。 | 6615| filter | Array\<keyof [AVPlaybackState](#avplaybackstate10)\> | 'all' | 是 | 'all' 表示关注播放状态所有字段变化;Array<keyof [AVPlaybackState](#avplaybackstate10)\> 表示关注Array中的字段变化。 | 6616| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | 是 | 回调函数,参数state是变化后的播放状态。| 6617 6618**错误码:** 6619 6620以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6621 6622| 错误码ID | 错误信息 | 6623| -------- | ------------------------------ | 6624| 6600101 | Session service exception. | 6625| 6600103 | The session controller does not exist. | 6626 6627**示例:** 6628 6629```ts 6630avsessionController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { 6631 console.info(`on playbackStateChange state : ${playbackState.state}`); 6632}); 6633 6634avsessionController.on('playbackStateChange', ['state', 'speed', 'loopMode'], (playbackState: avSession.AVPlaybackState) => { 6635 console.info(`on playbackStateChange state : ${playbackState.state}`); 6636}); 6637``` 6638 6639### off('playbackStateChange')<sup>10+</sup> 6640 6641off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void) 6642 6643媒体控制器取消监听播放状态变化的事件。 6644 6645**系统能力:** SystemCapability.Multimedia.AVSession.Core 6646 6647**参数:** 6648 6649| 参数名 | 类型 | 必填 | 说明 | 6650| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 6651| type | string | 是 | 取消对应的监听事件,支持事件`'playbackStateChange'`。 | 6652| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | 否 | 回调函数,参数state是变化后的播放状态。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 6653 6654**错误码:** 6655 6656以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6657 6658| 错误码ID | 错误信息 | 6659| -------- | ---------------- | 6660| 6600101 | Session service exception. | 6661| 6600103 | The session controller does not exist. | 6662 6663**示例:** 6664 6665```ts 6666avsessionController.off('playbackStateChange'); 6667``` 6668 6669### on('sessionDestroy')<sup>10+</sup> 6670 6671on(type: 'sessionDestroy', callback: () => void) 6672 6673会话销毁的监听事件。 6674 6675**系统能力:** SystemCapability.Multimedia.AVSession.Core 6676 6677**参数:** 6678 6679| 参数名 | 类型 | 必填 | 说明 | 6680| -------- | ---------- | ---- | ------------------------------------------------------------ | 6681| type | string | 是 | 事件回调类型,支持事件`'sessionDestroy'`:当检测到会话销毁时,触发该事件)。 | 6682| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | 6683 6684**错误码:** 6685 6686以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6687 6688| 错误码ID | 错误信息 | 6689| -------- | ------------------------------ | 6690| 6600101 | Session service exception. | 6691| 6600103 | The session controller does not exist. | 6692 6693**示例:** 6694 6695```ts 6696avsessionController.on('sessionDestroy', () => { 6697 console.info(`on sessionDestroy : SUCCESS `); 6698}); 6699``` 6700 6701### off('sessionDestroy')<sup>10+</sup> 6702 6703off(type: 'sessionDestroy', callback?: () => void) 6704 6705媒体控制器取消监听会话的销毁事件。 6706 6707**系统能力:** SystemCapability.Multimedia.AVSession.Core 6708 6709**参数:** 6710 6711| 参数名 | 类型 | 必填 | 说明 | 6712| -------- | ---------- | ---- | ----------------------------------------------------- | 6713| type | string | 是 | 取消对应的监听事件,支持事件`'sessionDestroy'`。 | 6714| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 6715 6716**错误码:** 6717 6718以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6719 6720| 错误码ID | 错误信息 | 6721| -------- | ---------------- | 6722| 6600101 | Session service exception. | 6723| 6600103 | The session controller does not exist. | 6724 6725**示例:** 6726 6727```ts 6728avsessionController.off('sessionDestroy'); 6729``` 6730 6731### on('activeStateChange')<sup>10+</sup> 6732 6733on(type: 'activeStateChange', callback: (isActive: boolean) => void) 6734 6735会话的激活状态的监听事件。 6736 6737**系统能力:** SystemCapability.Multimedia.AVSession.Core 6738 6739**参数:** 6740 6741| 参数名 | 类型 | 必填 | 说明 | 6742| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 6743| type | string | 是 | 事件回调类型,支持事件`'activeStateChange'`:当检测到会话的激活状态发生改变时,触发该事件。 | 6744| callback | (isActive: boolean) => void | 是 | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。 | 6745 6746**错误码:** 6747 6748以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6749 6750| 错误码ID | 错误信息 | 6751| -------- | ----------------------------- | 6752| 6600101 | Session service exception. | 6753| 6600103 |The session controller does not exist. | 6754 6755**示例:** 6756 6757```ts 6758avsessionController.on('activeStateChange', (isActive: boolean) => { 6759 console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`); 6760}); 6761``` 6762 6763### off('activeStateChange')<sup>10+</sup> 6764 6765off(type: 'activeStateChange', callback?: (isActive: boolean) => void) 6766 6767媒体控制器取消监听会话激活状态变化的事件。 6768 6769**系统能力:** SystemCapability.Multimedia.AVSession.Core 6770 6771**参数:** 6772 6773| 参数名 | 类型 | 必填 | 说明 | 6774| -------- | --------------------------- | ---- | ----------------------------------------------------- | 6775| type | string | 是 | 取消对应的监听事件,支持事件`'activeStateChange'`。 | 6776| callback | (isActive: boolean) => void | 否 | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 6777 6778**错误码:** 6779 6780以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6781 6782| 错误码ID | 错误信息 | 6783| -------- | ---------------- | 6784| 6600101 | Session service exception. | 6785| 6600103 | The session controller does not exist. | 6786 6787**示例:** 6788 6789```ts 6790avsessionController.off('activeStateChange'); 6791``` 6792 6793### on('validCommandChange')<sup>10+</sup> 6794 6795on(type: 'validCommandChange', callback: (commands: Array\<AVControlCommandType>) => void) 6796 6797会话支持的有效命令变化监听事件。 6798 6799**系统能力:** SystemCapability.Multimedia.AVSession.Core 6800 6801**参数:** 6802 6803| 参数名 | 类型 | 必填 | 说明 | 6804| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6805| type | string | 是 | 事件回调类型,支持事件`'validCommandChange'`:当检测到会话的合法命令发生改变时,触发该事件。 | 6806| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | 是 | 回调函数。参数commands是有效命令的集合。 | 6807 6808**错误码:** 6809 6810以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6811 6812| 错误码ID | 错误信息 | 6813| -------- | ------------------------------ | 6814| 6600101 | Session service exception. | 6815| 6600103 | The session controller does not exist. | 6816 6817**示例:** 6818 6819```ts 6820avsessionController.on('validCommandChange', (validCommands: avSession.AVControlCommandType[]) => { 6821 console.info(`validCommandChange : SUCCESS : size : ${validCommands.length}`); 6822 console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); 6823}); 6824``` 6825 6826### off('validCommandChange')<sup>10+</sup> 6827 6828off(type: 'validCommandChange', callback?: (commands: Array\<AVControlCommandType>) => void) 6829 6830媒体控制器取消监听会话有效命令变化的事件。 6831 6832**系统能力:** SystemCapability.Multimedia.AVSession.Core 6833 6834**参数:** 6835 6836| 参数名 | 类型 | 必填 | 说明 | 6837| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | 6838| type | string | 是 | 取消对应的监听事件,支持事件`'validCommandChange'`。 | 6839| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | 否 | 回调函数。参数commands是有效命令的集合。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 6840 6841**错误码:** 6842 6843以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6844 6845| 错误码ID | 错误信息 | 6846| -------- | ---------------- | 6847| 6600101 | Session service exception. | 6848| 6600103 | The session controller does not exist. | 6849 6850**示例:** 6851 6852```ts 6853avsessionController.off('validCommandChange'); 6854``` 6855 6856### on('outputDeviceChange')<sup>10+</sup> 6857 6858on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void 6859 6860设置播放设备变化的监听事件。 6861 6862**系统能力:** SystemCapability.Multimedia.AVSession.Core 6863 6864**参数:** 6865 6866| 参数名 | 类型 | 必填 | 说明 | 6867| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 6868| type | string | 是 | 事件回调类型,支持事件为`'outputDeviceChange'`:当播放设备变化时,触发该事件)。 | 6869| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 是 | 回调函数,参数device是设备相关信息。 | 6870 6871**错误码:** 6872 6873以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6874 6875| 错误码ID | 错误信息 | 6876| -------- | ----------------------- | 6877| 6600101 | Session service exception. | 6878| 6600103 | The session controller does not exist. | 6879 6880**示例:** 6881 6882```ts 6883avsessionController.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { 6884 console.info(`on outputDeviceChange state: ${state}, device : ${device}`); 6885}); 6886``` 6887 6888### off('outputDeviceChange')<sup>10+</sup> 6889 6890off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void 6891 6892媒体控制器取消监听分布式设备变化的事件。 6893 6894**系统能力:** SystemCapability.Multimedia.AVSession.Core 6895 6896**参数:** 6897 6898| 参数名 | 类型 | 必填 | 说明 | 6899| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | 6900| type | string | 是 | 取消对应的监听事件,支持事件`'outputDeviceChange'`。 | 6901| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 否 | 回调函数,参数device是设备相关信息。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 6902 6903**错误码:** 6904 6905以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6906 6907| 错误码ID | 错误信息 | 6908| -------- | ---------------- | 6909| 6600101 | Session service exception. | 6910| 6600103 | The session controller does not exist. | 6911 6912**示例:** 6913 6914```ts 6915avsessionController.off('outputDeviceChange'); 6916``` 6917 6918### on('sessionEvent')<sup>10+</sup> 6919 6920on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key:string]: Object}) => void): void 6921 6922媒体控制器设置会话自定义事件变化的监听器。 6923 6924**系统能力:** SystemCapability.Multimedia.AVSession.Core 6925 6926**参数:** 6927 6928| 参数名 | 类型 | 必填 | 说明 | 6929| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6930| type | string | 是 | 事件回调类型,支持事件`'sessionEvent'`:当会话事件变化时,触发该事件。 | 6931| callback | (sessionEvent: string, args: {[key:string]: object}) => void | 是 | 回调函数,sessionEvent为变化的会话事件名,args为事件的参数。 | 6932 6933**错误码:** 6934 6935以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6936 6937| 错误码ID | 错误信息 | 6938| -------- | ------------------------------ | 6939| 6600101 | Session service exception. | 6940| 6600103 | The session controller does not exist. | 6941 6942**示例:** 6943 6944```ts 6945import avSession from '@ohos.multimedia.avsession'; 6946import { BusinessError } from '@ohos.base'; 6947 6948let avSessionController: avSession.AVSessionController | undefined = undefined; 6949let currentAVSession: avSession.AVSession | undefined = undefined; 6950let tag = "createNewSession"; 6951let context: Context = getContext(this); 6952 6953avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 6954 if (err) { 6955 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 6956 } else { 6957 currentAVSession = data; 6958 } 6959}); 6960if (currentAVSession !== undefined) { 6961 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 6962 avSessionController = controller; 6963 }).catch((err: BusinessError) => { 6964 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 6965 }); 6966} 6967 6968if (avSessionController !== undefined) { 6969 (avSessionController as avSession.AVSessionController).on('sessionEvent', (sessionEvent, args) => { 6970 console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`); 6971 }); 6972} 6973``` 6974 6975### off('sessionEvent')<sup>10+</sup> 6976 6977off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key:string]: Object}) => void): void 6978 6979媒体控制器取消监听会话事件的变化通知。 6980 6981**系统能力:** SystemCapability.Multimedia.AVSession.Core 6982 6983**参数:** 6984 6985| 参数名 | 类型 | 必填 | 说明 | 6986| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | 6987| type | string | 是 | 取消对应的监听事件,支持事件`'sessionEvent'`。 | 6988| callback | (sessionEvent: string, args: {[key:string]: Object}) => void | 否 | 回调函数,参数sessionEvent是变化的事件名,args为事件的参数。<br>该参数为可选参数,若不填写该参数,则认为取消所有对sessionEvent事件的监听。 | 6989 6990**错误码:** 6991 6992以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 6993 6994| 错误码ID | 错误信息 | 6995| -------- | ---------------- | 6996| 6600101 | Session service exception. | 6997| 6600103 | The session controller does not exist. | 6998 6999**示例:** 7000 7001```ts 7002avsessionController.off('sessionEvent'); 7003``` 7004 7005### on('queueItemsChange')<sup>10+</sup> 7006 7007on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void 7008 7009媒体控制器设置会话自定义播放列表变化的监听器。 7010 7011**系统能力:** SystemCapability.Multimedia.AVSession.Core 7012 7013**参数:** 7014 7015| 参数名 | 类型 | 必填 | 说明 | 7016| -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------------------------------- | 7017| type | string | 是 | 事件回调类型,支持事件`'queueItemsChange'`:当session修改播放列表时,触发该事件。 | 7018| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | 是 | 回调函数,items为变化的播放列表。 | 7019 7020**错误码:** 7021 7022以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 7023 7024| 错误码ID | 错误信息 | 7025| -------- | ------------------------------ | 7026| 6600101 | Session service exception. | 7027| 6600103 | The session controller does not exist. | 7028 7029**示例:** 7030 7031```ts 7032avsessionController.on('queueItemsChange', (items: avSession.AVQueueItem[]) => { 7033 console.info(`OnQueueItemsChange, items length is ${items.length}`); 7034}); 7035``` 7036 7037### off('queueItemsChange')<sup>10+</sup> 7038 7039off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void 7040 7041媒体控制器取消监听播放列表变化的事件。 7042 7043**系统能力:** SystemCapability.Multimedia.AVSession.Core 7044 7045**参数:** 7046 7047| 参数名 | 类型 | 必填 | 说明 | 7048| -------- | ---------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------- | 7049| type | string | 是 | 取消对应的监听事件,支持事件`'queueItemsChange'`。 | 7050| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | 否 | 回调函数,参数items是变化的播放列表。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 7051 7052**错误码:** 7053 7054以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 7055 7056| 错误码ID | 错误信息 | 7057| -------- | ---------------- | 7058| 6600101 | Session service exception. | 7059| 6600103 | The session controller does not exist. | 7060 7061**示例:** 7062 7063```ts 7064avsessionController.off('queueItemsChange'); 7065``` 7066 7067### on('queueTitleChange')<sup>10+</sup> 7068 7069on(type: 'queueTitleChange', callback: (title: string) => void): void 7070 7071媒体控制器设置会话自定义播放列表的名称变化的监听器。 7072 7073**系统能力:** SystemCapability.Multimedia.AVSession.Core 7074 7075**参数:** 7076 7077| 参数名 | 类型 | 必填 | 说明 | 7078| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------- | 7079| type | string | 是 | 事件回调类型,支持事件`'queueTitleChange'`:当session修改播放列表名称时,触发该事件。 | 7080| callback | (title: string) => void | 是 | 回调函数,title为变化的播放列表名称。 | 7081 7082**错误码:** 7083 7084以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 7085 7086| 错误码ID | 错误信息 | 7087| -------- | ------------------------------ | 7088| 6600101 | Session service exception. | 7089| 6600103 | The session controller does not exist. | 7090 7091**示例:** 7092 7093```ts 7094avsessionController.on('queueTitleChange', (title: string) => { 7095 console.info(`queueTitleChange, title is ${title}`); 7096}); 7097``` 7098 7099### off('queueTitleChange')<sup>10+</sup> 7100 7101off(type: 'queueTitleChange', callback?: (title: string) => void): void 7102 7103媒体控制器取消监听播放列表名称变化的事件。 7104 7105**系统能力:** SystemCapability.Multimedia.AVSession.Core 7106 7107**参数:** 7108 7109| 参数名 | 类型 | 必填 | 说明 | 7110| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | 7111| type | string | 是 | 取消对应的监听事件,支持事件`'queueTitleChange'`。 | 7112| callback | (title: string) => void | 否 | 回调函数,参数items是变化的播放列表名称。<br>该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | 7113 7114**错误码:** 7115 7116以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 7117 7118| 错误码ID | 错误信息 | 7119| -------- | ---------------- | 7120| 6600101 | Session service exception. | 7121| 6600103 | The session controller does not exist. | 7122 7123**示例:** 7124 7125```ts 7126avsessionController.off('queueTitleChange'); 7127``` 7128 7129### on('extrasChange')<sup>10+</sup> 7130 7131on(type: 'extrasChange', callback: (extras: {[key:string]: Object}) => void): void 7132 7133媒体控制器设置自定义媒体数据包事件变化的监听器。 7134 7135**系统能力:** SystemCapability.Multimedia.AVSession.Core 7136 7137**参数:** 7138 7139| 参数名 | 类型 | 必填 | 说明 | 7140| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7141| type | string | 是 | 事件回调类型,支持事件`'extrasChange'`:当媒体提供方设置自定义媒体数据包时,触发该事件。 | 7142| callback | (extras: {[key:string]: object}) => void | 是 | 回调函数,extras为媒体提供方新设置的自定义媒体数据包,该自定义媒体数据包与dispatchSessionEvent方法设置的数据包完全一致。 | 7143 7144**错误码:** 7145 7146以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 7147 7148| 错误码ID | 错误信息 | 7149| -------- | ------------------------------ | 7150| 6600101 | Session service exception. | 7151| 6600103 | The session controller does not exist. | 7152 7153**示例:** 7154 7155```ts 7156import avSession from '@ohos.multimedia.avsession'; 7157import { BusinessError } from '@ohos.base'; 7158 7159let avSessionController: avSession.AVSessionController | undefined = undefined; 7160let currentAVSession: avSession.AVSession | undefined = undefined; 7161let tag = "createNewSession"; 7162let context: Context = getContext(this); 7163 7164avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { 7165 if (err) { 7166 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 7167 } else { 7168 currentAVSession = data; 7169 } 7170}); 7171if (currentAVSession !== undefined) { 7172 avSession.createController((currentAVSession as avSession.AVSession).sessionId).then((controller: avSession.AVSessionController) => { 7173 avSessionController = controller; 7174 }).catch((err: BusinessError) => { 7175 console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); 7176 }); 7177} 7178 7179if (avSessionController !== undefined) { 7180 (avSessionController as avSession.AVSessionController).on('extrasChange', (extras) => { 7181 console.info(`Caught extrasChange event,the new extra is: ${JSON.stringify(extras)}`); 7182 }); 7183} 7184``` 7185 7186### off('extrasChange')<sup>10+</sup> 7187 7188off(type: 'extrasChange', callback?: (extras: {[key:string]: Object}) => void): void 7189 7190媒体控制器取消监听自定义媒体数据包变化事件。 7191 7192**系统能力:** SystemCapability.Multimedia.AVSession.Core 7193 7194**参数:** 7195 7196| 参数名 | 类型 | 必填 | 说明 | 7197| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | 7198| type | string | 是 | 取消对应的监听事件,支持事件`'extrasChange'`。 | 7199| callback | ({[key:string]: Object}) => void | 否 | 注册监听事件时的回调函数。<br>该参数为可选参数,若不填写该参数,则认为取消会话所有与此事件相关的监听。 | 7200 7201**错误码:** 7202 7203以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 7204 7205| 错误码ID | 错误信息 | 7206| -------- | ---------------- | 7207| 6600101 | Session service exception. | 7208| 6600103 | The session controller does not exist. | 7209 7210**示例:** 7211 7212```ts 7213avsessionController.off('extrasChange'); 7214``` 7215 7216## AVControlCommandType<sup>10+</sup> 7217 7218会话可传递的命令。 7219 7220**系统能力:** SystemCapability.Multimedia.AVSession.Core 7221 7222| 名称 | 类型 | 说明 | 7223| -------------- | ------ | ------------ | 7224| play | string | 播放 | 7225| pause | string | 暂停 | 7226| stop | string | 停止 | 7227| playNext | string | 下一首 | 7228| playPrevious | string | 上一首 | 7229| fastForward | string | 快进 | 7230| rewind | string | 快退 | 7231| seek | string | 跳转某一节点 | 7232| setSpeed | string | 设置播放倍速 | 7233| setLoopMode | string | 设置循环模式 | 7234| toggleFavorite | string | 是否收藏 | 7235 7236## AVControlCommand<sup>10+</sup> 7237 7238会话接受的命令的对象描述。 7239 7240**系统能力:** SystemCapability.Multimedia.AVSession.Core 7241 7242| 名称 | 类型 | 必填 | 说明 | 7243| --------- | ------------------------------------------------- | ---- | -------------- | 7244| command | [AVControlCommandType](#avcontrolcommandtype10) | 是 | 命令 | 7245| parameter | [LoopMode](#loopmode10) | string | number | 否 | 命令对应的参数 | 7246 7247## AVSessionErrorCode<sup>10+</sup> 7248 7249会话发生错误时的错误码。 7250 7251**系统能力:** SystemCapability.Multimedia.AVSession.Core 7252 7253| 名称 | 值 | 说明 | 7254| -------------------------------------- | ------- | ------------------------------- | 7255| ERR_CODE_SERVICE_EXCEPTION | 6600101 | Session service exception. | 7256| ERR_CODE_SESSION_NOT_EXIST | 6600102 | The session does not exist. | 7257| ERR_CODE_CONTROLLER_NOT_EXIST | 6600103 | The session controller does not exist. | 7258| ERR_CODE_REMOTE_CONNECTION_ERR | 6600104 | The remote session connection failed. | 7259| ERR_CODE_COMMAND_INVALID | 6600105 | Invalid session command. | 7260| ERR_CODE_SESSION_INACTIVE | 6600106 | The session is not activated. | 7261| ERR_CODE_MESSAGE_OVERLOAD | 6600107 | Too many commands or events. | 7262| ERR_CODE_DEVICE_CONNECTION_FAILED | 6600108 | Device connecting failed. | 7263| ERR_CODE_REMOTE_CONNECTION_NOT_EXIST | 6600109 | The remote connection is not established. | 7264