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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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 的学习方向。
再次感谢
好了我们开始学习了

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: android