1# Functions 2 3> **NOTE** 4> 5> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 6 7## Modules to Import 8 9```ts 10import { audio } from '@kit.AudioKit'; 11``` 12 13## audio.getAudioManager 14 15getAudioManager(): AudioManager 16 17Obtains an AudioManager instance. 18 19**System capability**: SystemCapability.Multimedia.Audio.Core 20 21**Return value** 22 23| Type | Description | 24| ----------------------------- | ------------ | 25| [AudioManager](arkts-apis-audio-AudioManager.md) | AudioManager instance.| 26 27**Example** 28```ts 29import { audio } from '@kit.AudioKit'; 30 31let audioManager = audio.getAudioManager(); 32``` 33 34## audio.createAudioRenderer<sup>8+</sup> 35 36createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback\<AudioRenderer>): void 37 38Creates an AudioRenderer instance. This API uses an asynchronous callback to return the result. 39 40**System capability**: SystemCapability.Multimedia.Audio.Renderer 41 42**Parameters** 43 44| Name | Type | Mandatory| Description | 45| -------- | ----------------------------------------------- | ---- | ---------------- | 46| options | [AudioRendererOptions](arkts-apis-audio-i.md#audiorendereroptions8) | Yes | Renderer configurations. | 47| callback | AsyncCallback<[AudioRenderer](arkts-apis-audio-AudioRenderer.md)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the AudioRenderer instance obtained; otherwise, **err** is an error object.| 48 49**Example** 50 51```ts 52import { audio } from '@kit.AudioKit'; 53 54let audioStreamInfo: audio.AudioStreamInfo = { 55 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate. 56 channels: audio.AudioChannel.CHANNEL_2, // Channel. 57 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format. 58 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format. 59}; 60 61let audioRendererInfo: audio.AudioRendererInfo = { 62 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario. 63 rendererFlags: 0 // AudioRenderer flag. 64}; 65 66let audioRendererOptions: audio.AudioRendererOptions = { 67 streamInfo: audioStreamInfo, 68 rendererInfo: audioRendererInfo 69}; 70 71audio.createAudioRenderer(audioRendererOptions,(err, data) => { 72 if (err) { 73 console.error(`AudioRenderer Created: Error: ${err}`); 74 } else { 75 console.info('AudioRenderer Created: SUCCESS'); 76 let audioRenderer = data; 77 } 78}); 79``` 80 81## audio.createAudioRenderer<sup>8+</sup> 82 83createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer\> 84 85Creates an AudioRenderer instance. This API uses a promise to return the result. 86 87**System capability**: SystemCapability.Multimedia.Audio.Renderer 88 89**Parameters** 90 91| Name | Type | Mandatory| Description | 92| :------ | :--------------------------------------------- | :--- | :----------- | 93| options | [AudioRendererOptions](arkts-apis-audio-i.md#audiorendereroptions8) | Yes | Renderer configurations.| 94 95**Return value** 96 97| Type | Description | 98| ----------------------------------------- | ---------------- | 99| Promise<[AudioRenderer](arkts-apis-audio-AudioRenderer.md)> | Promise used to return the AudioRenderer instance.| 100 101**Example** 102 103```ts 104import { audio } from '@kit.AudioKit'; 105import { BusinessError } from '@kit.BasicServicesKit'; 106 107let audioStreamInfo: audio.AudioStreamInfo = { 108 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate. 109 channels: audio.AudioChannel.CHANNEL_2, // Channel. 110 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format. 111 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format. 112}; 113 114let audioRendererInfo: audio.AudioRendererInfo = { 115 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario. 116 rendererFlags: 0 // AudioRenderer flag. 117}; 118 119let audioRendererOptions: audio.AudioRendererOptions = { 120 streamInfo: audioStreamInfo, 121 rendererInfo: audioRendererInfo 122}; 123 124let audioRenderer: audio.AudioRenderer; 125 126audio.createAudioRenderer(audioRendererOptions).then((data) => { 127 audioRenderer = data; 128 console.info('AudioFrameworkRenderLog: AudioRenderer Created : SUCCESS'); 129}).catch((err: BusinessError) => { 130 console.error(`AudioFrameworkRenderLog: AudioRenderer Created : ERROR : ${err}`); 131}); 132``` 133 134## audio.createAudioCapturer<sup>8+</sup> 135 136createAudioCapturer(options: AudioCapturerOptions, callback: AsyncCallback<AudioCapturer\>): void 137 138Creates an AudioCapturer instance. This API uses an asynchronous callback to return the result. 139 140**System capability**: SystemCapability.Multimedia.Audio.Capturer 141 142**Required permissions**: ohos.permission.MICROPHONE 143 144This permission is required when [SourceType](arkts-apis-audio-e.md#sourcetype8) is set to **SOURCE_TYPE_MIC**, **SOURCE_TYPE_VOICE_RECOGNITION**, **SOURCE_TYPE_VOICE_COMMUNICATION**, **SOURCE_TYPE_VOICE_MESSAGE**, or **SOURCE_TYPE_CAMCORDER**. 145 146**Parameters** 147 148| Name | Type | Mandatory| Description | 149| :------- | :---------------------------------------------- | :--- | :--------------- | 150| options | [AudioCapturerOptions](arkts-apis-audio-i.md#audiocaptureroptions8) | Yes | Capturer configurations.| 151| callback | AsyncCallback<[AudioCapturer](arkts-apis-audio-AudioCapturer.md)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the AudioCapturer instance obtained; otherwise, **err** is an error object. If the operation fails, an error object with one of the following error codes is returned:<br>Error code 6800301: indicates a parameter verification exception, permission verification exception, or system processing exception. For details, see system logs.<br>Error code 6800101: indicates that a mandatory parameter is null or the parameter type is incorrect.| 152 153**Example** 154 155```ts 156import { audio } from '@kit.AudioKit'; 157 158let audioStreamInfo: audio.AudioStreamInfo = { 159 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate. 160 channels: audio.AudioChannel.CHANNEL_2, // Channel. 161 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format. 162 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format. 163}; 164 165let audioCapturerInfo: audio.AudioCapturerInfo = { 166 source: audio.SourceType.SOURCE_TYPE_MIC, // Audio source type: microphone. Set this parameter based on the service scenario. 167 capturerFlags: 0 // AudioCapturer flag. 168}; 169 170let audioCapturerOptions: audio.AudioCapturerOptions = { 171 streamInfo: audioStreamInfo, 172 capturerInfo: audioCapturerInfo 173}; 174 175audio.createAudioCapturer(audioCapturerOptions, (err, data) => { 176 if (err) { 177 console.error(`AudioCapturer Created : Error: ${err}`); 178 } else { 179 console.info('AudioCapturer Created : SUCCESS'); 180 let audioCapturer = data; 181 } 182}); 183``` 184 185## audio.createAudioCapturer<sup>8+</sup> 186 187createAudioCapturer(options: AudioCapturerOptions): Promise<AudioCapturer\> 188 189Creates an AudioCapturer instance. This API uses a promise to return the result. 190 191**System capability**: SystemCapability.Multimedia.Audio.Capturer 192 193**Required permissions**: ohos.permission.MICROPHONE 194 195This permission is required when [SourceType](arkts-apis-audio-e.md#sourcetype8) is set to **SOURCE_TYPE_MIC**, **SOURCE_TYPE_VOICE_RECOGNITION**, **SOURCE_TYPE_VOICE_COMMUNICATION**, **SOURCE_TYPE_VOICE_MESSAGE**, or **SOURCE_TYPE_CAMCORDER**. 196 197**Parameters** 198 199| Name | Type | Mandatory| Description | 200| :------ | :--------------------------------------------- | :--- | :--------------- | 201| options | [AudioCapturerOptions](arkts-apis-audio-i.md#audiocaptureroptions8) | Yes | Capturer configurations.| 202 203**Return value** 204 205| Type | Description | 206| ----------------------------------------- |----------------------| 207| Promise<[AudioCapturer](arkts-apis-audio-AudioCapturer.md)> | Promise used to return the result. If the operation is successful, an AudioCapturer instance is returned; otherwise, an error object with either of the following error codes is returned:<br>Error code 6800301: indicates a parameter verification exception, permission verification exception, or system processing exception. For details, see system logs.<br>Error code 6800101: indicates that a mandatory parameter is null or the parameter type is incorrect.| 208 209**Example** 210 211```ts 212import { audio } from '@kit.AudioKit'; 213import { BusinessError } from '@kit.BasicServicesKit'; 214 215let audioStreamInfo: audio.AudioStreamInfo = { 216 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate. 217 channels: audio.AudioChannel.CHANNEL_2, // Channel. 218 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format. 219 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format. 220}; 221 222let audioCapturerInfo: audio.AudioCapturerInfo = { 223 source: audio.SourceType.SOURCE_TYPE_MIC, // Audio source type: microphone. Set this parameter based on the service scenario. 224 capturerFlags: 0 // AudioCapturer flag. 225}; 226 227let audioCapturerOptions:audio.AudioCapturerOptions = { 228 streamInfo: audioStreamInfo, 229 capturerInfo: audioCapturerInfo 230}; 231 232let audioCapturer: audio.AudioCapturer; 233 234audio.createAudioCapturer(audioCapturerOptions).then((data) => { 235 audioCapturer = data; 236 console.info('AudioCapturer Created : SUCCESS'); 237}).catch((err: BusinessError) => { 238 console.error(`AudioCapturer Created : ERROR : ${err}`); 239}); 240``` 241 242## audio.createAudioLoopback<sup>20+</sup> 243 244createAudioLoopback(mode: AudioLoopbackMode): Promise<AudioLoopback\> 245 246Creates an AudioLoopback instance. This API uses a promise to return the result. 247 248Before using **createAudioLoopback**, call [isAudioLoopbackSupported](arkts-apis-audio-AudioStreamManager.md#isaudioloopbacksupported20) to check whether the system supports the audio loopback mode. 249 250 251**System capability**: SystemCapability.Multimedia.Audio.Capturer 252 253**Required permissions**: ohos.permission.MICROPHONE 254 255**Parameters** 256 257| Name | Type | Mandatory| Description | 258| :------ | :--------------------------------------------- | :--- | :--------------- | 259| mode | [AudioLoopbackMode](arkts-apis-audio-e.md#audioloopbackmode20) | Yes | Audio loopback mode.| 260 261**Return value** 262 263| Type | Description | 264| ----------------------------------------- |----------------------| 265| Promise<[AudioLoopback](arkts-apis-audio-AudioLoopback.md)> | Promise used to return the result. If the operation is successful, an AudioLoopback instance is returned; otherwise, an error object is returned.| 266 267**Error codes** 268 269For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 270 271| ID| Error Message| 272| ------- | -------------------------------| 273| 201 | Permission denied. | 274| 801 | Unsupported API. | 275| 6800101 | Parameter verification failed. | 276| 6800104 | Loopback mode is unsupported. | 277 278**Example** 279 280```ts 281import { audio } from '@kit.AudioKit'; 282import { BusinessError } from '@kit.BasicServicesKit'; 283 284let audioLoopback: audio.AudioLoopback; 285 286audio.createAudioLoopback(audio.AudioLoopbackMode.HARDWARE).then((data) => { 287 audioLoopback = data; 288 console.info('AudioLoopback Created : SUCCESS'); 289}).catch((err: BusinessError) => { 290 console.error(`AudioLoopback Created : ERROR : ${err}`); 291}); 292``` 293