您好,登錄后才能下訂單哦!
? OpenSL ES 是無授權費、跨平臺、針對嵌入式系統精心優化的硬件音頻加速API。該庫都允許使用C或C ++來實現高性能,低延遲的音頻操作。
? Android的OpenSL ES庫同樣位于NDK的platforms文件夾內。關于OpenSL ES的使用可以進入ndk-sample查看native-audio工程:https://github.com/googlesamples/android-ndk/blob/master/native-audio/app/src/main/cpp/native-audio-jni.c
OpenSL ES的開發流程主要有如下7個步驟:
? 1、創建接口對象
? 2、設置混音器
? 3、創建播放器
? 4、設置播放回調函數
? 5、設置播放狀態
? 6、啟動回調函數
? 7、釋放
1、創建引擎與接口
SLresult result;
// 創建引擎 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、設置混音器
// 創建混音器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;
}
//不啟用混響可以不用獲取接口
// 獲得混音器接口
//result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB,
// &outputMixEnvironmentalReverb);
//if (SL_RESULT_SUCCESS == result) {
//設置混響 : 默認。
//SL_I3DL2_ENVIRONMENT_PRESET_ROOM: 室內
//SL_I3DL2_ENVIRONMENT_PRESET_AUDITORIUM : 禮堂 等
//const SLEnvironmentalReverbSettings settings = SL_I3DL2_ENVIRONMENT_PRESET_DEFAULT;
//(*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(
// outputMixEnvironmentalReverb, &settings);
//}
3、創建播放器
/**
配置輸入聲音信息
*/
//創建buffer緩沖類型的隊列 2個隊列
SLDataLocator_AndroidSimpleBufferQueue android_queue = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,2};
//pcm數據格式
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};
//數據源 將上述配置信息放到這個數據源中
SLDataSource slDataSource = {&android_queue, &pcm};
//設置混音器
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};
//創建播放器
(engineInterface)->CreateAudioPlayer(engineInterface, &bqPlayerObject, &slDataSource,
&audioSnk, 1,
ids, req);
//初始化播放器
(bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
// 得到接口后調用 獲取Player接口
(*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerInterface);
4、設置播放回調
//獲取播放器隊列接口
(bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_BUFFERQUEUE,
&bqPlayerBufferQueue);
//設置回調
(bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, this);
5、設置播放狀態
// 設置播放狀態
(*bqPlayerInterface)->SetPlayState(bqPlayerInterface, SL_PLAYSTATE_PLAYING);
6、啟動回調函數
bqPlayerCallback(bqPlayerBufferQueue, this);
7、釋放
//設置停止狀態
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;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。