AVPlayer : Get codec type for all audio tracks available

I am working on a video app and I need to get the codec type and channel number of all audio tracks.

I succeed to get this value for the current track via assetTrack :

 if ([track.assetTrack.mediaType isEqual:AVMediaTypeAudio]){
    AVAssetTrack* assetTrack = track.assetTrack;
    NSArray* formatDesc = assetTrack.formatDescriptions;
    for(unsigned int j = 0; j < [formatDesc count]; ++j) {
        CMAudioFormatDescriptionRef item = (__bridge CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:j];
        const AudioStreamBasicDescription* audioDes = CMAudioFormatDescriptionGetStreamBasicDescription (item);
        if(audioDes) {
            NSLog(@"channel:%d",audioDes->mChannelsPerFrame);
        }
        CMFormatDescriptionRef item2 = (__bridge CMFormatDescriptionRef)[formatDesc objectAtIndex:j];
        FourCharCode result = CMFormatDescriptionGetMediaSubType(item2);
        if(result) {
            NSLog(@"codec:@%@",[self FourCCString:result]);
        }
    }
}     

How to get these values for the audio tracks not played ? Is it possible or this is a AVPlayer limitation ?

Thanks,

Fabien

Have you tried running the above code on all the tracks vended by -[AVAsset loadTracksWithMediaType:completionHandler:]?

Hi,

I just tried loadTracksWithMediaType and loadTracksWithMediaCharacteristic without success. I always got an array with 0 elements. I did the job when the item go the state AVPlayerItemStatusReadyToPlay. For info, I use the currentItem of the AVPlayer instance : self.currentItem.asset

Thanks Fabien

Ah, your asset is most likely HLS. Have you tried examining the AVAsset.variants?

AVPlayer : Get codec type for all audio tracks available
 
 
Q