Search code examples
swiftvideosprite-kitavplayeravplayerviewcontroller

Swift. Create an intro video when app launches


I want to play a local video as an intro video, when my app launches. The user should not be able to cancel the video or pause it in any way any idea how I can do that?

import AVKit
import AVFoundation

class GetInfoView: SKScene {

    var moviePlayer = AVPlayerViewController()
    var player = AVPlayer()

    override func didMoveToView(view: SKView) {

        player = AVPlayer(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("intro", ofType: "mp4")!))
        moviePlayer.showsPlaybackControls = false
        moviePlayer.player = player

        self.view?.window?.rootViewController?.presentViewController(moviePlayer, animated: false){
        moviePlayer.player?.play()
        }
    }
}

I've tried this, but when it runs player = AVPlayer(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("intro", ofType: "mp4")!)) It crashes.

The error:

"fatal error: unexpectedly found nil while unwrapping an Optional value"

I don't know what the problem is, and I did add "intro.mp4" to my build


Solution

  • add your movies project.File Inspector -> Project name -> Targets -> Project Name -> Build Phases -> Copy Bundle Resources -> + button