1# Using OHAudio for Audio Recording 2 3**OHAudio** is a set of native APIs introduced in API version 10. These APIs are normalized in design and support both common and low-latency audio channels. 4 5## Prerequisites 6 7To use the playback or recording capability of **OHAudio**, you must first import the corresponding header files. 8 9To use APIs for audio recording, import <[native_audiostreambuilder.h](../reference/native-apis/native__audiostreambuilder_8h.md)> and <[native_audiocapturer.h](../reference/native-apis/native__audiocapturer_8h.md)>. 10 11## Audio Stream Builder 12 13**OHAudio** provides the **OH_AudioStreamBuilder** class, which complies with the builder design pattern and is used to build audio streams. You need to specify [OH_AudioStream_Type](../reference/native-apis/_o_h_audio.md#oh_audiostream_type) based on your service scenarios. 14 15**OH_AudioStream_Type** can be set to either of the following: 16 17- AUDIOSTREAM_TYPE_RENDERER 18- AUDIOSTREAM_TYPE_CAPTURER 19 20The following code snippet shows how to use [OH_AudioStreamBuilder_Create](../reference/native-apis/_o_h_audio.md#oh_audiostreambuilder_create) to create a builder: 21 22``` 23OH_AudioStreamBuilder* builder; 24OH_AudioStreamBuilder_Create(&builder, streamType); 25``` 26 27After the audio service is complete, call [OH_AudioStreamBuilder_Destroy](../reference/native-apis/_o_h_audio.md#oh_audiostreambuilder_destroy) to destroy the builder. 28 29``` 30OH_AudioStreamBuilder_Destroy(builder); 31``` 32 33## How to Develop 34 35Read [OHAudio](../reference/native-apis/_o_h_audio.md) for the API reference. 36 37The following walks you through how to implement simple recording: 38 39 401. Create an audio stream builder. 41 42 ```c++ 43 OH_AudioStreamBuilder* builder; 44 OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_CAPTURER); 45 ``` 46 472. Set audio stream parameters. 48 49 After creating the builder for audio recording, set the parameters required. 50 51 ```c++ 52 OH_AudioStreamBuilder_SetSamplingRate(builder, rate); 53 OH_AudioStreamBuilder_SetChannelCount(builder, channelCount); 54 OH_AudioStreamBuilder_SetSampleFormat(builder, format); 55 OH_AudioStreamBuilder_SetEncodingType(builder, encodingType); 56 OH_AudioStreamBuilder_SetCapturerInfo(builder, sourceType); 57 ``` 58 59 Note that the audio data to record is written through callbacks. You must call **OH_AudioStreamBuilder_SetCapturerCallback** to implement the callbacks. For details about the declaration of the callback functions, see [OH_AudioCapturer_Callbacks](../reference/native-apis/_o_h_audio.md#oh_audiocapturer_callbacks). 60 613. Set the callback functions. 62 63 ```c++ 64 OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr); 65 ``` 66 674. Create an audio capturer instance. 68 69 ```c++ 70 OH_AudioCapturer* audioCapturer; 71 OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer); 72 ``` 73 745. Use the audio capturer. 75 76 You can use the APIs listed below to control the audio streams. 77 78 | API | Description | 79 | ------------------------------------------------------------ | ------------ | 80 | OH_AudioStream_Result OH_AudioCapturer_Start(OH_AudioCapturer* capturer) | Starts the audio capturer. | 81 | OH_AudioStream_Result OH_AudioCapturer_Pause(OH_AudioCapturer* capturer) | Pauses the audio capturer. | 82 | OH_AudioStream_Result OH_AudioCapturer_Stop(OH_AudioCapturer* capturer) | Stops the audio capturer. | 83 | OH_AudioStream_Result OH_AudioCapturer_Flush(OH_AudioCapturer* capturer) | Flushes obtained audio data.| 84 | OH_AudioStream_Result OH_AudioCapturer_Release(OH_AudioCapturer* capturer) | Releases the audio capturer instance.| 85 866. Destroy the audio stream builder. 87 88 When the builder is no longer used, release related resources. 89 90 ```c++ 91 OH_AudioStreamBuilder_Destroy(builder); 92 ``` 93