未验证 提交 15632dcc 编写于 作者: 杨帅 提交者: Gitee

Signed-off-by: yangshuai<yangshuai67@huawei.com>

上级 761359d3
# OpenSLES音频播放开发指导 OpenSL ES音频播放开发指导
## 场景介绍 场景介绍
OpenSL ES™ 是无授权费、跨平台、针对嵌入式系统精心优化的硬件音频加速API。 开发者可以通过本文了解到在OpenHarmony如何使用OpenSL ES进行音频播放相关操作;当前仅实现了部分OpenSL ES接口,未实现接口调用后会返回SL_RESULT_FEATURE_UNSUPPORTED
## 开发步骤 开发步骤
以下步骤描述了在**openHarmony**如何使用 **OpenSLES** 开发音频播放功能: 以下步骤描述了在OpenHarmony如何使用OpenSL ES开发音频播放功能:
1. 使用 **slCreateEngine** 接口和获取 **engine** 实例。 1. 添加头文件
#include <OpenSLES.h>
```c++ #include <OpenSLES_OpenHarmony.h>
SLObjectItf engineObject = nullptr; #include <OpenSLES_Platform.h>
slCreateEngine(&engineObject, 0, nullptr, 0, nullptr, nullptr);
(*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); 2. 使用 slCreateEngine 接口和获取 engine 实例。
``` SLObjectItf engineObject = nullptr;
slCreateEngine(&engineObject, 0, nullptr, 0, nullptr, nullptr);
(*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
2. 获取接口 **SL_IID_ENGINE****engineEngine** 实例 3. 获取接口 SL_IID_ENGINE 的 engineEngine 实例
SLEngineItf engineEngine = nullptr;
```c++ (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
SLEngineItf engineEngine = nullptr;
(*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine); 4. 配置播放器信息,创建 AudioPlayer 。
``` SLDataLocator_BufferQueue slBufferQueue = {
SL_DATALOCATOR_BUFFERQUEUE,
0
};
3. 配置播放器信息,创建 **AudioPlayer**
//具体参数需要根据音频文件格式进行适配
```c++ SLDataFormat_PCM pcmFormat = {
SLDataLocator_BufferQueue slBufferQueue = { SL_DATAFORMAT_PCM,
SL_DATALOCATOR_BUFFERQUEUE, 2,
0 48000,
}; 16,
0,
//具体参数需要根据音频文件格式进行适配 0,
SLDataFormat_PCM pcmFormat = { 0
SL_DATAFORMAT_PCM, };
2, SLDataSource slSource = {&slBufferQueue, &pcmFormat};
48000,
16, SLObjectItf pcmPlayerObject = nullptr;
0, (*engineEngine)->CreateAudioPlayer(engineEngine, &pcmPlayerObject, &slSource, null, 0, nullptr, nullptr);
0, (*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE);
0
}; 5. 获取接口 SL_IID_OH_BUFFERQUEUE 的 bufferQueueItf 实例
SLDataSource slSource = {&slBufferQueue, &pcmFormat}; SLOHBufferQueueItf bufferQueueItf;
(*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf);
SLObjectItf pcmPlayerObject = nullptr;
(*engineEngine)->CreateAudioPlayer(engineEngine, &pcmPlayerObject, &slSource, null, 0, nullptr, nullptr); 6. 打开音频文件,注册 BuqqerQueueCallback 回调
(*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE); FILE *wavFile_ = nullptr;
```
static void BuqqerQueueCallback (SLOHBufferQueueItf bufferQueueItf, void *pContext, SLuint32 size)
{
FILE *wavFile = (FILE *)pContext;
4. 获取接口 **SL_IID_OH_BUFFERQUEUE****bufferQueueItf** 实例 if (!feof(wavFile)) {
SLuint8 *buffer = nullptr;
``` SLuint32 pSize = 0;
SLOHBufferQueueItf bufferQueueItf; (*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, pSize);
(*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf); //从文件读取数据
``` fread(buffer, 1, size, wavFile);
(*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, size);
}
return;
5. 打开音频文件,注册 **BuqqerQueueCallback** 回调 }
```c++ //wavFile_ 需要设置为用户想要播放的文件描述符
FILE *wavFile_ = nullptr; wavFile_ = fopen(path, "rb");
(*bufferQueueItf)->RegisterCallback(bufferQueueItf, BuqqerQueueCallback, wavFile_);
static void BuqqerQueueCallback (SLOHBufferQueueItf bufferQueueItf, void *pContext, SLuint32 size)
{ 7. 获取接口 SL_PLAYSTATE_PLAYING 的 playItf 实例,开始播放
FILE *wavFile = (FILE *)pContext; SLPlayItf playItf = nullptr;
if (!feof(wavFile)) { (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &playItf);
SLuint8 *buffer = nullptr; (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
SLuint32 pSize = 0;
(*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, pSize); 8. 结束音频播放
//从文件读取数据 (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
fread(buffer, 1, size, wavFile); (*pcmPlayerObject)->Destroy(pcmPlayerObject);
(*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, size); (*engineObject)->Destroy(engineObject);
}
return;
}
//wavFile_ 需要设置为用户想要播放的文件描述符
wavFile_ = fopen(path, "rb");
(*bufferQueueItf)->RegisterCallback(bufferQueueItf, BuqqerQueueCallback, wavFile_);
```
6. 获取接口 **SL_PLAYSTATE_PLAYING****playItf** 实例,开始播放
```c++
SLPlayItf playItf = nullptr;
(*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &playItf);
(*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
```
7. 结束音频播放
```c++
(*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
(*pcmPlayerObject)->Destroy(pcmPlayerObject);
(*engineObject)->Destroy(engineObject);
```
[**OpenSLES** 音频播放代码 demo](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/services/test/audio_opensles_test.cpp)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册