AVAudioSourceNode如何设置设置格式

问题描述 投票:0回答:1

我正在使用新的AVAudioSourceNode提供通过我的音频接口播放的样本流。

但是我发现AVAudioSourceNode正在选择它自己的格式设置。

engine.connect(srcNode, to: engine.mainMixerNode, format: stimulusFormat)

我使用上面的代码行以'stimulusFormat'格式将源节点连接到主混音器我已使用以下格式设置了此格式:

let stimulusFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32,
        sampleRate: Double(sampleRate),
        channels: 1,
        interleaved: format.isInterleaved)

((交错参数查询输出期望看到的内容)。但是,每当我查询srcNode(AVAudioSourceNode)的格式时,控制台都会返回

<AVAudioFormat 0x600002131810:  2 ch,  44100 Hz, Float32, non-inter>

显然,在代码的连接行中未设置流格式。我也尝试过更改首选的采样率,但是这仍然不会更改AVSourceNode采样率。音频引擎中的所有其他内容均以48000Hz运行,因此我不确定从何处获得此数字。

try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
try AVAudioSession.sharedInstance().setPreferredSampleRate(Double(sampleRate))

有人可以建议一种定义流格式的方法吗?

swift audio avfoundation core-audio avaudioengine
1个回答
0
投票

尝试使用

engine.connect(srcNode, to: engine.mainMixerNode, format: srcNode.inputFormat(forBus: 0))
© www.soinside.com 2019 - 2024. All rights reserved.