我已嵌入视频并添加了一个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];
}
我无法解决两件事:
答案 0 :(得分:1)
最后,我想通了。我刚刚将Launch.storyborad添加到项目中并替换了代码
playerLayer.frame = [UIApplication sharedApplication].keyWindow.bounds;
与
playerLayer.frame = self.view.frame;
获取全屏视频。
答案 1 :(得分:0)
使用 Player.frame = self.view.bounds全屏