Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if AVPlayer has Video or just Audio?

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?

like image 506
Jeanette Müller Avatar asked Jul 28 '12 20:07

Jeanette Müller


3 Answers

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)
like image 141
wdyp Avatar answered Nov 16 '22 15:11

wdyp


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
    }
}
like image 21
Sean Stayns Avatar answered Nov 16 '22 17:11

Sean Stayns


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.

like image 3
Anil Avatar answered Nov 16 '22 17:11

Anil