一旦播放,AVPlayerViewController退出全屏

时间:2016-11-03 12:39:41

标签: ios objective-c avplayer avplayerviewcontroller

嗨,我用MPPoviePlayerController取代了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
 ???????????}

谢谢,梅格

1 个答案:

答案 0 :(得分:0)

#iOS 10和更高版本以及Swift 4.2可以正常工作。

将此代码写入播放器的init方法

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)
 }