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.
This helped me:
// source from https://stackoverflow.com/a/55581216 Changed for last release AVFoundation
func isAudioAvailable() -> Bool? {
return self.player.currentItem?.tracks.filter({$0.assetTrack!.mediaType == AVMediaType.video}).count != 0
}
func isVideoAvailable() -> Bool? {
return self.player.currentItem?.tracks.filter({$0.assetTrack!.mediaType == AVMediaType.video}).count != 0
}
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
}
}
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)
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.