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

时间:2021-09-01 18:57:52

I have embedded my video and added a notificationCenter observer as shown below:

我已嵌入视频并添加了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];
}

When the video is finished playing, the following method is called:

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

 -(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];
}

I'm unable to fix two things:

我无法解决两件事:

  1. I'm pushing from current AV view controller to next view controller. It's successfully segued to next view controller but the screen have top and bottom black bars. How can I get rid of this?
  2. 我正在从当前的AV视图控制器推送到下一个视图控制器。它成功地被视为下一个视图控制器,但屏幕有顶部和底部黑条。我怎么能摆脱这个?

  3. How can I make the AVPlayer fullscreen?
  4. 如何使AVPlayer全屏?

2 个解决方案

#1


1  

Finally, I figured it out. I just added Launch.storyborad to the project and replaced the code

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

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

with

playerLayer.frame = self.view.frame;

to get fullscreen Video.

获得全屏视频。

#2


0  

use Player.frame = self.view.bounds for full screen

使用Player.frame = self.view.bounds进行全屏显示

#1


1  

Finally, I figured it out. I just added Launch.storyborad to the project and replaced the code

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

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

with

playerLayer.frame = self.view.frame;

to get fullscreen Video.

获得全屏视频。

#2


0  

use Player.frame = self.view.bounds for full screen

使用Player.frame = self.view.bounds进行全屏显示