我尝试使用
AVAudioSourceNode
和 AVAudioSinkNode
获取音频输入(并同时输出)。问题是,即使在苹果网站上,我也无法找到有关如何设置它的正确示例。我基本上这样做:
AVAudioEngine* m_engine = [AVAudioEngine new];
AVAudioSession* m_avSession = AVAudioSession.sharedInstance;
if (![m_avSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error] ||
![m_avSession setActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error])
{
m_logger.Error("Could not activate Audio Session: ", error.description);
}
AVAudioSinkNode* CreateInputNode(AVAudioFormat* format)
{
AVAudioSinkNodeReceiverBlock block = [checker, format] (const AudioTimeStamp* timestamp, AVAudioFrameCount frameCount, const AudioBufferList* inputData) {
ProcessAudioForInput(format, inputData, frameCount);
return OSStatus(noErr);
};
return [[AVAudioSinkNode alloc] initWithReceiverBlock:block];
}
AVAudioSessionRouteDescription* route = m_avSession.currentRoute;
AVAudioFormat* inputFormat = [m_engine.inputNode inputFormatForBus:FIRST_AUDIO_BUS];
auto sinkNode = CreateInputNode(inputFormat);
if (sinkNode) {
[m_engine attachNode:sinkNode];
[m_engine connect:sinkNode to:m_engine.inputNode format:inputFormat];
channelCount = inputFormat.channelCount;
}
else {
m_logger.Error("Could not create input node");
}
[m_engine connect:sinkNode
行生成一个异常,我完全不知道如何调试:
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: inSrcImpl->NumberOutputs() > 0'
任何人都可以帮助我或向我指出适当的资源来解释如何设置它,而不是盲目地进行设置。请注意,我观看了 WWC 的一些视频,例如 https://developer.apple.com/videos/play/wwdc2019/510/ 但源代码和解释并不能帮助我解决这个问题。
我认为你的箭头弄反了,应该是这样
inputNode -> sinkNode
不是
sinkNode -> inputNode
,
所以试试这个吧
[m_engine connect: m_engine.inputNode to:sinkNode format:inputFormat];