以与 AVAudioSession 的采样率不同的频率捕获的 AVCaptureSession 音频样本

时间:2021-05-24 02:26:52

标签: audio avfoundation avcapturesession avaudiosession

我正在使用 AVFoundation 捕获会话通过 AVCaptureAudioDataOutput 输出音频缓冲区。捕获会话使用默认应用程序音频会话。 (即 captureSession.usesApplicationAudioSession = true)。我不会以任何方式更改音频会话。

奇怪的行为是捕获会话返回以与默认音频会话的采样率不同的频率捕获的音频缓冲区。

特别是:

print(AVAudioSession.sharedInstance().sampleRate) \\ 48000

但是

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        if connection.audioChannels.first != nil {
            print(sampleBuffer.presentationTimeStamp) \\ CMTime(value: 2199812320, timescale: 44100, flags: __C.CMTimeFlags(rawValue: 3), epoch: 0)
            delegate?.captureOutput(sampleBuffer: sampleBuffer, mediaType: .audio)
        } 
    }

我的预期行为是样本缓冲区的时间刻度也是 48000。

对于一些额外的信息,如果我确实更改了默认音频会话,例如,将首选采样率更改为 48000,则示例缓冲区的时间刻度将按预期更改为 48000。这是一个错误还是我误解了什么?

1 个答案:

答案 0 :(得分:0)

在开始捕获会话之前,您需要将捕获会话的 automaticallyConfiguresApplicationAudioSession 设置为 false 并进行自己的音频会话配置。

像这样:

// use audioSession.setPreferredSampleRate() to request desired sample rate
captureSession.automaticallyConfiguresApplicationAudioSession = false
try! AVAudioSession.sharedInstance().setCategory(.playAndRecord) // or just record
try! AVAudioSession.sharedInstance().setActive(true) // worked without this, but feels wrong