OpenSL ES
? OpenSL ES 是無授權(quán)費(fèi)、跨平臺(tái)、針對(duì)嵌入式系統(tǒng)精心優(yōu)化的硬件音頻加速API。該庫都允許使用C或C ++來實(shí)現(xiàn)高性能,低延遲的音頻操作。
? Android的OpenSL ES庫同樣位于NDK的platforms文件夾內(nèi)。關(guān)于OpenSL ES的使用可以進(jìn)入ndk-sample查看native-audio工程:https://github.com/googlesamples/android-ndk/blob/master/native-audio/app/src/main/cpp/native-audio-jni.c
OpenSL ES的開發(fā)流程主要有如下7個(gè)步驟:
? 1、創(chuàng)建接口對(duì)象
? 2、設(shè)置混音器
? 3、創(chuàng)建播放器
? 4、設(shè)置播放回調(diào)函數(shù)
? 5、設(shè)置播放狀態(tài)
? 6、啟動(dòng)回調(diào)函數(shù)
? 7、釋放
1、創(chuàng)建引擎與接口
SLresult result;
// 創(chuàng)建引擎 SLObjectItf engineObject
result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
if (SL_RESULT_SUCCESS != result) {
return;
}
// 初始化引擎
result = (engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
return;
}
// 獲取引擎接口SLEngineItf engineInterface
result = (engineObject)->GetInterface(engineObject, SL_IID_ENGINE,
&engineInterface);
if (SL_RESULT_SUCCESS != result) {
return;
}
2、設(shè)置混音器
// 創(chuàng)建混音器SLObjectItf outputMixObject
result = (engineInterface)->CreateOutputMix(engineInterface, &outputMixObject, 0,
0, 0);
if (SL_RESULT_SUCCESS != result) {
return;
}
// 初始化混音器outputMixObject
result = (outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
return;
}
3、創(chuàng)建播放器
/**
-
配置輸入聲音信息
*/
//創(chuàng)建buffer緩沖類型的隊(duì)列 2個(gè)隊(duì)列
SLDataLocator_AndroidSimpleBufferQueue android_queue = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,2};
//pcm數(shù)據(jù)格式
SLDataFormat_PCM pcm = {SL_DATAFORMAT_PCM, 2, SL_SAMPLINGRATE_44_1, SL_PCMSAMPLEFORMAT_FIXED_16,
SL_PCMSAMPLEFORMAT_FIXED_16,
SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT,
SL_BYTEORDER_LITTLEENDIAN};//數(shù)據(jù)源 將上述配置信息放到這個(gè)數(shù)據(jù)源中
SLDataSource slDataSource = {&android_queue, &pcm};//設(shè)置混音器
SLDataLocator_OutputMix outputMix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
SLDataSink audioSnk = {&outputMix, NULL};
//需要的接口
const SLInterfaceID ids[1] = {SL_IID_BUFFERQUEUE};
const SLboolean req[1] = {SL_BOOLEAN_TRUE};
//創(chuàng)建播放器
(engineInterface)->CreateAudioPlayer(engineInterface, &bqPlayerObject, &slDataSource,
&audioSnk, 1,
ids, req);
//初始化播放器
(bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
// 得到接口后調(diào)用 獲取Player接口
(*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerInterface);
4、設(shè)置播放回調(diào)
//獲取播放器隊(duì)列接口
(bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_BUFFERQUEUE,
&bqPlayerBufferQueue);
//設(shè)置回調(diào)
(bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, this);
5、設(shè)置播放狀態(tài)
// 設(shè)置播放狀態(tài)
(*bqPlayerInterface)->SetPlayState(bqPlayerInterface, SL_PLAYSTATE_PLAYING);
6、啟動(dòng)回調(diào)函數(shù)
bqPlayerCallback(bqPlayerBufferQueue, this);
7、釋放
//設(shè)置停止?fàn)顟B(tài)
if (bqPlayerInterface) {
(bqPlayerInterface)->SetPlayState(bqPlayerInterface, SL_PLAYSTATE_STOPPED);
bqPlayerInterface = 0;
}
//銷毀播放器
if (bqPlayerObject) {
(bqPlayerObject)->Destroy(bqPlayerObject);
bqPlayerObject = 0;
bqPlayerBufferQueue = 0;
}
//銷毀混音器
if (outputMixObject) {
(outputMixObject)->Destroy(outputMixObject);
outputMixObject = 0;
}
//銷毀引擎
if (engineObject) {
(engineObject)->Destroy(engineObject);
engineObject = 0;
engineInterface = 0;
}
轉(zhuǎn)載于:https://blog.51cto.com/14138284/2333670
總結(jié)
- 上一篇: ES6相关特性的整理(变量数据结构)
- 下一篇: 动态IP代理软件有话说:天下爬虫框架皆出