audioEngine.start() 调用时抛出异常

发布于 2025-01-14 01:45:32 字数 1502 浏览 5 评论 0原文



问题
我正在尝试使用 AVAudioEngine 录制麦克风数据。这是我的设置:

首先,我创建单例会话,设置会话类别并激活它:

let audioSession = AVAudioSession.sharedInstance()
    
do {
    try audioSession.setCategory(.record)
    try audioSession.setActive(true)
} catch {
    ...
}

之后,我为总线 0 创建输入格式,连接输入和输出节点并在输入节点上安装 tap(我也尝试点击输出节点):

let inputFormat = self.audioEngine.inputNode.inputFormat(forBus: 0)
self.audioEngine.connect(self.audioEngine.inputNode, to: self.audioEngine.outputNode, format: inputFormat)
self.audioEngine.outputNode.installTap(onBus: 0, bufferSize: bufferSize, format: inputFormat) { (buffer, time) in
    let theLength = Int(buffer.frameLength)
    var samplesAsDoubles:[Double] = []
    
    for i in 0 ..< theLength
    {
        let theSample = Double((buffer.floatChannelData?.pointee[i])!)
        samplesAsDoubles.append( theSample )
    }
    ...
}

全部上面的内容在一个函数中。我还有另一个名为 startRecording 的函数,其中包含以下内容:

do {
    try audioEngine.start()
} catch {
    ...
}

我还验证了麦克风权限是否已授予。

start 方法失败,这是响应:

操作无法完成。 (com.apple.coreaudio.avfaudio 错误-10875。)


问题
根据文档,可能有以下三种原因:

  • 图形结构存在问题,例如输入无法通过转换器节点路由到输出或记录分接头。

  • 发生 AVAudioSession 错误。

  • 驱动程序无法启动硬件。

我不相信这是会话一,因为我通过文档设置了它。

如果驱动程序出现故障,我将如何检测并处理它?

如果图表设置不正确,我哪里搞砸了?

Problem
I'm trying to record microphone data using AVAudioEngine. This is my setup:

First I create singleton session, set sessions category and activate it:

let audioSession = AVAudioSession.sharedInstance()
    
do {
    try audioSession.setCategory(.record)
    try audioSession.setActive(true)
} catch {
    ...
}

After that I create input format for bus 0, connect input and output nodes and install tap on input node (I also tried to tap output node):

let inputFormat = self.audioEngine.inputNode.inputFormat(forBus: 0)
self.audioEngine.connect(self.audioEngine.inputNode, to: self.audioEngine.outputNode, format: inputFormat)
self.audioEngine.outputNode.installTap(onBus: 0, bufferSize: bufferSize, format: inputFormat) { (buffer, time) in
    let theLength = Int(buffer.frameLength)
    var samplesAsDoubles:[Double] = []
    
    for i in 0 ..< theLength
    {
        let theSample = Double((buffer.floatChannelData?.pointee[i])!)
        samplesAsDoubles.append( theSample )
    }
    ...
}

All of the above is in one function. I also have another function called startRecording which contains the following:

do {
    try audioEngine.start()
} catch {
    ...
}

I have also verified microphone permissions are granted.

start method fails and this is the response:

The operation couldn’t be completed. (com.apple.coreaudio.avfaudio error -10875.)

Questions
Based on documentation there are three possible causes:

  • There’s a problem in the structure of the graph, such as the input can’t route to an output or to a recording tap through converter nodes.

  • An AVAudioSession error occurs.

  • The driver fails to start the hardware.

I don't believe it's the session one, because I set it up via documentation.

If the driver fails, how would I detect that and handle it?

If graph is setup incorrectly, where did I mess it up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2025-01-21 01:45:32

如果要录制麦克风,请将水龙头附加到输入节点,而不是输出节点。 Taps 观察节点的输出。输出节点没有“输出”。这是水槽。

如果您需要在“输出节点”之前安装一个水龙头(在更复杂的图中可能不是输入节点),您可以在输入和输出之间插入一个混音器,并将水龙头连接到混音器。

另外,请确保您确实想要将输入节点连接到此处的输出节点。除非您想实时播放音频,否则无需这样做。如果您只想录制麦克风,您只需将一个水龙头附加到输入节点,而无需构建更多图表。 (这也可能导致您的部分问题,因为您将类别设置为 .record,即不播放。我不知道在这种情况下将某些内容连接到输出会导致异常,但是这绝对没有意义。)

If you want to record the microphone, attach the tap to the inputNode, not the outputNode. Taps observe the output of a node. There is no "output" of the outputNode. It's the sink.

If you need to install a tap "immediately before the outputNode" (which might not be the input node in a more complicated graph), you can insert a mixer between the input(s) and the output and attach the tap to the mixer.

Also, make sure you really want to connect the input node to the output node here. There's no need to do that unless you want to playback audio live. You can just attach a tap to the input node without building any more of the graph if you just want to record the microphone. (This also might be causing part of your problem, since you set your category to .record, i.e. no playback. I don't know that wiring something to the output in that case causes an exception, but it definitely doesn't make sense.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文