2

I am using a AVPlayer to show some video streams (with a fixed length). The problem is when the content is finished the player (if it has been set to fullscreen by the user) still remains in fullscreen.

Any way to make the player go to it's minimized state immediately after the content is finished playing?

user1028028
  • 6,323
  • 9
  • 34
  • 59

3 Answers3

2

To add on to longilong's answer,

You could dismiss the video player with dismissViewControllerAnimated in the itemDidFinishPlaying function. You should also remember to remove the observer you created after you are done.

-(void)startPlaybackForItemWithURL:(NSURL*)url {

     // First create an AVPlayerItem
     AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];

     // Subscribe to the AVPlayerItem's DidPlayToEndTime notification.


    [[NSNotificationCenter defaultCenter] 
      addObserver:self 
        selector:@selector(itemDidFinishPlaying:) 
        name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

    // Begin playback
    [player play]
} 

-(void)itemDidFinishPlaying:(NSNotification *) notification {
     // Will be called when AVPlayer finishes playing playerItem
     [self dismissViewControllerAnimated:YES completion:Nil]
     [[NSNotificationCenter defaultCenter] 
        removeObserver:self 
        name:AVPlayerItemDidPlayToEndTimeNotification 
        object:Nil];

}
matt.writes.code
  • 674
  • 10
  • 22
1

its working me for #iOS version 10 and higher

    # declare your player and 
    var player = AVPlayer()
    var playerVC = AVPlayerViewController()


    let item = AVPlayerItem(url: module.fileURL!)
    DispatchQueue.main.async(execute: {
        self.playerVC.view.backgroundColor = UIColor.clear
        self.player.replaceCurrentItem(with: item)
        try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        self.playerVC.player = self.player

        self.player.play()

        self.playerVC.showsPlaybackControls = true
        if #available(iOS 11.0, *) {
            self.playerVC.exitsFullScreenWhenPlaybackEnds = true
        }
        self.playerVC.view.frame = self.videoView.bounds
        NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd(notification:)), name: .AVPlayerItemDidPlayToEndTime, object:self.playerVC.player!.currentItem)
        self.videoView.addSubview((self.playerVC.view)!)

})

Listen to the end of your video file

  @objc func playerItemDidReachEnd(notification: Notification?) {
    print("Finishing")
    dismiss(animated: true, completion: nil)
}
Ketan Sodvadiya
  • 464
  • 5
  • 11
0

Listen to the end of your video file and close/resize/reinit your View at the end

-(void)startPlaybackForItemWithURL:(NSURL*)url {

 // First create an AVPlayerItem
 AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];

 // Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

// Begin playback
[player play]

} 

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

}
longi
  • 11,104
  • 10
  • 55
  • 89