How to check if AVPlayer has video or just audio?

I'm trying to play some "media", but at the time that AVPlayer starts up, I don't know if it is audio or video.

I plugged in the player level and it works great.

self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:[[PCPlayerManager sharedManager] audioPlayer]]; avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect; avPlayerLayer.frame = CGRectMake(0, 0, videoView.frame.size.width, videoView.frame.size.height); [videoView.layer addSublayer:avPlayerLayer]; 

But how can I check if there is a video so that I can add / remove some parameters?

+5
source share
3 answers

From the Apples AVSimplePlayer sample code project:

 // Set up an AVPlayerLayer according to whether the asset contains video. if ([[(AVAsset *)asset tracksWithMediaType:AVMediaTypeVideo] count] != 0) 
+6
source

I'm not sure, but AVPlayerItem has the following array [mPlayerItem.asset.tracks] . It contains two objects: one for video and one for audio. For it, follow [mPlayerItem.asset.tracks objectAtIndex:0] for video and [mPlayerItem.asset.tracks objectAtIndex:1] for audio.

+2
source

For Swift 4.2 you can do the following:

 func isAudioAvailable() -> Bool? { return self.player?._asset?.tracks.filter({$0.mediaType == AVMediaType.audio}).count != 0 } func isVideoAvailable() -> Bool? { return self.player?._asset?.tracks.filter({$0.mediaType == AVMediaType.video}).count != 0 } 

or as an extension

 extension AVPlayer { var isAudioAvailable: Bool? { return self._asset?.tracks.filter({$0.mediaType == AVMediaType.audio}).count != 0 } var isVideoAvailable: Bool? { return self._asset?.tracks.filter({$0.mediaType == AVMediaType.video}).count != 0 } } 
0
source

All Articles