OpenSL ES (Open Sound Library for Embedded Systems)是无授权费、跨平台、针对嵌入式系统精心优化的硬件音频加速API。它为嵌入式移动多媒体设备上的本地应用程序开发者提供标准化, 高性能,低响应时间的音频功能实现方法,并实现软/硬件音频性能的直接跨平台部署,降低执行难度,促进高级音频市场的发展。简单来说OpenSL ES是一个嵌入式跨平台免费的音频处理库。
OpenSL ES 开发流程有如下6个步骤:
- 创建接口对象
- 设置混音器
- 创建播放器(录音器)
- 设置缓冲队列和回调函数
- 设置播放状态
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
OpenSL ES 开发重要的接口类。
其中包括所有对象的初始接口,称为 SLObjectItf
。对象本身没有句柄,只有一个连接到对象的 SLObjectItf
接口的句柄。
创建引擎接口对象 :SLObjectItf engineObject
SLresult result;
// create engine 创建引擎
result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// realize the engine //实现引擎
result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
(void) result;
创建并实现 (realize) 对象后,应用应当在初始 SLObjectItf
上使用 GetInterface
,为其需要的每种功能获取接口。
// get the engine interface, which is needed in order to create other objects
//获取引擎接口,这是创建其他对象所必需的
result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
assert(SL_RESULT_SUCCESS == result);
(void) result;
接下来创建混响器
// create output mix, with environmental reverb specified as a non-required interface
//创建输出混合,将环境混响指定为非必需接口
const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB};
const SLboolean req[1] = {SL_BOOLEAN_FALSE};
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// realize the output mix
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// get the environmental reverb interface
// this could fail if the environmental reverb effect is not available,
// either because the feature is not present, excessive CPU load, or
// the required MODIFY_AUDIO_SETTINGS permission was not requested and granted
//获取环境混响界面
//如果环境混响效应不可用,这可能会失败,
//可能是因为该功能不存在,CPU负载过大,或者
//未请求和授予所需的修改音频设置权限
result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB,
&outputMixEnvironmentalReverb);
if (SL_RESULT_SUCCESS == result) {
result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(
outputMixEnvironmentalReverb, &reverbSettings);
(void) result;
}
创建混响器之后,创建播放器。
// create audio player
const SLInterfaceID ids[5] = {SL_IID_SEEK,
SL_IID_MUTESOLO,
SL_IID_VOLUME,
SL_IID_BASSBOOST, //低音增强
SL_IID_VIRTUALIZER//环绕音效
// , SL_IID_RATEPITCH //速率
};
const SLboolean req[5] = {SL_BOOLEAN_TRUE,
SL_BOOLEAN_TRUE,
SL_BOOLEAN_TRUE,
SL_BOOLEAN_TRUE,
SL_BOOLEAN_TRUE};
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &fdPlayerObject, &audioSrc, &audioSnk,
5, ids, req);
LOGE("CreateAudioPlayer========= %d", result);
assert(SL_RESULT_SUCCESS == result);
(void) result;
创建播放器之后 设置播放状态
// set the player's state
result = (*fdPlayerPlay)->SetPlayState(fdPlayerPlay, is_playing ?
SL_PLAYSTATE_PLAYING
: SL_PLAYSTATE_PAUSED);
大概流程就是这样子。
具体Api:
#define SL_RESULT_SUCCESS ((SLuint32) 0x00000000)
#define SL_RESULT_PRECONDITIONS_VIOLATED ((SLuint32) 0x00000001)
#define SL_RESULT_PARAMETER_INVALID ((SLuint32) 0x00000002)
#define SL_RESULT_MEMORY_FAILURE ((SLuint32) 0x00000003)
#define SL_RESULT_RESOURCE_ERROR ((SLuint32) 0x00000004)
#define SL_RESULT_RESOURCE_LOST ((SLuint32) 0x00000005)
#define SL_RESULT_IO_ERROR ((SLuint32) 0x00000006)
#define SL_RESULT_BUFFER_INSUFFICIENT ((SLuint32) 0x00000007)
#define SL_RESULT_CONTENT_CORRUPTED ((SLuint32) 0x00000008)
#define SL_RESULT_CONTENT_UNSUPPORTED ((SLuint32) 0x00000009)
#define SL_RESULT_CONTENT_NOT_FOUND ((SLuint32) 0x0000000A)
#define SL_RESULT_PERMISSION_DENIED ((SLuint32) 0x0000000B)
#define SL_RESULT_FEATURE_UNSUPPORTED ((SLuint32) 0x0000000C)
#define SL_RESULT_INTERNAL_ERROR ((SLuint32) 0x0000000D)
#define SL_RESULT_UNKNOWN_ERROR ((SLuint32) 0x0000000E)
#define SL_RESULT_OPERATION_ABORTED ((SLuint32) 0x0000000F)
#define SL_RESULT_CONTROL_LOST ((SLuint32) 0x00000010)
open sl 里面定义了一些错误,基本是简明见名思意。
这篇文章只是抛转引玉,具体还是看什么需求。