1# Functions 2<!--Kit: AVSession Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @ccfriend; @liao_qian--> 5<!--SE: @ccfriend--> 6<!--TSE: @chenmingxi1_huawei--> 7 8> **NOTE** 9> 10> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 11 12## Modules to Import 13 14```ts 15import { avSession } from '@kit.AVSessionKit'; 16``` 17 18## avSession.createAVSession<sup>10+</sup> 19 20createAVSession(context: Context, tag: string, type: AVSessionType): Promise\<AVSession> 21 22Creates a media session. This API uses a promise to return the result. An ability can have only one session, and repeated calling of this API fails. 23 24**Atomic service API**: This API can be used in atomic services since API version 12. 25 26**System capability**: SystemCapability.Multimedia.AVSession.Core 27 28**Parameters** 29 30| Name| Type | Mandatory| Description | 31| ------ | ------------------------------- | ---- | ------------------------------ | 32| context| [Context](../apis-ability-kit/js-apis-inner-app-context.md) | Yes| Context of the UIAbility, which is used to obtain information about the application component.| 33| tag | string | Yes | Custom session name. | 34| type | [AVSessionType](arkts-apis-avsession-t.md#avsessiontype10) | Yes | Session type.| 35 36**Return value** 37 38| Type | Description | 39| --------------------------------- | ------------------------------------------------------------ | 40| Promise<[AVSession](arkts-apis-avsession-AVSession.md)\> | Promise used to return the media session obtained, which can be used to obtain the session ID, set the metadata and playback state information, and send key events.| 41 42**Error codes** 43 44For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 45 46| ID| Error Message| 47| -------- | ---------------------------------------- | 48| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 49| 6600101 | Session service exception. | 50 51**Example** 52 53```ts 54import { BusinessError } from '@kit.BasicServicesKit'; 55import { avSession } from '@kit.AVSessionKit'; 56@Entry 57@Component 58struct Index { 59 @State message: string = 'hello world'; 60 61 build() { 62 Column() { 63 Text(this.message) 64 .onClick(()=>{ 65 let currentAVSession: avSession.AVSession; 66 let tag = "createNewSession"; 67 let context: Context = this.getUIContext().getHostContext() as Context; 68 let sessionId: string; // Used as an input parameter of subsequent functions. 69 70 avSession.createAVSession(context, tag, "audio").then(async (data: avSession.AVSession) => { 71 currentAVSession = data; 72 sessionId = currentAVSession.sessionId; 73 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 74 }).catch((err: BusinessError) => { 75 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 76 }); 77 }) 78 } 79 .width('100%') 80 .height('100%') 81 } 82} 83``` 84 85## avSession.createAVSession<sup>10+</sup> 86 87createAVSession(context: Context, tag: string, type: AVSessionType, callback: AsyncCallback\<AVSession>): void 88 89Creates a media session. This API uses an asynchronous callback to return the result. An ability can have only one session, and repeated calling of this API fails. 90 91**System capability**: SystemCapability.Multimedia.AVSession.Core 92 93**Parameters** 94 95| Name | Type | Mandatory| Description | 96| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 97| context| [Context](../apis-ability-kit/js-apis-inner-app-context.md) | Yes| Context of the UIAbility, which is used to obtain information about the application component. | 98| tag | string | Yes | Custom session name. | 99| type | [AVSessionType](arkts-apis-avsession-t.md#avsessiontype10) | Yes | Session type. | 100| callback | AsyncCallback<[AVSession](arkts-apis-avsession-AVSession.md)\> | Yes | Callback used to return the media session obtained, which can be used to obtain the session ID, set the metadata and playback state information, and send key events.| 101 102**Error codes** 103 104For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 105 106| ID| Error Message| 107| -------- | ---------------------------------------- | 108| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | 109| 6600101 | Session service exception. | 110 111**Example** 112 113```ts 114import { BusinessError } from '@kit.BasicServicesKit'; 115import { avSession } from '@kit.AVSessionKit'; 116@Entry 117@Component 118struct Index { 119 @State message: string = 'hello world'; 120 121 build() { 122 Column() { 123 Text(this.message) 124 .onClick(()=>{ 125 let currentAVSession: avSession.AVSession; 126 let tag = "createNewSession"; 127 let context: Context = this.getUIContext().getHostContext() as Context; 128 let sessionId: string; // Used as an input parameter of subsequent functions. 129 130 avSession.createAVSession(context, tag, "audio", async (err: BusinessError, data: avSession.AVSession) => { 131 if (err) { 132 console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); 133 } else { 134 currentAVSession = data; 135 sessionId = currentAVSession.sessionId; 136 console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); 137 } 138 }); 139 }) 140 } 141 .width('100%') 142 .height('100%') 143 } 144} 145``` 146