开发者

Finding the number of Channels from an AVAsset

开发者 https://www.devze.com 2023-03-25 11:54 出处:网络
I am loading Audio assets via AVAssets.I want to figure开发者_如何学Python out how many channels (mono or stereo basically) are in the asset.What is the best way to do this?This appears to be what I a

I am loading Audio assets via AVAssets. I want to figure开发者_如何学Python out how many channels (mono or stereo basically) are in the asset. What is the best way to do this?


This appears to be what I am looking for.

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;
    }
}


Swift 5 implementation of TurqMage's answer

//
//  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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消