从AVAsset中查找通道数

时间:2011-08-03 17:49:52

标签: iphone ios ios4 core-audio

我通过AVAssets加载音频资源。我想弄清楚资产中有多少通道(基本上是单声道或立体声)。这样做的最佳方式是什么?

2 个答案:

答案 0 :(得分:8)

这似乎是我正在寻找的。

AVAssetTrack* songTrack = [mAssetToLoad.tracks objectAtIndex:0];
NSArray* formatDesc = songTrack.formatDescriptions;
for(unsigned int i = 0; i < [formatDesc count]; ++i) {
    CMAudioFormatDescriptionRef item = (CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:i];
    const AudioStreamBasicDescription* bobTheDesc = CMAudioFormatDescriptionGetStreamBasicDescription (item);
    if(bobTheDesc && bobTheDesc->mChannelsPerFrame == 1) {
        mIsMono = true;
    }
}

答案 1 :(得分:0)

TurqMage 答案的 Swift 5 实现

//
//  AVAssetTrack+IsStereo.swift
//

import AVFoundation

extension AVAssetTrack {
    
    var isStereo: Bool {
        
        for item in (formatDescriptions as? [CMAudioFormatDescription]) ?? [] {
            
            let basic = CMAudioFormatDescriptionGetStreamBasicDescription(item)
            let numberOfChannels = basic?.pointee.mChannelsPerFrame ?? 0
            
            if numberOfChannels == 2 {
                return true
            }
        }
        return false
    }
}