AVPlayer exit fullscreen on finish playing

放肆的年华 提交于 2019-12-08 02:50:00

问题


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?


回答1:


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

}



回答2:


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



回答3:


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

}


来源:https://stackoverflow.com/questions/27380264/avplayer-exit-fullscreen-on-finish-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!