1# Class (AVCastPickerHelper) 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> - The initial APIs of this class are supported since API version 14. 12 13AVCastPickerHelper implements a semi-modal object used for casting. It displays a semi-modal window for users to select a target cast device. Before using the APIs of this class, you need to create an AVCastPickerHelper instance. 14 15## Modules to Import 16 17```ts 18import { avSession } from '@kit.AVSessionKit'; 19``` 20 21## constructor<sup>14+</sup> 22 23constructor(context: Context) 24 25Creates an AVCastPickerHelper instance. For details about how to obtain the context, see [getContext](../apis-arkui/arkts-apis-uicontext-uicontext.md#gethostcontext12). 26 27**Atomic service API**: This API can be used in atomic services since API version 14. 28 29**System capability**: SystemCapability.Multimedia.AVSession.AVCast 30 31**Parameters** 32 33| Name | Type | Mandatory| Description | 34| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 35| context | Context | Yes | Application context. (Only [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) is supported.)| 36 37**Error codes** 38 39For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 40 41| ID| Error Message| 42| -------- | ---------------------------------------- | 43| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 44| 6600101 | Session service exception. | 45 46**Example** 47 48```ts 49import { common } from '@kit.AbilityKit'; 50import { avSession } from '@kit.AVSessionKit'; 51@Entry 52@Component 53struct Index { 54 @State message: string = 'hello world'; 55 56 build() { 57 Row() { 58 Column() { 59 Text(this.message) 60 .fontSize(40) 61 .fontWeight(FontWeight.Bold) 62 .onClick(()=>{ 63 let context = this.getUIContext().getHostContext() as Context; 64 let avCastPicker = new avSession.AVCastPickerHelper(context); 65 }) 66 } 67 .width('100%') 68 } 69 .height('100%') 70 } 71} 72``` 73 74## select<sup>14+</sup> 75 76select(options?: AVCastPickerOptions): Promise\<void> 77 78Starts the AVCastPicker dialog box, where users can select the target cast device. This API uses a promise to return the result. You can pass in **AVCastPickerOptions** to specify the properties for selection. 79 80**Atomic service API**: This API can be used in atomic services since API version 14. 81 82**System capability**: SystemCapability.Multimedia.AVSession.AVCast 83 84**Parameters** 85 86| Name | Type | Mandatory| Description | 87| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 88| options | [AVCastPickerOptions](arkts-apis-avsession-i.md#avcastpickeroptions14) | No | AVCastPicker selection options. If this parameter is not specified, the default value of **AVCastPickerOptions** is used.| 89 90**Return value** 91 92| Type | Description | 93| -------------- | ----------------------------- | 94| Promise\<void> | Promise used to return the result. If the command is sent, no value is returned; otherwise, an error object is returned.| 95 96**Error codes** 97 98For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 99 100| ID| Error Message| 101| -------- | ---------------------------------------- | 102| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 103 104**Example** 105 106```ts 107import { common } from '@kit.AbilityKit'; 108import { BusinessError } from '@kit.BasicServicesKit'; 109 110async function avCastPicker(context: common.Context) { 111 let avCastPickerOptions : avSession.AVCastPickerOptions = { 112 sessionType : 'video', 113 } 114 let avCastPicker = new avSession.AVCastPickerHelper(context); 115 avCastPicker.select(avCastPickerOptions).then(() => { 116 console.info('select successfully'); 117 }).catch((err: BusinessError) => { 118 console.error(`AVCastPicker.select failed with err: ${err.code}, ${err.message}`); 119 }); 120} 121``` 122 123## on('pickerStateChange')<sup>14+</sup> 124 125on(type: 'pickerStateChange', callback: Callback<AVCastPickerState\>) : void 126 127Subscribes to semi-modal window change events. 128 129Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes. 130 131**Atomic service API**: This API can be used in atomic services since API version 14. 132 133**System capability**: SystemCapability.Multimedia.AVSession.AVCast 134 135**Parameters** 136 137| Name | Type | Mandatory| Description | 138| --------| -----------|-----|------------| 139| type | string | Yes | Event type. The event **'pickerStateChange'** is triggered when the semi-modal window changes.| 140| callback | Callback\<[AVCastPickerState](js-apis-avCastPickerParam.md#avcastpickerstate11)> | Yes | Callback function, where the **state** parameter indicates the new state of the semi-modal window.| 141 142**Error codes** 143 144For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 145 146| ID| Error Message| 147| -------- | ---------------------------------------- | 148| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 149| 6600101 | Session service exception. | 150 151**Example** 152 153```ts 154import { common } from '@kit.AbilityKit'; 155import { AVCastPickerState } from '@kit.AVSessionKit'; 156 157async function onPickerStateChange(context: common.Context) { 158 let avCastPicker = new avSession.AVCastPickerHelper(context); 159 avCastPicker.on('pickerStateChange', (state: AVCastPickerState) => { 160 console.info(`picker state change : ${state}`); 161 }); 162} 163``` 164 165## off('pickerStateChange')<sup>14+</sup> 166 167off(type: 'pickerStateChange', callback?: Callback<AVCastPickerState\>) : void 168 169Unsubscribes from semi-modal window change events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered. 170 171**Atomic service API**: This API can be used in atomic services since API version 14. 172 173**System capability**: SystemCapability.Multimedia.AVSession.AVCast 174 175**Parameters** 176 177| Name | Type | Mandatory| Description | 178| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------ | 179| type | string | Yes | Event type, which is **'pickerStateChange'** in this case. | 180| callback | Callback\<[AVCastPickerState](js-apis-avCastPickerParam.md#avcastpickerstate11)> | No | Callback function, where the **state** parameter indicates the new state of the semi-modal window.<br>If the unsubscription is successful, **err** is **undefined**; otherwise, **err** is an error object.<br>The **callback** parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. | 181 182**Error codes** 183 184For details about the error codes, see [AVSession Management Error Codes](errorcode-avsession.md). 185 186| ID| Error Message| 187| -------- | ---------------------------------------- | 188| 401 | parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 189| 6600101 | Session service exception. | 190 191**Example** 192 193```ts 194import { common } from '@kit.AbilityKit'; 195 196async function onPickerStateChange(context: common.Context) { 197 let avCastPicker = new avSession.AVCastPickerHelper(context); 198 avCastPicker.off('pickerStateChange'); 199} 200``` 201