AVPlayer 播放在线视频和本地文件的设置区别

时间:2022-08-31 23:26:16

首先使用AVPlayer 必须导入框架:AVFoundation 以及 头文件:#<MediaPlayer/MediaPlayer.h>

两者的主要区别在路径的初始化:

1、播放本地视频

 NSURL *sourceMovieUrl = [NSURL fileURLWithPath:@"本地视频路径"];

    AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieUrl options:nil];

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];

    _myPlayerItem1 = playerItem;

    _player1 = [AVPlayer playerWithPlayerItem:playerItem];

    _playerLayer1 = [AVPlayerLayer playerLayerWithPlayer:_player1];

    _playerLayer1.frame = CGRectMake(0, 0, 1024, 600);

   _playerLayer1.videoGravity =AVLayerVideoGravityResizeAspect;

    [self.view.layer addSublayer:_playerLayer1];

     [_player1 play];

2、播放在线视频

 //使用playerItem获取视频的信息,当前播放时间,总时间等

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"在线视频路径"]];

    //player是视频播放的控制器,可以用来快进播放,暂停等

    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_LGCustomMoviePlayerController.player];

    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;

    //调用一下setter方法

    [_LGCustomMoviePlayerController setPlayer:player];

   

    [_LGCustomMoviePlayerController.player play];