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

时间:2017-04-19 16:24:22

标签: ios objective-c iphone segue avplayerviewcontroller

我已嵌入视频并添加了一个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设为全屏?

2 个答案:

答案 0 :(得分:1)

最后,我想通了。我刚刚将Launch.storyborad添加到项目中并替换了代码

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

playerLayer.frame = self.view.frame;

获取全屏视频。

答案 1 :(得分:0)

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