diff --git a/zh-cn/application-dev/media/using-ohaudio-for-playback.md b/zh-cn/application-dev/media/using-ohaudio-for-playback.md index f1d1689dcd85012801fd24a3148aa32d6361b772..68ef6f8df5636a2cdcf2092c4c95d1a49aeb2a87 100644 --- a/zh-cn/application-dev/media/using-ohaudio-for-playback.md +++ b/zh-cn/application-dev/media/using-ohaudio-for-playback.md @@ -14,7 +14,7 @@ OHAudio提供OH_AudioStreamBuilder接口,遵循构造器设计模式,用于 `OH_AudioStream_Type`包含两种类型: -- AUDIOSTREAM_TYPE_RERNDERER +- AUDIOSTREAM_TYPE_RENDERER - AUDIOSTREAM_TYPE_CAPTURER 使用[OH_AudioStreamBuilder_Create](../reference/native-apis/_o_h_audio.md#oh_audiostreambuilder_create)创建构造器示例: @@ -40,7 +40,7 @@ OH_AudioStreamBuilder_Destroy(builder); ```c++ OH_AudioStreamBuilder* builder; - OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RERNDERER); + OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_RENDERER); ``` 2. 配置音频流参数 @@ -55,21 +55,8 @@ OH_AudioStreamBuilder_Destroy(builder); OH_AudioStreamBuilder_SetRendererInfo(builder, usage, content); ``` - 注意,播放的音频数据要通过回调接口写入,开发者要实现回调接口,使用`OH_AudioStreamBuilder_SetRendererCallback`设置回调函数。回调函数接口声明如下。 + 注意,播放的音频数据要通过回调接口写入,开发者要实现回调接口,使用`OH_AudioStreamBuilder_SetRendererCallback`设置回调函数。回调函数的声明请查看[OH_AudioRenderer_Callbacks](../reference/native-apis/_o_h_audio.md#oh_audiorenderer_callbacks) 。 - ```c++ - typedef struct OH_AudioRenderer_Callbacks_Struct { - /** - * This function pointer will point to the callback function that - * is used to write audio data - */ - int32_t (*OH_AudioRenderer_OnWriteData)( - OH_AudioRenderer* renderer, - void* userData, - void* buffer, - int32_t lenth); - } OH_AudioRenderer_Callbacks; - ``` 3. 设置回调函数 @@ -96,131 +83,10 @@ OH_AudioStreamBuilder_Destroy(builder); | OH_AudioStream_Result OH_AudioRenderer_Flush(OH_AudioRenderer* renderer) | 释放缓存数据 | | OH_AudioStream_Result OH_AudioRenderer_Release(OH_AudioRenderer* renderer) | 释放播放实例 | -## 完整示例 - -参考以下示例,完成一次播放音频的完整流程。 - -```c++ -#include -#include -#include -#include -#include -#include -#include "common/native_audiostreambuilder.h" -#include "native_audiorenderer.h" - -#ifdef __cplusplus -extern "C" { -#endif -namespace AudioTestConstants { - constexpr int32_t FIRST_ARG_IDX = 1; - constexpr int32_t SECOND_ARG_IDX = 2; - constexpr int32_t THIRD_ARG_IDX = 3; - constexpr int32_t WAIT_INTERVAL = 1000; -} - -std::string g_filePath = "/data/data/oh_test_audio.pcm"; -FILE* g_file = nullptr; -bool g_readEnd = false; -int32_t g_samplingRate = 48000; -int32_t g_channelCount = 2; - -static int32_t AudioRendererOnWriteData(OH_AudioRenderer* capturer, - void* userData, - void* buffer, - int32_t bufferLen) -{ - size_t readCount = fread(buffer, bufferLen, 1, g_file); - if (!readCount) { - g_readEnd = true; - if (ferror(g_file)) { - printf("Error reading file"); - } else if (feof(g_file)) { - printf("EOF found"); - } - } - - return 0; -} - -void PlayerTest(char *argv[]) -{ - OH_AudioStream_Result ret; - - // 1. create builder - OH_AudioStreamBuilder* builder; - OH_AudioStream_Type type = AUDIOSTREAM_TYPE_RERNDERER; - ret = OH_AudioStreamBuilder_Create(&builder, type); - printf("createcallback ret: %d \n", ret); - - // 2. set params and callbacks - OH_AudioStreamBuilder_SetSamplingRate(builder, g_samplingRate); - OH_AudioStreamBuilder_SetChannelCount(builder, g_channelCount); +6. 释放构造器 - OH_AudioRenderer_Callbacks callbacks; - callbacks.OH_AudioRenderer_OnWriteData = AudioRendererOnWriteData; - ret = OH_AudioStreamBuilder_SetRendererCallback(builder, callbacks, nullptr); - printf("setcallback ret: %d \n", ret); +构造器不在使用时,需要释放它。 - // 3. create OH_AudioRenderer - OH_AudioRenderer* audioRenderer; - ret = OH_AudioStreamBuilder_GenerateRenderer(builder, &audioRenderer); - printf("create renderer client, ret: %d \n", ret); - - // 4. start - ret = OH_AudioRenderer_Start(audioRenderer); - printf("start ret: %d \n", ret); - - int timer = 0; - while (!g_readEnd) { - std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::WAIT_INTERVAL)); - printf("Wait for the audio to finish playing.(..%d s)\n", ++timer); - } - - // 5. stop and release client - ret = OH_AudioRenderer_Stop(audioRenderer); - printf("stop ret: %d \n", ret); - ret = OH_AudioRenderer_Release(audioRenderer); - printf("release ret: %d \n", ret); - - // 6. destroy the builder - ret = OH_AudioStreamBuilder_Destroy(builder); - printf("destroy builder ret: %d \n", ret); -} - -int main(int argc, char *argv[]) -{ - printf("start \n"); - if ((argv == nullptr) || (argc < AudioTestConstants::THIRD_ARG_IDX)) { - printf("input parms wrong. input format: filePath samplingRate channelCount \n"); - printf("input demo: ./oh_audio_renderer_test ./oh_test_audio.pcm 48000 2 \n"); - return 0; - } - printf("argc=%d ", argc); - printf("argv[1]=%s ", argv[AudioTestConstants::FIRST_ARG_IDX]); - printf("argv[2]=%s ", argv[AudioTestConstants::SECOND_ARG_IDX]); - printf("argv[3]=%s \n", argv[AudioTestConstants::THIRD_ARG_IDX]); - - g_samplingRate = atoi(argv[AudioTestConstants::SECOND_ARG_IDX]); - g_channelCount = atoi(argv[AudioTestConstants::THIRD_ARG_IDX]); - g_filePath = argv[AudioTestConstants::FIRST_ARG_IDX]; - printf("filePATH: %s \n", g_filePath.c_str()); - - g_file = fopen(g_filePath.c_str(), "rb"); - if (g_file == nullptr) { - printf("OHAudioRendererTest: Unable to open file \n"); - return 0; - } - - PlayerTest(argv); - - fclose(g_file); - g_file = nullptr; - return 0; -} - -#ifdef __cplusplus -} -#endif -``` + ```c++ + OH_AudioStreamBuilder_Destroy(builder); + ``` diff --git a/zh-cn/application-dev/media/using-ohaudio-for-recording.md b/zh-cn/application-dev/media/using-ohaudio-for-recording.md index 263835800b0876cac31690a4afb9b968ef1aa351..de58eb97cf990463a71bcbf874ebcb4711a9c6e4 100644 --- a/zh-cn/application-dev/media/using-ohaudio-for-recording.md +++ b/zh-cn/application-dev/media/using-ohaudio-for-recording.md @@ -14,7 +14,7 @@ OHAudio提供OH_AudioStreamBuilder接口,遵循构造器设计模式,用于 `OH_AudioStream_Type`包含两种类型: -- AUDIOSTREAM_TYPE_RERNDERER +- AUDIOSTREAM_TYPE_RENDERER - AUDIOSTREAM_TYPE_CAPTURER 使用[OH_AudioStreamBuilder_Create](../reference/native-apis/_o_h_audio.md#oh_audiostreambuilder_create)创建构造器示例: @@ -56,21 +56,7 @@ OH_AudioStreamBuilder_Destroy(builder); OH_AudioStreamBuilder_SetCapturerInfo(builder, sourceType); ``` - 同样,音频录制的音频数据要通过回调接口写入,开发者要实现回调接口,使用`OH_AudioStreamBuilder_SetCapturerCallback`设置回调函数。回调函数接口声明如下。 - - ```c++ - typedef struct OH_AudioCapturer_Callbacks_Struct { - /** - * This function pointer will point to the callback function that - * is used to read audio data - */ - int32_t (*OH_AudioCapturer_OnReadData)( - OH_AudioCapturer* capturer, - void* userData, - void* buffer, - int32_t lenth); - } OH_AudioCapturer_Callbacks; - ``` + 同样,音频录制的音频数据要通过回调接口写入,开发者要实现回调接口,使用`OH_AudioStreamBuilder_SetCapturerCallback`设置回调函数。回调函数的声明请查看[OH_AudioCapturer_Callbacks](../reference/native-apis/_o_h_audio.md#oh_audiocapturer_callbacks) 。 3. 设置音频回调函数 @@ -97,137 +83,10 @@ OH_AudioStreamBuilder_Destroy(builder); | OH_AudioStream_Result OH_AudioCapturer_Flush(OH_AudioCapturer* capturer) | 释放缓存数据 | | OH_AudioStream_Result OH_AudioCapturer_Release(OH_AudioCapturer* capturer) | 释放录制实例 | -## 完整示例 - -参考以下示例,完成一次录制`10s`时长音频的完整流程。 - -``` -#include -#include -#include -#include -#include "native_audiostreambuilder.h" -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif -namespace AudioTestConstants { - constexpr int32_t FIRST_ARG_IDX = 1; - constexpr int32_t SECOND_ARG_IDX = 2; - constexpr int32_t THIRD_ARG_IDX = 3; - constexpr int32_t RECODER_TIME = 10000; - constexpr int32_t COUNTDOWN_INTERVAL = 1000; - constexpr int32_t CONVERT_RATE = 1000; -} - -std::string g_filePath = "/data/data/oh_test_audio.pcm"; -FILE* g_file = nullptr; -int32_t g_samplingRate = 48000; -int32_t g_channelCount = 2; - -static int32_t AudioCapturerOnReadData(OH_AudioCapturer* capturer, - void* userData, - void* buffer, - int32_t bufferLen) -{ - size_t count = 1; - if (fwrite(buffer, bufferLen, count, g_file) != count) { - printf("buffer fwrite err"); - } - - return 0; -} - -void SleepWaitRecoder(bool* stop) -{ - std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::RECODER_TIME)); - *stop = true; -} - -void RecorderTest(char *argv[]) -{ - OH_AudioStream_Result ret; - - // 1. create builder - OH_AudioStreamBuilder* builder; - OH_AudioStream_Type type = AUDIOSTREAM_TYPE_CAPTURER; - ret = OH_AudioStreamBuilder_Create(&builder, type); - printf("create builder: %d \n", ret); +6. 释放构造器 - // 2. set params and callbacks - OH_AudioStreamBuilder_SetSamplingRate(builder, g_samplingRate); - OH_AudioStreamBuilder_SetChannelCount(builder, g_channelCount); - - OH_AudioCapturer_Callbacks callbacks; - callbacks.OH_AudioCapturer_OnReadData = AudioCapturerOnReadData; - ret = OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr); - printf("setcallback: %d \n", ret); - - // 3. create OH_AudioCapturer - OH_AudioCapturer* audioCapturer; - ret = OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer); - printf("create capturer client, ret: %d \n", ret); - - // 4. start - ret = OH_AudioCapturer_Start(audioCapturer); - printf("start ret: %d \n", ret); - - bool stop = false; - std::thread stopthread(SleepWaitRecoder, &stop); - stopthread.detach(); - - int timeLeft = AudioTestConstants::RECODER_TIME; - while (!stop) { - printf("Recording audio is in the countdown ... %d s \n", timeLeft / AudioTestConstants::CONVERT_RATE); - std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::COUNTDOWN_INTERVAL)); - timeLeft = timeLeft - AudioTestConstants::COUNTDOWN_INTERVAL; - } - - // 5. stop and release client - ret = OH_AudioCapturer_Stop(audioCapturer); - printf("stop ret: %d \n", ret); - ret = OH_AudioCapturer_Release(audioCapturer); - printf("release ret: %d \n", ret); - - // 6. destroy the builder - ret = OH_AudioStreamBuilder_Destroy(builder); - printf("destroy builder ret: %d \n", ret); -} - - -int main(int argc, char *argv[]) -{ - if ((argv == nullptr) || (argc <= AudioTestConstants::THIRD_ARG_IDX)) { - printf("input parms wrong. input format: samplingRate channelCount \n"); - printf("input demo: ./oh_audio_capturer_test 48000 2 \n"); - return 0; - } - - printf("argc=%d ", argc); - printf("argv[1]=%s ", argv[AudioTestConstants::FIRST_ARG_IDX]); - printf("argv[2]=%s ", argv[AudioTestConstants::SECOND_ARG_IDX]); - printf("argv[3]=%s \n", argv[AudioTestConstants::THIRD_ARG_IDX]); - - g_samplingRate = atoi(argv[AudioTestConstants::FIRST_ARG_IDX]); - g_channelCount = atoi(argv[AudioTestConstants::SECOND_ARG_IDX]); - - g_file = fopen(g_filePath.c_str(), "wb"); - if (g_file == nullptr) { - printf("OHAudioCapturerTest: Unable to open file \n"); - return 0; - } - - RecorderTest(argv); - - fclose(g_file); - g_file = nullptr; - return 0; -} -#ifdef __cplusplus -} -#endif -``` +构造器不在使用时,需要释放它。 + ```c++ + OH_AudioStreamBuilder_Destroy(builder); + ``` diff --git a/zh-cn/application-dev/reference/native-apis/_o_h_audio.md b/zh-cn/application-dev/reference/native-apis/_o_h_audio.md index 8491a7eab333825716499c9c3f3e054067d2a469..ebaf65e55f827b4f6c438a6646cc98a414d0cc52 100644 --- a/zh-cn/application-dev/reference/native-apis/_o_h_audio.md +++ b/zh-cn/application-dev/reference/native-apis/_o_h_audio.md @@ -49,7 +49,7 @@ | 名称 | 描述 | | -------- | -------- | | [OH_AudioStream_Result](#oh_audiostream_result) { AUDIOSTREAM_SUCCESS, AUDIOSTREAM_ERROR_INVALID_PARAM, AUDIOSTREAM_ERROR_ILLEGAL_STATE, AUDIOSTREAM_ERROR_SYSTEM } | 音频错误码。 | -| [OH_AudioStream_Type](#oh_audiostream_type) { AUDIOSTREAM_TYPE_RERNDERER = 1, AUDIOSTREAM_TYPE_CAPTURER = 2 } | 音频流类型。 | +| [OH_AudioStream_Type](#oh_audiostream_type) { AUDIOSTREAM_TYPE_RENDERER = 1, AUDIOSTREAM_TYPE_CAPTURER = 2 } | 音频流类型。 | | [OH_AudioStream_SampleFormat](#oh_audiostream_sampleformat) { AUDIOSTREAM_SAMPLE_U8 = 0, AUDIOSTREAM_SAMPLE_S16LE = 1, AUDIOSTREAM_SAMPLE_S24LE = 2, AUDIOSTREAM_SAMPLE_S32LE = 3 } | 定义音频流采样格式。 | | [OH_AudioStream_EncodingType](#oh_audiostream_encodingtype) { **AUDIOSTREAM_ENCODING_TYPE_RAW** = 0 } | 定义音频流编码类型。 | | [OH_AudioStream_Usage](#oh_audiostream_usage) { **AUDIOSTREAM_USAGE_UNKNOWN** = 0, **AUDIOSTREAM_USAGE_MUSIC** = 1, **AUDIOSTREAM_USAGE_COMMUNICATION** = 2, **AUDIOSTREAM_USAGE_GAME** = 11 } | 定义音频流使用场景。 | @@ -380,7 +380,7 @@ enum OH_AudioStream_Type | 枚举值 | 描述 | | -------- | -------- | -| AUDIOSTREAM_TYPE_RERNDERER | 该类型代表音频流是输出流。 | +| AUDIOSTREAM_TYPE_RENDERER | 该类型代表音频流是输出流。 | | AUDIOSTREAM_TYPE_CAPTURER | 该类型代表音频流是输入流。 | @@ -1126,7 +1126,7 @@ OH_AudioStream_Result OH_AudioStreamBuilder_Create (OH_AudioStreamBuilder ** bui | 名称 | 描述 | | -------- | -------- | | builder | 该引用指向创建的构造器的结果。 | -| type | 构造器的流类型。AUDIOSTREAM_TYPE_RERNDERER or AUDIOSTREAM_TYPE_CAPTURER | +| type | 构造器的流类型。AUDIOSTREAM_TYPE_RENDERER or AUDIOSTREAM_TYPE_CAPTURER | **返回:** diff --git a/zh-cn/application-dev/reference/native-apis/native__audiostream__base_8h.md b/zh-cn/application-dev/reference/native-apis/native__audiostream__base_8h.md index 4f8427f524af0805d2729b367e4b23a5f2e65975..abd1a4ea6dd8f4fbb32a50caaf4d34fe97654017 100644 --- a/zh-cn/application-dev/reference/native-apis/native__audiostream__base_8h.md +++ b/zh-cn/application-dev/reference/native-apis/native__audiostream__base_8h.md @@ -43,7 +43,7 @@ | 名称 | 描述 | | -------- | -------- | | [OH_AudioStream_Result](_o_h_audio.md#oh_audiostream_result) { [AUDIOSTREAM_SUCCESS](_o_h_audio.md), [AUDIOSTREAM_ERROR_INVALID_PARAM](_o_h_audio.md), [AUDIOSTREAM_ERROR_ILLEGAL_STATE](_o_h_audio.md), [AUDIOSTREAM_ERROR_SYSTEM](_o_h_audio.md) } | 音频错误码。 | -| [OH_AudioStream_Type](_o_h_audio.md#oh_audiostream_type) { [AUDIOSTREAM_TYPE_RERNDERER](_o_h_audio.md) = 1, [AUDIOSTREAM_TYPE_CAPTURER](_o_h_audio.md) = 2 } | 音频流类型。 | +| [OH_AudioStream_Type](_o_h_audio.md#oh_audiostream_type) { [AUDIOSTREAM_TYPE_RENDERER](_o_h_audio.md) = 1, [AUDIOSTREAM_TYPE_CAPTURER](_o_h_audio.md) = 2 } | 音频流类型。 | | [OH_AudioStream_SampleFormat](_o_h_audio.md#oh_audiostream_sampleformat) { [AUDIOSTREAM_SAMPLE_U8](_o_h_audio.md) = 0, [AUDIOSTREAM_SAMPLE_S16LE](_o_h_audio.md) = 1, [AUDIOSTREAM_SAMPLE_S24LE](_o_h_audio.md) = 2, [AUDIOSTREAM_SAMPLE_S32LE](_o_h_audio.md) = 3 } | 定义音频流采样格式。 | | [OH_AudioStream_EncodingType](_o_h_audio.md#oh_audiostream_encodingtype) { **AUDIOSTREAM_ENCODING_TYPE_RAW** = 0 } | 定义音频流编码类型。 | | [OH_AudioStream_Usage](_o_h_audio.md#oh_audiostream_usage) { **AUDIOSTREAM_USAGE_UNKNOWN** = 0, **AUDIOSTREAM_USAGE_MUSIC** = 1, **AUDIOSTREAM_USAGE_COMMUNICATION** = 2, **AUDIOSTREAM_USAGE_GAME** = 11 } | 定义音频流使用场景。 |