问题
I try to play some "media" but at the time the AVPlayer starts I don't know if it is audio or Video.
I connected the player Layer and it works fine.
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 Video so I can add/remove some Options?
回答1:
From Apple’s sample code project AVSimplePlayer:
// Set up an AVPlayerLayer according to whether the asset contains video.
if ([[(AVAsset *)asset tracksWithMediaType:AVMediaTypeVideo] count] != 0)
回答2:
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 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
}
}
回答3:
I'm not sure but AVPlayerItem
has the following array [mPlayerItem.asset.tracks]
This contains two objects,one for video and another for audio.Access it as follows [mPlayerItem.asset.tracks objectAtIndex:0]
for video and [mPlayerItem.asset.tracks objectAtIndex:1]
for audio.
来源:https://stackoverflow.com/questions/11704322/how-to-check-if-avplayer-has-video-or-just-audio