1e41f4b71Sopenharmony_ci# Using OHAudio for Audio Recording (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci**OHAudio** is a set of C APIs introduced in API version 10. These APIs are normalized in design and support both common and low-latency audio channels. They support the PCM format only. They are suitable for playback applications that implement audio input at the native layer. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## Prerequisites 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciTo use the playback or recording capability of **OHAudio**, you must first import the corresponding header files. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Linking the Dynamic Library in the CMake Script 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci``` cmake 12e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libohaudio.so) 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci### Adding Header Files 15e41f4b71Sopenharmony_ciTo use APIs for audio recording, import <[native_audiostreambuilder.h](../../reference/apis-audio-kit/native__audiostreambuilder_8h.md)> and <[native_audiocapturer.h](../../reference/apis-audio-kit/native__audiocapturer_8h.md)>. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci```cpp 18e41f4b71Sopenharmony_ci#include <ohaudio/native_audiocapturer.h> 19e41f4b71Sopenharmony_ci#include <ohaudio/native_audiostreambuilder.h> 20e41f4b71Sopenharmony_ci``` 21e41f4b71Sopenharmony_ci## Audio Stream Builder 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**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/apis-audio-kit/_o_h_audio.md#oh_audiostream_type) based on your service scenarios. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**OH_AudioStream_Type** can be set to either of the following: 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- AUDIOSTREAM_TYPE_RENDERER 28e41f4b71Sopenharmony_ci- AUDIOSTREAM_TYPE_CAPTURER 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ciThe following code snippet shows how to use [OH_AudioStreamBuilder_Create](../../reference/apis-audio-kit/_o_h_audio.md#oh_audiostreambuilder_create) to create a builder: 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci``` 33e41f4b71Sopenharmony_ciOH_AudioStreamBuilder* builder; 34e41f4b71Sopenharmony_ciOH_AudioStreamBuilder_Create(&builder, streamType); 35e41f4b71Sopenharmony_ci``` 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciAfter the audio service is complete, call [OH_AudioStreamBuilder_Destroy](../../reference/apis-audio-kit/_o_h_audio.md#oh_audiostreambuilder_destroy) to destroy the builder. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci``` 40e41f4b71Sopenharmony_ciOH_AudioStreamBuilder_Destroy(builder); 41e41f4b71Sopenharmony_ci``` 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci## How to Develop 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ciRead [OHAudio](../../reference/apis-audio-kit/_o_h_audio.md) for the API reference. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciThe following walks you through how to implement simple recording: 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci1. Create an audio stream builder. 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci ```c++ 53e41f4b71Sopenharmony_ci OH_AudioStreamBuilder* builder; 54e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_CAPTURER); 55e41f4b71Sopenharmony_ci ``` 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci2. Set audio stream parameters. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci After creating the builder for audio recording, set the parameters required. 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci ```c++ 62e41f4b71Sopenharmony_ci // Set the audio sampling rate. 63e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_SetSamplingRate(builder, 48000); 64e41f4b71Sopenharmony_ci // Set the number of audio channels. 65e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_SetChannelCount(builder, 2); 66e41f4b71Sopenharmony_ci // Set the audio sampling format. 67e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_SetSampleFormat(builder, AUDIOSTREAM_SAMPLE_S16LE); 68e41f4b71Sopenharmony_ci // Set the encoding type of the audio stream. 69e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_SetEncodingType(builder, AUDIOSTREAM_ENCODING_TYPE_RAW); 70e41f4b71Sopenharmony_ci // Set the usage scenario of the audio capturer. 71e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_SetCapturerInfo(builder, AUDIOSTREAM_SOURCE_TYPE_MIC); 72e41f4b71Sopenharmony_ci ``` 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci 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/apis-audio-kit/_o_h_audio.md#oh_audiocapturer_callbacks). 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci3. Set the callback functions. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci For details about concurrent processing of multiple audio streams, see [Processing Audio Interruption Events](audio-playback-concurrency.md). The procedure is similar, and the only difference is the API programming language in use. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci ```c++ 81e41f4b71Sopenharmony_ci // Customize a data writing function. 82e41f4b71Sopenharmony_ci int32_t MyOnReadData( 83e41f4b71Sopenharmony_ci OH_AudioCapturer* capturer, 84e41f4b71Sopenharmony_ci void* userData, 85e41f4b71Sopenharmony_ci void* buffer, 86e41f4b71Sopenharmony_ci int32_t length) 87e41f4b71Sopenharmony_ci { 88e41f4b71Sopenharmony_ci // Obtain the recording data of the specified length from the buffer. 89e41f4b71Sopenharmony_ci return 0; 90e41f4b71Sopenharmony_ci } 91e41f4b71Sopenharmony_ci // Customize an audio stream event function. 92e41f4b71Sopenharmony_ci int32_t MyOnStreamEvent( 93e41f4b71Sopenharmony_ci OH_AudioCapturer* capturer, 94e41f4b71Sopenharmony_ci void* userData, 95e41f4b71Sopenharmony_ci OH_AudioStream_Event event) 96e41f4b71Sopenharmony_ci { 97e41f4b71Sopenharmony_ci // Update the player status and UI based on the audio stream event information indicated by the event. 98e41f4b71Sopenharmony_ci return 0; 99e41f4b71Sopenharmony_ci } 100e41f4b71Sopenharmony_ci // Customize an audio interruption event function. 101e41f4b71Sopenharmony_ci int32_t MyOnInterruptEvent( 102e41f4b71Sopenharmony_ci OH_AudioCapturer* capturer, 103e41f4b71Sopenharmony_ci void* userData, 104e41f4b71Sopenharmony_ci OH_AudioInterrupt_ForceType type, 105e41f4b71Sopenharmony_ci OH_AudioInterrupt_Hint hint) 106e41f4b71Sopenharmony_ci { 107e41f4b71Sopenharmony_ci // Update the capturer status and UI based on the audio interruption information indicated by type and hint. 108e41f4b71Sopenharmony_ci return 0; 109e41f4b71Sopenharmony_ci } 110e41f4b71Sopenharmony_ci // Customize an exception callback function. 111e41f4b71Sopenharmony_ci int32_t MyOnError( 112e41f4b71Sopenharmony_ci OH_AudioCapturer* capturer, 113e41f4b71Sopenharmony_ci void* userData, 114e41f4b71Sopenharmony_ci OH_AudioStream_Result error) 115e41f4b71Sopenharmony_ci { 116e41f4b71Sopenharmony_ci // Perform operations based on the audio exception information indicated by error. 117e41f4b71Sopenharmony_ci return 0; 118e41f4b71Sopenharmony_ci } 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci OH_AudioCapturer_Callbacks callbacks; 121e41f4b71Sopenharmony_ci // Set the callbacks. 122e41f4b71Sopenharmony_ci callbacks.OH_AudioCapturer_OnReadData = MyOnReadData; 123e41f4b71Sopenharmony_ci callbacks.OH_AudioCapturer_OnStreamEvent = MyOnStreamEvent; 124e41f4b71Sopenharmony_ci callbacks.OH_AudioCapturer_OnInterruptEvent = MyOnInterruptEvent; 125e41f4b71Sopenharmony_ci callbacks.OH_AudioCapturer_OnError = MyOnError; 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci // Set the callbacks for audio input streams. 128e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr); 129e41f4b71Sopenharmony_ci ``` 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci To avoid unexpected behavior, ensure that each callback of [OH_AudioCapturer_Callbacks](../../reference/apis-audio-kit/_o_h_audio.md#oh_audiocapturer_callbacks) is initialized by a custom callback method or null pointer when being set. 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci ```c++ 134e41f4b71Sopenharmony_ci // Customize a data writing function. 135e41f4b71Sopenharmony_ci int32_t MyOnReadData( 136e41f4b71Sopenharmony_ci OH_AudioCapturer* capturer, 137e41f4b71Sopenharmony_ci void* userData, 138e41f4b71Sopenharmony_ci void* buffer, 139e41f4b71Sopenharmony_ci int32_t length) 140e41f4b71Sopenharmony_ci { 141e41f4b71Sopenharmony_ci // Obtain the recording data of the specified length from the buffer. 142e41f4b71Sopenharmony_ci return 0; 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci // Customize an audio interruption event function. 145e41f4b71Sopenharmony_ci int32_t MyOnInterruptEvent( 146e41f4b71Sopenharmony_ci OH_AudioCapturer* capturer, 147e41f4b71Sopenharmony_ci void* userData, 148e41f4b71Sopenharmony_ci OH_AudioInterrupt_ForceType type, 149e41f4b71Sopenharmony_ci OH_AudioInterrupt_Hint hint) 150e41f4b71Sopenharmony_ci { 151e41f4b71Sopenharmony_ci // Update the capturer status and UI based on the audio interruption information indicated by type and hint. 152e41f4b71Sopenharmony_ci return 0; 153e41f4b71Sopenharmony_ci } 154e41f4b71Sopenharmony_ci OH_AudioCapturer_Callbacks callbacks; 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci // Configure a callback function. If listening is required, assign a value. 157e41f4b71Sopenharmony_ci callbacks.OH_AudioCapturer_OnReadData = MyOnReadData; 158e41f4b71Sopenharmony_ci callbacks.OH_AudioCapturer_OnInterruptEvent = MyOnInterruptEvent; 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci // (Mandatory) If listening is not required, use a null pointer for initialization. 161e41f4b71Sopenharmony_ci callbacks.OH_AudioCapturer_OnStreamEvent = nullptr; 162e41f4b71Sopenharmony_ci callbacks.OH_AudioCapturer_OnError = nullptr; 163e41f4b71Sopenharmony_ci ``` 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci4. Create an audio capturer instance. 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci ```c++ 168e41f4b71Sopenharmony_ci OH_AudioCapturer* audioCapturer; 169e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer); 170e41f4b71Sopenharmony_ci ``` 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci5. Use the audio capturer. 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci You can use the APIs listed below to control the audio streams. 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci | API | Description | 177e41f4b71Sopenharmony_ci | ------------------------------------------------------------ | ------------ | 178e41f4b71Sopenharmony_ci | OH_AudioStream_Result OH_AudioCapturer_Start(OH_AudioCapturer* capturer) | Starts the audio capturer. | 179e41f4b71Sopenharmony_ci | OH_AudioStream_Result OH_AudioCapturer_Pause(OH_AudioCapturer* capturer) | Pauses the audio capturer. | 180e41f4b71Sopenharmony_ci | OH_AudioStream_Result OH_AudioCapturer_Stop(OH_AudioCapturer* capturer) | Stops the audio capturer. | 181e41f4b71Sopenharmony_ci | OH_AudioStream_Result OH_AudioCapturer_Flush(OH_AudioCapturer* capturer) | Flushes obtained audio data.| 182e41f4b71Sopenharmony_ci | OH_AudioStream_Result OH_AudioCapturer_Release(OH_AudioCapturer* capturer) | Releases the audio capturer instance.| 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci6. Destroy the audio stream builder. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci When the builder is no longer used, release related resources. 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci ```c++ 189e41f4b71Sopenharmony_ci OH_AudioStreamBuilder_Destroy(builder); 190e41f4b71Sopenharmony_ci ``` 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci## Setting the Low Latency Mode 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ciIf the device supports the low-latency channel, you can use the low-latency mode to create an audio capturer for a higher-quality audio experience. 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ciThe development process is similar to that in the common recording scenario. The only difference is that you need to set the low delay mode by calling [OH_AudioStreamBuilder_SetLatencyMode()](../../reference/apis-audio-kit/_o_h_audio.md#oh_audiostreambuilder_setlatencymode) when creating an audio stream builder. 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ciCode snippet: 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci```C 201e41f4b71Sopenharmony_ciOH_AudioStream_LatencyMode latencyMode = AUDIOSTREAM_LATENCY_MODE_FAST; 202e41f4b71Sopenharmony_ciOH_AudioStreamBuilder_SetLatencyMode(builder, latencyMode); 203e41f4b71Sopenharmony_ci``` 204