OpenSL ES是一個嵌入式、跨平臺、免費的、音頻處理庫,android ndk也引入了它,在android-ndk-r14b/platforms/android-21/arch-arm/usr/lib/libOpenSLES.so的目錄下,libOpenSLES.so就是適用於Android平臺的開發庫。OpenELES對於android平臺的支持只有部分API,因此,對於 OpenSL ES API 的使用,我們還需要特別留意哪些是 Android 支持的,哪些是不支持的,具體相關文檔的地址位於 NDK docs 目錄下:
NDK根目錄/docs/Additional_library_docs/opensles/index.html
NDK根目錄/docs/Additional_library_docs/opensles/OpenSL_ES_Specification_1.0.1.pdf
OpenSLES詳情請參考Google官網介紹:https://developer.android.com/ndk/guides/audio/opensl-for-android?hl=es
向大家介紹一款免費的播放pcm格式音頻的軟件windows和mac都有安裝包:Audacity
接口解釋:
類接口:SLObjectltf
通過SLObjectltf接口類,可以創建所需要的各種類型的類接口,比如:
創建引擎接口對象:SLObjectltf engineObject
創建混音器接口對象:SLObjectltf outputMixObject
創建播放器接口對象:SLObjectltf playerObject
具體接口類
引擎:SLEngineltf
播放器:SLPlayltf
聲音控制器:SLVolumeltf等
創建三部曲
以創建引擎接口對象爲例(create->realize->get)
SLObjectltf engineObject = NULL;//用SLObjectltf聲明引擎接口對象
SLEngineltf engineEngine = NULL;//聲明具體的引擎對象實例
void createEnngine()
{
SLresult result;//返回結果
//第一步:創建引擎
result=slCreateEngine(&engineObject,0,NULL,0,NULL,NULL);
//第二步:實現(Realize)engineObject,SL_BOOLEAN_FALSE);
result=(*engineObject)->Realize(engineObject,SL_BOOLEAN_FALSE);
//第三步:通過engineObject的GetInterface方法初始化enngineEngine
result=(*engineObject)->GetInterface(engineObject,SL_IID_ENGINNE,&engineEngine);
}
//銷燬
(*engineObject)->Destroy(engineObject);
OpenEL ES使用流程:
1、創建接口對象;
// TODO
//讀取pcm文件
pcmFile = fopen(url, "r");
if(pcmFile == NULL)
{
LOGE("%s", "fopen file error");
return;
}
out_buffer = (uint8_t *) malloc(44100 * 2 * 2);
SLresult result;
// 創建引擎對象
slCreateEngine(&engineObject, 0, 0, 0, 0, 0);
(*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
(*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
2、設置混音器;
//設置混音器;
const SLInterfaceID mids[1] = {SL_IID_ENVIRONMENTALREVERB};
const SLboolean mreq[1] = {SL_BOOLEAN_FALSE};
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, mids, mreq);
(void)result;
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
(void)result;
result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB, &outputMixEnvironmentalReverb);
if (SL_RESULT_SUCCESS == result) {
result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(
outputMixEnvironmentalReverb, &reverbSettings);
(void)result;
}
SLDataLocator_OutputMix outputMix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
3、創建播放器(錄音器);
// 創建播放器
SLDataLocator_AndroidSimpleBufferQueue android_queue={SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,2};
SLDataFormat_PCM pcm={
SL_DATAFORMAT_PCM,//播放pcm格式的數據
2,//2個聲道(立體聲)
SL_SAMPLINGRATE_44_1,//44100hz的頻率
SL_PCMSAMPLEFORMAT_FIXED_16,//位數 16位
SL_PCMSAMPLEFORMAT_FIXED_16,//和位數一致就行
SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT,//立體聲(前左前右)
SL_BYTEORDER_LITTLEENDIAN//結束標誌
};
SLDataSource slDataSource = {&android_queue, &pcm};
SLDataSink audioSnk = {&outputMix, NULL};
const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND, SL_IID_VOLUME};
const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &pcmPlayerObject, &slDataSource, &audioSnk, 3, ids, req);
// 初始化播放器
(*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE);
//得到接口後調用 獲取Player接口
(*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &pcmPlayerPlay);
4、設置緩衝隊列和回調函數;
// 創建緩衝區和回調函數
(*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_BUFFERQUEUE, &pcmBufferQueue);
//緩衝接口回調
(*pcmBufferQueue)->RegisterCallback(pcmBufferQueue, pcmBufferCallBack, NULL);
//獲取音量接口
(*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_VOLUME, &pcmPlayerVolume);
//初始化pcm文件數據
void getPcmData(void **pcm)
{
while(!feof(pcmFile))
{
fread(out_buffer, 44100 * 2 * 2, 1, pcmFile);
if(out_buffer == NULL)
{
LOGI("%s", "read end");
break;
} else{
LOGI("%s", "reading");
}
*pcm = out_buffer;
break;
}
}
//取出緩衝數據
void pcmBufferCallBack(SLAndroidSimpleBufferQueueItf bf, void * context)
{
//assert(NULL == context);
getPcmData(&buffer);
// for streaming playback, replace this test by logic to find and fill the next buffer
if (NULL != buffer) {
SLresult result;
// enqueue another buffer
result = (*pcmBufferQueue)->Enqueue(pcmBufferQueue, buffer, 44100 * 2 * 2);
// the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT,
// which for this code example would indicate a programming error
}
}
5、設置播放狀態;
// 設置播放狀態
(*pcmPlayerPlay)->SetPlayState(pcmPlayerPlay, SL_PLAYSTATE_PLAYING);
6、啓動回到函數;
// 主動調用回調函數開始工作
pcmBufferCallBack(pcmBufferQueue, NULL);