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