ScreenCaptureKit - Sample project doesn't capture audio on MacOS 13.3

It was working fine on previous versions.

Error message: CaptureSample[13228:113286] [AVAB] AVAudioBuffer.mm:248 the number of buffers (810878176) does not match the format's number of channel streams (2)

From what I've found, this error is coming from the function createPCMBuffer on CaptureEngine

Link to sample project https://developer.apple.com/documentation/screencapturekit/capturing_screen_content_in_macos

Replacing the createPCMBuffer func for this one fixed the issue (source: https://stackoverflow.com/questions/75228267/avaudioplayernode-causing-distortion)

Are there any concerns in doing it this way?

func createPCMBuffer(from sampleBuffer: CMSampleBuffer) -> AVAudioPCMBuffer? {
    let numSamples = AVAudioFrameCount(sampleBuffer.numSamples)
    let format = AVAudioFormat(cmAudioFormatDescription: sampleBuffer.formatDescription!)
    let pcmBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: numSamples)!
    pcmBuffer.frameLength = numSamples
    CMSampleBufferCopyPCMDataIntoAudioBufferList(sampleBuffer, at: 0, frameCount: Int32(numSamples), into: pcmBuffer.mutableAudioBufferList)
    return pcmBuffer
}

Thanks

I've also seen the createPCMBuffer implementation return nil in certain cases, breaking our microphone audiowave code and ScreenCaptureKit audio recording implementation.

Seems like copying the unsafe pointer and using it outside the block you pass to withAudioBufferList isn't a good idea. We've adjusted our implementation to this, and that fixes the problems:

func createPCMBuffer(for sampleBuffer: CMSampleBuffer) -> AVAudioPCMBuffer? {
    try? sampleBuffer.withAudioBufferList { audioBufferList, _ -> AVAudioPCMBuffer? in
        guard let absd = sampleBuffer.formatDescription?.audioStreamBasicDescription else { return nil }
        guard let format = AVAudioFormat(standardFormatWithSampleRate: absd.mSampleRate, channels: absd.mChannelsPerFrame) else { return nil }
        return AVAudioPCMBuffer(pcmFormat: format, bufferListNoCopy: audioBufferList.unsafePointer)
    }
}

I'm using code from this sample: https://developer.apple.com/documentation/screencapturekit/capturing-screen-content-in-macos

It's the same as the code posted by @mac_cain13 that uses .withAudioBufferList function.

It doesn't work for me when capturing microphone audio with ScreenCaptureKit on macOS 15.3.1 - the function always returns nil. However, the same code works without an issue when capturing system audio with ScreenCaptureKit. It only fails to create AVAudioPCMBuffer from CMSampleBuffer when capturing microphone. The sample buffer with microphone audio seems to be fine, because the audio is there (I can save it to file on disk using AVAssetWriter and play it back without an issue).

I only need the AVAudioPCMBuffer to visualize recorded audio in my app. Besides this use case, I write CMSampleBuffer to files on disk, and it works well. Perhaps there is another way?

ScreenCaptureKit - Sample project doesn't capture audio on MacOS 13.3
 
 
Q