How do I call CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer?

后端 未结 6 1028
北荒
北荒 2021-01-13 04:10

I\'m trying to figure out how to call this AVFoundation function in Swift. I\'ve spent a ton of time fiddling with declarations and syntax, and got this far.

6条回答
  •  有刺的猬
    2021-01-13 04:38

    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? = nil
    
    
    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
        sampleBuffer,
        nil,
        &audioBufferList,
        sizeof(audioBufferList.dynamicType), // instead of UInt(sizeof(audioBufferList.dynamicType))
        nil,
        nil,
        UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
        &blockBuffer
    )
    

提交回复
热议问题