1# Using OHAudio for Audio Playback 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 9Specifically, to use APIs for audio playback, import <[native_audiostreambuilder.h](../reference/native-apis/native__audiostreambuilder_8h.md)> and <[native_audiorenderer.h](../reference/native-apis/native__audiorenderer_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 playback: 38 391. Create an audio stream builder. 40 41 ```c++ 42 OH_AudioStreamBuilder* builder; 43 OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RENDERER); 44 ``` 45 462. Set audio stream parameters. 47 48 After creating the builder for audio playback, set the parameters required. 49 50 ```c++ 51 OH_AudioStreamBuilder_SetSamplingRate(builder, rate); 52 OH_AudioStreamBuilder_SetChannelCount(builder, channelCount); 53 OH_AudioStreamBuilder_SetSampleFormat(builder, format); 54 OH_AudioStreamBuilder_SetEncodingType(builder, encodingType); 55 OH_AudioStreamBuilder_SetRendererInfo(builder, usage); 56 ``` 57 58 Note that the audio data to play is written through callbacks. You must call **OH_AudioStreamBuilder_SetRendererCallback** to implement the callbacks. For details about the declaration of the callback functions, see [OH_AudioRenderer_Callbacks](../reference/native-apis/_o_h_audio.md#oh_audiorenderer_callbacks). 59 60 613. Set the callback functions. 62 63 ```c++ 64 OH_AudioStreamBuilder_SetRendererCallback(builder, callbacks, nullptr); 65 ``` 66 674. Create an audio renderer instance. 68 69 ```c++ 70 OH_AudioRenderer* audioRenderer; 71 OH_AudioStreamBuilder_GenerateRenderer(builder, &audioRenderer); 72 ``` 73 745. Use the audio renderer. 75 76 You can use the APIs listed below to control the audio streams. 77 78 | API | Description | 79 | ------------------------------------------------------------ | ------------ | 80 | OH_AudioStream_Result OH_AudioRenderer_Start(OH_AudioRenderer* renderer) | Starts the audio renderer. | 81 | OH_AudioStream_Result OH_AudioRenderer_Pause(OH_AudioRenderer* renderer) | Pauses the audio renderer. | 82 | OH_AudioStream_Result OH_AudioRenderer_Stop(OH_AudioRenderer* renderer) | Stops the audio renderer. | 83 | OH_AudioStream_Result OH_AudioRenderer_Flush(OH_AudioRenderer* renderer) | Flushes written audio data.| 84 | OH_AudioStream_Result OH_AudioRenderer_Release(OH_AudioRenderer* renderer) | Releases the audio renderer 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 94## Setting the Low Latency Mode 95 96If the device supports the low-latency channel, you can use the low-latency mode to create a player for a higher-quality audio experience. 97 98The development process is similar to that in the common playback scenario. The only difference is that you need to set the low delay mode by calling [OH_AudioStreamBuilder_SetLatencyMode()](../reference/native-apis/_o_h_audio.md#oh_audiostreambuilder_setlatencymode) when creating an audio stream builder. 99 100The code snippet is as follows: 101 102```C 103OH_AudioStream_LatencyMode latencyMode = AUDIOSTREAM_LATENCY_MODE_FAST; 104OH_AudioStreamBuilder_SetLatencyMode(builder, latencyMode); 105``` 106