嗨,我用AVPlayerViewController取代了MPMoviePlayerController,因为不推荐使用MPMoviePlayerController.
我快到了,但有一个问题.我的电影以视图中的视图开始.在全屏播放时我希望它在完成播放后跳回到全屏.但我不知道怎么做.这是我的代码:
我快到了,但有一个问题.我的电影以视图中的视图开始.在全屏播放时我希望它在完成播放后跳回到全屏.但我不知道怎么做.这是我的代码:
- (void)viewDidLoad { // grab a local URL to our video NSURL *videoURL = [[NSBundle mainBundle]URLForResource:@"movie" withExtension:@"m4v"]; // create an AVPlayer AVPlayer *player = [AVPlayer playerWithURL:videoURL]; // create a player view controller self.controller = [[AVPlayerViewController alloc]init]; controller.player = player; [player play]; // show the view controller [self addChildViewController:controller]; [self.view addSubview:controller.view]; controller.view.frame = CGRectMake(0,25,750,422); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:player]; }
使用MPMoviePlayer,它曾使用此代码:
- (void) playerPlaybackDidFinish:(NSNotification*)notification{ // movie finished playing [moviePlayerController setFullscreen:NO]; }
我需要用什么代码来替换它?
-(void)itemDidFinishPlaying:(NSNotification *) notification { // Will be called when AVPlayer finishes playing playerItem ???????????}
谢谢,梅格
解决方法
#iOS 10及更高版本和Swift 4.2这段代码正在运行.
if #available(iOS 11.0,*) { self.playerVC?.exitsFullScreenWhenPlaybackEnds = true } NotificationCenter.default.addObserver(self,selector: #selector(self.playerItemDidReachEnd(notification:)),name: .AVPlayerItemDidPlayToEndTime,object:self.playerVC?.player!.currentItem)
这是你的通知代表
func playerItemDidReachEnd(note:NSNotification){ print("finished") dismissViewControllerAnimated(true,completion: nil) }