swift2 无法从捕获输出中获取AudioStreamBasicDescription

trnvg8h3  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(181)

我正在尝试访问mSampleRate和mChannelsPerFrame,并将这些值赋给全局变量。

方法:

func setAudioFormat(format: CMFormatDescriptionRef) {
    let asbd: UnsafePointer<AudioStreamBasicDescription> = CMAudioFormatDescriptionGetStreamBasicDescription(format)

    sampleRate = asbd.memory.mSampleRate // breakpoint
    channels = asbd.memory.mChannelsPerFrame

}

方法调用:

func captureOutput(captureOutput: AVCaptureOutput!, var didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    ...

    let format: CMFormatDescriptionRef = CMSampleBufferGetFormatDescription(sampleBuffer)!
    self.setAudioFormat(format)

    ...
}

我做错了什么吗?有没有更好的方法从捕获输出samplebuffer获取AudioStreamBasicDescription

编辑:

格式正在保存这些值:

<CMAudioFormatDescription 0x14516150 [0x346c08a0]> {
mediaType:'soun' 
mediaSubType:'lpcm' 
mediaSpecific: {
    ASBD: {
        mSampleRate: 44100.000000 
        mFormatID: 'lpcm' 
        mFormatFlags: 0xc 
        mBytesPerPacket: 2 
        mFramesPerPacket: 1 
        mBytesPerFrame: 2 
        mChannelsPerFrame: 1 
        mBitsPerChannel: 16     } 
    cookie: {(null)} 
    ACL: {(null)} 
} 
extensions: {(null)}
}
h4cxqtbf

h4cxqtbf1#

一旦有了CMFormatDescriptionRef示例,就可以使用下面的代码(在Objective-C中,对不起)来检索ASBD数据:

const AudioFormatListItem *audioFormatListItem = CMAudioFormatDescriptionGetFormatList(formatDescription, nil);
AudioStreamBasicDescription asbd = audioFormatListItem->mASBD;
float sampleRate = asbd.mSampleRate;

相关问题