AVPlayer 到全屏,顶部和底部没有黑条

AVPlayer to fullscreen without black bars on top and bottom

我嵌入了我的视频并添加了一个 notificationCenter 观察器,如下所示:

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"export" ofType:@"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:filePath];

_playerViewController = [[AVPlayerViewController alloc] init];
// grab a local URL to our video
//NSURL *videoURL = [myBundle URLForResource:@"export" withExtension:@"mp4"];
NSLog(@"Video URL: %@",videoURL);
if(videoURL)
{

    _playerViewController.showsPlaybackControls = NO;

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:videoURL];

    // create an AVPlayer
    _playerViewController.player = [AVPlayer playerWithPlayerItem:playerItem];
   //self.player.volume = PLAYER_VOLUME;
   _playerViewController.modalPresentationStyle = 
   UIModalPresentationOverFullScreen;
   AVPlayerLayer *playerLayer = [AVPlayerLayer 
     playerLayerWithPlayer:_playerViewController.player];
   playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    playerLayer.zPosition = -1;
   playerLayer.frame = [UIApplication sharedApplication].keyWindow.bounds;
   [self.playerView.layer addSublayer:playerLayer];

   [_playerViewController.player play];
   // Adding Observer for your video file,
  NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  [notificationCenter addObserver:self selector:@selector(videoDidFinish:) 
  name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}

视频播放完毕后调用以下方法:

 -(void)videoDidFinish:(id)notification{
     AVPlayerItem *p = [notification object];
     //do something with player if you want
     [_playerViewController.player pause];

     //Remove Observer
     [[NSNotificationCenter defaultCenter] removeObserver:self];

      //your initial view can proceed from here
     UIViewController *controler = [self.storyboard 
    instantiateViewControllerWithIdentifier:@"loginScreenId"];
   [self.navigationController pushViewController:controler animated:YES];
}

我无法解决两件事:

  1. 我正在从当前的 AV 视图控制器推送到下一个视图控制器。它已成功连接到下一个视图控制器,但屏幕顶部和底部有黑条。我怎样才能摆脱这个?
  2. 如何让 AVPlayer 全屏显示?

我终于明白了。我刚刚在项目中添加了Launch.storyborad并替换了代码

playerLayer.frame = [UIApplication sharedApplication].keyWindow.bounds;

playerLayer.frame = self.view.frame;

获取全屏视频。

使用 Player.frame = self.view.bounds 全屏