我正在尝试在我的Android应用程序中播放Raw(int16 PCM)编码的音频文件。我一直关注并阅读Oboe文档/示例,试图获取我自己的音频文件。
我需要播放的音频文件大约是6kb,或1592帧(立体声)。
要么没有声音播放,要么声音/抖动在启动时播放(输出变化不同 - 见下文)
故障排除
更新我已经切换到浮点数进行缓冲区排队,而不是将所有内容保存到int16_t(并在完成时转换回int16_t),尽管现在我又回到没有声音了。
音频似乎要么没有播放,要么在启动时播放(这是错误的)。按'开始'后声音应播放。
(我试过运行systrace,但老实说我不知道我在找什么)
履行
我目前在构建器中尝试的内容:
createPlaybackStream()
或this
onAudioReady()
LowLatency
我在这里使用来自Rhythm Game样本的Exclusive
类和Player
类:AAssetManager
。我正在使用这些类加载我的资源并播放声音。 https://github.com/google/oboe/blob/master/samples/RhythmGame将音频数据写入输出缓冲区。
以下是我的音频引擎的相关方法:
Player.renderAudio
void AudioEngine::createPlaybackStream() {
// // Load the RAW PCM data files into memory
std::shared_ptr<AAssetDataSource> soundSource(AAssetDataSource::newFromAssetManager(assetManager, "sound.raw", ChannelCount::Mono));
if (soundSource == nullptr) {
LOGE("Could not load source data for sound");
return;
}
sound = std::make_shared<Player>(soundSource);
AudioStreamBuilder builder;
builder.setCallback(this);
builder.setPerformanceMode(PerformanceMode::LowLatency);
builder.setSharingMode(SharingMode::Exclusive);
builder.setChannelCount(mChannelCount);
Result result = builder.openStream(&stream);
if (result == Result::OK && stream != nullptr) {
mSampleRate = stream->getSampleRate();
mFramesPerBurst = stream->getFramesPerBurst();
int channelCount = stream->getChannelCount();
if (channelCount != mChannelCount) {
LOGW("Requested %d channels but received %d", mChannelCount, channelCount);
}
// Set the buffer size to (burst size * 2) - this will give us the minimum possible latency while minimizing underruns
stream->setBufferSizeInFrames(mFramesPerBurst * 2);
if (setBufferSizeResult != Result::OK) {
LOGW("Failed to set buffer size. Error: %s", convertToText(setBufferSizeResult.error()));
}
// Start the stream - the dataCallback function will start being called
result = stream->requestStart();
if (result != Result::OK) {
LOGE("Error starting stream. %s", convertToText(result));
}
} else {
LOGE("Failed to create stream. Error: %s", convertToText(result));
}
}
DataCallbackResult AudioEngine::onAudioReady(AudioStream *audioStream, void *audioData, int32_t numFrames) {
int16_t *outputBuffer = static_cast<int16_t *>(audioData);
sound->renderAudio(outputBuffer, numFrames);
return DataCallbackResult::Continue;
}
将缓冲区容量设置为(任何大于我的音频文件帧数)
您无需设置缓冲区容量。这将自动设置在合理的水平。通常~3000帧。请注意,缓冲区容量与缓冲区大小不同,默认为2 * framesPerBurst。
将突发大小(每次回调的帧数)设置为(等于或低于缓冲容量/ 2的任何值)
再说一次,不要这样做。每次流需要更多音频数据时都会调用// When the 'start' button is pressed, it calls this method with true
// There should be no sound on app start-up until this button is pressed
// Sound stops when 'stop' is pressed
setPlaying(bool isPlaying) {
sound->setPlaying(isPlaying);
}
,onAudioReady
表示你应该提供多少帧。如果使用的值不是音频设备的本机突发大小(典型值为128,192和240帧,具体取决于底层硬件)的精确比率,则覆盖此值,则可能会出现音频故障。
我已切换到浮动缓冲区排队
提供数据所需的格式由音频流决定,只有在打开流后才能知道。你可以通过调用numFrames
来获得它。
在stream->getFormat()
样本(至少RhythmGame
)中,格式如何工作:
AAssetDataSource::newFromAssetManager
内部1592帧(立体声)。
你说你的源是立体声,但你在这里指定为单声道:
std :: shared_ptr soundSource(AAssetDataSource :: newFromAssetManager(assetManager,“sound.raw”,ChannelCount :: Mono));
毫无疑问,这将导致音频问题,因为onAudioReady
将具有AAssetDataSource
的值,这是正确值的两倍。这将导致音频故障,因为有一半的时间你将播放系统内存的随机部分。