How to check if AVPlayer has Video or just Audio?

爱⌒轻易说出口 提交于 2020-01-11 05:05:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!