2017-04-19 14 views
0

我有嵌入我的視頻,並添加了通知中心觀察員在頂部和底部的黑邊全屏:如下圖所示AVPlayer無需

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視圖控制器推到下一個視圖CON troller。它被成功地引導到下一個視圖控制器,但是屏幕具有頂部和底部黑色條。我怎樣才能擺脫這一點?
  2. 如何使AVPlayer全屏?

回答

1

最後,我想通了。我只是說Launch.storyborad到項目並替換代碼

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

playerLayer.frame = self.view.frame; 

獲得全屏視頻。

0

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