在Android上使用OpenSL播放内存中的PCM WAVE声音

我正在尝试将OpenSL AudioPlayer设置为使用分配给我播放wav文件的内存.我想这样做,所以我可以有多个共享相同数据并节省内存的AudioPlayers.

我试图给openSL整个文件,并告诉它这是带有format_mime的WAVE

 SLDataLocator_Address loc_fd = {SL_DATALOCATOR_ADDRESS, data, size};
SLDataFormat_MIME format_mime = { SL_DATAFORMAT_MIME, (SLchar*)"audio/x-wav",SL_CONTAINERTYPE_WAV};
SLDataSource audioSrc = { &loc_fd, &format_mime };
// configure audio sink
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX,outputMixObject };
SLDataSink audioSnk = { &loc_outmix, 0 };
// create audio player
const SLInterfaceID ids[2] = { SL_IID_SEEK, SL_IID_PLAYBACKRATE };
const SLboolean req[2] = { SL_BOOLEAN_FALSE, SL_BOOLEAN_FALSE };
result = (*engineEngine)->CreateAudioPlayer(engineEngine,&uriPlayerObject[cntSOUND],&audioSrc, &audioSnk, 0, ids, req);

我自己解析了WAVE数据并加载了format_pcm

SLDataFormat_PCM format_pcm;
format_pcm.formatType = SL_DATAFORMAT_PCM;
char* wavParser = isWAVE(data);
if(wavParser == NULL)
{
    Log("NOT A WAVE!");
    return -1;
}
char* fmtChunk = getChunk("fmt ", data, size);
parsefmtChunk(fmtChunk, &format_pcm);
char* dataChunk = getChunk("data",data, size);
dataChunk += 4;
unsigned int dataSize = *((unsigned int*)dataChunk);
dataChunk += 4;
format_pcm.channelMask = 0;
format_pcm.containerSize = 16;
format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
loc_fd.pAddress = dataChunk;
loc_fd.length = dataSize;

parsefmtChunk函数是

void parsefmtChunk(char* fmtchunk, SLDataFormat_PCM* pcm)
{
char* data = fmtchunk + 8;
unsigned short audioFormat = *((unsigned short*)data);
if(audioFormat != 1)
{
    Log("Not PCM!");
    Log("Reached Line:%d in File %s", __LINE__, __FILE__);
    return;
}
data += 2;
pcm->numChannels = *((unsigned short*)data);
data += 2;
pcm->samplesPerSec = *((unsigned int*)data);
data += 4;
//Byte Rate
data += 4;
//Block Align
data += 2;
//BitsPerSample
pcm->bitsPerSample = *((unsigned short*)data);

(是否应该以某种方式使用字节速率和块对齐来填充pcm结构?)

但是每当我创建音频播放器时,我都会得到SL_RESULT_CONTENT_UNSUPPORTED

这是我从parsefmt函数记录的内容
频道数:2
样品数:44100
bitsPerSample:16

解决方法:

来自android-ndk-r8b / docs / opensles / index.html

PCM data format

The PCM data format can be used with buffer queues only.

因此,SLDataFormat_PCM不能与我假设的SLDataLocator_Address一起使用.

我可以使用Buffer Queue来做我想做的事情,而不是只使用一个大队列,就像这样

bufferqueueitf->Enqueue(bufferqueueitf,dataChunk,dataSize);
上一篇:鸿蒙内核源码分析(内存映射篇) | 虚拟地址<->物理地址是如何映射的 | 百篇博客分析HarmonyOS源码 | v15.02


下一篇:hdu2222(ac自动机模板)