Search code examples
iosswiftxcodelaunch-screen

How to have my App open automatically after Launch Screen Video finished?


I want to integrate a video as my launch screen. I found a tutorial on this online, but now my question is: how can I integrate this into my app, in a way that the video will be shown once and then when it's finished, the app opens up automatically?

currently, the video plays once (which is alright) but then it just stops -

how do I get it to open the app automatically once the video has finished??

This is my code:

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

    var player: AVPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load the video from the app bundle.
        let videoURL: URL = Bundle.main.url(forResource: "video", withExtension: "mov")!

        player = AVPlayer(url: videoURL)
        player?.actionAtItemEnd = .none
        player?.isMuted = true

        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        playerLayer.zPosition = -1

        playerLayer.frame = view.frame

        view.layer.addSublayer(playerLayer)

        player?.play()
     }

     func loopVideo() {
         player?.seek(to: kCMTimeZero)
         player?.play()
     }
}

Solution

  • You can NSNotification to know qhwn playback is finished.

    Register for the itemDidFinished notification:

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

    And handle playerDidFinishPlaying Notification as follow:

      func playerDidFinishPlaying(notification: NSNotification){
            //push your first view controller
        }