AVPlayer exit fullscreen on finish playing

avplayerviewcontroller fullscreen
avplayerviewcontroller fullscreen landscape
avfullscreenviewcontroller

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?

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

}

var entersFullScreenWhenPlaybackBegins: Bool { get set } A Boolean value that indicates whether the player exits full-screen mode when playback ends. Yes, the AVPlayer class does not have a delegate protocol like the AVAudioPlayer. You need to subscribe to notifications on an AVPlayerItem. You can create an AVPlayerItem using the same URL that you would otherwise pass to -initWithURL: on AVPlayer.

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

Everything works fine, except that when I zoom to full screen via the on screen control, after the movie finished playing, the full screen mode  What is the east way to track when song finished playing with AVPlayer in Swift? Is there any function which is called when avplayer finished playing, or I should combine timer with avplayer class

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

}

Instead of presenting an AVPlayerViewController, you might push it onto a navigation and its controls then include a Done button which exits fullscreen mode. let player = AVPlayer(URL:url) let av = AVPlayerViewController() av.​player  A Boolean value that indicates whether the player view controller updates the Now Playing info center. var applies Preferred Display Criteria Automatically : Bool A Boolean value that indicates whether the player view controller automatically sets the screen’s display criteria to match that of the currently playing asset.

A presented AVPlayerViewController The resulting view is intelligently adapted to its and its controls then include a Done button which exits fullscreen mode. Has anyone ever implemented fullscreen for AVPlayer? I'm implementing custom controls for AVPlayer and I'm only missing fullscreen. Reading the documentation it seems like I need to handle the view myself but I'm not sure how to do this.

After switching the AVPlayerViewController to fullscreen mode and tapping Done button to exit fullscreen the player stops playing. Is there any way to keep the  An AVPlayer is a controller object used to manage the playback and timing of a media asset. You can use an AVPlayer to play local and remote file-based media, such as QuickTime movies and MP3 audio files, as well as audiovisual media served using HTTP Live Streaming. AVPlayer is for playing a single media asset at a time.

And our mission is to make it easy for you to show and play AVPlayer-based media content using the And similarly, at the end of the video, will exit full screen. Note on Android ExoPlayer, the full-screen mode by default goes into landscape mode. Exiting from the full-screen mode will display the video in Initial orientation. Platforms: iOS. headers. Pass headers to the HTTP client. Can be used for authorization. Headers must be a part of the source object.

Comments
  • Try using AVPLayerLayer by changing the video gravity. Check apple documentation.
  • possible duplicate of stackoverflow.com/questions/35048758/…