How to check if AVPlayer has Video or just Audio?

后端 未结 4 1694
一向
一向 2021-01-12 09:48

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.



        
相关标签:
4条回答
  • 2021-01-12 10:21

    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
        }
    
    0 讨论(0)
  • 2021-01-12 10:24

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-12 10:29

    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)
    
    0 讨论(0)
  • 2021-01-12 10:38

    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.

    0 讨论(0)
提交回复
热议问题