如何在启动屏幕视频完成后自动打开我的应用程序?

时间:2016-12-12 11:45:19

标签: ios swift xcode launch-screen

我想将视频集成为我的启动画面。 我在网上发现了一个教程,但现在我的问题是:如何将这个集成到我的应用程序中,以一种方式显示视频,然后当它完成时,应用程序会自动打开?

目前,视频播放一次(没关系),但之后就停止了 -

如何在视频播放完毕后自动打开应用程序?

这是我的代码:

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

2 个答案:

答案 0 :(得分:2)

试试这个 Swift 3.0

let videoPlayer = AVPlayer(URL: url)

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

func playerDidFinishPlaying(note: NSNotification){
        print("Video Finished")
    }

答案 1 :(得分:2)

您可以NSNotification知道qhwn播放已完成。

注册itemDidFinished通知:

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

并按照以下方式处理playerDidFinishPlaying通知:

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