我目前正在做一个需要低延迟音频的项目,所以我决定使用 Oboe(谷歌库)来处理它。
我已经阅读了文档,但其中出现了多个问题。
首先,一旦我使用构建器模式创建了流,打开流并请求启动流,我如何捕获来自麦克风的数据?
我知道有两种方法:使用读取函数或回调。
问题:
但是,如果我使用 read 函数,只要流启动,我如何确保它正在被调用?
如何使用回调获取数据并进行处理?
我将在下面留下我的代码
void HelloOboeEngine::start() {
isRecording = true;
oboe::Result result = createStream();
if (result != oboe::Result::OK) {
closeStream(mRecordingStream);
}
result = mRecordingStream->requestStart();
if (result != oboe::Result::OK) {
closeStream(mRecordingStream);
}
}
oboe::Result HelloOboeEngine::createStream() {
oboe::AudioStreamBuilder builder;
return builder.setPerformanceMode(oboe::PerformanceMode::LowLatency)
->setDirection(oboe::Direction::Input)
->setFormat(mFormat)
->setFormatConversionAllowed(true)
->setChannelConversionAllowed(true)
->setSampleRateConversionQuality(oboe::SampleRateConversionQuality::Medium)
->openStream(mRecordingStream);
}
void HelloOboeEngine::closeStream(std::shared_ptr<oboe::AudioStream> &stream) {
if (stream) {
oboe::Result result = stream->stop();
if (result != oboe::Result::OK) {
LOGW("Error stopping stream: %s", oboe::convertToText(result));
}
result = stream->close();
if (result != oboe::Result::OK) {
LOGE("Error closing stream: %s", oboe::convertToText(result));
} else {
LOGW("Successfully closed stream");
}
stream.reset();
}
}