0

I have been adding AVPlayerViewcontroller Programatically like this

     playerViewController = [[AVPlayerViewController alloc]init];
     playerViewController.view.frame = CGRectMake(40,60, 500, 700);
     playerViewController.showsPlaybackControls=YES;
     [self.view addSubview:playerViewController.view];

I am getting so many constraints warning

> <NSAutoresizingMaskLayoutConstraint:0x14defa560 h=-&- v=-&- _UIBackdropContentView:0x14deae530.width == _UIBackdropView:0x14deab370.width>",
"<NSLayoutConstraint:0x14dee1b70 H:|-(14)-[UILabel:0x14dee0ea0'Hi-Speed Scrubbing']   (Names: '|':_UIBackdropContentView:0x14deae530 )>",
"<NSLayoutConstraint:0x14dee1bf0 H:[UILabel:0x14dee0ea0'Hi-Speed Scrubbing']-(14)-|   (Names: '|':_UIBackdropContentView:0x14deae530 )>",
"<NSLayoutConstraint:0x14dee1670 H:|-(0)-[_UIBackdropView:0x14deab370]   (Names: '|':UIView:0x14deab090 )>",
"<NSLayoutConstraint:0x14dee16f0 H:[_UIBackdropView:0x14deab370]-(0)-|   (Names: '|':UIView:0x14deab090 )>",
"<NSLayoutConstraint:0x14dee1330 H:|-(0)-[UIView:0x14deab090]   (Names: '|':AVAlphaUpdatingView:0x14deaaad0 )>",
"<NSLayoutConstraint:0x14dee13b0 H:[UIView:0x14deab090]-(0)-|   (Names: '|':AVAlphaUpdatingView:0x14deaaad0 )>",
"<NSLayoutConstraint:0x14defb080 'UIView-Encapsulated-Layout-Width' H:[AVAlphaUpdatingView:0x14deaaad0(0)]>"

I have also tried giving

playerViewController.view.translatesAutoresizingMaskIntoConstraints = NO

Still I am getting Constraints issues. Could anyone please give me a solution?

kb920
  • 3,039
  • 2
  • 33
  • 44
jeremy gv
  • 77
  • 1
  • 10
  • Are you sure you get constraints warning only after adding playerViewController to your view hierarchy. – karthik Sep 06 '16 at 15:48

2 Answers2

0

I think you should not set frame of your playerviewcontroller. You can manage it something like,

 NSURL *videoURL = [NSURL URLWithString:@"url string here"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];

or you can add playerViewController's view to self.view instead of presenting it like,

 [self.view addSubview:playerViewController.view];

Update :

Try this,

 NSURL *videoURL = [NSURL URLWithString:@"url string"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
[player play];

And make sure that if you have another controls like label or button then you have set proper constraints to that.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

Use autolayout on your playerViewController.

tony508
  • 153
  • 1
  • 7