Playing video in an Apple TV App with tvOS in Swift

So yesterday I did a small little experiment with tvOS, namely a TVML based app which was fun to see how Apple is trying to make it more accessible to new developers (TV networks, content providers etc) without having to know Swift or Objective-C and Cocoa to get going.

I wonder if Newsstand has taught them anything? The quality of publications were very inconsistent, and I bet they'll stretch themselves in order to make it easier for content providers to adopt the new Apple TV as a platform for distribution.

But today I wanted to try native tvOS development, since I'm familiar with Cocoa it should be very accessible to get going.

I found the (old) Big Buck BUNNY video that is publicly available for the very purpose of testing video players on a range of devices.

But since the video is not hosted on a site supporting HTTPS, we need to relax the App Transport Security to allow NSAllowsArbitraryLoads. This is done in the app's Info.plist file:

App Transport Security

For the UI, I just added a button to the storyboard and made it push onto another ViewController - the AVPlayerViewController subclass that contains all the necessary logic to easily begin playing video.

Playing a video from an instance of AVPlayerViewController is as easy as playing audio or video just as you're used to do in iOS.

player = AVPlayer(URL: NSURL(string: "VideoUrl.mp4")!)  
player?.play()  

I noticed in the documentation that there's a property called contentOverlayView so I created a silly little image to use as a watermark logo at the top left corner of the screen:

overlayView.addSubview(UIImageView(image: UIImage(named: "tv-watermark")))  
contentOverlayView?.addSubview(overlayView)  

Video playback starts more or less immediately, the quality of the source video is nowhere near good so it is kind of stretched to power up the pixels of a large screen.

The code for this is on Github  💻

Martin H. Normark

Product and UX Hacker. Web and iOS developer.

Subscribe to Martin Normark's Blog

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!