How do I play a video on tvOS for Apple TV?

Ethan Allen picture Ethan Allen · Sep 10, 2015 · Viewed 21.7k times · Source

I started a blank tvOS project and created the following code:

- (void)viewDidLoad  
{  
    [super viewDidLoad];  

    AVPlayer *avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://www.myurl.com/myvideo.mp4"]];  
    AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];  

    avPlayerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);  
    [self.view.layer addSublayer:avPlayerLayer];  

    [avPlayer play];  
}  

Nothing happens in the simulator though once the app loads. No video, nothing, just a blank translucent screen in my Apple TV simulator.

What's the proper way to play a sample video on app launch for an Apple TV app from an HTTP source?

Answer

lemonmojo picture lemonmojo · Sep 10, 2015

I just pasted your code in my tvOS sample project, replaced the URL and ran it.

Nothing happened. Well, except for the fact that there's a log entry telling me that App Transport Security has blocked my URL request.

So I headed to the Info.plist, disabled ATS and upon next launch the video showed up just fine.

So if you're also using a non-HTTPS URL you're very likely running into this issue which is easily fixed by either using an HTTPS URL, disabling ATS completely or allowing specific non-HTTPs URLs in your Info.plist.

P.S.: I used this video for testing.