4

Hi, I replaced MPMoviePlayerController with AVPlayerViewController since MPMoviePlayerController is deprecated. I'm nearly there, but have one question. My movie start as a view within a view. When playing fullscreen I want it to jump back to NO fullscreen when finished playing. But I don't know how. Here is my code:

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

With MPMoviePlayer it used to work with this code:

    - (void) playerPlaybackDidFinish:(NSNotification*)notification{
// movie finished playing
[moviePlayerController setFullscreen:NO];
}

With what code do I need to replace it??

-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
 ???????????}

Thanks, Meg

user1737746
  • 101
  • 10

1 Answers1

0

#iOS 10 and higher and Swift 4.2 this code are working.

Write this code in your player init methods

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)

This is youy notification delegate

func playerItemDidReachEnd(note:NSNotification){
     print("finished")
     dismissViewControllerAnimated(true, completion: nil)
 }
Ketan Sodvadiya
  • 464
  • 5
  • 11