0

I was wondering how exactly I can implement the Core Media method CMSampleBufferGetAudioBufferList in swift.

I'm following this tutorial, which uses the method to get a list of AudioBuffers from a CMSampleBuffer.

I've tried over and over, but the compiler keeps giving me the generic

Cannot invoke CMSampleBuffer...Buffer with an argument list of type ...

which isn't very helpful.

I've already seen this StackOverflow question, but the only answer there seems to throw the exact same error I've been getting.

Basically, I just want someone to show me how to get this method to compile without errors in swift.

Community
  • 1
  • 1
abm
  • 1
  • 1

2 Answers2

2

I assume you have access to a sampleBuffer in a delegate method such as captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)

I've already struggled with the CMSampleBuffer methods, and I agree it is not obvious how to make them compile.

    var sizeOut = UnsafeMutablePointer<Int>.alloc(1)
    var listOut = UnsafeMutablePointer<AudioBufferList>.alloc(1)
    let listSize: Int = 10

    var blockBufferOut: Unmanaged<CMBlockBuffer>?


    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, sizeOut , listOut, listSize, kCFAllocatorDefault, kCFAllocatorDefault, UInt32(2), &blockBufferOut)

you will then need to call takeRetainedValueon the block buffer out, and handle your pointer to release them manually.

HHK
  • 1,352
  • 1
  • 15
  • 25
0

it works for me. try it...

            let musicUrl: NSURL = mediaItemCollection.items[0].valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL
            let asset: AVURLAsset = AVURLAsset(URL: musicUrl, options: nil)
            let assetOutput = AVAssetReaderTrackOutput(track: asset.tracks[0] as! AVAssetTrack, outputSettings: nil)

            var error : NSError?

            let assetReader: AVAssetReader = AVAssetReader(asset: asset, error: &error)

            if error != nil {
                print("Error asset Reader: \(error?.localizedDescription)")
            }

            assetReader.addOutput(assetOutput)
            assetReader.startReading()

            let sampleBuffer: CMSampleBufferRef = assetOutput.copyNextSampleBuffer()

            var audioBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
            var blockBuffer: Unmanaged<CMBlockBuffer>? = nil


            CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
                sampleBuffer,
                nil,
                &audioBufferList,
                sizeof(audioBufferList.dynamicType), // instead of UInt(sizeof(audioBufferList.dynamicType))
                nil,
                nil,
                UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
                &blockBuffer
            )
Włodzimierz Woźniak
  • 3,106
  • 1
  • 26
  • 23