# Functions > **NOTE** > > 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. ## Modules to Import ```ts import { avSession } from '@kit.AVSessionKit'; ``` ## avSession.createAVSession10+ createAVSession(context: Context, tag: string, type: AVSessionType): Promise\ Creates 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. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Multimedia.AVSession.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------------- | ---- | ------------------------------ | | 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.| | tag | string | Yes | Custom session name. | | type | [AVSessionType](arkts-apis-avsession-t.md#avsessiontype10) | Yes | Session type.| **Return value** | Type | Description | | --------------------------------- | ------------------------------------------------------------ | | 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.| **Error codes** For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). | ID| Error Message| | -------- | ---------------------------------------- | | 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | | 6600101 | Session service exception. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { avSession } from '@kit.AVSessionKit'; @Entry @Component struct Index { @State message: string = 'hello world'; build() { Column() { Text(this.message) .onClick(()=>{ let currentAVSession: avSession.AVSession; let tag = "createNewSession"; let context: Context = this.getUIContext().getHostContext() as Context; let sessionId: string; // Used as an input parameter of subsequent functions. avSession.createAVSession(context, tag, "audio").then(async (data: avSession.AVSession) => { currentAVSession = data; sessionId = currentAVSession.sessionId; console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); }).catch((err: BusinessError) => { console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); }); }) } .width('100%') .height('100%') } } ``` ## avSession.createAVSession10+ createAVSession(context: Context, tag: string, type: AVSessionType, callback: AsyncCallback\): void Creates 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. **System capability**: SystemCapability.Multimedia.AVSession.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | | 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. | | tag | string | Yes | Custom session name. | | type | [AVSessionType](arkts-apis-avsession-t.md#avsessiontype10) | Yes | Session type. | | 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.| **Error codes** For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). | ID| Error Message| | -------- | ---------------------------------------- | | 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed. | | 6600101 | Session service exception. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { avSession } from '@kit.AVSessionKit'; @Entry @Component struct Index { @State message: string = 'hello world'; build() { Column() { Text(this.message) .onClick(()=>{ let currentAVSession: avSession.AVSession; let tag = "createNewSession"; let context: Context = this.getUIContext().getHostContext() as Context; let sessionId: string; // Used as an input parameter of subsequent functions. avSession.createAVSession(context, tag, "audio", async (err: BusinessError, data: avSession.AVSession) => { if (err) { console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); } else { currentAVSession = data; sessionId = currentAVSession.sessionId; console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); } }); }) } .width('100%') .height('100%') } } ```