我一直以编程方式添加AVPlayerViewcontroller
playerViewController = [[AVPlayerViewController alloc]init];
playerViewController.view.frame = CGRectMake(40,60, 500, 700);
playerViewController.showsPlaybackControls=YES;
[self.view addSubview:playerViewController.view];
我收到了很多限制警告
> <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)]>"
我也尝试过给予
playerViewController.view.translatesAutoresizingMaskIntoConstraints = NO
我仍然遇到限制问题。有谁能请给我一个解决方案?
答案 0 :(得分:0)
我认为你不应该设置playerviewcontroller
的框架。你可以管理它,比如
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];
或者您可以将playerViewController's view
添加到self.view
,而不是像
[self.view addSubview:playerViewController.view];
更新:
试试这个,
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];
并确保如果您有其他控件,例如label
或button
,那么您已为此设置了适当的约束。
答案 1 :(得分:0)
在playerViewController上使用autolayout。