Search code examples
swifturltvos

Play video from local file with tvOS


I want to play a little video (20 seconds) when my app opens on the Apple TV. But I can't find how to implement the URL from a local file.

This is what I've got so far, but unfortunately it doesn't work:

    override func viewDidLoad() {
        super.viewDidLoad()

        // I know this is how to play a file from a webserver:
        // player = AVPlayer(URL: NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!)

        // But I can't find how to set the URL to a local file in the app
        let path = NSBundle.mainBundle().pathForResource("bunny", ofType:"mp4")
        let url = NSURL.fileURLWithPath(path!)
        player = AVPlayer(URL: url)

        player?.play()

    }

Solution

  • Your code seems fine. Maybe, you should try this:

    In the Project Navigator select your Project Root > Your Target > Build Phases > Copy Bundle Resources. Check to see that your video is there, if not click the plus sign to add it.

    Source: http://www.brianjcoleman.com/tutorial-play-video-swift/