AVCaptureSession audio samples captured at different frequency than AVAudioSession’s sample rate

Asked By: Anonymous

I’m using AVFoundation capture session to output audio buffers through AVCaptureAudioDataOutput. The capture session is using the default application audio session. (ie. captureSession.usesApplicationAudioSession = true). I don’t alter the audio session in any way.

The strange behavior is that the capture session returns audio buffers captured at a different frequency than the default audio session’s sample rate.

Specifically:

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

but

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)
        } 
    }

My expected behavior is that the sample buffer’s timescale would also be 48000.

For a little extra info, if I do change the default audio session, for example, change preferred sample rate to 48000, the sample buffer’s timescale will change to 48000 as expected. Is this a bug or am I misunderstanding something?


Solution

Answered By: Anonymous

You need to set the capture session’s automaticallyConfiguresApplicationAudioSession to false and do your own audio session configuration before starting the capture session.

Like this:

// 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
techinplanet staff

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno