1# Functions 2<!--Kit: AVSession Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @ccfriend; @liao_qian--> 5<!--Designer: @ccfriend--> 6<!--Tester: @chenmingxi1_huawei--> 7<!--Adviser: @zengyawen--> 8 9> **说明:** 10> 11> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12 13## 导入模块 14 15```ts 16import { avSession } from '@kit.AVSessionKit'; 17``` 18 19## avSession.createAVSession<sup>10+</sup> 20 21createAVSession(context: Context, tag: string, type: AVSessionType): Promise\<AVSession> 22 23创建会话对象,一个应用进程仅允许存在一个会话,重复创建会失败,结果通过Promise异步回调方式返回。 24 25**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 26 27**系统能力:** SystemCapability.Multimedia.AVSession.Core 28 29**参数:** 30 31| 参数名 | 类型 | 必填 | 说明 | 32| ------ | ------------------------------- | ---- | ------------------------------ | 33| context| [Context](../apis-ability-kit/js-apis-inner-app-context.md) | 是| 需要使用UIAbilityContext,用于系统获取应用组件的相关信息。 | 34| tag | string | 是 | 会话的自定义名称。 | 35| type | [AVSessionType](arkts-apis-avsession-t.md#avsessiontype10) | 是 | 会话类型。 | 36 37**返回值:** 38 39| 类型 | 说明 | 40| --------------------------------- | ------------------------------------------------------------ | 41| Promise<[AVSession](arkts-apis-avsession-AVSession.md)\> | Promise对象。回调返回会话实例对象,可用于获取会话ID,以及设置元数据、播放状态,发送按键事件等操作。| 42 43**错误码:** 44 45以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 46 47| 错误码ID | 错误信息 | 48| -------- | ---------------------------------------- | 49| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 50| 6600101 | Session service exception. | 51 52**示例:** 53 54```ts 55import { BusinessError } from '@kit.BasicServicesKit'; 56import { avSession } from '@kit.AVSessionKit'; 57@Entry 58@Component 59struct Index { 60 @State message: string = 'hello world'; 61 62 build() { 63 Column() { 64 Text(this.message) 65 .onClick(()=>{ 66 let currentAVSession: avSession.AVSession; 67 let tag = "createNewSession"; 68 let context: Context = this.getUIContext().getHostContext() as Context; 69 let sessionId: string; // 供后续函数入参使用。 70 71 avSession.createAVSession(context, tag, "audio").then(async (data: avSession.AVSession) => { 72 currentAVSession = data; 73 sessionId = currentAVSession.sessionId; 74 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 75 }).catch((err: BusinessError) => { 76 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 77 }); 78 }) 79 } 80 .width('100%') 81 .height('100%') 82 } 83} 84``` 85 86## avSession.createAVSession<sup>10+</sup> 87 88createAVSession(context: Context, tag: string, type: AVSessionType, callback: AsyncCallback\<AVSession>): void 89 90创建会话对象,一个应用程序仅允许存在一个会话,重复创建会失败,结果通过callback异步回调方式返回。 91 92**系统能力:** SystemCapability.Multimedia.AVSession.Core 93 94**参数:** 95 96| 参数名 | 类型 | 必填 | 说明 | 97| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 98| context| [Context](../apis-ability-kit/js-apis-inner-app-context.md) | 是| 需要使用UIAbilityContext,用于系统获取应用组件的相关信息。 | 99| tag | string | 是 | 会话的自定义名称。 | 100| type | [AVSessionType](arkts-apis-avsession-t.md#avsessiontype10) | 是 | 会话类型。 | 101| callback | AsyncCallback<[AVSession](arkts-apis-avsession-AVSession.md)\> | 是 | 回调函数。回调返回会话实例对象,可用于获取会话ID,以及设置元数据、播放状态,发送按键事件等操作。 | 102 103**错误码:** 104 105以下错误码的详细介绍请参见[媒体会话管理错误码](errorcode-avsession.md)。 106 107| 错误码ID | 错误信息 | 108| -------- | ---------------------------------------- | 109| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 110| 6600101 | Session service exception. | 111 112**示例:** 113 114```ts 115import { BusinessError } from '@kit.BasicServicesKit'; 116import { avSession } from '@kit.AVSessionKit'; 117@Entry 118@Component 119struct Index { 120 @State message: string = 'hello world'; 121 122 build() { 123 Column() { 124 Text(this.message) 125 .onClick(()=>{ 126 let currentAVSession: avSession.AVSession; 127 let tag = "createNewSession"; 128 let context: Context = this.getUIContext().getHostContext() as Context; 129 let sessionId: string; // 供后续函数入参使用。 130 131 avSession.createAVSession(context, tag, "audio", async (err: BusinessError, data: avSession.AVSession) => { 132 if (err) { 133 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 134 } else { 135 currentAVSession = data; 136 sessionId = currentAVSession.sessionId; 137 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 138 } 139 }); 140 }) 141 } 142 .width('100%') 143 .height('100%') 144 } 145} 146``` 147