gpt4 book ai didi

android - OpenSL:是什么原因导致通过AAsset_read加载WAV文件时出现静态现象?

转载 作者:行者123 更新时间:2023-12-03 02:01:27 27 4
gpt4 key购买 nike

我正在开发一个本地android项目,并尝试使用OpenSL播放一些音频效果。从VisualGDB提供的本地音频示例项目开始,我编写了以下代码。

在结尾附近,您可以看到我注释了一行,该行将名为hello的变量的内容存储在目标缓冲区中。你好来自示例项目,包含大约700行字符字节,如下所示:

“\ x02 \ x00 \ x01 \ x00 \ xff \ xff \ x09 \ x00 \ x0c \ x00 \ x10 \ x00 \ x07 \ x00 \ x07 \ x00”

制作一个人说“你好”的音频文件。当将该字节数据读入流中时,我的代码工作正常,并且在运行该应用程序时听到“你好”。但是,当我从wav文件读取以播放所需的 Assets 时,我只会听到静态声音。数据缓冲区的大小与文件的大小相同,因此似乎已在正确读取它。静态文件会在wav文件的持续时间内播放(或非常接近它)。

我真的对数据格式或音频编程一无所知。我尝试过调整一些具有不同枚举值的format_pcm变量,但是没有成功。使用我在Internet上找到的名为GSpot的工具,我了解以下有关我尝试播放的音频文件的信息:

文件大小:557 KB(570503字节)(此大小与数据缓冲区的大小相同)
AAsset_read返回

编解码器:PCM音频

采样率:48000Hz

比特率:1152 kb / s

channel 数:1

任何帮助或指示将不胜感激。

SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 1 };
SLDataFormat_PCM format_pcm;
format_pcm.formatType = SL_DATAFORMAT_PCM;
format_pcm.numChannels = 1;
format_pcm.samplesPerSec = SL_SAMPLINGRATE_48;// SL_SAMPLINGRATE_8;
format_pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_8; // SL_PCMSAMPLEFORMAT_FIXED_16;
format_pcm.containerSize = 16;
format_pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;

SLDataSource audioSrc = { &loc_bufq, &format_pcm };

// configure audio sink
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, manager->GetOutputMixObject() };
SLDataSink audioSnk = { &loc_outmix, NULL };

//create audio player
const SLInterfaceID ids[3] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND, SL_IID_VOLUME };
const SLboolean req[3] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };

SLEngineItf engineEngine = manager->GetEngine();
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk,
3, ids, req);

// realize the player
result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);

// get the play interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);

// get the buffer queue interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_BUFFERQUEUE,
&bqPlayerBufferQueue);

// register callback on the buffer queue
result = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, NULL);

// get the effect send interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_EFFECTSEND,
&bqPlayerEffectSend);

// get the volume interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);

// set the player's state to playing
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);

uint8* pOutBytes = nullptr;
uint32 outSize = 0;
result = MyFileManager::GetInstance()->OpenFile(m_strAbsolutePath, (void**)&pOutBytes, &outSize, true);
const char* filename = m_strAbsolutePath->GetUTF8String();
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, pOutBytes, outSize);
// result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, hello, sizeof(hello));
if (SL_RESULT_SUCCESS != result) {
return JNI_FALSE;
}

最佳答案

归咎于几件事。我正在测试的wave文件的格式不是规范描述的格式。在头数据的第一块之后,似乎有很多空数据。同样,需要传递给队列的缓冲区必须只是wav数据的char *,而不是 header 。我错误地认为队列将 header 解析了出来。

关于android - OpenSL:是什么原因导致通过AAsset_read加载WAV文件时出现静态现象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31211845/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com