前言
iOS8之后系统自带使用AVPlayerViewController播放视频
AVPlayerViewController
AVPlayerViewController和导航控制器差不多,需要将它添加在一个视图控制器上,将它的视图添加视图上
1.导入头文件
#import#import
2.声明成员变量
AVPlayerViewController *_playerController; AVPlayer *_player; AVAudioSession *_session; NSString *_urlString;
3.使用播放器
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; _session = [AVAudioSession sharedInstance]; [_session setCategory:AVAudioSessionCategoryPlayback error:nil]; _player = [AVPlayer playerWithURL:[NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]]; _playerController = [[AVPlayerViewController alloc] init]; _playerController.player = _player; _playerController.videoGravity = AVLayerVideoGravityResizeAspect; _playerController.allowsPictureInPicturePlayback = true; //画中画,iPad可用 _playerController.showsPlaybackControls = true; [self addChildViewController:_playerController]; _playerController.view.translatesAutoresizingMaskIntoConstraints = true; //AVPlayerViewController 内部可能是用约束写的,这句可以禁用自动约束,消除报错 //self.view.bounds _playerController.view.frame = CGRectMake(0, 0, 320, 300); [self.view addSubview:_playerController.view]; [_playerController.player play]; //自动播放}