Android 深入系统完全讲解(33)

Posted 程序员入门进阶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 深入系统完全讲解(33)相关的知识,希望对你有一定的参考价值。

result = (*p->bqPlayerObject)->GetInterface(p->bqPlayerObject, SL_IID_androidSIMPLEBUFFERQUEUE, &(p->bqPlayerBufferQueue));
if(result != SL_RESULT_SUCCESS) return result;
// register callback on the buffer queue
result = (*p->bqPlayerBufferQueue)->RegisterCallback(p->bqPlayerBufferQueue, bqPlayerCallback, p);
if(result != SL_RESULT_SUCCESS) return result;
// set the player’s state to playing
result = (*p->bqPlayerPlay)->SetPlayState(p->bqPlayerPlay, SL_PLAYSTATE_PLAYING);
return result;

return SL_RESULT_SUCCESS;

// close the OpenSL IO and destroy the audio engine
static void openSLDestroyEngine(OPENSL_STREAM *p)

// destroy buffer queue audio player object, and invalidate all associated interfaces
if (p->bqPlayerObject != NULL)
(*p->bqPlayerObject)->Destroy(p->bqPlayerObject);
p->bqPlayerObject = NULL;
p->bqPlayerPlay = NULL;
p->bqPlayerBufferQueue = NULL;

// destroy output mix object, and invalidate all associated interfaces
if (p->outputMixObject != NULL)
(*p->outputMixObject)->Destroy(p->outputMixObject);
p->outputMixObject = NULL;

// destroy engine object, and invalidate all associated interfaces
if (p->engineObject != NULL)
(*p->engineObject)->Destroy(p->engineObject);
p->engineObject = NULL;
p->engineEngine = NULL;

// open the android audio device for input and/or output
OPENSL_STREAM *android_OpenAudioDevice(uint32_t sr, uint32_t inchannels, uint32_t
outchannels, uint32_t bufferframes)

OPENSL_STREAM *p;
//分配内存空间并初始化
p = (OPENSL_STREAM *) calloc(1,sizeof(OPENSL_STREAM));
//采样率
p->sampleRate = sr;
//创建引擎对象及接口
if(openSLCreateEngine§ != SL_RESULT_SUCCESS)
android_CloseAudioDevice§;
return NULL;

p->inputDataCount = 0;
//输出声道数
p->outchannels = outchannels;
if(openSLPlayOpen§ != SL_RESULT_SUCCESS)
android_CloseAudioDevice§;
return NULL;

return p;

// close the android audio device
void android_CloseAudioDevice(OPENSL_STREAM *p)

if (p == NULL)
return;
openSLDestroyEngine§;
free§;

// returns timestamp of the processed stream
double android_GetTimestamp(OPENSL_STREAM *p)

return p->time;

// this callback handler is called every time a buffer finishes playingvoid bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)

OPENSL_STREAM *p = (OPENSL_STREAM *) context;
p->inputDataCount --;
// free(p->inputBuffer);

// puts a buffer of size samples to the device
uint32_t android_AudioOut(OPENSL_STREAM *p, uint16_t *buffer,uint32_t size)

(*p->bqPlayerBufferQueue)->Enqueue(p->bqPlayerBufferQueue, buffer, size);
p->inputDataCount ++;
return 0;

int android_SetPlayRate(OPENSL_STREAM *p,int rateChange)
SLmillibel value;
SLresult result;
if (!p) return -1;
if (!p->bqPlayerRate) return -1;
result = (p->bqPlayerRate)->GetRate(p->bqPlayerRate,&value);
if (result != SL_RESULT_SUCCESS)return -1;
if (rateChange<0)
value -= 100;
else
value += 100;
if (value < 500) value = 500;
if (value > 2000) value = 2000;
result = (p->bqPlayerRate)->SetRate(p->bqPlayerRate,value);
if (result == SL_RESULT_SUCCESS)
return 0;
else
return -1;

我们应用:
#include “android_snd.h” 然后
aspec.format = format;aspec.freq = rate;
aspec.channels = channels;
aspec.samples = SAMPLESIZE;
//aspec.callback = outputaudio;
aspec.userdata = NULL;
pOpensl=android_OpenAudioDevice(aspec.freq,audioChannels,audioChannels,aspec.sampl
es);
传入参数,然后使用
android_AudioOut(pOpensl,(uint16_t
)(pOutput[currentbuffer]),audioBufferSize);
完成数据输出。这个方法我们是在每一帧调用。
这个库封装起来就几个方法:
int android_SetPlayRate(OPENSL_STREAM p,int playRate);
/
Open the audio device with a given sampling rate (sr), input and output channels and IO
buffer size
in frames. Returns a handle to the OpenSL stream
/
OPENSL_STREAM
android_OpenAudioDevice(uint32_t sr, uint32_t inchannels, uint32_t
outchannels, uint32_t bufferframes);
/
Close the audio device
*/
void android_CloseAudioDevice(OPENSL_STREAM p);
/
Write a buffer to the OpenSL stream *p, of size samples. Returns the number of samples
written. */
uint32_t android_AudioOut(OPENSL_STREAM *p, uint16_t *buffer,uint32_t size);说到这里,我们顺便开始讲一下,关于 NDK 的知识:
官网 https://developer.android.google.cn/ndk/guides
这里插入我之前创作的 NDK 教程,大家可以学习,不懂的直接联系我 code_gg_boy
首先,感谢大家的支持。相信每个加入进来的朋友,都希望从文章中有所收获。本文通过一
条学习的线路,使用直接,没有太多官方语言的描述,给大家一个 NDK 的学习方向。
再次,感谢!
好了,我们开始学习了:

以上是关于Android 深入系统完全讲解(33)的主要内容,如果未能解决你的问题,请参考以下文章

Android 深入系统完全讲解(33)

Android 深入系统完全讲解(31)

Android 深入系统完全讲解(31)

Android深入系统完全讲解(38)

Android深入系统完全讲解(38)

Android 深入系统完全讲解(25)