在 AVAssetTrack 中检测 AudioLayout

Detect AudioLayout in AVAssetTrack

我需要从 AVAssetTrack 检测通道数和音频格式(交错或非交错)。我尝试了以下代码来检测通道数。从代码中可以看出,有两种方法可以检测通道数。我想知道哪个更可靠和正确,或者 none 个(不考虑音频格式)?

  if let formatDescriptions = track.formatDescriptions as? [CMAudioFormatDescription],
        let audioFormatDesc = formatDescriptions.first,
        let asbd = CMAudioFormatDescriptionGetStreamBasicDescription(audioFormatDesc)
    {                                   
       //First way to detect number of channels
        numChannels = asbd.pointee.mChannelsPerFrame 
        
        var aclSize:size_t = 0
        var currentChannelLayout:UnsafePointer<AudioChannelLayout>? = nil
            
        currentChannelLayout = CMAudioFormatDescriptionGetChannelLayout(audioFormatDesc, sizeOut: &aclSize)
        
        if let currentChannelLayout = currentChannelLayout, aclSize > 0 {
            let channelLayout = currentChannelLayout.pointee
            
            //second way of detecting number of channels
            numChannels = AudioChannelLayoutTag_GetNumberOfChannels(channelLayout.mChannelLayoutTag) 
        }

}

而且我不知道如何获取音频格式的详细信息(交错或非交错)。在这方面寻求帮助。

使用AudioStreamBasicDescription。所有音频 CMFormat 都有一个,而 AudioChannelLayout 是可选的:

https://developer.apple.com/documentation/coremedia/1489137-cmaudioformatdescriptiongetchann?language=objc

AudioChannelLayouts are optional; this API returns NULL if one doesn’t exist.