Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
4cdadde9
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
4cdadde9
编写于
6月 27, 2023
作者:
Z
zhanhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改拼写错误,去掉完整示例。
Signed-off-by:
N
zhanhang
<
zhanhang2@huawei.com
>
上级
c441d0ce
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
19 addition
and
294 deletion
+19
-294
zh-cn/application-dev/media/using-ohaudio-for-playback.md
zh-cn/application-dev/media/using-ohaudio-for-playback.md
+8
-142
zh-cn/application-dev/media/using-ohaudio-for-recording.md
zh-cn/application-dev/media/using-ohaudio-for-recording.md
+7
-148
zh-cn/application-dev/reference/native-apis/_o_h_audio.md
zh-cn/application-dev/reference/native-apis/_o_h_audio.md
+3
-3
zh-cn/application-dev/reference/native-apis/native__audiostream__base_8h.md
...dev/reference/native-apis/native__audiostream__base_8h.md
+1
-1
未找到文件。
zh-cn/application-dev/media/using-ohaudio-for-playback.md
浏览文件 @
4cdadde9
...
...
@@ -14,7 +14,7 @@ OHAudio提供OH_AudioStreamBuilder接口,遵循构造器设计模式,用于
`OH_AudioStream_Type`
包含两种类型:
-
AUDIOSTREAM_TYPE_RE
R
NDERER
-
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_RE
R
NDERER);
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 <iostream>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <thread>
#include <chrono>
#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);
```
zh-cn/application-dev/media/using-ohaudio-for-recording.md
浏览文件 @
4cdadde9
...
...
@@ -14,7 +14,7 @@ OHAudio提供OH_AudioStreamBuilder接口,遵循构造器设计模式,用于
`OH_AudioStream_Type`
包含两种类型:
-
AUDIOSTREAM_TYPE_RE
R
NDERER
-
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 <iostream>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include "native_audiostreambuilder.h"
#include <native_audiocapturer.h>
#include <thread>
#include <chrono>
#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);
```
zh-cn/application-dev/reference/native-apis/_o_h_audio.md
浏览文件 @
4cdadde9
...
...
@@ -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_RE
R
NDERER = 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_RE
R
NDERER | 该类型代表音频流是输出流。 |
| AUDIOSTREAM_TYPE_RENDERER | 该类型代表音频流是输出流。 |
| AUDIOSTREAM_TYPE_CAPTURER | 该类型代表音频流是输入流。 |
...
...
@@ -1126,7 +1126,7 @@ OH_AudioStream_Result OH_AudioStreamBuilder_Create (OH_AudioStreamBuilder ** bui
| 名称 | 描述 |
| -------- | -------- |
| builder | 该引用指向创建的构造器的结果。 |
| type | 构造器的流类型。AUDIOSTREAM_TYPE_RE
R
NDERER or AUDIOSTREAM_TYPE_CAPTURER |
| type | 构造器的流类型。AUDIOSTREAM_TYPE_RENDERER or AUDIOSTREAM_TYPE_CAPTURER |
**返回:**
...
...
zh-cn/application-dev/reference/native-apis/native__audiostream__base_8h.md
浏览文件 @
4cdadde9
...
...
@@ -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_RE
R
NDERER
](
_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 } | 定义音频流使用场景。 |
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录