加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何调用CMSampleBufferGetAudioBufferListWithRetainedBlockBuf

发布时间:2020-12-14 04:45:17 所属栏目:百科 来源:网络整理
导读:我试图弄清楚如何在 Swift中调用这个AVFoundation函数.我花了很多时间摆弄声明和语法,并且做到了这一点.编译器大部分都很开心,但我最后还是陷入了困境. public func captureOutput( captureOutput: AVCaptureOutput!,didOutputSampleBuffer sampleBuffer: CM
我试图弄清楚如何在 Swift中调用这个AVFoundation函数.我花了很多时间摆弄声明和语法,并且做到了这一点.编译器大部分都很开心,但我最后还是陷入了困境.

public func captureOutput(
    captureOutput: AVCaptureOutput!,didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,fromConnection connection: AVCaptureConnection!
) {
    let samplesInBuffer = CMSampleBufferGetNumSamples(sampleBuffer)
    var audioBufferList: AudioBufferList

    var buffer: Unmanaged<CMBlockBuffer>? = nil

    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
        sampleBuffer,nil,&audioBufferList,UInt(sizeof(audioBufferList.dynamicType)),UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),&buffer
    )

    // do stuff
}

编译器抱怨第3和第4个参数:

Address of variable ‘audioBufferList’ taken before it is initialized

Variable ‘audioBufferList’ used before being initialized

那我该怎么办呢?

我正在使用this StackOverflow answer,但它是Objective-C.我正试图将其翻译成Swift,但遇到了这个问题.

或者是否有更好的方法?我需要从缓冲区读取数据,一次一个样本,所以我基本上试图得到一些我可以迭代的样本数组.

解决方法

免责声明:我刚刚尝试将代码从 Reading audio samples via AVAssetReader转换为Swift,并验证它是否已编译.我还没有
测试它是否真的有效.

// Needs to be initialized somehow,even if we take only the address
var audioBufferList = AudioBufferList(mNumberBuffers: 1,mBuffers: AudioBuffer(mNumberChannels: 0,mDataByteSize: 0,mData: nil))

var buffer: Unmanaged<CMBlockBuffer>? = nil

CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
    sampleBuffer,&buffer
)

// Ensure that the buffer is released automatically.
let buf = buffer!.takeRetainedValue() 

// Create UnsafeBufferPointer from the variable length array starting at audioBufferList.mBuffers
let audioBuffers = UnsafeBufferPointer<AudioBuffer>(start: &audioBufferList.mBuffers,count: Int(audioBufferList.mNumberBuffers))

for audioBuffer in audioBuffers {
    // Create UnsafeBufferPointer<Int16> from the buffer data pointer
    var samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(audioBuffer.mData),count: Int(audioBuffer.mDataByteSize)/sizeof(Int16))

    for sample in samples {
        // ....
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读