1

When my video completes, I want it to pull down the AVPlayer and return to the Main View Controller.

I also want to pull the video down from my external screen/display when that's connected.

How do I do this?

I've tried a few different answers on here and none of them have worked so far.

NOTE: Four is the name of my video

@IBAction func fourVideoPlayButton(_ sender: Any) {

    if let path = Bundle.main.path(forResource: "Four", ofType: "mp4") {

        let fourVideo = AVPlayer(url: URL(fileURLWithPath: path))
        let fourVideoPlayer = AVPlayerViewController()
        fourVideoPlayer.player = fourVideo

        present(fourVideoPlayer, animated: true, completion: {
            fourVideo.play()
        })
    }
}
J.Kearney
  • 7
  • 1
  • 7
  • 7
    Possible duplicate of [How to detect when AVPlayer video ends playing?](https://stackoverflow.com/questions/29386531/how-to-detect-when-avplayer-video-ends-playing) – Kuldeep Dec 26 '18 at 06:53

2 Answers2

1

You need to add observer to detect when player is finished playing by

NotificationCenter.default.addObserver(self, selector: Selector(("playerDidFinishPlaying:")), 
       name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: fourVideoPlayer.player.currentItem)

func playerDidFinishPlaying(note: NSNotification) {
    fourVideoPlayer.dismiss(animated: true, completion: nil)
}

Hope this help!

Rurouni
  • 963
  • 10
  • 31
0

In Swift 4.2,

let fourPlayerController = AVPlayerViewController()

    @IBAction func fourVideoPlayButton(_ sender: Any) {
        guard let path = Bundle.main.path(forResource: "Four", ofType:"mp4") else {
            return
        }
        let fourVideoPlayer = AVPlayer(url: URL(fileURLWithPath: path))
        fourPlayerController.player = fourVideoPlayer
        NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: fourPlayerController.player?.currentItem)

        present(fourPlayerController, animated: true) {
            fourVideoPlayer.play()
        }
    }

    @objc func playerDidFinishPlaying(note: NSNotification) {
        fourPlayerController.dismiss(animated: true, completion: nil)
        fourPlayerController.dismiss(true, completion: nil)
        fourPlayerController.view.removeFromSuperview()
        self.presentedViewController?. dismiss(true, completion: nil)
    }
Bappaditya
  • 9,494
  • 3
  • 20
  • 29
  • 1
    Thanks @Bappaditya it worked. I'm using an external display also, Do you know why it won't dismiss off my external display also? It just displays the end frame of the video when complete. I want it to return to my main external view. – J.Kearney Dec 26 '18 at 07:17
  • If you can share some code snippet, that will help me to understand the flow better – Bappaditya Dec 26 '18 at 09:11
  • var secondWindow: UIWindow! var secondScreenView: UIView! @objc func setupScreen(){ if UIScreen.screens.count > 1{ let secondScreen = UIScreen.screens[1] secondWindow = UIWindow(frame: secondScreen.bounds) let viewController = UIViewController() secondWindow!.rootViewController = viewController secondWindow!.screen = secondScreen secondScreenView = UIView(frame: secondWindow!.frame) secondWindow!.addSubview(secondScreenView) secondWindow!.isHidden = false – J.Kearney Dec 26 '18 at 09:29
  • Sorry for the mess above. You can view here if it's easier. https://codeshare.io/2EwxqK – J.Kearney Dec 26 '18 at 09:38
  • Were you able to figure it out @Bappaditya? – J.Kearney Dec 27 '18 at 15:08
  • @ Bappaditya still same issue. Could it be I don't have an AVPlayerItem. Also Xcode made me change fourPlayerController.dismissViewControllerAnimated(true, completion: nil) TO fourPlayerController.dismiss(animated: true, completion: nil) – J.Kearney Dec 28 '18 at 02:57