AVPlayer实现视频播放功能

时间:2022-04-01 04:45:23

    NSURL *sourceMovieURL = [NSURLURLWithString:@"http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGQ"];

    

    AVAsset *movieAsset = [AVURLAssetURLAssetWithURL:sourceMovieURLoptions:nil];

    AVPlayerItem *playerItem = [AVPlayerItemplayerItemWithAsset:movieAsset];

    self.playerItem = playerItem;

    AVPlayer *player = [AVPlayerplayerWithPlayerItem:playerItem];

    self.player = player;

    AVPlayerLayer *playerLayer = [AVPlayerLayerplayerLayerWithPlayer:player];

    playerLayer.frame = self.view.layer.bounds;

    playerLayer.videoGravity =AVLayerVideoGravityResizeAspect;

    [self.view.layeraddSublayer:playerLayer];


[self.playerplay];

注:1.AVPlayer本身是无法显示在界面上的,所以需要将其添加到View的layer上

     2.AVPlayer能播放视频的格式有限,并不是所有的网络视频都能播放


简单demo:https://github.com/WLChopSticks/AVPlayerVideo/tree/master